Skip to content

Commit

Permalink
Merge pull request #30 from veewee/dom-assert
Browse files Browse the repository at this point in the history
Add assertion methods
  • Loading branch information
veewee authored Mar 11, 2022
2 parents 75ec9f9 + e813453 commit 567c9bf
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/dom.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ $count = $xpath->evaluate('count(.//item)', Type\int(), $currentNode);
Of course, the example above only gives you a small idea of all the implemented features.
Let's find out more by segregating the DOM component into its composable blocks:

* [Assertions](#assertions): Assert if a DOMNode is of a specific type.
* [Builders](#builders): Let you build XML by using a declarative API.
* [Collection](#collection): A wrapper for dealing with lists of nodes.
* [Configurators](#configurators): Specify how you want to configure your DOM document.
Expand All @@ -54,6 +55,47 @@ Let's find out more by segregating the DOM component into its composable blocks:
* [XPath](#xpath): Query for specific elements based on XPath queries.


## Assertions

Assert if a DOMNode is of a specific type.

#### assert_document

Assert if a node is of type `DOMDocument`.

```php
use Psl\Type\Exception\AssertException;
use function VeeWee\Xml\Dom\Assert\assert_document;

try {
assert_document($someNode)
} catch (AssertException $e) {
// Deal with it
}
```

#### assert_element

Assert if a node is of type `DOMElement`.

```php
use Psl\Type\Exception\AssertException;
use VeeWee\XML\DOM\Document;
use function VeeWee\Xml\Dom\Assert\assert_element;

$doc = Document::fromXmlFile('some.xml');
$item = $doc->xpath()->query('item')->item(0);

use Psl\Type\Exception\AssertException;
use function VeeWee\Xml\Dom\Assert\assert_document;

try {
assert_element($someNode)
} catch (AssertException $e) {
// Deal with it
}
```

## Builders

Lets you build XML by using a declarative API.
Expand Down
19 changes: 19 additions & 0 deletions src/Xml/Dom/Assert/assert_document.php
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);
}
19 changes: 19 additions & 0 deletions src/Xml/Dom/Assert/assert_element.php
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);
}
2 changes: 2 additions & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php declare(strict_types=1);

require_once __DIR__.'/Xml/Dom/Assert/assert_document.php';
require_once __DIR__.'/Xml/Dom/Assert/assert_element.php';
require_once __DIR__.'/Xml/Dom/Builder/attribute.php';
require_once __DIR__.'/Xml/Dom/Builder/attributes.php';
require_once __DIR__.'/Xml/Dom/Builder/children.php';
Expand Down
46 changes: 46 additions & 0 deletions tests/Xml/Dom/Assert/AssertDocumentTest.php
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];
}
}
46 changes: 46 additions & 0 deletions tests/Xml/Dom/Assert/AssertElementTest.php
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];
}
}

0 comments on commit 567c9bf

Please sign in to comment.