Skip to content

Commit 30dd424

Browse files
authored
Migrate Serializable tests to traits (#5)
* Bump xml-common * Remove test-code * Migrate SerializableXML tests to traits
1 parent 952639d commit 30dd424

15 files changed

Lines changed: 125 additions & 151 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
"robrichards/xmlseclibs": "^3.1.1",
4242
"simplesamlphp/assert": "~0.1.0",
43-
"simplesamlphp/xml-common": "~0.2.1"
43+
"simplesamlphp/xml-common": "~0.3.0"
4444
},
4545
"require-dev": {
4646
"simplesamlphp/simplesamlphp-test-framework": "^1.0.5"

src/XMLSecurityDSig.php

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -46,67 +46,4 @@
4646

4747
class XMLSecurityDSig extends \RobRichards\XMLSecLibs\XMLSecurityDSig
4848
{
49-
/** @var DomXPath|null */
50-
private ?DOMXPath $xPathCtx = null;
51-
52-
53-
/**
54-
* @return bool
55-
* @throws Exception
56-
*/
57-
public function validateReference()
58-
{
59-
$sigNode = $this->sigNode;
60-
$docElem = $sigNode->ownerDocument->documentElement;
61-
62-
// enveloped signature, remove it
63-
if (!$docElem->isSameNode($sigNode)) {
64-
if ($sigNode->parentNode !== null) {
65-
$sigNode->parentNode->removeChild($sigNode);
66-
}
67-
}
68-
$xpath = $this->getXPathObj();
69-
$query = "./secdsig:SignedInfo[1]/secdsig:Reference";
70-
$nodeset = $xpath->query($query, $sigNode);
71-
if ($nodeset->length < 1) {
72-
throw new Exception("Reference nodes not found");
73-
}
74-
75-
/* Initialize/reset the list of validated nodes. */
76-
$this->validatedNodes = [];
77-
78-
foreach ($nodeset as $refNode) {
79-
if (!$this->processRefNode($refNode)) {
80-
/* Clear the list of validated nodes. */
81-
$this->validatedNodes = null;
82-
throw new Exception("Reference validation failed");
83-
}
84-
}
85-
return true;
86-
}
87-
88-
89-
/**
90-
* Returns the XPathObj or null if xPathCtx is set and sigNode is empty.
91-
*
92-
* @return DOMXPath|null
93-
*/
94-
private function getXPathObj()
95-
{
96-
if (empty($this->xPathCtx) && ! empty($this->sigNode)) {
97-
$xpath = new DOMXPath($this->sigNode->ownerDocument);
98-
$xpath->registerNamespace('secdsig', self::XMLDSIGNS);
99-
$this->xPathCtx = $xpath;
100-
}
101-
return $this->xPathCtx;
102-
}
103-
104-
105-
/**
106-
* Reset the XPathObj to null
107-
*/
108-
private function resetXPathObj()
109-
{
110-
$this->xPathCtx = null;
111-
}
11249
}

tests/XML/ds/KeyInfoTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
66

77
use DOMDocument;
8+
use PHPUnit\Framework\TestCase;
89
use SimpleSAML\Assert\AssertionFailedException;
9-
use SimpleSAML\Test\XML\SerializableXMLTest;
10+
use SimpleSAML\Test\XML\SerializableXMLTestTrait;
1011
use SimpleSAML\XML\DOMDocumentFactory;
1112
use SimpleSAML\XML\Chunk;
1213
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
@@ -25,8 +26,10 @@
2526
*
2627
* @package simplesamlphp/xml-security
2728
*/
28-
final class KeyInfoTest extends SerializableXMLTest
29+
final class KeyInfoTest extends TestCase
2930
{
31+
use SerializableXMLTestTrait;
32+
3033
/** @var string */
3134
private string $certificate;
3235

@@ -38,9 +41,9 @@ final class KeyInfoTest extends SerializableXMLTest
3841
*/
3942
public function setUp(): void
4043
{
41-
self::$element = KeyInfo::class;
44+
$this->testedClass = KeyInfo::class;
4245

43-
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
46+
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
4447
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_KeyInfo.xml'
4548
);
4649

@@ -99,7 +102,7 @@ public function testMarshalling(): void
99102
$this->assertInstanceOf(Chunk::class, $info[3]);
100103
$this->assertEquals('abc123', $keyInfo->getId());
101104

102-
$this->assertEquals(self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($keyInfo));
105+
$this->assertEquals($this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), strval($keyInfo));
103106
}
104107

105108

@@ -118,7 +121,7 @@ public function testMarshallingEmpty(): void
118121
*/
119122
public function testUnmarshalling(): void
120123
{
121-
$keyInfo = KeyInfo::fromXML(self::$xmlRepresentation->documentElement);
124+
$keyInfo = KeyInfo::fromXML($this->xmlRepresentation->documentElement);
122125
$this->assertEquals('abc123', $keyInfo->getId());
123126

124127
$info = $keyInfo->getInfo();

tests/XML/ds/KeyNameTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
66

77
use DOMDocument;
8-
use SimpleSAML\Test\XML\SerializableXMLTest;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\Test\XML\SerializableXMLTestTrait;
910
use SimpleSAML\XML\DOMDocumentFactory;
1011
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
1112
use SimpleSAML\XMLSecurity\XMLSecurityDSig;
@@ -18,15 +19,17 @@
1819
*
1920
* @package simplesamlphp/xml-security
2021
*/
21-
final class KeyNameTest extends SerializableXMLTest
22+
final class KeyNameTest extends TestCase
2223
{
24+
use SerializableXMLTestTrait;
25+
2326
/**
2427
*/
2528
protected function setUp(): void
2629
{
27-
self::$element = KeyName::class;
30+
$this->testedClass = KeyName::class;
2831

29-
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
32+
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
3033
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_KeyName.xml'
3134
);
3235
}
@@ -40,15 +43,15 @@ public function testMarshalling(): void
4043

4144
$this->assertEquals('testkey', $keyName->getName());
4245

43-
$this->assertEquals(self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($keyName));
46+
$this->assertEquals($this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), strval($keyName));
4447
}
4548

4649

4750
/**
4851
*/
4952
public function testUnmarshalling(): void
5053
{
51-
$keyName = KeyName::fromXML(self::$xmlRepresentation->documentElement);
54+
$keyName = KeyName::fromXML($this->xmlRepresentation->documentElement);
5255

5356
$this->assertEquals('testkey', $keyName->getName());
5457
}

tests/XML/ds/X509CertificateTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
66

77
use DOMDocument;
8-
use SimpleSAML\Test\XML\SerializableXMLTest;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\Test\XML\SerializableXMLTestTrait;
910
use SimpleSAML\XML\DOMDocumentFactory;
1011
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
1112
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
@@ -18,8 +19,10 @@
1819
*
1920
* @package simplesamlphp/xml-security
2021
*/
21-
final class X509CertificateTest extends SerializableXMLTest
22+
final class X509CertificateTest extends TestCase
2223
{
24+
use SerializableXMLTestTrait;
25+
2326
/** @var string */
2427
private string $certificate;
2528

@@ -28,9 +31,9 @@ final class X509CertificateTest extends SerializableXMLTest
2831
*/
2932
public function setUp(): void
3033
{
31-
self::$element = X509Certificate::class;
34+
$this->testedClass = X509Certificate::class;
3235

33-
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
36+
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
3437
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_X509Certificate.xml'
3538
);
3639

@@ -64,16 +67,15 @@ public function testMarshalling(): void
6467

6568
$this->assertEquals($this->certificate, $X509cert->getCertificate());
6669

67-
$this->assertEquals(self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($X509cert));
70+
$this->assertEquals($this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), strval($X509cert));
6871
}
6972

7073

7174
/**
7275
*/
7376
public function testUnmarshalling(): void
7477
{
75-
$X509cert = X509Certificate::fromXML(self::$xmlRepresentation->documentElement);
76-
78+
$X509cert = X509Certificate::fromXML($this->xmlRepresentation->documentElement);
7779
$this->assertEquals($this->certificate, $X509cert->getCertificate());
7880
}
7981
}

tests/XML/ds/X509DataTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
66

77
use DOMDocument;
8+
use PHPUnit\Framework\TestCase;
89
use SimpleSAML\Assert\AssertionFailedException;
9-
use SimpleSAML\Test\XML\SerializableXMLTest;
10+
use SimpleSAML\Test\XML\SerializableXMLTestTrait;
1011
use SimpleSAML\XML\Chunk;
1112
use SimpleSAML\XML\DOMDocumentFactory;
1213
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
@@ -23,8 +24,10 @@
2324
*
2425
* @package simplesamlphp/xml-security
2526
*/
26-
final class X509DataTest extends SerializableXMLTest
27+
final class X509DataTest extends TestCase
2728
{
29+
use SerializableXMLTestTrait;
30+
2831
/** @var string */
2932
private string $certificate;
3033

@@ -36,9 +39,9 @@ final class X509DataTest extends SerializableXMLTest
3639
*/
3740
public function setUp(): void
3841
{
39-
self::$element = X509Data::class;
42+
$this->testedClass = X509Data::class;
4043

41-
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
44+
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
4245
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_X509Data.xml'
4346
);
4447

@@ -90,15 +93,15 @@ public function testMarshalling(): void
9093
$this->assertInstanceOf(X509SubjectName::class, $data[2]);
9194
$this->assertInstanceOf(Chunk::class, $data[3]);
9295

93-
$this->assertEquals(self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($X509data));
96+
$this->assertEquals($this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), strval($X509data));
9497
}
9598

9699

97100
/**
98101
*/
99102
public function testUnmarshalling(): void
100103
{
101-
$X509data = X509Data::fromXML(self::$xmlRepresentation->documentElement);
104+
$X509data = X509Data::fromXML($this->xmlRepresentation->documentElement);
102105

103106
$data = $X509data->getData();
104107
$this->assertInstanceOf(Chunk::class, $data[0]);

tests/XML/ds/X509SubjectNameTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use DOMDocument;
88
use PHPUnit\Framework\TestCase;
9-
use SimpleSAML\Test\XML\SerializableXMLTest;
9+
use SimpleSAML\Test\XML\SerializableXMLTestTrait;
1010
use SimpleSAML\XML\DOMDocumentFactory;
1111
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName;
1212
use SimpleSAML\XMLSecurity\XMLSecurityDSig;
@@ -19,15 +19,17 @@
1919
*
2020
* @package simplesamlphp/xml-security
2121
*/
22-
final class X509SubjectNameTest extends SerializableXMLTest
22+
final class X509SubjectNameTest extends TestCase
2323
{
24+
use SerializableXMLTestTrait;
25+
2426
/**
2527
*/
2628
protected function setUp(): void
2729
{
28-
self::$element = X509SubjectName::class;
30+
$this->testedClass = X509SubjectName::class;
2931

30-
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
32+
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
3133
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_X509SubjectName.xml'
3234
);
3335
}
@@ -41,15 +43,15 @@ public function testMarshalling(): void
4143

4244
$this->assertEquals('some name', $subjectName->getName());
4345

44-
$this->assertEquals(self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($subjectName));
46+
$this->assertEquals($this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement), strval($subjectName));
4547
}
4648

4749

4850
/**
4951
*/
5052
public function testUnmarshalling(): void
5153
{
52-
$subjectName = X509SubjectName::fromXML(self::$xmlRepresentation->documentElement);
54+
$subjectName = X509SubjectName::fromXML($this->xmlRepresentation->documentElement);
5355

5456
$this->assertEquals('some name', $subjectName->getName());
5557
}

tests/XML/xenc/CipherDataTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace SimpleSAML\XMLSecurity\Test\XML\xenc;
66

77
use DOMDocument;
8-
use SimpleSAML\Test\XML\SerializableXMLTest;
8+
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\Test\XML\SerializableXMLTestTrait;
910
use SimpleSAML\XML\Chunk;
1011
use SimpleSAML\XML\DOMDocumentFactory;
1112
use SimpleSAML\XMLSecurity\XML\xenc\CipherData;
@@ -19,15 +20,17 @@
1920
*
2021
* @package simplesamlphp/xml-security
2122
*/
22-
final class CipherDataTest extends SerializableXMLTest
23+
final class CipherDataTest extends TestCase
2324
{
25+
use SerializableXMLTestTrait;
26+
2427
/**
2528
*/
2629
public function setup(): void
2730
{
28-
self::$element = CipherData::class;
31+
$this->testedClass = CipherData::class;
2932

30-
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
33+
$this->xmlRepresentation = DOMDocumentFactory::fromFile(
3134
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/xenc_CipherData.xml'
3235
);
3336
}
@@ -45,7 +48,7 @@ public function testMarshallingCipherValue(): void
4548
$this->assertEquals('c29tZSB0ZXh0', $cipherData->getCipherValue());
4649

4750
$this->assertEquals(
48-
self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
51+
$this->xmlRepresentation->saveXML($this->xmlRepresentation->documentElement),
4952
strval($cipherData)
5053
);
5154
}
@@ -58,7 +61,7 @@ public function testMarshallingCipherValue(): void
5861
*/
5962
public function testUnmarshalling(): void
6063
{
61-
$cipherData = CipherData::fromXML(self::$xmlRepresentation->documentElement);
64+
$cipherData = CipherData::fromXML($this->xmlRepresentation->documentElement);
6265

6366
$this->assertEquals('c29tZSB0ZXh0', $cipherData->getCipherValue());
6467
}

0 commit comments

Comments
 (0)