|
| 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\Test\XML\SerializableXMLTestTrait; |
| 9 | +use SimpleSAML\XML\DOMDocumentFactory; |
| 10 | +use SimpleSAML\XMLSecurity\XML\ds\XPath; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class \SimpleSAML\XMLSecurity\Test\XML\ds\XPathTest |
| 14 | + * |
| 15 | + * @covers \SimpleSAML\XMLSecurity\XML\ds\XPath |
| 16 | + * |
| 17 | + * @package simplesamlphp/xml-security |
| 18 | + */ |
| 19 | +class XPathTest extends TestCase |
| 20 | +{ |
| 21 | + use SerializableXMLTestTrait; |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + */ |
| 26 | + public function setUp(): void |
| 27 | + { |
| 28 | + $this->testedClass = XPath::class; |
| 29 | + |
| 30 | + $this->xmlRepresentation = DOMDocumentFactory::fromFile( |
| 31 | + dirname(dirname(dirname(__FILE__))) . '/resources/xml/ds_XPath.xml' |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + public function testMarshalling(): void |
| 37 | + { |
| 38 | + $xpath = new XPath( |
| 39 | + 'self::xenc:CipherValue[@Id="example1"]', |
| 40 | + [ |
| 41 | + 'xenc' => 'http://www.w3.org/2001/04/xmlenc#' |
| 42 | + ] |
| 43 | + ); |
| 44 | + |
| 45 | + $this->assertEquals('self::xenc:CipherValue[@Id="example1"]', $xpath->getExpression()); |
| 46 | + $namespaces = $xpath->getNamespaces(); |
| 47 | + $this->assertCount(1, $namespaces); |
| 48 | + $this->assertArrayHasKey('xenc', $namespaces); |
| 49 | + $this->assertEquals('http://www.w3.org/2001/04/xmlenc#', $namespaces['xenc']); |
| 50 | + |
| 51 | + $this->assertEquals( |
| 52 | + $this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), |
| 53 | + strval($xpath) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + */ |
| 60 | + public function testUnmarshalling(): void |
| 61 | + { |
| 62 | + $xpath = XPath::fromXML($this->xmlRepresentation->documentElement); |
| 63 | + $this->assertEquals('self::xenc:CipherValue[@Id="example1"]', $xpath->getExpression()); |
| 64 | + $namespaces = $xpath->getNamespaces(); |
| 65 | + $this->assertCount(2, $namespaces); |
| 66 | + $this->assertEquals('xenc', array_keys($namespaces)[0]); |
| 67 | + $this->assertEquals('ds', array_keys($namespaces)[1]); |
| 68 | + |
| 69 | + |
| 70 | + $this->assertEquals( |
| 71 | + $this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), |
| 72 | + strval($xpath) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments