Skip to content

Commit

Permalink
fix wrong assertions and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
sinnbeck committed Oct 19, 2022
1 parent 4f81f07 commit 3da1192
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ $this->get('/some-route')
```
For even more power you are allowed to use a closure as the second argument. This lets you traverse the dom as deep as you need to.
```php
$this->get('nesting')
$this->get('/some-route')
->assertElement(function (ElementAssert $element) {
$element->contains('div', function (ElementAssert $element) {
$element->is('div');
Expand Down
25 changes: 0 additions & 25 deletions src/Asserts/FormAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,6 @@ public function hasCSRF(): self
return $this;
}

public function containsTextarea(array $attributes): self
{
return $this->contains('textarea', $attributes);
}

public function containsInput(array $attributes): self
{
Assert::assertNotNull(
$this->makeScopedParser('form')->query($this->getSelectorFromAttributes('input', $attributes)),
sprintf('Could not find a matching input with data: %s', json_encode($attributes, JSON_PRETTY_PRINT))
);

return $this;
}

public function doesntContainInput(array $attributes, $name = ''): self
{
Assert::assertNull(
$this->makeScopedParser('form')->query($this->getSelectorFromAttributes('input', $attributes)),
sprintf('Found a matching input with data: %s', json_encode($attributes, JSON_PRETTY_PRINT))
);

return $this;
}

protected function getAttributeFromForm(string $attribute)
{
return $this->parser->getAttributeForRoot($attribute);
Expand Down
51 changes: 43 additions & 8 deletions src/Asserts/Traits/HasElementAsserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public function __call(string $method, array $arguments)
$this->contains($elementName, ...$arguments);
}

if (Str::startsWith($method, 'doesntContain')) {
$elementName = Str::of($method)->after('doesntContain')->camel();
$this->doesntContain($elementName, ...$arguments);
}

if (Str::startsWith($method, 'has')) {
$property = Str::of($method)->after('has')->snake()->slug('-');
$this->has($property, $arguments[0]);
Expand Down Expand Up @@ -68,7 +73,7 @@ public function contains(string $elementName, mixed $attributes = null): self
$this->gatherAttributes($elementName);

$first = collect($this->attributes[$elementName])
->search(fn ($input) => array_intersect_key($attributes, $input) === $attributes);
->search(fn ($attribute) => $this->compareAttributesArrays($attributes, $attribute));

Assert::assertNotFalse(
$first,
Expand All @@ -78,6 +83,41 @@ public function contains(string $elementName, mixed $attributes = null): self
return $this;
}

public function doesntContain(string $elementName, array $attributes = []): self
{
if (! $attributes) {
Assert::assertNull(
$this->parser->query($elementName),
sprintf('Found a matching element of type "%s"', $elementName)
);

return $this;
}

if (! preg_match('/^[\w]+$/', $elementName)) {
$element = $this->parser->query($elementName);
foreach ($attributes as $attribute => $value) {
Assert::assertEquals(
$value,
$this->getAttributeFor($element, $attribute),
sprintf('Found attribute "%s" with value "%s"', $attribute, $value)
);
}
}

$this->gatherAttributes($elementName);

$first = collect($this->attributes[$elementName])
->search(fn ($foundAttributes) => $this->compareAttributesArrays($attributes, $foundAttributes));

Assert::assertFalse(
$first,
sprintf('Found a matching %s with data: %s', $elementName, json_encode($attributes, JSON_PRETTY_PRINT))
);

return $this;
}

public function is(string $type): self
{
PHPUnit::assertEquals(
Expand All @@ -89,13 +129,8 @@ public function is(string $type): self
return $this;
}

protected function getAttribute(string $attribute)
{
return $this->parser->getAttributeForRoot($attribute);
}

protected function getAttributeFor($for, string $attribute)
private function compareAttributesArrays($attributes, $foundAttributes)
{
return $this->parser->getAttributeFor($for, $attribute);
return ! array_diff($attributes, $foundAttributes);
}
}
10 changes: 10 additions & 0 deletions src/Asserts/Traits/InteractsWithParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ protected function getContent()
{
return $this->parser->getContent();
}

protected function getAttribute(string $attribute)
{
return $this->parser->getAttributeForRoot($attribute);
}

protected function getAttributeFor($for, string $attribute)
{
return $this->parser->getAttributeFor($for, $attribute);
}
}
2 changes: 1 addition & 1 deletion tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function (OptionAssert $optionAssert) {
$this->get('form')
->assertForm(function (FormAssert $form) {
$form->containsLabel([
'for' => 'bar',
'for' => 'comment',
]);
})->assertOk();
});

0 comments on commit 3da1192

Please sign in to comment.