File: InMemoryTest.php

package info (click to toggle)
php-lcobucci-jwt 5.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,312 kB
  • sloc: php: 6,674; makefile: 49
file content (102 lines) | stat: -rw-r--r-- 2,998 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);

namespace Lcobucci\JWT\Tests\Signer\Key;

use Lcobucci\JWT\Encoding\CannotDecodeContent;
use Lcobucci\JWT\Signer\InvalidKeyProvided;
use Lcobucci\JWT\Signer\Key\FileCouldNotBeRead;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\SodiumBase64Polyfill;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;

use function base64_encode;

#[PHPUnit\CoversClass(CannotDecodeContent::class)]
#[PHPUnit\CoversClass(FileCouldNotBeRead::class)]
#[PHPUnit\CoversClass(InMemory::class)]
#[PHPUnit\CoversClass(InvalidKeyProvided::class)]
#[PHPUnit\UsesClass(SodiumBase64Polyfill::class)]
final class InMemoryTest extends TestCase
{
    #[PHPUnit\Test]
    public function exceptionShouldBeRaisedWhenInvalidBase64CharsAreUsed(): void
    {
        $this->expectException(CannotDecodeContent::class);
        $this->expectExceptionMessage('Error while decoding from Base64Url, invalid base64 characters detected');

        InMemory::base64Encoded('รกรกรก');
    }

    #[PHPUnit\Test]
    public function base64EncodedShouldDecodeKeyContents(): void
    {
        $key = InMemory::base64Encoded(base64_encode('testing'));

        self::assertSame('testing', $key->contents());
    }

    #[PHPUnit\Test]
    public function exceptionShouldBeRaisedWhenFileDoesNotExists(): void
    {
        $path = __DIR__ . '/not-found.pem';

        $this->expectException(FileCouldNotBeRead::class);
        $this->expectExceptionMessage('The path "' . $path . '" does not contain a valid key file');
        $this->expectExceptionCode(0);

        InMemory::file($path);
    }

    #[PHPUnit\Test]
    public function exceptionShouldBeRaisedWhenFileIsEmpty(): void
    {
        $this->expectException(InvalidKeyProvided::class);
        $this->expectExceptionMessage('Key cannot be empty');

        InMemory::file(__DIR__ . '/empty.pem');
    }

    #[PHPUnit\Test]
    public function contentsShouldReturnConfiguredData(): void
    {
        $key = InMemory::plainText('testing', 'test');

        self::assertSame('testing', $key->contents());
    }

    #[PHPUnit\Test]
    public function contentsShouldReturnFileContentsWhenFilePathHasBeenPassed(): void
    {
        $key = InMemory::file(__DIR__ . '/test.pem');

        self::assertSame('testing', $key->contents());
    }

    #[PHPUnit\Test]
    public function passphraseShouldReturnConfiguredData(): void
    {
        $key = InMemory::plainText('testing', 'test');

        self::assertSame('test', $key->passphrase());
    }

    #[PHPUnit\Test]
    public function emptyPlainTextContentShouldRaiseException(): void
    {
        $this->expectException(InvalidKeyProvided::class);

        // @phpstan-ignore-next-line
        InMemory::plainText('');
    }

    #[PHPUnit\Test]
    public function emptyBase64ContentShouldRaiseException(): void
    {
        $this->expectException(InvalidKeyProvided::class);

        // @phpstan-ignore-next-line
        InMemory::base64Encoded('');
    }
}