|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\OpenID; |
| 6 | + |
| 7 | +use DateInterval; |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Psr\Log\LoggerInterface; |
| 12 | +use SimpleSAML\OpenID\Algorithms\AlgorithmManagerDecorator; |
| 13 | +use SimpleSAML\OpenID\Decorators\DateIntervalDecorator; |
| 14 | +use SimpleSAML\OpenID\Factories\AlgorithmManagerDecoratorFactory; |
| 15 | +use SimpleSAML\OpenID\Factories\ClaimFactory; |
| 16 | +use SimpleSAML\OpenID\Factories\DateIntervalDecoratorFactory; |
| 17 | +use SimpleSAML\OpenID\Factories\JwsSerializerManagerDecoratorFactory; |
| 18 | +use SimpleSAML\OpenID\Helpers; |
| 19 | +use SimpleSAML\OpenID\Jwks\Factories\JwksDecoratorFactory; |
| 20 | +use SimpleSAML\OpenID\Jws; |
| 21 | +use SimpleSAML\OpenID\Jws\Factories\JwsDecoratorBuilderFactory; |
| 22 | +use SimpleSAML\OpenID\Jws\Factories\JwsVerifierDecoratorFactory; |
| 23 | +use SimpleSAML\OpenID\Jws\Factories\ParsedJwsFactory; |
| 24 | +use SimpleSAML\OpenID\Jws\JwsDecoratorBuilder; |
| 25 | +use SimpleSAML\OpenID\Jws\JwsVerifierDecorator; |
| 26 | +use SimpleSAML\OpenID\Serializers\JwsSerializerManagerDecorator; |
| 27 | +use SimpleSAML\OpenID\SupportedAlgorithms; |
| 28 | +use SimpleSAML\OpenID\SupportedSerializers; |
| 29 | + |
| 30 | +#[CoversClass(Jws::class)] |
| 31 | +#[UsesClass(AlgorithmManagerDecoratorFactory::class)] |
| 32 | +#[UsesClass(ClaimFactory::class)] |
| 33 | +#[UsesClass(DateIntervalDecoratorFactory::class)] |
| 34 | +#[UsesClass(JwsSerializerManagerDecoratorFactory::class)] |
| 35 | +#[UsesClass(JwksDecoratorFactory::class)] |
| 36 | +#[UsesClass(JwsDecoratorBuilderFactory::class)] |
| 37 | +#[UsesClass(JwsVerifierDecoratorFactory::class)] |
| 38 | +#[UsesClass(ParsedJwsFactory::class)] |
| 39 | +#[UsesClass(Helpers::class)] |
| 40 | +#[UsesClass(SupportedAlgorithms::class)] |
| 41 | +#[UsesClass(SupportedSerializers::class)] |
| 42 | +#[UsesClass(AlgorithmManagerDecorator::class)] |
| 43 | +#[UsesClass(JwsDecoratorBuilder::class)] |
| 44 | +#[UsesClass(JwsVerifierDecorator::class)] |
| 45 | +#[UsesClass(JwsSerializerManagerDecorator::class)] |
| 46 | +#[UsesClass(DateIntervalDecorator::class)] |
| 47 | +final class JwsTest extends TestCase |
| 48 | +{ |
| 49 | + protected \PHPUnit\Framework\MockObject\Stub $supportedAlgorithmsMock; |
| 50 | + |
| 51 | + protected \PHPUnit\Framework\MockObject\Stub $supportedSerializerMock; |
| 52 | + |
| 53 | + protected DateInterval $timestampValidationLeeway; |
| 54 | + |
| 55 | + protected \PHPUnit\Framework\MockObject\Stub $loggerMock; |
| 56 | + |
| 57 | + |
| 58 | + protected function setUp(): void |
| 59 | + { |
| 60 | + $this->supportedAlgorithmsMock = $this->createStub(SupportedAlgorithms::class); |
| 61 | + $this->supportedSerializerMock = $this->createStub(SupportedSerializers::class); |
| 62 | + $this->timestampValidationLeeway = new DateInterval('PT1M'); |
| 63 | + $this->loggerMock = $this->createStub(LoggerInterface::class); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + protected function sut( |
| 68 | + ?SupportedAlgorithms $supportedAlgorithms = null, |
| 69 | + ?SupportedSerializers $supportedSerializers = null, |
| 70 | + ?DateInterval $timestampValidationLeeway = null, |
| 71 | + ?LoggerInterface $logger = null, |
| 72 | + ): Jws { |
| 73 | + $supportedAlgorithms ??= $this->supportedAlgorithmsMock; |
| 74 | + $supportedSerializers ??= $this->supportedSerializerMock; |
| 75 | + $timestampValidationLeeway ??= $this->timestampValidationLeeway; |
| 76 | + $logger ??= $this->loggerMock; |
| 77 | + |
| 78 | + return new Jws( |
| 79 | + $supportedAlgorithms, |
| 80 | + $supportedSerializers, |
| 81 | + $timestampValidationLeeway, |
| 82 | + $logger, |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public function testCanCreateInstance(): void |
| 88 | + { |
| 89 | + $this->assertInstanceOf( |
| 90 | + Jws::class, |
| 91 | + $this->sut(), |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + public function testCanBuildTools(): void |
| 97 | + { |
| 98 | + $sut = $this->sut(); |
| 99 | + |
| 100 | + $this->assertInstanceOf( |
| 101 | + DateIntervalDecoratorFactory::class, |
| 102 | + $sut->dateIntervalDecoratorFactory(), |
| 103 | + ); |
| 104 | + |
| 105 | + $this->assertInstanceOf( |
| 106 | + ParsedJwsFactory::class, |
| 107 | + $sut->parsedJwsFactory(), |
| 108 | + ); |
| 109 | + |
| 110 | + $this->assertInstanceOf( |
| 111 | + JwsDecoratorBuilder::class, |
| 112 | + $sut->jwsDecoratorBuilder(), |
| 113 | + ); |
| 114 | + |
| 115 | + $this->assertInstanceOf( |
| 116 | + JwsDecoratorBuilderFactory::class, |
| 117 | + $sut->jwsDecoratorBuilderFactory(), |
| 118 | + ); |
| 119 | + |
| 120 | + $this->assertInstanceOf( |
| 121 | + JwsSerializerManagerDecorator::class, |
| 122 | + $sut->jwsSerializerManagerDecorator(), |
| 123 | + ); |
| 124 | + |
| 125 | + $this->assertInstanceOf( |
| 126 | + JwsSerializerManagerDecoratorFactory::class, |
| 127 | + $sut->jwsSerializerManagerDecoratorFactory(), |
| 128 | + ); |
| 129 | + |
| 130 | + $this->assertInstanceOf( |
| 131 | + AlgorithmManagerDecorator::class, |
| 132 | + $sut->algorithmManagerDecorator(), |
| 133 | + ); |
| 134 | + |
| 135 | + $this->assertInstanceOf( |
| 136 | + AlgorithmManagerDecoratorFactory::class, |
| 137 | + $sut->algorithmManagerDecoratorFactory(), |
| 138 | + ); |
| 139 | + |
| 140 | + $this->assertInstanceOf( |
| 141 | + Helpers::class, |
| 142 | + $sut->helpers(), |
| 143 | + ); |
| 144 | + |
| 145 | + $this->assertInstanceOf( |
| 146 | + JwsVerifierDecorator::class, |
| 147 | + $sut->jwsVerifierDecorator(), |
| 148 | + ); |
| 149 | + |
| 150 | + $this->assertInstanceOf( |
| 151 | + JwsVerifierDecoratorFactory::class, |
| 152 | + $sut->jwsVerifierDecoratorFactory(), |
| 153 | + ); |
| 154 | + |
| 155 | + $this->assertInstanceOf( |
| 156 | + JwksDecoratorFactory::class, |
| 157 | + $sut->jwksDecoratorFactory(), |
| 158 | + ); |
| 159 | + |
| 160 | + $this->assertInstanceOf( |
| 161 | + ClaimFactory::class, |
| 162 | + $sut->claimFactory(), |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + |
| 167 | + public function testLazyInitialization(): void |
| 168 | + { |
| 169 | + $sut = $this->sut(); |
| 170 | + |
| 171 | + $factory1 = $sut->dateIntervalDecoratorFactory(); |
| 172 | + $factory2 = $sut->dateIntervalDecoratorFactory(); |
| 173 | + $this->assertSame($factory1, $factory2); |
| 174 | + |
| 175 | + $factory1 = $sut->parsedJwsFactory(); |
| 176 | + $factory2 = $sut->parsedJwsFactory(); |
| 177 | + $this->assertSame($factory1, $factory2); |
| 178 | + |
| 179 | + $builder1 = $sut->jwsDecoratorBuilder(); |
| 180 | + $builder2 = $sut->jwsDecoratorBuilder(); |
| 181 | + $this->assertSame($builder1, $builder2); |
| 182 | + |
| 183 | + $factory1 = $sut->jwsDecoratorBuilderFactory(); |
| 184 | + $factory2 = $sut->jwsDecoratorBuilderFactory(); |
| 185 | + $this->assertSame($factory1, $factory2); |
| 186 | + |
| 187 | + $decorator1 = $sut->jwsSerializerManagerDecorator(); |
| 188 | + $decorator2 = $sut->jwsSerializerManagerDecorator(); |
| 189 | + $this->assertSame($decorator1, $decorator2); |
| 190 | + |
| 191 | + $factory1 = $sut->jwsSerializerManagerDecoratorFactory(); |
| 192 | + $factory2 = $sut->jwsSerializerManagerDecoratorFactory(); |
| 193 | + $this->assertSame($factory1, $factory2); |
| 194 | + |
| 195 | + $decorator1 = $sut->algorithmManagerDecorator(); |
| 196 | + $decorator2 = $sut->algorithmManagerDecorator(); |
| 197 | + $this->assertSame($decorator1, $decorator2); |
| 198 | + |
| 199 | + $factory1 = $sut->algorithmManagerDecoratorFactory(); |
| 200 | + $factory2 = $sut->algorithmManagerDecoratorFactory(); |
| 201 | + $this->assertSame($factory1, $factory2); |
| 202 | + |
| 203 | + $helpers1 = $sut->helpers(); |
| 204 | + $helpers2 = $sut->helpers(); |
| 205 | + $this->assertSame($helpers1, $helpers2); |
| 206 | + |
| 207 | + $verifier1 = $sut->jwsVerifierDecorator(); |
| 208 | + $verifier2 = $sut->jwsVerifierDecorator(); |
| 209 | + $this->assertSame($verifier1, $verifier2); |
| 210 | + |
| 211 | + $factory1 = $sut->jwsVerifierDecoratorFactory(); |
| 212 | + $factory2 = $sut->jwsVerifierDecoratorFactory(); |
| 213 | + $this->assertSame($factory1, $factory2); |
| 214 | + |
| 215 | + $factory1 = $sut->jwksDecoratorFactory(); |
| 216 | + $factory2 = $sut->jwksDecoratorFactory(); |
| 217 | + $this->assertSame($factory1, $factory2); |
| 218 | + |
| 219 | + $factory1 = $sut->claimFactory(); |
| 220 | + $factory2 = $sut->claimFactory(); |
| 221 | + $this->assertSame($factory1, $factory2); |
| 222 | + } |
| 223 | +} |
0 commit comments