From c40b5e6adcbc8546ab8c207e27a62f51b3f0b773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Sinnbeck?= Date: Sun, 23 Oct 2022 18:02:09 +0200 Subject: [PATCH] clean up assertion failure --- src/Asserts/Traits/UsesElementAsserts.php | 3 ++- tests/DomTest.php | 25 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Asserts/Traits/UsesElementAsserts.php b/src/Asserts/Traits/UsesElementAsserts.php index f63a939..11cbf38 100644 --- a/src/Asserts/Traits/UsesElementAsserts.php +++ b/src/Asserts/Traits/UsesElementAsserts.php @@ -101,8 +101,9 @@ public function contains(string $elementName, $attributes = null, $count = 0): s public function doesntContain(string $elementName, array $attributes = []): self { if (! $attributes) { + $found = $this->getParser()->query($elementName); Assert::assertNull( - $this->getParser()->query($elementName), + $found ? get_class($found): null, sprintf('Found a matching element of type "%s"', $elementName) ); diff --git a/tests/DomTest.php b/tests/DomTest.php index 412eef8..ab38e75 100644 --- a/tests/DomTest.php +++ b/tests/DomTest.php @@ -212,3 +212,28 @@ }); }); }); + +it('can fail finding an contained element', function () { + $this->get('nesting') + ->assertElement(function (ElementAssert $element) { + $element->findDiv(function (ElementAssert $element) { + $element->doesntContain('div'); + }); + }); +})->throws( + AssertionFailedError::class, + 'Found a matching element of type "div' +); + + +it('can fail finding an contained element with query', function () { + $this->get('nesting') + ->assertElement(function (ElementAssert $element) { + $element->findDiv(function (ElementAssert $element) { + $element->doesntContain('div.foobar'); + }); + }); +})->throws( + AssertionFailedError::class, + 'Found a matching element of type "div' +);