diff --git a/README.md b/README.md index 22120ce..4e96a76 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ $this->get('/some-route') }, 'select:nth-of-type(2)'); }); ``` -It also works with closures if you prefer that syntax. The closure retuns an instance of `\Sinnbeck\DomAssertions\Asserts\OptionAssert` +It also works with closures if you prefer that syntax. The closure returns an instance of `\Sinnbeck\DomAssertions\Asserts\OptionAssert` ```php $this->get('/some-route') ->assertForm(function (FormAssert $form) { @@ -159,6 +159,28 @@ $this->get('/some-route') }); }); ``` +You can of course also check that a select has a certain value +```php +$this->get('/some-route') + ->assertForm('#form1', function (FormAssert $form) { + $form->containsSelect('select:nth-of-type(2)', function (SelectAssert $selectAssert) { + $selectAssert->hasValue('dk'); + }); + }); +``` +or that an option is selected +```php +$this->get('/some-route') + ->assertForm('#form1', function (FormAssert $form) { + $form->containsSelect('select:nth-of-type(2)', function (SelectAssert $selectAssert) { + $selectAssert->containsOption(function (OptionAssert $optionAssert) { + $optionAssert->hasValue('dk'); + $optionAssert->hasText('Denmark'); + $optionAssert->isSelected(); + }); + }); + }); +``` ### Testing regular dom The testing of generic html elements works a lot like forms. When calling a route in a test you might want to make sure that the view contains certain elements. To test this you can use the `->assertElement()` method on the test response. diff --git a/src/Asserts/OptionAssert.php b/src/Asserts/OptionAssert.php index 9a2252d..7739f93 100644 --- a/src/Asserts/OptionAssert.php +++ b/src/Asserts/OptionAssert.php @@ -3,9 +3,16 @@ namespace Sinnbeck\DomAssertions\Asserts; use PHPUnit\Framework\Assert; +use Sinnbeck\DomAssertions\Asserts\Traits\CanGatherAttributes; +use Sinnbeck\DomAssertions\Asserts\Traits\HasElementAsserts; +use Sinnbeck\DomAssertions\Asserts\Traits\InteractsWithParser; class OptionAssert { + use CanGatherAttributes; + use HasElementAsserts; + use InteractsWithParser; + protected $options; protected $touched = []; @@ -45,4 +52,14 @@ public function validate() sprintf('Could not find a matching option with data: %s', json_encode($this->touched, JSON_PRETTY_PRINT)) ); } + + public function isSelected(): self + { + $this->touched['value'] = 'selected'; + if ($this->options->firstWhere('selected', 'selected')) { + $this->matched['selected'] = 'selected'; + } + + return $this; + } } diff --git a/src/Asserts/SelectAssert.php b/src/Asserts/SelectAssert.php index 48d990c..8b823d9 100644 --- a/src/Asserts/SelectAssert.php +++ b/src/Asserts/SelectAssert.php @@ -2,6 +2,7 @@ namespace Sinnbeck\DomAssertions\Asserts; +use PHPUnit\Framework\Assert; use Sinnbeck\DomAssertions\Asserts\Traits\CanGatherAttributes; use Sinnbeck\DomAssertions\Asserts\Traits\HasElementAsserts; use Sinnbeck\DomAssertions\Asserts\Traits\InteractsWithParser; @@ -42,6 +43,20 @@ public function containsOption(mixed $attributes): self return $this; } + public function hasValue($value) + { + Assert::assertNotNull( + $option = $this->parser->query('option[selected="selected"]'), + 'No options are selected!' + ); + Assert::assertEquals( + $value, + $option->getAttribute('value') + ); + + return $this; + } + public function containsOptions(...$attributes): self { foreach ($attributes as $attribute) { diff --git a/tests/FormTest.php b/tests/FormTest.php index 9d7a675..476d4db 100644 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -172,6 +172,28 @@ function (OptionAssert $optionAssert) { })->assertOk(); }); +it('can assert that select has value', function () { + $this->get('form') + ->assertForm('#form2', function (FormAssert $form) { + $form->containsSelect('select:nth-of-type(2)', function (SelectAssert $selectAssert) { + $selectAssert->hasValue('none'); + }); + })->assertOk(); +}); + +it('can assert that option is selected', function () { + $this->get('form') + ->assertForm('#form2', function (FormAssert $form) { + $form->containsSelect('select:nth-of-type(2)', function (SelectAssert $selectAssert) { + $selectAssert->containsOption(function (OptionAssert $optionAssert) { + $optionAssert->hasValue('none'); + $optionAssert->hasText('None'); + $optionAssert->isSelected(); + }); + }); + })->assertOk(); +}); + it('can find a button', function () { $this->get('form') ->assertForm('#form2', function (FormAssert $form) { diff --git a/tests/views/form.blade.php b/tests/views/form.blade.php index 8bee7d7..f1f622c 100644 --- a/tests/views/form.blade.php +++ b/tests/views/form.blade.php @@ -22,7 +22,7 @@