|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Jose\Tests\Bundle\JoseFramework\Functional; |
| 6 | + |
| 7 | +use Jose\Bundle\JoseFramework\Services\HeaderCheckerManagerFactory; |
| 8 | +use Jose\Bundle\JoseFramework\Services\JWEDecrypterFactory; |
| 9 | +use Jose\Bundle\JoseFramework\Services\JWELoaderFactory; |
| 10 | +use Jose\Bundle\JoseFramework\Services\JWSLoaderFactory; |
| 11 | +use Jose\Bundle\JoseFramework\Services\JWSVerifierFactory; |
| 12 | +use Jose\Component\Core\AlgorithmManagerFactory; |
| 13 | +use Jose\Component\Encryption\JWETokenSupport; |
| 14 | +use Jose\Component\Encryption\Serializer\JWESerializerManagerFactory; |
| 15 | +use Jose\Component\Signature\JWSTokenSupport; |
| 16 | +use Jose\Component\Signature\Serializer\JWSSerializerManagerFactory; |
| 17 | +use Jose\Tests\Bundle\JoseFramework\WebTestCase; |
| 18 | +use PHPUnit\Framework\Attributes\Test; |
| 19 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 20 | +use function restore_error_handler; |
| 21 | +use function set_error_handler; |
| 22 | +use function sprintf; |
| 23 | +use const E_USER_DEPRECATED; |
| 24 | + |
| 25 | +/** |
| 26 | + * @internal |
| 27 | + */ |
| 28 | +final class LoaderFactoriesWithoutHeaderCheckerManagerFactoryTest extends WebTestCase |
| 29 | +{ |
| 30 | + private const MESSAGE = 'Since web-token/jwt-framework 4.3.0: Creating a "%s" without a header checker manager factory is deprecated and will not be possible in 5.0.0: the loaders it creates check no header parameter, not even those listed in "crit". Pass a "%s" that has the "%s" token type support.'; |
| 31 | + |
| 32 | + #[Test] |
| 33 | + public function theLoaderFactoriesWithoutHeaderCheckerManagerFactoryAreDeprecated(): void |
| 34 | + { |
| 35 | + $eventDispatcher = new EventDispatcher(); |
| 36 | + $algorithmManagerFactory = new AlgorithmManagerFactory([]); |
| 37 | + $jweSerializerManagerFactory = new JWESerializerManagerFactory(); |
| 38 | + $jweDecrypterFactory = new JWEDecrypterFactory($algorithmManagerFactory, $eventDispatcher); |
| 39 | + $jwsSerializerManagerFactory = new JWSSerializerManagerFactory(); |
| 40 | + $jwsVerifierFactory = new JWSVerifierFactory($algorithmManagerFactory, $eventDispatcher); |
| 41 | + |
| 42 | + $deprecations = $this->collectDeprecations(static function () use ( |
| 43 | + $eventDispatcher, |
| 44 | + $jweSerializerManagerFactory, |
| 45 | + $jweDecrypterFactory, |
| 46 | + $jwsSerializerManagerFactory, |
| 47 | + $jwsVerifierFactory |
| 48 | + ): void { |
| 49 | + new JWELoaderFactory($jweSerializerManagerFactory, $jweDecrypterFactory, null, $eventDispatcher); |
| 50 | + new JWSLoaderFactory($jwsSerializerManagerFactory, $jwsVerifierFactory, null, $eventDispatcher); |
| 51 | + }); |
| 52 | + |
| 53 | + static::assertSame([ |
| 54 | + sprintf(self::MESSAGE, JWELoaderFactory::class, HeaderCheckerManagerFactory::class, JWETokenSupport::class), |
| 55 | + sprintf(self::MESSAGE, JWSLoaderFactory::class, HeaderCheckerManagerFactory::class, JWSTokenSupport::class), |
| 56 | + ], $deprecations); |
| 57 | + } |
| 58 | + |
| 59 | + #[Test] |
| 60 | + public function theLoaderFactoriesOfTheContainerAreNotDeprecated(): void |
| 61 | + { |
| 62 | + static::ensureKernelShutdown(); |
| 63 | + $client = static::createClient(); |
| 64 | + $container = $client->getContainer(); |
| 65 | + |
| 66 | + $deprecations = $this->collectDeprecations(static function () use ($container): void { |
| 67 | + $container->get(JWELoaderFactory::class); |
| 68 | + $container->get(JWSLoaderFactory::class); |
| 69 | + }); |
| 70 | + |
| 71 | + static::assertSame([], $deprecations); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return list<string> |
| 76 | + */ |
| 77 | + private function collectDeprecations(callable $callback): array |
| 78 | + { |
| 79 | + $deprecations = []; |
| 80 | + set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool { |
| 81 | + $deprecations[] = $errstr; |
| 82 | + |
| 83 | + return true; |
| 84 | + }, E_USER_DEPRECATED); |
| 85 | + |
| 86 | + try { |
| 87 | + $callback(); |
| 88 | + } finally { |
| 89 | + restore_error_handler(); |
| 90 | + } |
| 91 | + |
| 92 | + return $deprecations; |
| 93 | + } |
| 94 | +} |
0 commit comments