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

PEMTest::testData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Test\CryptoEncoding;
6
7
use PHPUnit\Framework\TestCase;
8
use RuntimeException;
9
use SimpleSAML\XMLSecurity\CryptoEncoding\PEM;
10
use UnexpectedValueException;
11
12
use function base64_encode;
13
use function dirname;
14
use function file_get_contents;
15
16
/**
17
 * @group pem
18
 *
19
 * @internal
20
 */
21
class PEMTest extends TestCase
22
{
23
    private static string $baseDir;
24
25
26
    /**
27
     */
28
    public static function setUpBeforeClass(): void
29
    {
30
        self::$baseDir = dirname(__FILE__, 3);
31
    }
32
33
34
    public function testFromString(): void
35
    {
36
        $str = file_get_contents(self::$baseDir . '/resources/keys/pubkey.pem');
37
        $pem = PEM::fromString($str);
38
        $this->assertInstanceOf(PEM::class, $pem);
39
    }
40
41
42
    /**
43
     * @return \SimpleSAML\XMLSecurity\CryptoEncoding\PEM
44
     */
45
    public function testFromFile(): PEM
46
    {
47
        $pem = PEM::fromFile(self::$baseDir . '/resources/keys/pubkey.pem');
48
        $this->assertInstanceOf(PEM::class, $pem);
49
        return $pem;
50
    }
51
52
53
    /**
54
     * @depends testFromFile
55
     *
56
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEM $pem
57
     */
58
    public function testType(PEM $pem): void
59
    {
60
        $this->assertEquals(PEM::TYPE_PUBLIC_KEY, $pem->type());
61
    }
62
63
64
    public function testData(): void
65
    {
66
        $data = 'payload';
67
        $encoded = base64_encode($data);
68
        $str = <<<DATA
69
-----BEGIN TEST-----
70
{$encoded}
71
-----END TEST-----
72
DATA;
73
        $this->assertEquals($data, PEM::fromString($str)->data());
74
    }
75
76
77
    public function testInvalidPEM(): void
78
    {
79
        $this->expectException(UnexpectedValueException::class);
80
        PEM::fromString('invalid');
81
    }
82
83
84
    public function testInvalidPEMData(): void
85
    {
86
        $str = <<<'DATA'
87
-----BEGIN TEST-----
88
%%%
89
-----END TEST-----
90
DATA;
91
        $this->expectException(UnexpectedValueException::class);
92
        PEM::fromString($str);
93
    }
94
95
96
    public function testInvalidFile(): void
97
    {
98
        $this->expectException(RuntimeException::class);
99
        PEM::fromFile('/phpunit/some/nonexistent');
100
    }
101
102
103
    /**
104
     * @depends testFromFile
105
     *
106
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEM $pem
107
     */
108
    public function testString(PEM $pem): void
109
    {
110
        $this->assertIsString($pem->string());
111
    }
112
113
114
    /**
115
     * @depends testFromFile
116
     *
117
     * @param \SimpleSAML\XMLSecurity\CryptoEncoding\PEM $pem
118
     */
119
    public function testToString(PEM $pem): void
120
    {
121
        $this->assertIsString(strval($pem));
122
    }
123
}
124