-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from veewee/dom-assert
Add assertion methods
- Loading branch information
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Dom\Assert; | ||
|
||
use DOMDocument; | ||
use DOMNode; | ||
use Psl\Type\Exception\AssertException; | ||
use function Psl\Type\object; | ||
|
||
/** | ||
* @psalm-assert DOMDocument $node | ||
* @throws AssertException | ||
*/ | ||
function assert_document(?DOMNode $node): DOMDocument | ||
{ | ||
return object(DOMDocument::class)->assert($node); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Dom\Assert; | ||
|
||
use DOMElement; | ||
use DOMNode; | ||
use Psl\Type\Exception\AssertException; | ||
use function Psl\Type\object; | ||
|
||
/** | ||
* @psalm-assert DOMElement $node | ||
* @throws AssertException | ||
*/ | ||
function assert_element(?DOMNode $node): DOMElement | ||
{ | ||
return object(DOMElement::class)->assert($node); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Tests\Xml\Dom\Assert; | ||
|
||
use DOMNode; | ||
use PHPUnit\Framework\TestCase; | ||
use Psl\Type\Exception\AssertException; | ||
use VeeWee\Xml\Dom\Document; | ||
use function VeeWee\Xml\Dom\Assert\assert_document; | ||
|
||
final class AssertDocumentTest extends TestCase | ||
{ | ||
/** | ||
* | ||
* @dataProvider provideTestCases | ||
*/ | ||
public function test_it_knows_documents(?DOMNode $node, bool $expected): void | ||
{ | ||
if (!$expected) { | ||
$this->expectException(AssertException::class); | ||
} | ||
|
||
$actual = assert_document($node); | ||
static::assertSame($node, $actual); | ||
} | ||
|
||
public function provideTestCases() | ||
{ | ||
$doc = Document::fromXmlString( | ||
<<<EOXML | ||
<doc> | ||
<item attr="val">Hello</item> | ||
</doc> | ||
EOXML | ||
)->toUnsafeDocument(); | ||
|
||
yield [$doc, true]; | ||
yield [$doc->documentElement, false]; | ||
yield [$doc->documentElement->firstElementChild, false]; | ||
yield [$doc->documentElement->firstElementChild->attributes->getNamedItem('attr'), false]; | ||
yield [$doc->documentElement->firstElementChild->firstChild, false]; | ||
yield [null, false]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Tests\Xml\Dom\Assert; | ||
|
||
use DOMNode; | ||
use PHPUnit\Framework\TestCase; | ||
use Psl\Type\Exception\AssertException; | ||
use VeeWee\Xml\Dom\Document; | ||
use function VeeWee\Xml\Dom\Assert\assert_element; | ||
|
||
final class AssertElementTest extends TestCase | ||
{ | ||
/** | ||
* | ||
* @dataProvider provideTestCases | ||
*/ | ||
public function test_it_knows_elements(?DOMNode $node, bool $expected): void | ||
{ | ||
if (!$expected) { | ||
$this->expectException(AssertException::class); | ||
} | ||
|
||
$actual = assert_element($node); | ||
static::assertSame($node, $actual); | ||
} | ||
|
||
public function provideTestCases() | ||
{ | ||
$doc = Document::fromXmlString( | ||
<<<EOXML | ||
<doc> | ||
<item attr="val">Hello</item> | ||
</doc> | ||
EOXML | ||
)->toUnsafeDocument(); | ||
|
||
yield [$doc, false]; | ||
yield [$doc->documentElement, true]; | ||
yield [$doc->documentElement->firstElementChild, true]; | ||
yield [$doc->documentElement->firstElementChild->attributes->getNamedItem('attr'), false]; | ||
yield [$doc->documentElement->firstElementChild->firstChild, false]; | ||
yield [null, false]; | ||
} | ||
} |