Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ $this->get('/some-route')
});
```


For simple and quick checks, you can use `assertContainsElement`.
This method allows you to verify that a specific element exists on the page you can optionally include an array of expected attributes.
```
$this->get('/some-route')
->assertContainsElement('#content')
->assertContainsElement('div.banner', ['text' => 'Successfully deleted', 'data-status' => 'success']);
```

### Testing forms
Testing forms allows using all the dom asserts from above, but has a few special helpers to help test for forms.
Instead of using `->assertElementExists()` you can use `->assertFormExists()`, or the alias `assertForm()` on the test response.
Expand Down
18 changes: 18 additions & 0 deletions ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public function assertFormExists($selector = 'form', $callback = null)
/** @var \Illuminate\Testing\TestResponse $instance */
return $instance;
}

public function assertContainsElement($selector, array $attributes = [])
{
/** @var \Illuminate\Testing\TestResponse $instance */
return $instance;
}
}

class TestView
Expand Down Expand Up @@ -65,6 +71,12 @@ public function assertFormExists($selector = 'form', $callback = null)
/** @var \Illuminate\Testing\TestResponse $instance */
return $instance;
}

public function assertContainsElement($selector, array $attributes = [])
{
/** @var \Illuminate\Testing\TestResponse $instance */
return $instance;
}
}

class TestComponent
Expand Down Expand Up @@ -98,5 +110,11 @@ public function assertFormExists($selector = 'form', $callback = null)
/** @var \Illuminate\Testing\TestResponse $instance */
return $instance;
}

public function assertContainsElement($selector, array $attributes = [])
{
/** @var \Illuminate\Testing\TestComponent $instance */
return $instance;
}
}
}
69 changes: 69 additions & 0 deletions src/TestComponentMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sinnbeck\DomAssertions;

use Closure;
use DOMElement;
use DOMException;
use Illuminate\Testing\TestComponent;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -85,6 +86,74 @@ public function assertElementExists(): Closure
};
}

public function assertContainsElement(): Closure
{
return function (string $selector, array $attributes = []): TestComponent {
/** @var TestComponent $this */
Assert::assertNotEmpty(
(string) $this,
'The component is empty!'
);

try {
$parser = DomParser::new((string) $this);
} catch (DOMException $exception) {
Assert::fail($exception->getMessage());
}

$element = $parser->query($selector);

Assert::assertNotNull(
$element,
sprintf('No element found with selector: %s', $selector)
);

if (! $element instanceof DOMElement) {
Assert::fail('The element found is not a DOMElement!');
}

foreach ($attributes as $attribute => $expected) {
switch ($attribute) {
case 'text':
$actual = trim($element->textContent);
Assert::assertStringContainsString(
$expected,
$actual,
sprintf(
'Failed asserting that element [%s] text contains "%s". Actual: "%s".',
$selector,
$expected,
$actual
)
);
break;

default:
$actual = $element->getAttribute($attribute);
Assert::assertNotEmpty(
$actual,
sprintf('Attribute [%s] not found in element [%s].', $attribute, $selector)
);

Assert::assertStringContainsString(
$expected,
$actual,
sprintf(
'Failed asserting that attribute [%s] of element [%s] contains "%s". Actual: "%s".',
$attribute,
$selector,
$expected,
$actual
)
);
break;
}
}

return $this;
};
}

public function assertForm(): Closure
{
return $this->assertFormExists();
Expand Down
69 changes: 69 additions & 0 deletions src/TestResponseMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sinnbeck\DomAssertions;

use Closure;
use DOMElement;
use DOMException;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -85,6 +86,74 @@ public function assertElementExists(): Closure
};
}

public function assertContainsElement(): Closure
{
return function (string $selector, array $attributes = []): TestResponse {
/** @var TestResponse $this */
Assert::assertNotEmpty(
(string) $this->getContent(),
'The response is empty!'
);

try {
$parser = DomParser::new($this->getContent());
} catch (DOMException $exception) {
Assert::fail($exception->getMessage());
}

$element = $parser->query($selector);

Assert::assertNotNull(
$element,
sprintf('No element found with selector: %s', $selector)
);

if (! $element instanceof DOMElement) {
Assert::fail('The element found is not a DOMElement!');
}

foreach ($attributes as $attribute => $expected) {
switch ($attribute) {
case 'text':
$actual = trim($element->textContent);
Assert::assertStringContainsString(
$expected,
$actual,
sprintf(
'Failed asserting that element [%s] text contains "%s". Actual: "%s".',
$selector,
$expected,
$actual
)
);
break;

default:
$actual = $element->getAttribute($attribute);
Assert::assertNotEmpty(
$actual,
sprintf('Attribute [%s] not found in element [%s].', $attribute, $selector)
);

Assert::assertStringContainsString(
$expected,
$actual,
sprintf(
'Failed asserting that attribute [%s] of element [%s] contains "%s". Actual: "%s".',
$attribute,
$selector,
$expected,
$actual
)
);
break;
}
}

return $this;
};
}

public function assertForm(): Closure
{
return $this->assertFormExists();
Expand Down
70 changes: 70 additions & 0 deletions src/TestViewMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sinnbeck\DomAssertions;

use Closure;
use DOMElement;
use DOMException;
use Illuminate\Testing\TestView;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -85,6 +86,75 @@ public function assertElementExists(): Closure
};
}

public function assertContainsElement(): Closure
{
return function (string $selector, array $attributes = []): 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());
}

$element = $parser->query($selector);

Assert::assertNotNull(
$element,
sprintf('No element found with selector: %s', $selector)
);

if (! $element instanceof DOMElement) {
Assert::fail('The element found is not a DOMElement!');
}

foreach ($attributes as $attribute => $expected) {
switch ($attribute) {
case 'text':
$actual = trim($element->textContent);
Assert::assertStringContainsString(
$expected,
$actual,
sprintf(
'Failed asserting that element [%s] text contains "%s". Actual: "%s".',
$selector,
$expected,
$actual
)
);
break;

default:
$actual = $element->getAttribute($attribute);

Assert::assertNotEmpty(
$actual,
sprintf('Attribute [%s] not found in element [%s].', $attribute, $selector)
);

Assert::assertStringContainsString(
$expected,
$actual,
sprintf(
'Failed asserting that attribute [%s] of element [%s] contains "%s". Actual: "%s".',
$attribute,
$selector,
$expected,
$actual
)
);
break;
}
}

return $this;
};
}

public function assertForm(): Closure
{
return $this->assertFormExists();
Expand Down
21 changes: 21 additions & 0 deletions tests/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@
});
});

it('assertContainsElement works as expects', function () {
$this->component(NestedComponent::class)
->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo'])
->assertContainsElement('nav');
});

it('assertContainsElement throws if selector not found', function () {
$this->component(NestedComponent::class)
->assertContainsElement('span.non-existing', ['text' => 'Foo']);
})->throws(AssertionFailedError::class, 'No element found with selector: span.non-existing');

it('assertContainsElement throws if contains text does not exist', function () {
$this->component(NestedComponent::class)
->assertContainsElement('span.foo', ['text' => 'non-existing']);
})->throws(AssertionFailedError::class, 'Failed asserting that element [span.foo] text contains "non-existing". Actual: "Foo"');

it('assertContainsElement throws if contains attribute does not exist', function () {
$this->view('nesting')
->assertContainsElement('span.foo', ['non-existing-attribute' => 'non-existing']);
})->throws(AssertionFailedError::class, 'Attribute [non-existing-attribute] not found in element [span.foo]');

it('can handle an empty component', function () {
$this->component(EmptyComponent::class)
->assertElementExists();
Expand Down
21 changes: 21 additions & 0 deletions tests/DomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@
});
});

it('assertContainsElement works as expects', function () {
$this->get('nesting')
->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo'])
->assertContainsElement('nav');
});

it('assertContainsElement throws if selector not found', function () {
$this->get('nesting')
->assertContainsElement('span.non-existing', ['text' => 'Foo']);
})->throws(AssertionFailedError::class, 'No element found with selector: span.non-existing');

it('assertContainsElement throws if contains text does not exist', function () {
$this->get('nesting')
->assertContainsElement('span.foo', ['text' => 'non-existing']);
})->throws(AssertionFailedError::class, 'Failed asserting that element [span.foo] text contains "non-existing". Actual: "Foo"');

it('assertContainsElement throws if contains attribute does not exist', function () {
$this->view('nesting')
->assertContainsElement('span.foo', ['non-existing-attribute' => 'non-existing']);
})->throws(AssertionFailedError::class, 'Attribute [non-existing-attribute] not found in element [span.foo]');

it('can handle an empty view', function () {
$this->get('empty')
->assertElementExists();
Expand Down
21 changes: 21 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
use PHPUnit\Framework\AssertionFailedError;
use Sinnbeck\DomAssertions\Asserts\AssertElement;

it('assertContainsElement works as expects', function () {
$this->view('nesting')
->assertContainsElement('span.foo', ['text' => 'Foo'])
->assertContainsElement('nav');
});

it('assertContainsElement throws if selector not found', function () {
$this->view('nesting')
->assertContainsElement('span.non-existing', ['text' => 'Foo']);
})->throws(AssertionFailedError::class, 'No element found with selector: span.non-existing');

it('assertContainsElement throws if contains text does not exist', function () {
$this->view('nesting')
->assertContainsElement('span.foo', ['text' => 'non-existing']);
})->throws(AssertionFailedError::class, 'Failed asserting that element [span.foo] text contains "non-existing". Actual: "Foo"');

it('assertContainsElement throws if contains attribute does not exist', function () {
$this->view('nesting')
->assertContainsElement('span.foo', ['non-existing-attribute' => 'non-existing']);
})->throws(AssertionFailedError::class, 'Attribute [non-existing-attribute] not found in element [span.foo]');

it('assertElement alias works for assertElementExists', function () {
$this->view('nesting')
->assertElement('body', function (AssertElement $assert) {
Expand Down
Loading
Loading