-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add possibility to test blade views (#14)
- Loading branch information
1 parent
b50351b
commit 14364cc
Showing
5 changed files
with
526 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.