Skip to content

Commit 14364cc

Browse files
add possibility to test blade views (#14)
1 parent b50351b commit 14364cc

File tree

5 files changed

+526
-1
lines changed

5 files changed

+526
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ The assertion uses the `\Sinnbeck\DomAssertions\Asserts\AssertDatalist` class.
337337
$this->get('/some-route')
338338
->assertFormExists('#form1', function (AssertForm $form) {
339339
$form->findDatalist('#skills', function (AssertDatalist $list) {
340-
$list ->containsOptions(
340+
$list->containsOptions(
341341
[
342342
'value' => 'PHP',
343343
],
@@ -361,6 +361,17 @@ Livewire::test(UserForm::class)
361361
});
362362
```
363363

364+
### Usage with Blade views
365+
You can also use this package to test blade views.
366+
```php
367+
$this->view('navigation')
368+
->assertElementExists('nav > ul', function(AssertElement $ul) {
369+
$ul->contains('li', [
370+
'class' => 'active',
371+
]);
372+
});
373+
```
374+
364375
## Overview of methods
365376
| Base methods | Description |
366377
|------------------------------------------------|--------------------------------------------------------------------------------------|

src/DomAssertionsServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use Illuminate\Support\ServiceProvider;
66
use Illuminate\Testing\TestResponse;
7+
use Illuminate\Testing\TestView;
78

89
class DomAssertionsServiceProvider extends ServiceProvider
910
{
1011
public function boot()
1112
{
1213
if ($this->app->runningUnitTests()) {
1314
TestResponse::mixin(new TestResponseMacros());
15+
TestView::mixin(new TestViewMacros());
1416
}
1517
}
1618
}

src/TestViewMacros.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sinnbeck\DomAssertions;
6+
7+
use Closure;
8+
use DOMException;
9+
use Illuminate\Testing\TestView;
10+
use PHPUnit\Framework\Assert;
11+
use Sinnbeck\DomAssertions\Asserts\AssertElement;
12+
use Sinnbeck\DomAssertions\Asserts\AssertForm;
13+
use Sinnbeck\DomAssertions\Support\DomParser;
14+
15+
/**
16+
* @internal
17+
*
18+
* @mixin TestView
19+
*/
20+
class TestViewMacros
21+
{
22+
public function assertHtml5()
23+
{
24+
return function () {
25+
/** @var TestView $this */
26+
Assert::assertNotEmpty(
27+
(string) $this,
28+
'The view is empty!'
29+
);
30+
31+
try {
32+
$parser = DomParser::new((string) $this);
33+
} catch (DOMException $exception) {
34+
Assert::fail($exception->getMessage());
35+
}
36+
37+
Assert::assertEquals(
38+
'html',
39+
$parser->getDocType(),
40+
'Not a html5 doctype!'
41+
);
42+
43+
return $this;
44+
};
45+
}
46+
47+
public function assertElementExists(): Closure
48+
{
49+
return function ($selector = 'body', $callback = null): TestView {
50+
/** @var TestView $this */
51+
Assert::assertNotEmpty(
52+
(string) $this,
53+
'The view is empty!'
54+
);
55+
56+
try {
57+
$parser = DomParser::new((string) $this);
58+
} catch (DOMException $exception) {
59+
Assert::fail($exception->getMessage());
60+
}
61+
62+
if ($selector instanceof Closure) {
63+
$callback = $selector;
64+
$selector = 'body';
65+
}
66+
67+
if (is_string($selector)) {
68+
$element = $parser->query($selector);
69+
} else {
70+
Assert::fail('Invalid selector!');
71+
}
72+
73+
Assert::assertNotNull($element, sprintf('No element found with selector: %s', $selector));
74+
75+
if ($callback) {
76+
$callback(new AssertElement((string) $this, $element));
77+
}
78+
79+
return $this;
80+
};
81+
}
82+
83+
public function assertFormExists(): Closure
84+
{
85+
return function ($selector = 'form', $callback = null): TestView {
86+
/** @var TestView $this */
87+
Assert::assertNotEmpty(
88+
(string) $this,
89+
'The view is empty!'
90+
);
91+
92+
try {
93+
$parser = DomParser::new((string) $this);
94+
} catch (DOMException $exception) {
95+
Assert::fail($exception->getMessage());
96+
}
97+
98+
if ($selector instanceof Closure) {
99+
$callback = $selector;
100+
$selector = 'form';
101+
}
102+
103+
if (is_string($selector)) {
104+
$form = $parser->query($selector);
105+
} else {
106+
Assert::fail('Invalid selector!');
107+
}
108+
109+
Assert::assertNotNull(
110+
$form,
111+
sprintf('No form was found with selector "%s"', $selector)
112+
);
113+
Assert::assertEquals(
114+
'form',
115+
$form->nodeName,
116+
'Element is not of type form!');
117+
118+
if ($callback) {
119+
$callback(new AssertForm((string) $this, $form));
120+
}
121+
122+
return $this;
123+
};
124+
}
125+
}

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Tests;
44

5+
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
56
use Illuminate\Support\Facades\Route;
67

78
class TestCase extends \Orchestra\Testbench\TestCase
89
{
10+
use InteractsWithViews;
11+
912
protected function getPackageProviders($app)
1013
{
1114
return [

0 commit comments

Comments
 (0)