|
| 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 | +} |
0 commit comments