Passed
Branch master (c86cc6)
by Tim
11:28
created

PEMBundleTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 158
rs 10
c 0
b 0
f 0
wmc 16

15 Methods

Rating   Name   Duplication   Size   Complexity  
A testToString() 0 3 1
A testCount() 0 3 1
A testFirstEmptyFail() 0 5 1
A testInvalidPEM() 0 4 1
A setUpBeforeClass() 0 3 1
A testBundle() 0 5 1
A testFirst() 0 4 1
A testWithPEMs() 0 4 1
A testInvalidPEMData() 0 9 1
A testLast() 0 4 1
A testAll() 0 3 1
A testIterator() 0 7 2
A testString() 0 3 1
A testLastEmptyFail() 0 5 1
A testInvalidFile() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\CryptoEncoding;
6
7
use LogicException;
8
use PHPUnit\Framework\TestCase;
9
use RuntimeException;
10
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM;
11
use SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle;
12
use SimpleSAML\XMLSecurity\Exception\IOException;
13
use UnexpectedValueException;
14
15
use function dirname;
16
17
/**
18
 * @group pem
19
 *
20
 * @internal
21
 */
22
class PEMBundleTest extends TestCase
23
{
24
    private static string $baseDir;
25
26
27
    /**
28
     */
29
    public static function setUpBeforeClass(): void
30
    {
31
        self::$baseDir = dirname(__FILE__, 3);
32
    }
33
34
35
    /**
36
     * @return \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle
37
     */
38
    public function testBundle(): PEMBundle
39
    {
40
        $bundle = PEMBundle::fromFile(self::$baseDir . '/resources/certificates/cacert.pem');
41
        $this->assertInstanceOf(PEMBundle::class, $bundle);
42
        return $bundle;
43
    }
44
45
46
    /**
47
     * @depends testBundle
48
     *
49
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle
50
     */
51
    public function testAll(PEMBundle $bundle): void
52
    {
53
        $this->assertContainsOnlyInstancesOf(PEM::class, $bundle->all());
54
    }
55
56
57
    /**
58
     * @depends testBundle
59
     *
60
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle
61
     */
62
    public function testFirst(PEMBundle $bundle): void
63
    {
64
        $this->assertInstanceOf(PEM::class, $bundle->first());
65
        $this->assertEquals($bundle->all()[0], $bundle->first());
66
    }
67
68
69
    /**
70
     * @depends testBundle
71
     *
72
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle
73
     */
74
    public function testLast(PEMBundle $bundle): void
75
    {
76
        $this->assertInstanceOf(PEM::class, $bundle->last());
77
        $this->assertEquals($bundle->all()[149], $bundle->last());
78
    }
79
80
81
    /**
82
     * @depends testBundle
83
     *
84
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle
85
     */
86
    public function testCount(PEMBundle $bundle): void
87
    {
88
        $this->assertCount(150, $bundle);
89
    }
90
91
92
    /**
93
     * @depends testBundle
94
     *
95
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle
96
     */
97
    public function testIterator(PEMBundle $bundle): void
98
    {
99
        $values = [];
100
        foreach ($bundle as $pem) {
101
            $values[] = $pem;
102
        }
103
        $this->assertContainsOnlyInstancesOf(PEM::class, $values);
104
    }
105
106
107
    /**
108
     * @depends testBundle
109
     *
110
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle
111
     */
112
    public function testString(PEMBundle $bundle): void
113
    {
114
        $this->assertIsString($bundle->string());
115
    }
116
117
118
    /**
119
     * @depends testBundle
120
     *
121
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle
122
     */
123
    public function testToString(PEMBundle $bundle): void
124
    {
125
        $this->assertIsString(strval($bundle));
126
    }
127
128
129
    public function testInvalidPEM(): void
130
    {
131
        $this->expectException(UnexpectedValueException::class);
132
        PEMBundle::fromString('invalid');
133
    }
134
135
136
    public function testInvalidPEMData(): void
137
    {
138
        $str = <<<'DATA'
139
-----BEGIN TEST-----
140
%%%
141
-----END TEST-----
142
DATA;
143
        $this->expectException(UnexpectedValueException::class);
144
        PEMBundle::fromString($str);
145
    }
146
147
148
    public function testInvalidFile(): void
149
    {
150
        $this->expectException(IOException::class);
151
        PEMBundle::fromFile('/phpunit/some/nonexistent');
152
    }
153
154
155
    public function testFirstEmptyFail(): void
156
    {
157
        $bundle = new PEMBundle();
158
        $this->expectException(LogicException::class);
159
        $bundle->first();
160
    }
161
162
163
    public function testLastEmptyFail(): void
164
    {
165
        $bundle = new PEMBundle();
166
        $this->expectException(LogicException::class);
167
        $bundle->last();
168
    }
169
170
171
    /**
172
     * @depends testBundle
173
     *
174
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEMBundle $bundle
175
     */
176
    public function testWithPEMs(PEMBundle $bundle): void
177
    {
178
        $bundle = $bundle->withPEMs(new PEM('TEST', 'data'));
179
        $this->assertCount(151, $bundle);
180
    }
181
}
182