Skip to content

Commit

Permalink
add possibility to test blade views (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
helloiamlukas authored Jan 3, 2024
1 parent b50351b commit 14364cc
Show file tree
Hide file tree
Showing 5 changed files with 526 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ The assertion uses the `\Sinnbeck\DomAssertions\Asserts\AssertDatalist` class.
$this->get('/some-route')
->assertFormExists('#form1', function (AssertForm $form) {
$form->findDatalist('#skills', function (AssertDatalist $list) {
$list ->containsOptions(
$list->containsOptions(
[
'value' => 'PHP',
],
Expand All @@ -361,6 +361,17 @@ Livewire::test(UserForm::class)
});
```

### Usage with Blade views
You can also use this package to test blade views.
```php
$this->view('navigation')
->assertElementExists('nav > ul', function(AssertElement $ul) {
$ul->contains('li', [
'class' => 'active',
]);
});
```

## Overview of methods
| Base methods | Description |
|------------------------------------------------|--------------------------------------------------------------------------------------|
Expand Down
2 changes: 2 additions & 0 deletions src/DomAssertionsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Illuminate\Support\ServiceProvider;
use Illuminate\Testing\TestResponse;
use Illuminate\Testing\TestView;

class DomAssertionsServiceProvider extends ServiceProvider
{
public function boot()
{
if ($this->app->runningUnitTests()) {
TestResponse::mixin(new TestResponseMacros());
TestView::mixin(new TestViewMacros());
}
}
}
125 changes: 125 additions & 0 deletions src/TestViewMacros.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types=1);

namespace Sinnbeck\DomAssertions;

use Closure;
use DOMException;
use Illuminate\Testing\TestView;
use PHPUnit\Framework\Assert;
use Sinnbeck\DomAssertions\Asserts\AssertElement;
use Sinnbeck\DomAssertions\Asserts\AssertForm;
use Sinnbeck\DomAssertions\Support\DomParser;

/**
* @internal
*
* @mixin TestView
*/
class TestViewMacros
{
public function assertHtml5()
{
return function () {
/** @var TestView $this */
Assert::assertNotEmpty(
(string) $this,
'The view is empty!'
);

try {
$parser = DomParser::new((string) $this);
} catch (DOMException $exception) {
Assert::fail($exception->getMessage());
}

Assert::assertEquals(
'html',
$parser->getDocType(),
'Not a html5 doctype!'
);

return $this;
};
}

public function assertElementExists(): Closure
{
return function ($selector = 'body', $callback = null): TestView {
/** @var TestView $this */
Assert::assertNotEmpty(
(string) $this,
'The view is empty!'
);

try {
$parser = DomParser::new((string) $this);
} catch (DOMException $exception) {
Assert::fail($exception->getMessage());
}

if ($selector instanceof Closure) {
$callback = $selector;
$selector = 'body';
}

if (is_string($selector)) {
$element = $parser->query($selector);
} else {
Assert::fail('Invalid selector!');
}

Assert::assertNotNull($element, sprintf('No element found with selector: %s', $selector));

if ($callback) {
$callback(new AssertElement((string) $this, $element));
}

return $this;
};
}

public function assertFormExists(): Closure
{
return function ($selector = 'form', $callback = null): TestView {
/** @var TestView $this */
Assert::assertNotEmpty(
(string) $this,
'The view is empty!'
);

try {
$parser = DomParser::new((string) $this);
} catch (DOMException $exception) {
Assert::fail($exception->getMessage());
}

if ($selector instanceof Closure) {
$callback = $selector;
$selector = 'form';
}

if (is_string($selector)) {
$form = $parser->query($selector);
} else {
Assert::fail('Invalid selector!');
}

Assert::assertNotNull(
$form,
sprintf('No form was found with selector "%s"', $selector)
);
Assert::assertEquals(
'form',
$form->nodeName,
'Element is not of type form!');

if ($callback) {
$callback(new AssertForm((string) $this, $form));
}

return $this;
};
}
}
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Tests;

use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
use Illuminate\Support\Facades\Route;

class TestCase extends \Orchestra\Testbench\TestCase
{
use InteractsWithViews;

protected function getPackageProviders($app)
{
return [
Expand Down
Loading

0 comments on commit 14364cc

Please sign in to comment.