Skip to content

Commit

Permalink
add better handling of empty or broken views
Browse files Browse the repository at this point in the history
  • Loading branch information
sinnbeck committed Oct 22, 2022
1 parent cfff92e commit f254bb0
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/Macros/AssertElementMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ public function __invoke()
{
return function ($selector = 'body', $callback = null): TestResponse {
/** @var TestResponse $this */
$parser = DomParser::new($this->getContent());
Assert::assertNotEmpty(
$this->getContent(),
'The view is empty!'
);

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

if (is_callable($selector)) {
$callback = $selector;
Expand Down
11 changes: 10 additions & 1 deletion src/Macros/AssertFormMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ public function __invoke()
{
return function ($selector = 'form', $callback = null): TestResponse {
/** @var TestResponse $this */
$parser = DomParser::new($this->getContent());
Assert::assertNotEmpty(
$this->getContent(),
'The view is empty!'
);

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

if (is_callable($selector)) {
$callback = $selector;
Expand Down
6 changes: 5 additions & 1 deletion src/Parsers/DomParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public function setContent($html): void

$html = '<?xml encoding="UTF-8">'.trim($html);
$dom->loadHTML($html, LIBXML_NOERROR | LIBXML_COMPACT | LIBXML_HTML_NODEFDTD | LIBXML_NOBLANKS | LIBXML_NOXMLDECL);
$this->setRoot($dom->getElementsByTagName('body')->item(0));
$root = $dom->getElementsByTagName('body')->item(0);
if (is_null($root)) {
throw new \DOMException('No body element found!');
}
$this->setRoot($root);
}

public function getElementOfType(string $type, $index = 0): ?DOMNode
Expand Down
1 change: 1 addition & 0 deletions tests/DomParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
$parser->setRoot($parser->query('#form2'));

$this->assertNotNull($parser->query('select:nth-of-type(2)'));
$this->assertNotNull($parser->query('input'));
});

it('can get an attribute', function () {
Expand Down
23 changes: 23 additions & 0 deletions tests/DomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@
use PHPUnit\Framework\AssertionFailedError;
use Sinnbeck\DomAssertions\Asserts\ElementAssert;

it('can handle an empty view', function () {
$this->get('empty')
->assertElement();
})->throws(
AssertionFailedError::class,
'The view is empty!'
);

it('can handle an empty body', function () {
$this->get('empty-body')
->assertElement();
})->throws(
AssertionFailedError::class,
'No body element found!'
);

it('can parse broken html', function () {
$this->get('broken')
->assertElement(function ($d) {
$d->dd();
});
});

it('can find the an element', function () {
$this->get('nesting')
->assertElement();
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ protected function defineRoutes($router)
{
Route::view('form', 'form');
Route::view('nesting', 'nesting');
Route::view('empty', 'empty');
Route::view('empty-body', 'empty-body');
Route::view('broken', 'broken');
}
}
21 changes: 21 additions & 0 deletions tests/views/broken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>
<div class="foo">
<span></span>
<span></span>
<span><p></p>
<table>
<td><div></div></td>
</table>
</div>
</body>
</html>
10 changes: 10 additions & 0 deletions tests/views/empty-body.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
</html>
Empty file added tests/views/empty.blade.php
Empty file.

0 comments on commit f254bb0

Please sign in to comment.