Passed
Push — master ( be09b3...67068d )
by Tim
13:28
created

X509CertificateTest::testUnmarshalling()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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