Passed
Branch master (c86cc6)
by Tim
03:54
created

testMarshallingInvalidBase64()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\ds;
6
7
use DOMDocument;
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\Assert\AssertionFailedException;
10
use SimpleSAML\XML\DOMDocumentFactory;
11
use SimpleSAML\XML\TestUtils\SchemaValidationTestTrait;
12
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
13
use SimpleSAML\XMLSecurity\Test\XML\XMLDumper;
14
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
15
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
16
17
use function dirname;
18
use function str_replace;
19
use function strval;
20
use function substr;
21
22
/**
23
 * Class \SimpleSAML\XMLSecurity\Test\XML\ds\X509CertificateTest
24
 *
25
 * @covers \SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement
26
 * @covers \SimpleSAML\XMLSecurity\XML\ds\X509Certificate
27
 *
28
 * @package simplesamlphp/xml-security
29
 */
30
final class X509CertificateTest extends TestCase
31
{
32
    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\T...\ds\X509CertificateTest.
Loading history...
33
34
    /** @var string */
35
    private static string $certificate;
36
37
38
    /**
39
     */
40
    public static function setUpBeforeClass(): void
41
    {
42
        self::$testedClass = X509Certificate::class;
43
44
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
45
            dirname(__FILE__, 3) . '/resources/xml/ds_X509Certificate.xml',
46
        );
47
48
        self::$certificate = str_replace(
49
            [
50
                '-----BEGIN CERTIFICATE-----',
51
                '-----END CERTIFICATE-----',
52
                '-----BEGIN RSA PUBLIC KEY-----',
53
                '-----END RSA PUBLIC KEY-----',
54
                "\r\n",
55
                "\n",
56
            ],
57
            [
58
                '',
59
                '',
60
                '',
61
                '',
62
                "\n",
63
                ''
64
            ],
65
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
66
        );
67
    }
68
69
70
    /**
71
     */
72
    public function testMarshalling(): void
73
    {
74
        $x509cert = new X509Certificate(self::$certificate);
75
76
        $this->assertEquals(
77
            XMLDumper::dumpDOMDocumentXMLWithBase64Content(self::$xmlRepresentation),
78
            strval($x509cert),
79
        );
80
    }
81
82
83
    /**
84
     */
85
    public function testMarshallingInvalidBase64(): void
86
    {
87
        $certificate = str_replace(substr(self::$certificate, 1), '', self::$certificate);
88
        $this->expectException(AssertionFailedException::class);
89
        new X509Certificate($certificate);
90
    }
91
92
93
    /**
94
     */
95
    public function testUnmarshalling(): void
96
    {
97
        $x509cert = X509Certificate::fromXML(self::$xmlRepresentation->documentElement);
98
99
        $this->assertEquals(
100
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
101
            strval($x509cert),
102
        );
103
    }
104
}
105