Passed
Push — master ( 6e1344...0156f2 )
by Tim
02:07
created

RecipientKeyInfoTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testUnmarshallingEmpty() 0 10 1
A testMarshallingEmpty() 0 6 1
A testMarshalling() 0 21 1
A setUp() 0 30 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\XML\xenc;
6
7
use PHPUnit\Framework\Attributes\CoversClass;
1 ignored issue
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PHPUnit\Framework\TestCase;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\DOMDocumentFactory;
11
use SimpleSAML\XML\TestUtils\SerializableElementTestTrait;
12
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
13
use SimpleSAML\XMLSecurity\TestUtils\PEMCertificatesMock;
14
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
15
use SimpleSAML\XMLSecurity\XML\ds\AbstractKeyInfoType;
16
use SimpleSAML\XMLSecurity\XML\ds\KeyName;
17
use SimpleSAML\XMLSecurity\XML\ds\X509Certificate;
18
use SimpleSAML\XMLSecurity\XML\ds\X509Data;
19
use SimpleSAML\XMLSecurity\XML\ds\X509SubjectName;
20
use SimpleSAML\XMLSecurity\XML\xenc\RecipientKeyInfo;
21
22
use function dirname;
23
use function openssl_x509_parse;
24
use function str_replace;
25
use function strval;
26
27
/**
28
 * Class \SimpleSAML\XMLSecurity\Test\XML\xenc\RecipientKeyInfoTest
29
 *
30
 * @package simplesamlphp/xml-security
31
 */
32
#[CoversClass(AbstractDsElement::class)]
33
#[CoversClass(AbstractKeyInfoType::class)]
34
#[CoversClass(RecipientKeyInfo::class)]
35
final class RecipientKeyInfoTest extends TestCase
36
{
37
    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...nc\RecipientKeyInfoTest.
Loading history...
38
39
    /** @var string */
40
    private static string $certificate;
41
42
    /** @var string[] */
43
    private static array $certData;
44
45
46
    /**
47
     */
48
    public function setUp(): void
49
    {
50
        self::$testedClass = RecipientKeyInfo::class;
51
52
        self::$xmlRepresentation = DOMDocumentFactory::fromFile(
53
            dirname(__FILE__, 3) . '/resources/xml/xenc_RecipientKeyInfo.xml',
54
        );
55
56
        self::$certificate = str_replace(
57
            [
58
                '-----BEGIN CERTIFICATE-----',
59
                '-----END CERTIFICATE-----',
60
                '-----BEGIN RSA PUBLIC KEY-----',
61
                '-----END RSA PUBLIC KEY-----',
62
                "\r\n",
63
                "\n",
64
            ],
65
            [
66
                '',
67
                '',
68
                '',
69
                '',
70
                "\n",
71
                '',
72
            ],
73
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
74
        );
75
76
        self::$certData = openssl_x509_parse(
77
            PEMCertificatesMock::getPlainCertificate(PEMCertificatesMock::SELFSIGNED_CERTIFICATE),
78
        );
79
    }
80
81
82
    /**
83
     */
84
    public function testMarshalling(): void
85
    {
86
        $recipientKeyInfo = new RecipientKeyInfo(
87
            [
88
                new KeyName('testkey'),
89
                new X509Data(
90
                    [
91
                        new X509Certificate(self::$certificate),
92
                        new X509SubjectName(self::$certData['name']),
93
                    ],
94
                ),
95
                new Chunk(DOMDocumentFactory::fromString(
96
                    '<ssp:Chunk xmlns:ssp="urn:x-simplesamlphp:namespace">some</ssp:Chunk>',
97
                )->documentElement),
98
            ],
99
            'fed654',
100
        );
101
102
        $this->assertEquals(
103
            self::$xmlRepresentation->saveXML(self::$xmlRepresentation->documentElement),
104
            strval($recipientKeyInfo),
105
        );
106
    }
107
108
109
    /**
110
     */
111
    public function testMarshallingEmpty(): void
112
    {
113
        $this->expectException(InvalidArgumentException::class);
114
        $this->expectExceptionMessage('xenc:RecipientKeyInfo cannot be empty');
115
116
        new RecipientKeyInfo([]);
117
    }
118
119
120
    /**
121
     */
122
    public function testUnmarshallingEmpty(): void
123
    {
124
        $document = DOMDocumentFactory::fromString(
125
            '<xenc:RecipientKeyInfo xmlns:xenc="' . RecipientKeyInfo::NS . '"/>',
126
        );
127
128
        $this->expectException(InvalidArgumentException::class);
129
        $this->expectExceptionMessage('xenc:RecipientKeyInfo cannot be empty');
130
131
        RecipientKeyInfo::fromXML($document->documentElement);
132
    }
133
}
134