Skip to content

Commit 952639d

Browse files
authored
Code de-duplication; use generic XML serialization-test (#4)
* Migrate ds-tests to use generic serialization tests * Migrate xenc-tests to use generic serialization tests
1 parent 59fb84c commit 952639d

14 files changed

Lines changed: 113 additions & 286 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.1.2"
43+
"simplesamlphp/xml-common": "~0.2.1"
4444
},
4545
"require-dev": {
4646
"simplesamlphp/simplesamlphp-test-framework": "^1.0.5"

tests/XML/ds/KeyInfoTest.php

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

77
use DOMDocument;
8-
use PHPUnit\Framework\TestCase;
98
use SimpleSAML\Assert\AssertionFailedException;
9+
use SimpleSAML\Test\XML\SerializableXMLTest;
1010
use SimpleSAML\XML\DOMDocumentFactory;
1111
use SimpleSAML\XML\Chunk;
1212
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
@@ -25,22 +25,25 @@
2525
*
2626
* @package simplesamlphp/xml-security
2727
*/
28-
final class KeyInfoTest extends TestCase
28+
final class KeyInfoTest extends SerializableXMLTest
2929
{
3030
/** @var string */
3131
private string $certificate;
3232

3333
/** @var string[] */
3434
private array $certData;
3535

36-
/** @var \DOMDocument */
37-
private DOMDocument $document;
38-
3936

4037
/**
4138
*/
4239
public function setUp(): void
4340
{
41+
self::$element = KeyInfo::class;
42+
43+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
44+
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_KeyInfo.xml'
45+
);
46+
4447
$this->certificate = str_replace(
4548
[
4649
'-----BEGIN CERTIFICATE-----',
@@ -64,10 +67,6 @@ public function setUp(): void
6467
$this->certData = openssl_x509_parse(
6568
PEMCertificatesMock::getPlainPublicKey(PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY)
6669
);
67-
68-
$this->document = DOMDocumentFactory::fromFile(
69-
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_KeyInfo.xml'
70-
);
7170
}
7271

7372

@@ -100,7 +99,7 @@ public function testMarshalling(): void
10099
$this->assertInstanceOf(Chunk::class, $info[3]);
101100
$this->assertEquals('abc123', $keyInfo->getId());
102101

103-
$this->assertEquals($this->document->saveXML($this->document->documentElement), strval($keyInfo));
102+
$this->assertEquals(self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($keyInfo));
104103
}
105104

106105

@@ -119,7 +118,7 @@ public function testMarshallingEmpty(): void
119118
*/
120119
public function testUnmarshalling(): void
121120
{
122-
$keyInfo = KeyInfo::fromXML($this->document->documentElement);
121+
$keyInfo = KeyInfo::fromXML(self::$xmlRepresentation->documentElement);
123122
$this->assertEquals('abc123', $keyInfo->getId());
124123

125124
$info = $keyInfo->getInfo();
@@ -143,16 +142,4 @@ public function testUnmarshallingEmpty(): void
143142

144143
$keyInfo = KeyInfo::fromXML($document->documentElement);
145144
}
146-
147-
148-
/**
149-
* Test serialization / unserialization
150-
*/
151-
public function testSerialization(): void
152-
{
153-
$this->assertEquals(
154-
$this->document->saveXML($this->document->documentElement),
155-
strval(unserialize(serialize(KeyInfo::fromXML($this->document->documentElement))))
156-
);
157-
}
158145
}

tests/XML/ds/KeyNameTest.php

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

77
use DOMDocument;
8-
use PHPUnit\Framework\TestCase;
8+
use SimpleSAML\Test\XML\SerializableXMLTest;
99
use SimpleSAML\XML\DOMDocumentFactory;
1010
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
1111
use SimpleSAML\XMLSecurity\XMLSecurityDSig;
@@ -18,16 +18,15 @@
1818
*
1919
* @package simplesamlphp/xml-security
2020
*/
21-
final class KeyNameTest extends TestCase
21+
final class KeyNameTest extends SerializableXMLTest
2222
{
23-
/** @var \DOMDocument */
24-
private DOMDocument $document;
25-
2623
/**
2724
*/
2825
protected function setUp(): void
2926
{
30-
$this->document = DOMDocumentFactory::fromFile(
27+
self::$element = KeyName::class;
28+
29+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
3130
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_KeyName.xml'
3231
);
3332
}
@@ -41,28 +40,16 @@ public function testMarshalling(): void
4140

4241
$this->assertEquals('testkey', $keyName->getName());
4342

44-
$this->assertEquals($this->document->saveXML($this->document->documentElement), strval($keyName));
43+
$this->assertEquals(self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($keyName));
4544
}
4645

4746

4847
/**
4948
*/
5049
public function testUnmarshalling(): void
5150
{
52-
$keyName = KeyName::fromXML($this->document->documentElement);
51+
$keyName = KeyName::fromXML(self::$xmlRepresentation->documentElement);
5352

5453
$this->assertEquals('testkey', $keyName->getName());
5554
}
56-
57-
58-
/**
59-
* Test serialization / unserialization
60-
*/
61-
public function testSerialization(): void
62-
{
63-
$this->assertEquals(
64-
$this->document->saveXML($this->document->documentElement),
65-
strval(unserialize(serialize(KeyName::fromXML($this->document->documentElement))))
66-
);
67-
}
6855
}

tests/XML/ds/X509CertificateTest.php

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

77
use DOMDocument;
8-
use PHPUnit\Framework\TestCase;
8+
use SimpleSAML\Test\XML\SerializableXMLTest;
99
use SimpleSAML\XML\DOMDocumentFactory;
1010
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
1111
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
@@ -18,11 +18,8 @@
1818
*
1919
* @package simplesamlphp/xml-security
2020
*/
21-
final class X509CertificateTest extends TestCase
21+
final class X509CertificateTest extends SerializableXMLTest
2222
{
23-
/** @var \DOMDocument */
24-
private DOMDocument $document;
25-
2623
/** @var string */
2724
private string $certificate;
2825

@@ -31,6 +28,12 @@ final class X509CertificateTest extends TestCase
3128
*/
3229
public function setUp(): void
3330
{
31+
self::$element = X509Certificate::class;
32+
33+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
34+
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_X509Certificate.xml'
35+
);
36+
3437
$this->certificate = str_replace(
3538
[
3639
'-----BEGIN CERTIFICATE-----',
@@ -50,10 +53,6 @@ public function setUp(): void
5053
],
5154
PEMCertificatesMock::getPlainPublicKey(PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY)
5255
);
53-
54-
$this->document = DOMDocumentFactory::fromFile(
55-
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_X509Certificate.xml'
56-
);
5756
}
5857

5958

@@ -65,28 +64,16 @@ public function testMarshalling(): void
6564

6665
$this->assertEquals($this->certificate, $X509cert->getCertificate());
6766

68-
$this->assertEquals($this->document->saveXML($this->document->documentElement), strval($X509cert));
67+
$this->assertEquals(self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($X509cert));
6968
}
7069

7170

7271
/**
7372
*/
7473
public function testUnmarshalling(): void
7574
{
76-
$X509cert = X509Certificate::fromXML($this->document->documentElement);
75+
$X509cert = X509Certificate::fromXML(self::$xmlRepresentation->documentElement);
7776

7877
$this->assertEquals($this->certificate, $X509cert->getCertificate());
7978
}
80-
81-
82-
/**
83-
* Test serialization / unserialization
84-
*/
85-
public function testSerialization(): void
86-
{
87-
$this->assertEquals(
88-
$this->document->saveXML($this->document->documentElement),
89-
strval(unserialize(serialize(X509Certificate::fromXML($this->document->documentElement))))
90-
);
91-
}
9279
}

tests/XML/ds/X509DataTest.php

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

77
use DOMDocument;
8-
use PHPUnit\Framework\TestCase;
98
use SimpleSAML\Assert\AssertionFailedException;
9+
use SimpleSAML\Test\XML\SerializableXMLTest;
1010
use SimpleSAML\XML\Chunk;
1111
use SimpleSAML\XML\DOMDocumentFactory;
1212
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
@@ -23,11 +23,8 @@
2323
*
2424
* @package simplesamlphp/xml-security
2525
*/
26-
final class X509DataTest extends TestCase
26+
final class X509DataTest extends SerializableXMLTest
2727
{
28-
/** @var \DOMDocument */
29-
private DOMDocument $document;
30-
3128
/** @var string */
3229
private string $certificate;
3330

@@ -39,6 +36,12 @@ final class X509DataTest extends TestCase
3936
*/
4037
public function setUp(): void
4138
{
39+
self::$element = X509Data::class;
40+
41+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
42+
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_X509Data.xml'
43+
);
44+
4245
$this->certificate = str_replace(
4346
[
4447
'-----BEGIN CERTIFICATE-----',
@@ -62,10 +65,6 @@ public function setUp(): void
6265
$this->certData = openssl_x509_parse(
6366
PEMCertificatesMock::getPlainPublicKey(PEMCertificatesMock::SELFSIGNED_PUBLIC_KEY)
6467
);
65-
66-
$this->document = DOMDocumentFactory::fromFile(
67-
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_X509Data.xml'
68-
);
6968
}
7069

7170

@@ -91,32 +90,20 @@ public function testMarshalling(): void
9190
$this->assertInstanceOf(X509SubjectName::class, $data[2]);
9291
$this->assertInstanceOf(Chunk::class, $data[3]);
9392

94-
$this->assertEquals($this->document->saveXML($this->document->documentElement), strval($X509data));
93+
$this->assertEquals(self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($X509data));
9594
}
9695

9796

9897
/**
9998
*/
10099
public function testUnmarshalling(): void
101100
{
102-
$X509data = X509Data::fromXML($this->document->documentElement);
101+
$X509data = X509Data::fromXML(self::$xmlRepresentation->documentElement);
103102

104103
$data = $X509data->getData();
105104
$this->assertInstanceOf(Chunk::class, $data[0]);
106105
$this->assertInstanceOf(X509Certificate::class, $data[1]);
107106
$this->assertInstanceOf(X509SubjectName::class, $data[2]);
108107
$this->assertInstanceOf(Chunk::class, $data[3]);
109108
}
110-
111-
112-
/**
113-
* Test serialization / unserialization
114-
*/
115-
public function testSerialization(): void
116-
{
117-
$this->assertEquals(
118-
$this->document->saveXML($this->document->documentElement),
119-
strval(unserialize(serialize(X509Data::fromXML($this->document->documentElement))))
120-
);
121-
}
122109
}

tests/XML/ds/X509SubjectNameTest.php

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

77
use DOMDocument;
88
use PHPUnit\Framework\TestCase;
9+
use SimpleSAML\Test\XML\SerializableXMLTest;
910
use SimpleSAML\XML\DOMDocumentFactory;
1011
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName;
1112
use SimpleSAML\XMLSecurity\XMLSecurityDSig;
@@ -18,17 +19,15 @@
1819
*
1920
* @package simplesamlphp/xml-security
2021
*/
21-
final class X509SubjectNameTest extends TestCase
22+
final class X509SubjectNameTest extends SerializableXMLTest
2223
{
23-
/** @var \DOMDocument */
24-
private DOMDocument $document;
25-
26-
2724
/**
2825
*/
2926
protected function setUp(): void
3027
{
31-
$this->document = DOMDocumentFactory::fromFile(
28+
self::$element = X509SubjectName::class;
29+
30+
self::$xmlRepresentation = DOMDocumentFactory::fromFile(
3231
dirname(dirname(dirname(dirname(__FILE__)))) . '/tests/resources/xml/ds_X509SubjectName.xml'
3332
);
3433
}
@@ -42,28 +41,16 @@ public function testMarshalling(): void
4241

4342
$this->assertEquals('some name', $subjectName->getName());
4443

45-
$this->assertEquals($this->document->saveXML($this->document->documentElement), strval($subjectName));
44+
$this->assertEquals(self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement), strval($subjectName));
4645
}
4746

4847

4948
/**
5049
*/
5150
public function testUnmarshalling(): void
5251
{
53-
$subjectName = X509SubjectName::fromXML($this->document->documentElement);
52+
$subjectName = X509SubjectName::fromXML(self::$xmlRepresentation->documentElement);
5453

5554
$this->assertEquals('some name', $subjectName->getName());
5655
}
57-
58-
59-
/**
60-
* Test serialization / unserialization
61-
*/
62-
public function testSerialization(): void
63-
{
64-
$this->assertEquals(
65-
$this->document->saveXML($this->document->documentElement),
66-
strval(unserialize(serialize(X509SubjectName::fromXML($this->document->documentElement))))
67-
);
68-
}
6956
}

0 commit comments

Comments
 (0)