Skip to content

Commit

Permalink
clean up select and option assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
sinnbeck committed Oct 20, 2022
1 parent 6e41414 commit aefb6e2
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 176 deletions.
50 changes: 8 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ Testing for selects is also easy and works a bit like the `assertForm()`. It tak
```php
$this->get('/some-route')
->assertForm(function (FormAssert $form) {
$form->containsSelect('select:nth-of-type(2)', function (SelectAssert $selectAssert) {
$form->findSelect('select:nth-of-type(2)', function (SelectAssert $selectAssert) {
$selectAssert->has('name', 'country')
});
});
Expand All @@ -223,7 +223,7 @@ You can also assert that it has certain options. You can either check for one sp
```php
$this->get('/some-route')
->assertForm(function (FormAssert $form) {
$form->containsSelect(function (SelectAssert $selectAssert) {
$form->findSelect(function (SelectAssert $selectAssert) {
$selectAssert->containsOption([
[
'x-data' => 'none',
Expand All @@ -244,56 +244,22 @@ $this->get('/some-route')
}, 'select:nth-of-type(2)');
});
```
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) {
$form->containsSelect('select:nth-of-type(2)', function (SelectAssert $selectAssert) {
$selectAssert->containsOption(function (OptionAssert $optionAssert) {
$optionAssert->hasValue('none');
$optionAssert->hasText('None');
$optionAssert->hasXData('none');
})
->containsOptions(
function (OptionAssert $optionAssert) {
$optionAssert->hasValue('dk');
$optionAssert->hasText('Denmark');
},
function (OptionAssert $optionAssert) {
$optionAssert->hasValue('us')
->hasText('USA');
},
);
});
});
```
You can of course also check that a select has a certain value
You can check if a select has a 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();
});
$form->findSelect('select', function (SelectAssert $selectAssert) {
$selectAssert->hasValue('da');
});
});
```


You can also check selects with multiple values
```php
$this->get('/some-route')
->assertForm('#form1', function (FormAssert $form) {
$form->containsSelect('select', function (SelectAssert $selectAssert) {
$form->findSelect('select', function (SelectAssert $selectAssert) {
$selectAssert->hasValues(['da', 'en']);
});
});
Expand Down
22 changes: 11 additions & 11 deletions src/Asserts/FormAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function getAttributeFromForm(string $attribute)
return $this->parser->getAttributeForRoot($attribute);
}

public function containsSelect($selector = 'select', $callback = null): static
public function findSelect($selector = 'select', $callback = null): static
{
if (is_callable($selector)) {
$callback = $selector;
Expand All @@ -111,14 +111,14 @@ public function containsSelect($selector = 'select', $callback = null): static
return $this;
}

protected function getSelectorFromAttributes($type, array $attributes): string
{
$selector = $type;

foreach ($attributes as $attribute => $value) {
$selector .= sprintf('[%s="%s"]', $attribute, $value);
}

return $selector;
}
// protected function getSelectorFromAttributes($type, array $attributes): string
// {
// $selector = $type;
//
// foreach ($attributes as $attribute => $value) {
// $selector .= sprintf('[%s="%s"]', $attribute, $value);
// }
//
// return $selector;
// }
}
65 changes: 0 additions & 65 deletions src/Asserts/OptionAssert.php

This file was deleted.

16 changes: 1 addition & 15 deletions src/Asserts/SelectAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,7 @@ public function __construct(string $html, $root)

public function containsOption(mixed $attributes): self
{
if (is_array($attributes)) {
return $this->contains('option', $attributes);
}

$this->gatherAttributes('option');

if (is_callable($attributes)) {
tap(
new OptionAssert(
$this->attributes['option']
), fn ($option) => $attributes($option)
)->validate();
}

return $this;
return $this->contains('option', $attributes);
}

public function containsOptions(...$attributes): self
Expand Down
2 changes: 1 addition & 1 deletion src/Asserts/Traits/CanGatherAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function gatherAttributes($type): void
foreach ($elements as $element) {
$attributes = [];
foreach ($element->attributes as $attribute) {
$attributes[$attribute->nodeName] = $attribute->value;
$attributes[$attribute->nodeName] = $attribute->value ?: true;
}

if ($type === 'textarea') {
Expand Down
15 changes: 12 additions & 3 deletions src/Asserts/Traits/HasElementAsserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __call(string $method, array $arguments)
{
if (Str::startsWith($method, 'has')) {
$property = Str::of($method)->after('has')->snake()->slug();
$this->has($property, $arguments[0]);
$this->has($property, $arguments[0] ?? null);
}

if (Str::startsWith($method, 'is')) {
Expand All @@ -23,7 +23,7 @@ public function __call(string $method, array $arguments)

if (Str::startsWith($method, 'find')) {
$property = Str::of($method)->after('find')->snake()->slug();
$this->find($property, $arguments[0]);
$this->find($property, $arguments[0] ?? null);
}

if (Str::startsWith($method, 'contains')) {
Expand All @@ -39,8 +39,17 @@ public function __call(string $method, array $arguments)
return $this;
}

public function has(string $attribute, mixed $value): self
public function has(string $attribute, mixed $value = null): self
{
if (! $value) {
PHPUnit::assertTrue(
$this->hasAttribute($attribute),
sprintf('Could not find an attribute "%s"', $attribute)
);

return $this;
}

PHPUnit::assertEquals(
$value,
$this->getAttribute($attribute),
Expand Down
9 changes: 9 additions & 0 deletions src/Asserts/Traits/InteractsWithParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ protected function getContent()

protected function getAttribute(string $attribute)
{
if ($this->getParser()->getType() === 'option' && $attribute === 'text') {
return $this->getParser()->getText();
}

return $this->getParser()->getAttributeForRoot($attribute);
}

protected function hasAttribute(string $attribute)
{
return $this->getParser()->hasAttributeForRoot($attribute);
}

protected function getAttributeFor($for, string $attribute)
{
return $this->getParser()->getAttributeFor($for, $attribute);
Expand Down
23 changes: 9 additions & 14 deletions src/DomParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ public function getElementOfType(string $type, $index = 0): ?DOMNode
return $this->getRoot()->getElementsByTagName($type)->item($index);
}

public function getFirstElementOfType(string $type): ?DOMNode
{
return $this->getRoot()->getElementsByTagName($type)->item(0);
}

public function getElementsByType(string $type): \DOMNodeList
{
return $this->getRoot()->getElementsByTagName($type);
}

public function getRoot(): DOMElement|DOMNode
{
return $this->root;
Expand All @@ -70,11 +60,10 @@ public function getAttributeForRoot(string $attribute)
return $this->root->getAttribute($attribute);
}

public function setRootFromString(string $root): static
{
$this->setRoot($this->getFirstElementOfType($root));

return $this;
public function hasAttributeForRoot(string $attribute)
{
return $this->root->hasAttribute($attribute);
}

public function getContent(): string
Expand Down Expand Up @@ -108,4 +97,10 @@ public function queryAll(string $selector): \DOMNodeList

return (new \DOMXPath($parser->getRoot()->ownerDocument))->query($converter->toXpath($selector));
}

public function getText()
{
return $this->getRoot()->nodeValue;
}

}
22 changes: 19 additions & 3 deletions tests/DomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@
'Element is not of type "div"'
);

it('can fail with wrong type of selector', function () {
$this->get('form')
->assertElement(['div']);
})->throws(AssertionFailedError::class, 'Invalid selector!');

it('can find a nested element', function () {
$this->get('nesting')
->assertElement(function (ElementAssert $element) {
$element->containsDiv();
}, 'div');
});
});

it('can find a nested element with content', function () {
Expand All @@ -58,16 +63,27 @@
$element->contains('div', [
'class' => 'foobar',
]);
}, 'div');
});
});

it('can fail finding a nested element with content', function () {
$this->get('nesting')
->assertElement(function (ElementAssert $element) {
$element->contains('div', [
'class' => 'foo',
]);
});
})->throws(AssertionFailedError::class, 'Could not find a matching "div" with data: {
"class": "foo"
}');

it('can find a nested element with content functional', function () {
$this->get('nesting')
->assertElement(function (ElementAssert $element) {
$element->findDiv(function (ElementAssert $element) {
$element->is('div');
});
}, 'div');
});
});

it('can find a nested element multiple levels', function () {
Expand Down
Loading

0 comments on commit aefb6e2

Please sign in to comment.