PHP code example of thomasweinert / phpunit-xpath-assertions

1. Go to this page and download the library: Download thomasweinert/phpunit-xpath-assertions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thomasweinert / phpunit-xpath-assertions example snippets


use PHPUnit\Framework\TestCase;
use PHPUnit\Xpath\Assert as XpathAssertions;

class MyProjectExampleTest extends TestCase
{
    use XpathAssertions;

    public function testChildElementExistsInDocument()
    {
        $document = new \DOMDocument();
        $document->loadXML('<root><child>TEXT</child></root>');

        self::assertXpathMatch('//child', $document);
    }

    public function testCompareChildElementFromDocument()
    {
        $document = new \DOMDocument();
        $document->loadXML('<root><child>TEXT</child></root>');

        self::assertXpathEquals('<child>TEXT</child>', '//child', $document);
    }
}

use PHPUnit\Xpath\Assert as XpathAssertions;
use PHPUnit\Xpath\Constraint as XpathConstraints;

class MyProjectExampleTest extends \PHPUnit\Framework\TestCase
{
    use XpathAssertions;
    use XpathConstraints;
}

function matchesXpathExpression(string $expression, array|\ArrayAccess $namespaces = [])

public function testChildElementExistsInDocument()
{
    $document = new \DOMDocument();
    $document->loadXML('<root><child>TEXT</child></root>');

    self::assertThat(
        $document,
        self::matchesXpathExpression('//child')
    );
}

function matchesXpathResultCount(
    int $expectedCount, string $expression, array|\ArrayAccess $namespaces = array()
)

public function testChildElementExistsOnTimeInDocument()
{
    $document = new \DOMDocument();
    $document->loadXML('<root><child>TEXT</child></root>');

    self::assertThat(
        $document,
        self::matchesXpathResultCount(1, '//child')
    );
}

function equalToXpathResult(
    mixed $expected, 
    string $expression, 
    array|\ArrayAccess, 
    $namespaces = array()
)

public function testCompareChildElementFromDocument()
{
    $document = new \DOMDocument();
    $document->loadXML('<root><child>TEXT</child></root>');

    self::assertThat(
        $document,
        self::equalToXpathResult(
            '<child>TEXT</child>',
            '//child'
        )
    );
}

public function testCompareChildElementFromDocumentAsString()
{
    $document = new \DOMDocument();
    $document->loadXML('<root><child>TEXT</child></root>');

    self::assertThat(
        $document,
        self::equalToXpathResult(
            'TEXT',
            'string(//child)'
        )
    );
}

public function testChildWithNamespaceElementExistsTwoTimesInDocument()
{
    $document = new \DOMDocument();
    $document->loadXML(
        '<example:root xmlns:example="urn:example">
        <example:child>TEXT</example:child>
        <example:child>TEXT</example:child>
        </example:root>'
    );

    self::assertThat(
        $document,
        self::matchesXpathResultCount(2, '//e:child', ['e' => 'urn:example'])
    );
}

public function testHomePhoneNumbersEqualsExpected()
{
    self::assertXpathEquals(
        [
            [ 'type' => 'home', 'number' => '212 555-1234' ]
        ],
        'phoneNumbers/*[type="home"]',
        json_decode($wikipediaJsonExample)
    );
}