Passed
Branch master (c86cc6)
by Tim
01:57
created

XPathTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testUnmarshalling() 0 13 1
A setUpBeforeClass() 0 6 1
A testMarshalling() 0 18 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
6
7
use PHPUnit\Framework\TestCase;
8
use SimpleSAML\XML\DOMDocumentFactory;
9
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
10
use SimpleSAML\XMLSecurity\XML\ds\XPath;
11
12
use function dirname;
13
use function strval;
14
15
/**
16
 * Class \SimpleSAML\XMLSecurity\Test\XML\ds\XPathTest
17
 *
18
 * @covers \SimpleSAML\XMLSecurity\XML\ds\XPath
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
class XPathTest extends TestCase
23
{
24
    use SerializableElementTestTrait;
0 ignored issues
show
Bug introduced by
The trait SimpleSAML\XML\TestUtils...lizableElementTestTrait requires the property $documentElement which is not provided by SimpleSAML\XMLSecurity\Test\XML\ds\XPathTest.
Loading history...
25
26
27
    /**
28
     */
29
    public static function setUpBeforeClass(): void
30
    {
31
        self::$testedClass = XPath::class;
32
33
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
34
            dirname(__FILE__, 3) . '/resources/xml/ds_XPath.xml',
35
        );
36
    }
37
38
39
    public function testMarshalling(): void
40
    {
41
        $xpath = new XPath(
42
            'self::xenc:CipherValue[@Id="example1"]',
43
            [
44
                'xenc' => 'http://www.w3.org/2001/04/xmlenc#',
45
            ],
46
        );
47
48
        $this->assertEquals('self::xenc:CipherValue[@Id="example1"]', $xpath->getExpression());
49
        $namespaces = $xpath->getNamespaces();
50
        $this->assertCount(1, $namespaces);
51
        $this->assertArrayHasKey('xenc', $namespaces);
0 ignored issues
show
Bug introduced by
It seems like $namespaces can also be of type Countable; however, parameter $array of PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept ArrayAccess|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->assertArrayHasKey('xenc', /** @scrutinizer ignore-type */ $namespaces);
Loading history...
52
        $this->assertEquals('http://www.w3.org/2001/04/xmlenc#', $namespaces['xenc']);
53
54
        $this->assertEquals(
55
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
56
            strval($xpath),
57
        );
58
    }
59
60
61
    /**
62
     */
63
    public function testUnmarshalling(): void
64
    {
65
        $xpath = XPath::fromXML(self::$xmlRepresentation->documentElement);
66
        $this->assertEquals('self::xenc:CipherValue[@Id="example1"]', $xpath->getExpression());
67
        $namespaces = $xpath->getNamespaces();
68
        $this->assertCount(2, $namespaces);
69
        $this->assertEquals('xenc', array_keys($namespaces)[0]);
0 ignored issues
show
Bug introduced by
$namespaces of type Countable|iterable is incompatible with the type array expected by parameter $array of array_keys(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
        $this->assertEquals('xenc', array_keys(/** @scrutinizer ignore-type */ $namespaces)[0]);
Loading history...
70
        $this->assertEquals('ds', array_keys($namespaces)[1]);
71
72
73
        $this->assertEquals(
74
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
75
            strval($xpath),
76
        );
77
    }
78
}
79