diff --git a/Makefile b/Makefile index 744df992..1fedebfb 100644 --- a/Makefile +++ b/Makefile @@ -37,4 +37,4 @@ endif .PHONY: phpbench phpbench: - @vendor/bin/phpbench run -l dots --report=$(PHPBENCH_REPORT) $(PHPBENCH_FLAGS) + @vendor/bin/phpbench run -l dots --retry-threshold=5 --report=$(PHPBENCH_REPORT) $(PHPBENCH_FLAGS) diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 8bf9f940..99a91556 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -17,21 +17,16 @@ use Lcobucci\JWT\Token\Parser as ParserImpl; use Lcobucci\JWT\Validation\Constraint; use Lcobucci\JWT\Validator; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -/** - * @covers ::__construct - * @coversDefaultClass \Lcobucci\JWT\Configuration - * - * @uses \Lcobucci\JWT\Encoding\ChainedFormatter - * @uses \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion - * @uses \Lcobucci\JWT\Encoding\UnifyAudience - * @uses \Lcobucci\JWT\Signer\Key\InMemory - * @uses \Lcobucci\JWT\Token\Builder - * @uses \Lcobucci\JWT\Token\Parser - * @uses \Lcobucci\JWT\Validation\Validator - */ +#[PHPUnit\CoversClass(Configuration::class)] +#[PHPUnit\UsesClass(ChainedFormatter::class)] +#[PHPUnit\UsesClass(InMemory::class)] +#[PHPUnit\UsesClass(BuilderImpl::class)] +#[PHPUnit\UsesClass(ParserImpl::class)] +#[PHPUnit\UsesClass(\Lcobucci\JWT\Validation\Validator::class)] final class ConfigurationTest extends TestCase { private Parser&MockObject $parser; @@ -41,7 +36,7 @@ final class ConfigurationTest extends TestCase private Validator&MockObject $validator; private Constraint&MockObject $validationConstraints; - /** @before */ + #[PHPUnit\Before] public function createDependencies(): void { $this->signer = $this->createMock(Signer::class); @@ -52,14 +47,7 @@ public function createDependencies(): void $this->validationConstraints = $this->createMock(Constraint::class); } - /** - * @test - * - * @covers ::forAsymmetricSigner - * @covers ::signer - * @covers ::signingKey - * @covers ::verificationKey - */ + #[PHPUnit\Test] public function forAsymmetricSignerShouldConfigureSignerAndBothKeys(): void { $signingKey = InMemory::plainText('private'); @@ -72,14 +60,7 @@ public function forAsymmetricSignerShouldConfigureSignerAndBothKeys(): void self::assertSame($verificationKey, $config->verificationKey()); } - /** - * @test - * - * @covers ::forSymmetricSigner - * @covers ::signer - * @covers ::signingKey - * @covers ::verificationKey - */ + #[PHPUnit\Test] public function forSymmetricSignerShouldConfigureSignerAndBothKeys(): void { $key = InMemory::plainText('private'); @@ -90,13 +71,7 @@ public function forSymmetricSignerShouldConfigureSignerAndBothKeys(): void self::assertSame($key, $config->verificationKey()); } - /** - * @test - * - * @covers ::builder - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function builderShouldCreateABuilderWithDefaultEncoderAndClaimFactory(): void { $config = Configuration::forSymmetricSigner( @@ -110,13 +85,7 @@ public function builderShouldCreateABuilderWithDefaultEncoderAndClaimFactory(): self::assertEquals(new BuilderImpl(new JoseEncoder(), ChainedFormatter::default()), $builder); } - /** - * @test - * - * @covers ::builder - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function builderShouldCreateABuilderWithCustomizedEncoderAndClaimFactory(): void { $config = Configuration::forSymmetricSigner( @@ -130,14 +99,7 @@ public function builderShouldCreateABuilderWithCustomizedEncoderAndClaimFactory( self::assertEquals(new BuilderImpl($this->encoder, ChainedFormatter::default()), $builder); } - /** - * @test - * - * @covers ::builder - * @covers ::setBuilderFactory - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function builderShouldUseBuilderFactoryWhenThatIsConfigured(): void { $builder = $this->createMock(Builder::class); @@ -154,13 +116,7 @@ static function () use ($builder): Builder { self::assertSame($builder, $config->builder()); } - /** - * @test - * - * @covers ::parser - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function parserShouldReturnAParserWithDefaultDecoder(): void { $config = Configuration::forSymmetricSigner( @@ -172,13 +128,7 @@ public function parserShouldReturnAParserWithDefaultDecoder(): void self::assertNotEquals(new ParserImpl($this->decoder), $parser); } - /** - * @test - * - * @covers ::parser - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function parserShouldReturnAParserWithCustomizedDecoder(): void { $config = Configuration::forSymmetricSigner( @@ -191,14 +141,7 @@ public function parserShouldReturnAParserWithCustomizedDecoder(): void self::assertEquals(new ParserImpl($this->decoder), $parser); } - /** - * @test - * - * @covers ::parser - * @covers ::setParser - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function parserShouldNotCreateAnInstanceIfItWasConfigured(): void { $config = Configuration::forSymmetricSigner( @@ -210,13 +153,7 @@ public function parserShouldNotCreateAnInstanceIfItWasConfigured(): void self::assertSame($this->parser, $config->parser()); } - /** - * @test - * - * @covers ::validator - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function validatorShouldReturnTheDefaultWhenItWasNotConfigured(): void { $config = Configuration::forSymmetricSigner( @@ -228,14 +165,7 @@ public function validatorShouldReturnTheDefaultWhenItWasNotConfigured(): void self::assertNotSame($this->validator, $validator); } - /** - * @test - * - * @covers ::validator - * @covers ::setValidator - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function validatorShouldReturnTheConfiguredValidator(): void { $config = Configuration::forSymmetricSigner( @@ -247,13 +177,7 @@ public function validatorShouldReturnTheConfiguredValidator(): void self::assertSame($this->validator, $config->validator()); } - /** - * @test - * - * @covers ::validationConstraints - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function validationConstraintsShouldReturnAnEmptyArrayWhenItWasNotConfigured(): void { $config = Configuration::forSymmetricSigner( @@ -264,14 +188,7 @@ public function validationConstraintsShouldReturnAnEmptyArrayWhenItWasNotConfigu self::assertSame([], $config->validationConstraints()); } - /** - * @test - * - * @covers ::validationConstraints - * @covers ::setValidationConstraints - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function validationConstraintsShouldReturnTheConfiguredValidator(): void { $config = Configuration::forSymmetricSigner( @@ -283,13 +200,7 @@ public function validationConstraintsShouldReturnTheConfiguredValidator(): void self::assertSame([$this->validationConstraints], $config->validationConstraints()); } - /** - * @test - * - * @covers ::builder - * - * @uses \Lcobucci\JWT\Configuration::forSymmetricSigner - */ + #[PHPUnit\Test] public function customClaimFormatterCanBeUsed(): void { $formatter = $this->createMock(ClaimsFormatter::class); diff --git a/tests/ES512TokenTest.php b/tests/ES512TokenTest.php index 99dc3b26..d8a3c9bd 100644 --- a/tests/ES512TokenTest.php +++ b/tests/ES512TokenTest.php @@ -4,48 +4,56 @@ namespace Lcobucci\JWT\Tests; use Lcobucci\JWT\Configuration; +use Lcobucci\JWT\Encoding\ChainedFormatter; +use Lcobucci\JWT\Encoding\JoseEncoder; +use Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion; +use Lcobucci\JWT\Encoding\UnifyAudience; +use Lcobucci\JWT\Signer\Ecdsa; use Lcobucci\JWT\Signer\Ecdsa\Sha256; use Lcobucci\JWT\Signer\Ecdsa\Sha512; use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\OpenSSL; +use Lcobucci\JWT\SodiumBase64Polyfill; use Lcobucci\JWT\Token; use Lcobucci\JWT\Validation\Constraint\SignedWith; +use Lcobucci\JWT\Validation\ConstraintViolation; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; +use Lcobucci\JWT\Validation\Validator; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; -/** - * @covers \Lcobucci\JWT\Configuration - * @covers \Lcobucci\JWT\Encoding\JoseEncoder - * @covers \Lcobucci\JWT\Encoding\ChainedFormatter - * @covers \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion - * @covers \Lcobucci\JWT\Encoding\UnifyAudience - * @covers \Lcobucci\JWT\Token\Builder - * @covers \Lcobucci\JWT\Token\Parser - * @covers \Lcobucci\JWT\Token\Plain - * @covers \Lcobucci\JWT\Token\DataSet - * @covers \Lcobucci\JWT\Token\Signature - * @covers \Lcobucci\JWT\Signer\Key\InMemory - * @covers \Lcobucci\JWT\Signer\Ecdsa - * @covers \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha256 - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha512 - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\SodiumBase64Polyfill - * @covers \Lcobucci\JWT\Validation\Validator - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\RequiredConstraintsViolated - * @covers \Lcobucci\JWT\Validation\Constraint\SignedWith - */ +#[PHPUnit\CoversClass(Configuration::class)] +#[PHPUnit\CoversClass(JoseEncoder::class)] +#[PHPUnit\CoversClass(ChainedFormatter::class)] +#[PHPUnit\CoversClass(MicrosecondBasedDateConversion::class)] +#[PHPUnit\CoversClass(UnifyAudience::class)] +#[PHPUnit\CoversClass(Token\Builder::class)] +#[PHPUnit\CoversClass(Token\Parser::class)] +#[PHPUnit\CoversClass(Token\Plain::class)] +#[PHPUnit\CoversClass(Token\DataSet::class)] +#[PHPUnit\CoversClass(Token\Signature::class)] +#[PHPUnit\CoversClass(Ecdsa::class)] +#[PHPUnit\CoversClass(Ecdsa\MultibyteStringConverter::class)] +#[PHPUnit\CoversClass(Sha256::class)] +#[PHPUnit\CoversClass(Sha512::class)] +#[PHPUnit\CoversClass(InMemory::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(SodiumBase64Polyfill::class)] +#[PHPUnit\CoversClass(Validator::class)] +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(SignedWith::class)] +#[PHPUnit\CoversClass(RequiredConstraintsViolated::class)] class ES512TokenTest extends TestCase { use Keys; private Configuration $config; - /** @before */ + #[PHPUnit\Before] public function createConfiguration(): void { $this->config = Configuration::forAsymmetricSigner( @@ -55,7 +63,7 @@ public function createConfiguration(): void ); } - /** @test */ + #[PHPUnit\Test] public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void { $builder = $this->config->builder(); @@ -70,7 +78,7 @@ public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void ->getToken($this->config->signer(), InMemory::plainText('testing')); } - /** @test */ + #[PHPUnit\Test] public function builderShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(): void { $builder = $this->config->builder(); @@ -85,7 +93,7 @@ public function builderShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(): void ->getToken($this->config->signer(), static::$rsaKeys['private']); } - /** @test */ + #[PHPUnit\Test] public function builderCanGenerateAToken(): Token { $user = ['name' => 'testing', 'email' => 'testing@abc.com']; @@ -111,10 +119,8 @@ public function builderCanGenerateAToken(): Token return $token; } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function parserCanReadAToken(Token $generated): void { $read = $this->config->parser()->parse($generated->toString()); @@ -124,10 +130,8 @@ public function parserCanReadAToken(Token $generated): void self::assertSame('testing', $read->claims()->get('user')['name']); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $token): void { $this->expectException(RequiredConstraintsViolated::class); @@ -142,10 +146,8 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(Token $token): void { $this->expectException(RequiredConstraintsViolated::class); @@ -160,10 +162,8 @@ public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(T ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(Token $token): void { $this->expectException(InvalidKeyProvided::class); @@ -175,10 +175,8 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotEcdsaCompatibl ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureValidationShouldSucceedWhenKeyIsRight(Token $token): void { $constraint = new SignedWith( diff --git a/tests/EcdsaTokenTest.php b/tests/EcdsaTokenTest.php index ad3daa65..42376183 100644 --- a/tests/EcdsaTokenTest.php +++ b/tests/EcdsaTokenTest.php @@ -4,50 +4,59 @@ namespace Lcobucci\JWT\Tests; use Lcobucci\JWT\Configuration; +use Lcobucci\JWT\Encoding\ChainedFormatter; +use Lcobucci\JWT\Encoding\JoseEncoder; +use Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion; +use Lcobucci\JWT\Encoding\UnifyAudience; +use Lcobucci\JWT\Signer\Ecdsa; +use Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter; use Lcobucci\JWT\Signer\Ecdsa\Sha256; use Lcobucci\JWT\Signer\Ecdsa\Sha512; use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\OpenSSL; +use Lcobucci\JWT\SodiumBase64Polyfill; use Lcobucci\JWT\Token; use Lcobucci\JWT\Validation\Constraint\SignedWith; +use Lcobucci\JWT\Validation\ConstraintViolation; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; +use Lcobucci\JWT\Validation\Validator; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; use const PHP_EOL; -/** - * @covers \Lcobucci\JWT\Configuration - * @covers \Lcobucci\JWT\Encoding\JoseEncoder - * @covers \Lcobucci\JWT\Encoding\ChainedFormatter - * @covers \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion - * @covers \Lcobucci\JWT\Encoding\UnifyAudience - * @covers \Lcobucci\JWT\Token\Builder - * @covers \Lcobucci\JWT\Token\Parser - * @covers \Lcobucci\JWT\Token\Plain - * @covers \Lcobucci\JWT\Token\DataSet - * @covers \Lcobucci\JWT\Token\Signature - * @covers \Lcobucci\JWT\Signer\Key\InMemory - * @covers \Lcobucci\JWT\Signer\Ecdsa - * @covers \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha256 - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha512 - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\SodiumBase64Polyfill - * @covers \Lcobucci\JWT\Validation\Validator - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\Constraint\SignedWith - * @covers \Lcobucci\JWT\Validation\RequiredConstraintsViolated - */ +#[PHPUnit\CoversClass(Configuration::class)] +#[PHPUnit\CoversClass(JoseEncoder::class)] +#[PHPUnit\CoversClass(ChainedFormatter::class)] +#[PHPUnit\CoversClass(MicrosecondBasedDateConversion::class)] +#[PHPUnit\CoversClass(UnifyAudience::class)] +#[PHPUnit\CoversClass(Token\Builder::class)] +#[PHPUnit\CoversClass(Token\Parser::class)] +#[PHPUnit\CoversClass(Token\Plain::class)] +#[PHPUnit\CoversClass(Token\DataSet::class)] +#[PHPUnit\CoversClass(Token\Signature::class)] +#[PHPUnit\CoversClass(InMemory::class)] +#[PHPUnit\CoversClass(Ecdsa::class)] +#[PHPUnit\CoversClass(MultibyteStringConverter::class)] +#[PHPUnit\CoversClass(Sha256::class)] +#[PHPUnit\CoversClass(Sha512::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(SodiumBase64Polyfill::class)] +#[PHPUnit\CoversClass(Validator::class)] +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(SignedWith::class)] +#[PHPUnit\CoversClass(RequiredConstraintsViolated::class)] class EcdsaTokenTest extends TestCase { use Keys; private Configuration $config; - /** @before */ + #[PHPUnit\Before] public function createConfiguration(): void { $this->config = Configuration::forAsymmetricSigner( @@ -57,7 +66,7 @@ public function createConfiguration(): void ); } - /** @test */ + #[PHPUnit\Test] public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void { $builder = $this->config->builder(); @@ -72,7 +81,7 @@ public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void ->getToken($this->config->signer(), InMemory::plainText('testing')); } - /** @test */ + #[PHPUnit\Test] public function builderShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(): void { $builder = $this->config->builder(); @@ -87,7 +96,7 @@ public function builderShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(): void ->getToken($this->config->signer(), static::$rsaKeys['private']); } - /** @test */ + #[PHPUnit\Test] public function builderCanGenerateAToken(): Token { $user = ['name' => 'testing', 'email' => 'testing@abc.com']; @@ -113,10 +122,8 @@ public function builderCanGenerateAToken(): Token return $token; } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function parserCanReadAToken(Token $generated): void { $read = $this->config->parser()->parse($generated->toString()); @@ -126,10 +133,8 @@ public function parserCanReadAToken(Token $generated): void self::assertSame('testing', $read->claims()->get('user')['name']); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $token): void { $this->expectException(RequiredConstraintsViolated::class); @@ -144,10 +149,8 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(Token $token): void { $this->expectException(RequiredConstraintsViolated::class); @@ -162,10 +165,8 @@ public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(T ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotEcdsaCompatible(Token $token): void { $this->expectException(InvalidKeyProvided::class); @@ -177,10 +178,8 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotEcdsaCompatibl ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureValidationShouldSucceedWhenKeyIsRight(Token $token): void { $constraint = new SignedWith( @@ -191,7 +190,7 @@ public function signatureValidationShouldSucceedWhenKeyIsRight(Token $token): vo self::assertTrue($this->config->validator()->validate($token, $constraint)); } - /** @test */ + #[PHPUnit\Test] public function everythingShouldWorkWithAKeyWithParams(): void { $builder = $this->config->builder(); @@ -212,7 +211,7 @@ public function everythingShouldWorkWithAKeyWithParams(): void self::assertTrue($this->config->validator()->validate($token, $constraint)); } - /** @test */ + #[PHPUnit\Test] public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void { $data = 'eyJhbGciOiJFUzUxMiIsInR5cCI6IkpXVCJ9.eyJoZWxsbyI6IndvcmxkIn0.' diff --git a/tests/EddsaTokenTest.php b/tests/EddsaTokenTest.php index 995a0a35..6d9bb735 100644 --- a/tests/EddsaTokenTest.php +++ b/tests/EddsaTokenTest.php @@ -4,46 +4,51 @@ namespace Lcobucci\JWT\Tests; use Lcobucci\JWT\Configuration; +use Lcobucci\JWT\Encoding\ChainedFormatter; +use Lcobucci\JWT\Encoding\JoseEncoder; +use Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion; +use Lcobucci\JWT\Encoding\UnifyAudience; use Lcobucci\JWT\Signer\Eddsa; use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\OpenSSL; +use Lcobucci\JWT\SodiumBase64Polyfill; use Lcobucci\JWT\Token; use Lcobucci\JWT\Validation\Constraint\SignedWith; +use Lcobucci\JWT\Validation\ConstraintViolation; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; +use Lcobucci\JWT\Validation\Validator; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; -/** - * @covers \Lcobucci\JWT\Configuration - * @covers \Lcobucci\JWT\Encoding\JoseEncoder - * @covers \Lcobucci\JWT\Encoding\ChainedFormatter - * @covers \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion - * @covers \Lcobucci\JWT\Encoding\UnifyAudience - * @covers \Lcobucci\JWT\Token\Builder - * @covers \Lcobucci\JWT\Token\Parser - * @covers \Lcobucci\JWT\Token\Plain - * @covers \Lcobucci\JWT\Token\DataSet - * @covers \Lcobucci\JWT\Token\Signature - * @covers \Lcobucci\JWT\Signer\Key\InMemory - * @covers \Lcobucci\JWT\Signer\Eddsa - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\SodiumBase64Polyfill - * @covers \Lcobucci\JWT\Validation\Validator - * @covers \Lcobucci\JWT\Validation\Constraint\SignedWith - * @covers \Lcobucci\JWT\Validation\Validator - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\RequiredConstraintsViolated - * @covers \Lcobucci\JWT\Validation\Constraint\SignedWith - */ +#[PHPUnit\CoversClass(Configuration::class)] +#[PHPUnit\CoversClass(JoseEncoder::class)] +#[PHPUnit\CoversClass(ChainedFormatter::class)] +#[PHPUnit\CoversClass(MicrosecondBasedDateConversion::class)] +#[PHPUnit\CoversClass(UnifyAudience::class)] +#[PHPUnit\CoversClass(Token\Builder::class)] +#[PHPUnit\CoversClass(Token\Parser::class)] +#[PHPUnit\CoversClass(Token\Plain::class)] +#[PHPUnit\CoversClass(Token\DataSet::class)] +#[PHPUnit\CoversClass(Token\Signature::class)] +#[PHPUnit\CoversClass(InMemory::class)] +#[PHPUnit\CoversClass(Eddsa::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(SodiumBase64Polyfill::class)] +#[PHPUnit\CoversClass(Validator::class)] +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(SignedWith::class)] +#[PHPUnit\CoversClass(RequiredConstraintsViolated::class)] class EddsaTokenTest extends TestCase { use Keys; private Configuration $config; - /** @before */ + #[PHPUnit\Before] public function createConfiguration(): void { $this->config = Configuration::forAsymmetricSigner( @@ -53,7 +58,7 @@ public function createConfiguration(): void ); } - /** @test */ + #[PHPUnit\Test] public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void { $builder = $this->config->builder(); @@ -68,7 +73,7 @@ public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void ->getToken($this->config->signer(), InMemory::plainText('testing')); } - /** @test */ + #[PHPUnit\Test] public function builderCanGenerateAToken(): Token { $user = ['name' => 'testing', 'email' => 'testing@abc.com']; @@ -94,10 +99,8 @@ public function builderCanGenerateAToken(): Token return $token; } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function parserCanReadAToken(Token $generated): void { $read = $this->config->parser()->parse($generated->toString()); @@ -107,10 +110,8 @@ public function parserCanReadAToken(Token $generated): void self::assertSame('testing', $read->claims()->get('user')['name']); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $token): void { $this->expectException(RequiredConstraintsViolated::class); @@ -125,10 +126,8 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureValidationShouldSucceedWhenKeyIsRight(Token $token): void { $constraint = new SignedWith( diff --git a/tests/Encoding/ChainedFormatterTest.php b/tests/Encoding/ChainedFormatterTest.php index 5fc07104..0f9c5cc3 100644 --- a/tests/Encoding/ChainedFormatterTest.php +++ b/tests/Encoding/ChainedFormatterTest.php @@ -5,24 +5,20 @@ use DateTimeImmutable; use Lcobucci\JWT\Encoding\ChainedFormatter; +use Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion; +use Lcobucci\JWT\Encoding\UnifyAudience; +use Lcobucci\JWT\Encoding\UnixTimestampDates; use Lcobucci\JWT\Token\RegisteredClaims; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; -/** @coversDefaultClass \Lcobucci\JWT\Encoding\ChainedFormatter */ +#[PHPUnit\CoversClass(ChainedFormatter::class)] +#[PHPUnit\UsesClass(MicrosecondBasedDateConversion::class)] +#[PHPUnit\UsesClass(UnifyAudience::class)] +#[PHPUnit\UsesClass(UnixTimestampDates::class)] final class ChainedFormatterTest extends TestCase { - /** - * @test - * - * @covers ::__construct - * @covers ::default - * @covers ::withUnixTimestampDates - * @covers ::formatClaims - * - * @uses \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion - * @uses \Lcobucci\JWT\Encoding\UnifyAudience - * @uses \Lcobucci\JWT\Encoding\UnixTimestampDates - */ + #[PHPUnit\Test] public function formatClaimsShouldApplyAllConfiguredFormatters(): void { $expiration = DateTimeImmutable::createFromFormat('U.u', '1487285080.123456'); diff --git a/tests/Encoding/JoseEncoderTest.php b/tests/Encoding/JoseEncoderTest.php index b72e02fe..ba55ae99 100644 --- a/tests/Encoding/JoseEncoderTest.php +++ b/tests/Encoding/JoseEncoderTest.php @@ -6,24 +6,21 @@ use Lcobucci\JWT\Encoding\CannotDecodeContent; use Lcobucci\JWT\Encoding\CannotEncodeContent; use Lcobucci\JWT\Encoding\JoseEncoder; +use Lcobucci\JWT\SodiumBase64Polyfill; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; use function base64_decode; use function is_string; -/** - * @covers \Lcobucci\JWT\Encoding\CannotDecodeContent - * @covers \Lcobucci\JWT\Encoding\CannotEncodeContent - * @coversDefaultClass \Lcobucci\JWT\Encoding\JoseEncoder - */ +#[PHPUnit\CoversClass(JoseEncoder::class)] +#[PHPUnit\CoversClass(CannotDecodeContent::class)] +#[PHPUnit\CoversClass(CannotEncodeContent::class)] +#[PHPUnit\UsesClass(SodiumBase64Polyfill::class)] final class JoseEncoderTest extends TestCase { - /** - * @test - * - * @covers ::jsonEncode - */ + #[PHPUnit\Test] public function jsonEncodeMustReturnAJSONString(): void { $encoder = new JoseEncoder(); @@ -31,11 +28,7 @@ public function jsonEncodeMustReturnAJSONString(): void self::assertSame('{"test":"test"}', $encoder->jsonEncode(['test' => 'test'])); } - /** - * @test - * - * @covers ::jsonEncode - */ + #[PHPUnit\Test] public function jsonEncodeShouldNotEscapeUnicode(): void { $encoder = new JoseEncoder(); @@ -43,11 +36,7 @@ public function jsonEncodeShouldNotEscapeUnicode(): void self::assertSame('"汉语"', $encoder->jsonEncode('汉语')); } - /** - * @test - * - * @covers ::jsonEncode - */ + #[PHPUnit\Test] public function jsonEncodeShouldNotEscapeSlashes(): void { $encoder = new JoseEncoder(); @@ -55,11 +44,7 @@ public function jsonEncodeShouldNotEscapeSlashes(): void self::assertSame('"https://google.com"', $encoder->jsonEncode('https://google.com')); } - /** - * @test - * - * @covers ::jsonEncode - */ + #[PHPUnit\Test] public function jsonEncodeMustRaiseExceptionWhenAnErrorHasOccurred(): void { $encoder = new JoseEncoder(); @@ -71,11 +56,7 @@ public function jsonEncodeMustRaiseExceptionWhenAnErrorHasOccurred(): void $encoder->jsonEncode("\xB1\x31"); } - /** - * @test - * - * @covers ::jsonDecode - */ + #[PHPUnit\Test] public function jsonDecodeMustReturnTheDecodedData(): void { $decoder = new JoseEncoder(); @@ -86,11 +67,7 @@ public function jsonDecodeMustReturnTheDecodedData(): void ); } - /** - * @test - * - * @covers ::jsonDecode - */ + #[PHPUnit\Test] public function jsonDecodeMustRaiseExceptionWhenAnErrorHasOccurred(): void { $decoder = new JoseEncoder(); @@ -102,13 +79,7 @@ public function jsonDecodeMustRaiseExceptionWhenAnErrorHasOccurred(): void $decoder->jsonDecode('{"test":\'test\'}'); } - /** - * @test - * - * @covers ::base64UrlEncode - * - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::bin2base64() - */ + #[PHPUnit\Test] public function base64UrlEncodeMustReturnAUrlSafeBase64(): void { $data = base64_decode('0MB2wKB+L3yvIdzeggmJ+5WOSLaRLTUPXbpzqUe0yuo=', true); @@ -118,17 +89,10 @@ public function base64UrlEncodeMustReturnAUrlSafeBase64(): void self::assertSame('0MB2wKB-L3yvIdzeggmJ-5WOSLaRLTUPXbpzqUe0yuo', $encoder->base64UrlEncode($data)); } - /** - * @link https://tools.ietf.org/html/rfc7520#section-4 - * - * @test - * - * @covers ::base64UrlEncode - * - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::bin2base64() - */ + #[PHPUnit\Test] public function base64UrlEncodeMustEncodeBilboMessageProperly(): void { + /** @link https://tools.ietf.org/html/rfc7520#section-4 */ $message = 'It’s a dangerous business, Frodo, going out your door. You step ' . "onto the road, and if you don't keep your feet, there’s no knowing " . 'where you might be swept off to.'; @@ -142,13 +106,7 @@ public function base64UrlEncodeMustEncodeBilboMessageProperly(): void self::assertSame($expected, $encoder->base64UrlEncode($message)); } - /** - * @test - * - * @covers ::base64UrlDecode - * - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::base642bin() - */ + #[PHPUnit\Test] public function base64UrlDecodeMustRaiseExceptionWhenInvalidBase64CharsAreUsed(): void { $decoder = new JoseEncoder(); @@ -160,13 +118,7 @@ public function base64UrlDecodeMustRaiseExceptionWhenInvalidBase64CharsAreUsed() $decoder->base64UrlDecode('ááá'); } - /** - * @test - * - * @covers ::base64UrlDecode - * - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::base642bin() - */ + #[PHPUnit\Test] public function base64UrlDecodeMustReturnTheRightData(): void { $data = base64_decode('0MB2wKB+L3yvIdzeggmJ+5WOSLaRLTUPXbpzqUe0yuo=', true); @@ -175,17 +127,10 @@ public function base64UrlDecodeMustReturnTheRightData(): void self::assertSame($data, $decoder->base64UrlDecode('0MB2wKB-L3yvIdzeggmJ-5WOSLaRLTUPXbpzqUe0yuo')); } - /** - * @link https://tools.ietf.org/html/rfc7520#section-4 - * - * @test - * - * @covers ::base64UrlDecode - * - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::base642bin() - */ + #[PHPUnit\Test] public function base64UrlDecodeMustDecodeBilboMessageProperly(): void { + /** @link https://tools.ietf.org/html/rfc7520#section-4 */ $message = 'SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IH' . 'lvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBk' . 'b24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcm' diff --git a/tests/Encoding/MicrosecondBasedDateConversionTest.php b/tests/Encoding/MicrosecondBasedDateConversionTest.php index e1b3c480..1b9ddb07 100644 --- a/tests/Encoding/MicrosecondBasedDateConversionTest.php +++ b/tests/Encoding/MicrosecondBasedDateConversionTest.php @@ -6,17 +6,13 @@ use DateTimeImmutable; use Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion; use Lcobucci\JWT\Token\RegisteredClaims; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; -/** @coversDefaultClass \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion */ +#[PHPUnit\CoversClass(MicrosecondBasedDateConversion::class)] final class MicrosecondBasedDateConversionTest extends TestCase { - /** - * @test - * - * @covers ::formatClaims - * @covers ::convertDate - */ + #[PHPUnit\Test] public function dateClaimsHaveMicrosecondsOrSeconds(): void { $issuedAt = new DateTimeImmutable('@1487285080'); @@ -42,12 +38,7 @@ public function dateClaimsHaveMicrosecondsOrSeconds(): void self::assertSame('test', $formatted['testing']); // this should remain untouched } - /** - * @test - * - * @covers ::formatClaims - * @covers ::convertDate - */ + #[PHPUnit\Test] public function notAllDateClaimsNeedToBeConfigured(): void { $issuedAt = new DateTimeImmutable('@1487285080'); diff --git a/tests/Encoding/UnifyAudienceTest.php b/tests/Encoding/UnifyAudienceTest.php index 22afd2fd..c9404eb1 100644 --- a/tests/Encoding/UnifyAudienceTest.php +++ b/tests/Encoding/UnifyAudienceTest.php @@ -5,16 +5,13 @@ use Lcobucci\JWT\Encoding\UnifyAudience; use Lcobucci\JWT\Token\RegisteredClaims; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; -/** @coversDefaultClass \Lcobucci\JWT\Encoding\UnifyAudience */ +#[PHPUnit\CoversClass(UnifyAudience::class)] final class UnifyAudienceTest extends TestCase { - /** - * @test - * - * @covers ::formatClaims - */ + #[PHPUnit\Test] public function nothingShouldBeDoneWhenAudienceIsNotSet(): void { $claims = ['testing' => 'test']; @@ -25,11 +22,7 @@ public function nothingShouldBeDoneWhenAudienceIsNotSet(): void self::assertSame('test', $formatted['testing']); } - /** - * @test - * - * @covers ::formatClaims - */ + #[PHPUnit\Test] public function audienceShouldBeFormattedAsSingleStringWhenOneValueIsUsed(): void { $claims = [ @@ -44,11 +37,7 @@ public function audienceShouldBeFormattedAsSingleStringWhenOneValueIsUsed(): voi self::assertSame('test', $formatted['testing']); // this should remain untouched } - /** - * @test - * - * @covers ::formatClaims - */ + #[PHPUnit\Test] public function audienceShouldBeFormattedAsArrayWhenMultipleValuesAreUsed(): void { $claims = [ diff --git a/tests/Encoding/UnixTimestampDatesTest.php b/tests/Encoding/UnixTimestampDatesTest.php index b08f20b8..fdd19c53 100644 --- a/tests/Encoding/UnixTimestampDatesTest.php +++ b/tests/Encoding/UnixTimestampDatesTest.php @@ -6,17 +6,13 @@ use DateTimeImmutable; use Lcobucci\JWT\Encoding\UnixTimestampDates; use Lcobucci\JWT\Token\RegisteredClaims; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; -/** @coversDefaultClass \Lcobucci\JWT\Encoding\UnixTimestampDates */ +#[PHPUnit\CoversClass(UnixTimestampDates::class)] final class UnixTimestampDatesTest extends TestCase { - /** - * @test - * - * @covers ::formatClaims - * @covers ::convertDate - */ + #[PHPUnit\Test] public function dateClaimsHaveMicrosecondsOrSeconds(): void { $issuedAt = new DateTimeImmutable('@1487285080'); @@ -42,12 +38,7 @@ public function dateClaimsHaveMicrosecondsOrSeconds(): void self::assertSame('test', $formatted['testing']); // this should remain untouched } - /** - * @test - * - * @covers ::formatClaims - * @covers ::convertDate - */ + #[PHPUnit\Test] public function notAllDateClaimsNeedToBeConfigured(): void { $issuedAt = new DateTimeImmutable('@1487285080'); diff --git a/tests/HmacTokenTest.php b/tests/HmacTokenTest.php index e3503d77..1d16b659 100644 --- a/tests/HmacTokenTest.php +++ b/tests/HmacTokenTest.php @@ -4,13 +4,23 @@ namespace Lcobucci\JWT\Tests; use Lcobucci\JWT\Configuration; +use Lcobucci\JWT\Encoding\ChainedFormatter; +use Lcobucci\JWT\Encoding\JoseEncoder; +use Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion; +use Lcobucci\JWT\Encoding\UnifyAudience; +use Lcobucci\JWT\Signer\Hmac; use Lcobucci\JWT\Signer\Hmac\Sha256; use Lcobucci\JWT\Signer\Hmac\Sha512; +use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\OpenSSL; use Lcobucci\JWT\SodiumBase64Polyfill; use Lcobucci\JWT\Token; use Lcobucci\JWT\Validation\Constraint\SignedWith; +use Lcobucci\JWT\Validation\ConstraintViolation; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; +use Lcobucci\JWT\Validation\Validator; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; @@ -18,32 +28,32 @@ use function sys_get_temp_dir; use function tempnam; -/** - * @covers \Lcobucci\JWT\Configuration - * @covers \Lcobucci\JWT\Encoding\JoseEncoder - * @covers \Lcobucci\JWT\Encoding\ChainedFormatter - * @covers \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion - * @covers \Lcobucci\JWT\Encoding\UnifyAudience - * @covers \Lcobucci\JWT\Token\Builder - * @covers \Lcobucci\JWT\Token\Parser - * @covers \Lcobucci\JWT\Token\Plain - * @covers \Lcobucci\JWT\Token\DataSet - * @covers \Lcobucci\JWT\Token\Signature - * @covers \Lcobucci\JWT\Signer\Key\InMemory - * @covers \Lcobucci\JWT\Signer\Hmac - * @covers \Lcobucci\JWT\Signer\Hmac\Sha256 - * @covers \Lcobucci\JWT\Signer\Hmac\Sha512 - * @covers \Lcobucci\JWT\SodiumBase64Polyfill - * @covers \Lcobucci\JWT\Validation\Validator - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\RequiredConstraintsViolated - * @covers \Lcobucci\JWT\Validation\Constraint\SignedWith - */ +#[PHPUnit\CoversClass(Configuration::class)] +#[PHPUnit\CoversClass(JoseEncoder::class)] +#[PHPUnit\CoversClass(ChainedFormatter::class)] +#[PHPUnit\CoversClass(MicrosecondBasedDateConversion::class)] +#[PHPUnit\CoversClass(UnifyAudience::class)] +#[PHPUnit\CoversClass(Token\Builder::class)] +#[PHPUnit\CoversClass(Token\Parser::class)] +#[PHPUnit\CoversClass(Token\Plain::class)] +#[PHPUnit\CoversClass(Token\DataSet::class)] +#[PHPUnit\CoversClass(Token\Signature::class)] +#[PHPUnit\CoversClass(InMemory::class)] +#[PHPUnit\CoversClass(Hmac::class)] +#[PHPUnit\CoversClass(Sha256::class)] +#[PHPUnit\CoversClass(Sha512::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(SodiumBase64Polyfill::class)] +#[PHPUnit\CoversClass(Validator::class)] +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(SignedWith::class)] +#[PHPUnit\CoversClass(RequiredConstraintsViolated::class)] class HmacTokenTest extends TestCase { private Configuration $config; - /** @before */ + #[PHPUnit\Before] public function createConfiguration(): void { $this->config = Configuration::forSymmetricSigner( @@ -52,7 +62,7 @@ public function createConfiguration(): void ); } - /** @test */ + #[PHPUnit\Test] public function builderCanGenerateAToken(): Token { $user = ['name' => 'testing', 'email' => 'testing@abc.com']; @@ -73,10 +83,8 @@ public function builderCanGenerateAToken(): Token return $token; } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function parserCanReadAToken(Token $generated): void { $read = $this->config->parser()->parse($generated->toString()); @@ -86,10 +94,8 @@ public function parserCanReadAToken(Token $generated): void self::assertSame('testing', $read->claims()->get('user')['name']); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $token): void { $this->expectException(RequiredConstraintsViolated::class); @@ -104,10 +110,8 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(Token $token): void { $this->expectException(RequiredConstraintsViolated::class); @@ -119,10 +123,8 @@ public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(T ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureValidationShouldSucceedWhenKeyIsRight(Token $token): void { $constraint = new SignedWith($this->config->signer(), $this->config->verificationKey()); @@ -130,7 +132,7 @@ public function signatureValidationShouldSucceedWhenKeyIsRight(Token $token): vo self::assertTrue($this->config->validator()->validate($token, $constraint)); } - /** @test */ + #[PHPUnit\Test] public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void { $config = Configuration::forSymmetricSigner( @@ -149,7 +151,7 @@ public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void self::assertSame('world', $token->claims()->get('hello')); } - /** @test */ + #[PHPUnit\Test] public function signatureValidationWithLocalFileKeyReferenceWillOperateWithKeyContents(): void { $key = tempnam(sys_get_temp_dir(), 'a-very-long-prefix-to-create-a-longer-key'); diff --git a/tests/JwtFacadeTest.php b/tests/JwtFacadeTest.php index abf9a0f0..75cae39d 100644 --- a/tests/JwtFacadeTest.php +++ b/tests/JwtFacadeTest.php @@ -7,59 +7,56 @@ use DateTimeImmutable; use Lcobucci\Clock\FrozenClock; use Lcobucci\JWT\Builder; +use Lcobucci\JWT\Encoding; use Lcobucci\JWT\JwtFacade; -use Lcobucci\JWT\Signer\Hmac\Sha256; -use Lcobucci\JWT\Signer\Hmac\Sha384; +use Lcobucci\JWT\Signer\Hmac; use Lcobucci\JWT\Signer\Key\InMemory; -use Lcobucci\JWT\Token\Plain; -use Lcobucci\JWT\Validation\Constraint\IssuedBy; -use Lcobucci\JWT\Validation\Constraint\SignedWith; -use Lcobucci\JWT\Validation\Constraint\SignedWithOneInSet; -use Lcobucci\JWT\Validation\Constraint\SignedWithUntilDate; -use Lcobucci\JWT\Validation\Constraint\StrictValidAt; +use Lcobucci\JWT\SodiumBase64Polyfill; +use Lcobucci\JWT\Token; +use Lcobucci\JWT\Validation\Constraint; +use Lcobucci\JWT\Validation\ConstraintViolation; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; +use Lcobucci\JWT\Validation\Validator; use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use Psr\Clock\ClockInterface; -/** - * @covers \Lcobucci\JWT\JwtFacade - * - * @uses \Lcobucci\JWT\Token\Parser - * @uses \Lcobucci\JWT\Encoding\JoseEncoder - * @uses \Lcobucci\JWT\Encoding\ChainedFormatter - * @uses \Lcobucci\JWT\Encoding\UnifyAudience - * @uses \Lcobucci\JWT\Encoding\UnixTimestampDates - * @uses \Lcobucci\JWT\Signer\Hmac - * @uses \Lcobucci\JWT\Signer\Hmac\Sha256 - * @uses \Lcobucci\JWT\Signer\Hmac\Sha384 - * @uses \Lcobucci\JWT\Signer\Key\InMemory - * @uses \Lcobucci\JWT\SodiumBase64Polyfill - * @uses \Lcobucci\JWT\Token\Builder - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - * @uses \Lcobucci\JWT\Validation\Validator - * @uses \Lcobucci\JWT\Validation\Constraint\IssuedBy - * @uses \Lcobucci\JWT\Validation\Constraint\SignedWith - * @uses \Lcobucci\JWT\Validation\Constraint\SignedWithOneInSet - * @uses \Lcobucci\JWT\Validation\Constraint\SignedWithUntilDate - * @uses \Lcobucci\JWT\Validation\Constraint\StrictValidAt - * @uses \Lcobucci\JWT\Validation\ConstraintViolation - * @uses \Lcobucci\JWT\Validation\RequiredConstraintsViolated - */ +#[PHPUnit\CoversClass(JwtFacade::class)] +#[PHPUnit\UsesClass(Token\Builder::class)] +#[PHPUnit\UsesClass(Token\Parser::class)] +#[PHPUnit\UsesClass(Token\Plain::class)] +#[PHPUnit\UsesClass(Token\DataSet::class)] +#[PHPUnit\UsesClass(Token\Signature::class)] +#[PHPUnit\UsesClass(Encoding\JoseEncoder::class)] +#[PHPUnit\UsesClass(Encoding\ChainedFormatter::class)] +#[PHPUnit\UsesClass(Encoding\UnixTimestampDates::class)] +#[PHPUnit\UsesClass(Encoding\UnifyAudience::class)] +#[PHPUnit\UsesClass(Hmac::class)] +#[PHPUnit\UsesClass(Hmac\Sha256::class)] +#[PHPUnit\UsesClass(Hmac\Sha384::class)] +#[PHPUnit\UsesClass(SodiumBase64Polyfill::class)] +#[PHPUnit\UsesClass(InMemory::class)] +#[PHPUnit\UsesClass(Validator::class)] +#[PHPUnit\UsesClass(Constraint\IssuedBy::class)] +#[PHPUnit\UsesClass(Constraint\SignedWith::class)] +#[PHPUnit\UsesClass(Constraint\SignedWithOneInSet::class)] +#[PHPUnit\UsesClass(Constraint\SignedWithUntilDate::class)] +#[PHPUnit\UsesClass(Constraint\StrictValidAt::class)] +#[PHPUnit\UsesClass(ConstraintViolation::class)] +#[PHPUnit\UsesClass(RequiredConstraintsViolated::class)] final class JwtFacadeTest extends TestCase { private FrozenClock $clock; - private Sha256 $signer; + private Hmac\Sha256 $signer; private InMemory $key; /** @var non-empty-string */ private string $issuer; - protected function setUp(): void + #[PHPUnit\Before] + public function configureDependencies(): void { $this->clock = new FrozenClock(new DateTimeImmutable('2021-07-10')); - $this->signer = new Sha256(); + $this->signer = new Hmac\Sha256(); $this->key = InMemory::base64Encoded('qOIXmZRqZKY80qg0BjtCrskM6OK7gPOea8mz1H7h/dE='); $this->issuer = 'bar'; } @@ -143,12 +140,12 @@ public function goodJwt(): void { $token = (new JwtFacade())->parse( $this->createToken(), - new SignedWith($this->signer, $this->key), - new StrictValidAt($this->clock), - new IssuedBy($this->issuer), + new Constraint\SignedWith($this->signer, $this->key), + new Constraint\StrictValidAt($this->clock), + new Constraint\IssuedBy($this->issuer), ); - self::assertInstanceOf(Plain::class, $token); + self::assertInstanceOf(Token\Plain::class, $token); } #[PHPUnit\Test] @@ -159,9 +156,9 @@ public function badSigner(): void (new JwtFacade())->parse( $this->createToken(), - new SignedWith(new Sha384(), $this->key), - new StrictValidAt($this->clock), - new IssuedBy($this->issuer), + new Constraint\SignedWith(new Hmac\Sha384(), $this->key), + new Constraint\StrictValidAt($this->clock), + new Constraint\IssuedBy($this->issuer), ); } @@ -173,9 +170,12 @@ public function badKey(): void (new JwtFacade())->parse( $this->createToken(), - new SignedWith($this->signer, InMemory::base64Encoded('czyPTpN595zVNSuvoNNlXCRFgXS2fHscMR36dGojaUE=')), - new StrictValidAt($this->clock), - new IssuedBy($this->issuer), + new Constraint\SignedWith( + $this->signer, + InMemory::base64Encoded('czyPTpN595zVNSuvoNNlXCRFgXS2fHscMR36dGojaUE='), + ), + new Constraint\StrictValidAt($this->clock), + new Constraint\IssuedBy($this->issuer), ); } @@ -190,9 +190,9 @@ public function badTime(): void (new JwtFacade())->parse( $token, - new SignedWith($this->signer, $this->key), - new StrictValidAt($this->clock), - new IssuedBy($this->issuer), + new Constraint\SignedWith($this->signer, $this->key), + new Constraint\StrictValidAt($this->clock), + new Constraint\IssuedBy($this->issuer), ); } @@ -204,9 +204,9 @@ public function badIssuer(): void (new JwtFacade())->parse( $this->createToken(), - new SignedWith($this->signer, $this->key), - new StrictValidAt($this->clock), - new IssuedBy('xyz'), + new Constraint\SignedWith($this->signer, $this->key), + new Constraint\StrictValidAt($this->clock), + new Constraint\IssuedBy('xyz'), ); } @@ -217,9 +217,9 @@ public function parserForNonUnencryptedTokens(): void (new JwtFacade(new UnsupportedParser()))->parse( 'a.very-broken.token', - new SignedWith($this->signer, $this->key), - new StrictValidAt($this->clock), - new IssuedBy($this->issuer), + new Constraint\SignedWith($this->signer, $this->key), + new Constraint\StrictValidAt($this->clock), + new Constraint\IssuedBy($this->issuer), ); } @@ -245,8 +245,8 @@ public function now(): DateTimeImmutable $token, $facade->parse( $token->toString(), - new SignedWith($this->signer, $this->key), - new StrictValidAt($clock), + new Constraint\SignedWith($this->signer, $this->key), + new Constraint\StrictValidAt($clock), ), ); } @@ -258,24 +258,24 @@ public function multipleKeys(): void $token = (new JwtFacade())->parse( $this->createToken(), - new SignedWithOneInSet( - new SignedWithUntilDate( + new Constraint\SignedWithOneInSet( + new Constraint\SignedWithUntilDate( $this->signer, InMemory::base64Encoded('czyPTpN595zVNSuvoNNlXCRFgXS2fHscMR36dGojaUE='), new DateTimeImmutable('2024-11-19 22:10:00'), $clock, ), - new SignedWithUntilDate( + new Constraint\SignedWithUntilDate( $this->signer, $this->key, new DateTimeImmutable('2025-11-19 22:10:00'), $clock, ), ), - new StrictValidAt($this->clock), - new IssuedBy($this->issuer), + new Constraint\StrictValidAt($this->clock), + new Constraint\IssuedBy($this->issuer), ); - self::assertInstanceOf(Plain::class, $token); + self::assertInstanceOf(Token\Plain::class, $token); } } diff --git a/tests/Keys.php b/tests/Keys.php index 1f2af536..44cf15e3 100644 --- a/tests/Keys.php +++ b/tests/Keys.php @@ -4,6 +4,7 @@ namespace Lcobucci\JWT\Tests; use Lcobucci\JWT\Signer\Key; +use PHPUnit\Framework\Attributes as PHPUnit; trait Keys { @@ -16,7 +17,7 @@ trait Keys /** @var array */ protected static array $eddsaKeys; - /** @beforeClass */ + #[PHPUnit\BeforeClass] public static function createRsaKeys(): void { if (isset(static::$rsaKeys)) { @@ -33,7 +34,7 @@ public static function createRsaKeys(): void ]; } - /** @beforeClass */ + #[PHPUnit\BeforeClass] public static function createEcdsaKeys(): void { if (isset(static::$ecdsaKeys)) { @@ -54,7 +55,7 @@ public static function createEcdsaKeys(): void ]; } - /** @beforeClass */ + #[PHPUnit\BeforeClass] public static function createEddsaKeys(): void { if (isset(static::$eddsaKeys)) { diff --git a/tests/MaliciousTamperingPreventionTest.php b/tests/MaliciousTamperingPreventionTest.php index 30746e8a..12b97285 100644 --- a/tests/MaliciousTamperingPreventionTest.php +++ b/tests/MaliciousTamperingPreventionTest.php @@ -4,12 +4,20 @@ namespace Lcobucci\JWT\Tests; use Lcobucci\JWT\Configuration; +use Lcobucci\JWT\Encoding; use Lcobucci\JWT\Encoding\JoseEncoder; +use Lcobucci\JWT\Signer\Ecdsa; use Lcobucci\JWT\Signer\Ecdsa\Sha512 as ES512; +use Lcobucci\JWT\Signer\Hmac; use Lcobucci\JWT\Signer\Hmac\Sha256 as HS512; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\SodiumBase64Polyfill; +use Lcobucci\JWT\Token; use Lcobucci\JWT\Token\Plain; use Lcobucci\JWT\Validation\Constraint\SignedWith; +use Lcobucci\JWT\Validation\ConstraintViolation; +use Lcobucci\JWT\Validation\Validator; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; @@ -19,13 +27,27 @@ use const PHP_EOL; +#[PHPUnit\CoversClass(Configuration::class)] +#[PHPUnit\CoversClass(Encoding\JoseEncoder::class)] +#[PHPUnit\CoversClass(Token\Parser::class)] +#[PHPUnit\CoversClass(Token\Plain::class)] +#[PHPUnit\CoversClass(Token\DataSet::class)] +#[PHPUnit\CoversClass(Token\Signature::class)] +#[PHPUnit\CoversClass(Ecdsa::class)] +#[PHPUnit\CoversClass(Ecdsa\Sha512::class)] +#[PHPUnit\CoversClass(Hmac\Sha256::class)] +#[PHPUnit\CoversClass(InMemory::class)] +#[PHPUnit\CoversClass(SodiumBase64Polyfill::class)] +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(Validator::class)] +#[PHPUnit\CoversClass(SignedWith::class)] final class MaliciousTamperingPreventionTest extends TestCase { use Keys; private Configuration $config; - /** @before */ + #[PHPUnit\Before] public function createConfiguration(): void { $this->config = Configuration::forAsymmetricSigner( @@ -42,28 +64,7 @@ public function createConfiguration(): void ); } - /** - * @test - * - * @covers \Lcobucci\JWT\Configuration - * @covers \Lcobucci\JWT\Encoding\JoseEncoder - * @covers \Lcobucci\JWT\Token\Builder - * @covers \Lcobucci\JWT\Token\Parser - * @covers \Lcobucci\JWT\Token\Plain - * @covers \Lcobucci\JWT\Token\DataSet - * @covers \Lcobucci\JWT\Token\Signature - * @covers \Lcobucci\JWT\Signer\Key\InMemory - * @covers \Lcobucci\JWT\Signer\Ecdsa - * @covers \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha512 - * @covers \Lcobucci\JWT\Signer\Hmac - * @covers \Lcobucci\JWT\Signer\Hmac\Sha256 - * @covers \Lcobucci\JWT\Signer\Hmac\Sha512 - * @covers \Lcobucci\JWT\SodiumBase64Polyfill - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\Constraint\SignedWith - * @covers \Lcobucci\JWT\Validation\Validator - */ + #[PHPUnit\Test] public function preventRegressionsThatAllowsMaliciousTampering(): void { $data = 'eyJhbGciOiJFUzUxMiIsInR5cCI6IkpXVCJ9.eyJoZWxsbyI6IndvcmxkIn0.' diff --git a/tests/RFC6978VectorTest.php b/tests/RFC6978VectorTest.php index 9e849abe..53b33f19 100644 --- a/tests/RFC6978VectorTest.php +++ b/tests/RFC6978VectorTest.php @@ -9,12 +9,21 @@ use Lcobucci\JWT\Signer\Ecdsa\Sha512; use Lcobucci\JWT\Signer\Key; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\OpenSSL; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function hex2bin; use const PHP_EOL; +#[PHPUnit\CoversClass(InMemory::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(Ecdsa::class)] +#[PHPUnit\CoversClass(Ecdsa\MultibyteStringConverter::class)] +#[PHPUnit\CoversClass(Ecdsa\Sha256::class)] +#[PHPUnit\CoversClass(Ecdsa\Sha384::class)] +#[PHPUnit\CoversClass(Ecdsa\Sha512::class)] final class RFC6978VectorTest extends TestCase { /** @@ -22,19 +31,10 @@ final class RFC6978VectorTest extends TestCase * @see https://tools.ietf.org/html/rfc6979#appendix-A.2.6 * @see https://tools.ietf.org/html/rfc6979#appendix-A.2.7 * - * @test - * @dataProvider dataRFC6979 - * - * @covers \Lcobucci\JWT\Signer\Key\InMemory - * @covers \Lcobucci\JWT\Signer\Ecdsa - * @covers \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha256 - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha384 - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha512 - * @covers \Lcobucci\JWT\Signer\OpenSSL - * * @param non-empty-string $payload */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('dataRFC6979')] public function theVectorsFromRFC6978CanBeVerified( Ecdsa $signer, Key $key, diff --git a/tests/RsaTokenTest.php b/tests/RsaTokenTest.php index eaeb2076..37a7d6d4 100644 --- a/tests/RsaTokenTest.php +++ b/tests/RsaTokenTest.php @@ -4,47 +4,52 @@ namespace Lcobucci\JWT\Tests; use Lcobucci\JWT\Configuration; +use Lcobucci\JWT\Encoding; use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\OpenSSL; +use Lcobucci\JWT\Signer\Rsa; use Lcobucci\JWT\Signer\Rsa\Sha256; use Lcobucci\JWT\Signer\Rsa\Sha512; +use Lcobucci\JWT\SodiumBase64Polyfill; use Lcobucci\JWT\Token; use Lcobucci\JWT\Validation\Constraint\SignedWith; +use Lcobucci\JWT\Validation\ConstraintViolation; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; +use Lcobucci\JWT\Validation\Validator; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; -/** - * @covers \Lcobucci\JWT\Configuration - * @covers \Lcobucci\JWT\Encoding\JoseEncoder - * @covers \Lcobucci\JWT\Encoding\ChainedFormatter - * @covers \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion - * @covers \Lcobucci\JWT\Encoding\UnifyAudience - * @covers \Lcobucci\JWT\Token\Builder - * @covers \Lcobucci\JWT\Token\Parser - * @covers \Lcobucci\JWT\Token\Plain - * @covers \Lcobucci\JWT\Token\DataSet - * @covers \Lcobucci\JWT\Token\Signature - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\Signer\Key\InMemory - * @covers \Lcobucci\JWT\Signer\Rsa - * @covers \Lcobucci\JWT\Signer\Rsa\Sha256 - * @covers \Lcobucci\JWT\Signer\Rsa\Sha512 - * @covers \Lcobucci\JWT\SodiumBase64Polyfill - * @covers \Lcobucci\JWT\Validation\Validator - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\RequiredConstraintsViolated - * @covers \Lcobucci\JWT\Validation\Constraint\SignedWith - */ +#[PHPUnit\CoversClass(Configuration::class)] +#[PHPUnit\CoversClass(Encoding\JoseEncoder::class)] +#[PHPUnit\CoversClass(Encoding\ChainedFormatter::class)] +#[PHPUnit\CoversClass(Encoding\MicrosecondBasedDateConversion::class)] +#[PHPUnit\CoversClass(Encoding\UnifyAudience::class)] +#[PHPUnit\CoversClass(Token\Builder::class)] +#[PHPUnit\CoversClass(Token\Parser::class)] +#[PHPUnit\CoversClass(Token\Plain::class)] +#[PHPUnit\CoversClass(Token\DataSet::class)] +#[PHPUnit\CoversClass(Token\Signature::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(Rsa::class)] +#[PHPUnit\CoversClass(Rsa\Sha256::class)] +#[PHPUnit\CoversClass(Rsa\Sha512::class)] +#[PHPUnit\CoversClass(InMemory::class)] +#[PHPUnit\CoversClass(SodiumBase64Polyfill::class)] +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(RequiredConstraintsViolated::class)] +#[PHPUnit\CoversClass(Validator::class)] +#[PHPUnit\CoversClass(SignedWith::class)] class RsaTokenTest extends TestCase { use Keys; private Configuration $config; - /** @before */ + #[PHPUnit\Before] public function createConfiguration(): void { $this->config = Configuration::forAsymmetricSigner( @@ -54,7 +59,7 @@ public function createConfiguration(): void ); } - /** @test */ + #[PHPUnit\Test] public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void { $builder = $this->config->builder(); @@ -69,7 +74,7 @@ public function builderShouldRaiseExceptionWhenKeyIsInvalid(): void ->getToken($this->config->signer(), InMemory::plainText('testing')); } - /** @test */ + #[PHPUnit\Test] public function builderShouldRaiseExceptionWhenKeyIsNotRsaCompatible(): void { $builder = $this->config->builder(); @@ -84,7 +89,7 @@ public function builderShouldRaiseExceptionWhenKeyIsNotRsaCompatible(): void ->getToken($this->config->signer(), static::$ecdsaKeys['private']); } - /** @test */ + #[PHPUnit\Test] public function builderCanGenerateAToken(): Token { $user = ['name' => 'testing', 'email' => 'testing@abc.com']; @@ -105,10 +110,8 @@ public function builderCanGenerateAToken(): Token return $token; } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function parserCanReadAToken(Token $generated): void { $read = $this->config->parser()->parse($generated->toString()); @@ -118,10 +121,8 @@ public function parserCanReadAToken(Token $generated): void self::assertSame('testing', $read->claims()->get('user')['name']); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $token): void { $this->expectException(RequiredConstraintsViolated::class); @@ -133,10 +134,8 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $t ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(Token $token): void { $this->expectException(RequiredConstraintsViolated::class); @@ -148,10 +147,8 @@ public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(T ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRsaCompatible(Token $token): void { $this->expectException(InvalidKeyProvided::class); @@ -166,10 +163,8 @@ public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRsaCompatible( ); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function signatureValidationShouldSucceedWhenKeyIsRight(Token $token): void { $constraint = new SignedWith($this->config->signer(), $this->config->verificationKey()); @@ -177,7 +172,7 @@ public function signatureValidationShouldSucceedWhenKeyIsRight(Token $token): vo self::assertTrue($this->config->validator()->validate($token, $constraint)); } - /** @test */ + #[PHPUnit\Test] public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void { $data = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXUyJ9.eyJoZWxsbyI6IndvcmxkIn0.s' diff --git a/tests/Signer/Blake2bTest.php b/tests/Signer/Blake2bTest.php index 9ba8da9a..19109ab0 100644 --- a/tests/Signer/Blake2bTest.php +++ b/tests/Signer/Blake2bTest.php @@ -7,16 +7,15 @@ use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key\InMemory; use Lcobucci\JWT\SodiumBase64Polyfill; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function hash_equals; -/** - * @coversDefaultClass \Lcobucci\JWT\Signer\Blake2b - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::base642bin() - */ +#[PHPUnit\CoversClass(Blake2b::class)] +#[PHPUnit\UsesClass(InMemory::class)] +#[PHPUnit\UsesClass(InvalidKeyProvided::class)] +#[PHPUnit\UsesClass(SodiumBase64Polyfill::class)] final class Blake2bTest extends TestCase { private const KEY_ONE = 'GOu4rLyVCBxmxP+sbniU68ojAja5PkRdvv7vNvBCqDQ='; @@ -31,22 +30,19 @@ final class Blake2bTest extends TestCase /** @var non-empty-string */ private string $expectedHashWithKeyOne; - /** @before */ + #[PHPUnit\Before] public function initializeKey(): void { - $this->keyOne = InMemory::base64Encoded(self::KEY_ONE); - $this->keyTwo = InMemory::base64Encoded(self::KEY_TWO); + $this->keyOne = InMemory::base64Encoded(self::KEY_ONE); + $this->keyTwo = InMemory::base64Encoded(self::KEY_TWO); + $this->expectedHashWithKeyOne = SodiumBase64Polyfill::base642bin( self::EXPECTED_HASH_WITH_KEY_ONE, SodiumBase64Polyfill::SODIUM_BASE64_VARIANT_ORIGINAL, ); } - /** - * @test - * - * @covers ::algorithmId - */ + #[PHPUnit\Test] public function algorithmIdMustBeCorrect(): void { $signer = new Blake2b(); @@ -54,12 +50,7 @@ public function algorithmIdMustBeCorrect(): void self::assertSame('BLAKE2B', $signer->algorithmId()); } - /** - * @test - * - * @covers ::sign - * @covers ::verify - */ + #[PHPUnit\Test] public function generatedSignatureMustBeSuccessfullyVerified(): void { $signer = new Blake2b(); @@ -68,13 +59,7 @@ public function generatedSignatureMustBeSuccessfullyVerified(): void self::assertTrue($signer->verify($this->expectedHashWithKeyOne, self::CONTENTS, $this->keyOne)); } - /** - * @test - * - * @covers ::sign - * - * @uses \Lcobucci\JWT\Signer\InvalidKeyProvided - */ + #[PHPUnit\Test] public function signShouldRejectShortKeys(): void { $signer = new Blake2b(); @@ -85,12 +70,7 @@ public function signShouldRejectShortKeys(): void $signer->sign(self::CONTENTS, InMemory::base64Encoded(self::SHORT_KEY)); } - /** - * @test - * - * @covers ::sign - * @covers ::verify - */ + #[PHPUnit\Test] public function verifyShouldReturnFalseWhenExpectedHashWasNotCreatedWithSameInformation(): void { $signer = new Blake2b(); diff --git a/tests/Signer/Ecdsa/EcdsaTestCase.php b/tests/Signer/Ecdsa/EcdsaTestCase.php index 06ac0568..d0c76472 100644 --- a/tests/Signer/Ecdsa/EcdsaTestCase.php +++ b/tests/Signer/Ecdsa/EcdsaTestCase.php @@ -9,6 +9,7 @@ use Lcobucci\JWT\Signer\Key; use Lcobucci\JWT\Tests\Keys; use OpenSSLAsymmetricKey; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; @@ -24,7 +25,7 @@ abstract class EcdsaTestCase extends TestCase protected MultibyteStringConverter $pointsManipulator; - /** @after */ + #[PHPUnit\After] final public function clearOpenSSLErrors(): void { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedWhile @@ -32,7 +33,7 @@ final public function clearOpenSSLErrors(): void } } - /** @before */ + #[PHPUnit\Before] final public function createDependencies(): void { $this->pointsManipulator = new MultibyteStringConverter(); @@ -52,31 +53,31 @@ abstract protected function verificationKey(): Key; abstract protected function signingKey(): Key; - /** @test */ + #[PHPUnit\Test] final public function algorithmIdMustBeCorrect(): void { self::assertSame($this->algorithmId(), $this->algorithm()->algorithmId()); } - /** @test */ + #[PHPUnit\Test] final public function signatureAlgorithmMustBeCorrect(): void { self::assertSame($this->signatureAlgorithm(), $this->algorithm()->algorithm()); } - /** @test */ + #[PHPUnit\Test] final public function pointLengthMustBeCorrect(): void { self::assertSame($this->pointLength(), $this->algorithm()->pointLength()); } - /** @test */ + #[PHPUnit\Test] final public function expectedKeyLengthMustBeCorrect(): void { self::assertSame($this->keyLength(), $this->algorithm()->expectedKeyLength()); } - /** @test */ + #[PHPUnit\Test] public function signShouldReturnTheAHashBasedOnTheOpenSslSignature(): void { $payload = 'testing'; @@ -98,10 +99,8 @@ public function signShouldReturnTheAHashBasedOnTheOpenSslSignature(): void ); } - /** - * @test - * @dataProvider incompatibleKeys - */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('incompatibleKeys')] public function signShouldRaiseAnExceptionWhenKeyLengthIsNotTheExpectedOne( string $keyId, int $keyLength, @@ -120,7 +119,7 @@ public function signShouldRaiseAnExceptionWhenKeyLengthIsNotTheExpectedOne( /** @return iterable */ abstract public static function incompatibleKeys(): iterable; - /** @test */ + #[PHPUnit\Test] public function signShouldRaiseAnExceptionWhenKeyTypeIsNotEC(): void { $this->expectException(InvalidKeyProvided::class); @@ -129,7 +128,7 @@ public function signShouldRaiseAnExceptionWhenKeyTypeIsNotEC(): void $this->algorithm()->sign('testing', self::$rsaKeys['private']); } - /** @test */ + #[PHPUnit\Test] public function verifyShouldDelegateToEcdsaSignerUsingPublicKey(): void { $payload = 'testing'; diff --git a/tests/Signer/Ecdsa/MultibyteStringConverterTest.php b/tests/Signer/Ecdsa/MultibyteStringConverterTest.php index eec52365..d1de00fe 100644 --- a/tests/Signer/Ecdsa/MultibyteStringConverterTest.php +++ b/tests/Signer/Ecdsa/MultibyteStringConverterTest.php @@ -5,6 +5,7 @@ use Lcobucci\JWT\Signer\Ecdsa\ConversionFailed; use Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function bin2hex; @@ -12,20 +13,17 @@ use function strlen; /** @coversDefaultClass \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter */ +#[PHPUnit\CoversClass(MultibyteStringConverter::class)] +#[PHPUnit\CoversClass(ConversionFailed::class)] final class MultibyteStringConverterTest extends TestCase { /** - * @test - * @dataProvider pointsConversionData - * - * @covers ::toAsn1 - * @covers ::octetLength - * @covers ::preparePositiveInteger - * * @param non-empty-string $r * @param non-empty-string $s * @param non-empty-string $asn1 */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('pointsConversionData')] public function toAsn1ShouldReturnThePointsInAnAsn1SequenceFormat( string $r, string $s, @@ -39,13 +37,7 @@ public function toAsn1ShouldReturnThePointsInAnAsn1SequenceFormat( self::assertSame($asn1, bin2hex($converter->toAsn1($message, strlen($r)))); } - /** - * @test - * - * @covers ::toAsn1 - * @covers ::octetLength - * @covers \Lcobucci\JWT\Signer\Ecdsa\ConversionFailed - */ + #[PHPUnit\Test] public function toAsn1ShouldRaiseExceptionWhenPointsDoNotHaveCorrectLength(): void { $converter = new MultibyteStringConverter(); @@ -55,15 +47,8 @@ public function toAsn1ShouldRaiseExceptionWhenPointsDoNotHaveCorrectLength(): vo $converter->toAsn1('a very wrong string', 64); } - /** - * @test - * @dataProvider pointsConversionData - * - * @covers ::fromAsn1 - * @covers ::readAsn1Content - * @covers ::readAsn1Integer - * @covers ::retrievePositiveInteger - */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('pointsConversionData')] public function fromAsn1ShouldReturnTheConcatenatedPoints(string $r, string $s, string $asn1): void { $converter = new MultibyteStringConverter(); @@ -77,42 +62,34 @@ public function fromAsn1ShouldReturnTheConcatenatedPoints(string $r, string $s, /** @return string[][] */ public static function pointsConversionData(): iterable { - return [ - [ - 'efd48b2aacb6a8fd1140dd9cd45e81d69d2c877b56aaf991c34d0ea84eaf3716', - 'f7cb1c942d657c41d436c7a1b6e29f65f3e900dbb9aff4064dc4ab2f843acda8', - '3046022100efd48b2aacb6a8fd1140dd9cd45e81d69d2c877b56aaf991c34d0ea84eaf3716022100f7cb1c942d657c41d436c7' - . 'a1b6e29f65f3e900dbb9aff4064dc4ab2f843acda8', - ], - [ - '94edbb92a5ecb8aad4736e56c691916b3f88140666ce9fa73d64c4ea95ad133c81a648152e44acf96e36dd1e80fabe46', - '99ef4aeb15f178cea1fe40db2603138f130e740a19624526203b6351d0a3a94fa329c145786e679e7b82c71a38628ac8', - '306602310094edbb92a5ecb8aad4736e56c691916b3f88140666ce9fa73d64c4ea95ad133c81a648152e44acf96e36dd1e80fa' - . 'be4602310099ef4aeb15f178cea1fe40db2603138f130e740a19624526203b6351d0a3a94fa329c145786e679e7b82c71a38' - . '628ac8', - ], - [ - '00c328fafcbd79dd77850370c46325d987cb525569fb63c5d3bc53950e6d4c5f174e25a1ee9017b5d450606add152b534931d7' - . 'd4e8455cc91f9b15bf05ec36e377fa', - '00617cce7cf5064806c467f678d3b4080d6f1cc50af26ca209417308281b68af282623eaa63e5b5c0723d8b8c37ff0777b1a20' - . 'f8ccb1dccc43997f1ee0e44da4a67a', - '308187024200c328fafcbd79dd77850370c46325d987cb525569fb63c5d3bc53950e6d4c5f174e25a1ee9017b5d450606add15' - . '2b534931d7d4e8455cc91f9b15bf05ec36e377fa0241617cce7cf5064806c467f678d3b4080d6f1cc50af26ca20941730828' - . '1b68af282623eaa63e5b5c0723d8b8c37ff0777b1a20f8ccb1dccc43997f1ee0e44da4a67a', - ], + yield [ + 'efd48b2aacb6a8fd1140dd9cd45e81d69d2c877b56aaf991c34d0ea84eaf3716', + 'f7cb1c942d657c41d436c7a1b6e29f65f3e900dbb9aff4064dc4ab2f843acda8', + '3046022100efd48b2aacb6a8fd1140dd9cd45e81d69d2c877b56aaf991c34d0ea84eaf3716022100f7cb1c942d657c41d436c7' + . 'a1b6e29f65f3e900dbb9aff4064dc4ab2f843acda8', + ]; + + yield [ + '94edbb92a5ecb8aad4736e56c691916b3f88140666ce9fa73d64c4ea95ad133c81a648152e44acf96e36dd1e80fabe46', + '99ef4aeb15f178cea1fe40db2603138f130e740a19624526203b6351d0a3a94fa329c145786e679e7b82c71a38628ac8', + '306602310094edbb92a5ecb8aad4736e56c691916b3f88140666ce9fa73d64c4ea95ad133c81a648152e44acf96e36dd1e80fa' + . 'be4602310099ef4aeb15f178cea1fe40db2603138f130e740a19624526203b6351d0a3a94fa329c145786e679e7b82c71a38' + . '628ac8', + ]; + + yield [ + '00c328fafcbd79dd77850370c46325d987cb525569fb63c5d3bc53950e6d4c5f174e25a1ee9017b5d450606add152b534931d7' + . 'd4e8455cc91f9b15bf05ec36e377fa', + '00617cce7cf5064806c467f678d3b4080d6f1cc50af26ca209417308281b68af282623eaa63e5b5c0723d8b8c37ff0777b1a20' + . 'f8ccb1dccc43997f1ee0e44da4a67a', + '308187024200c328fafcbd79dd77850370c46325d987cb525569fb63c5d3bc53950e6d4c5f174e25a1ee9017b5d450606add15' + . '2b534931d7d4e8455cc91f9b15bf05ec36e377fa0241617cce7cf5064806c467f678d3b4080d6f1cc50af26ca20941730828' + . '1b68af282623eaa63e5b5c0723d8b8c37ff0777b1a20f8ccb1dccc43997f1ee0e44da4a67a', ]; } - /** - * @test - * @dataProvider invalidAsn1Structures - * - * @covers ::fromAsn1 - * @covers ::readAsn1Content - * @covers ::readAsn1Integer - * @covers ::retrievePositiveInteger - * @covers \Lcobucci\JWT\Signer\Ecdsa\ConversionFailed - */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('invalidAsn1Structures')] public function fromAsn1ShouldRaiseExceptionOnInvalidMessage(string $message, string $expectedMessage): void { $converter = new MultibyteStringConverter(); @@ -127,12 +104,10 @@ public function fromAsn1ShouldRaiseExceptionOnInvalidMessage(string $message, st /** @return string[][] */ public static function invalidAsn1Structures(): iterable { - return [ - 'Not a sequence' => ['', 'Should start with a sequence'], - 'Sequence without length' => ['30', 'Should contain an integer'], - 'Only one string element' => ['3006030204f0', 'Should contain an integer'], - 'Only one integer element' => ['3004020101', 'Should contain an integer'], - 'Integer+string elements' => ['300a020101030204f0', 'Should contain an integer'], - ]; + yield 'Not a sequence' => ['', 'Should start with a sequence']; + yield 'Sequence without length' => ['30', 'Should contain an integer']; + yield 'Only one string element' => ['3006030204f0', 'Should contain an integer']; + yield 'Only one integer element' => ['3004020101', 'Should contain an integer']; + yield 'Integer+string elements' => ['300a020101030204f0', 'Should contain an integer']; } } diff --git a/tests/Signer/Ecdsa/Sha256Test.php b/tests/Signer/Ecdsa/Sha256Test.php index bdb184c0..8d825700 100644 --- a/tests/Signer/Ecdsa/Sha256Test.php +++ b/tests/Signer/Ecdsa/Sha256Test.php @@ -5,19 +5,19 @@ use Lcobucci\JWT\Signer\Ecdsa; use Lcobucci\JWT\Signer\Ecdsa\Sha256; +use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key; +use Lcobucci\JWT\Signer\OpenSSL; +use PHPUnit\Framework\Attributes as PHPUnit; use const OPENSSL_ALGO_SHA256; -/** - * @covers \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha256 - * @covers \Lcobucci\JWT\Signer\Ecdsa - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ +#[PHPUnit\CoversClass(Ecdsa::class)] +#[PHPUnit\CoversClass(Ecdsa\MultibyteStringConverter::class)] +#[PHPUnit\CoversClass(Ecdsa\Sha256::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\UsesClass(Key\InMemory::class)] final class Sha256Test extends EcdsaTestCase { protected function algorithm(): Ecdsa diff --git a/tests/Signer/Ecdsa/Sha384Test.php b/tests/Signer/Ecdsa/Sha384Test.php index 86ac534c..b978c359 100644 --- a/tests/Signer/Ecdsa/Sha384Test.php +++ b/tests/Signer/Ecdsa/Sha384Test.php @@ -5,19 +5,19 @@ use Lcobucci\JWT\Signer\Ecdsa; use Lcobucci\JWT\Signer\Ecdsa\Sha384; +use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key; +use Lcobucci\JWT\Signer\OpenSSL; +use PHPUnit\Framework\Attributes as PHPUnit; use const OPENSSL_ALGO_SHA384; -/** - * @covers \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha384 - * @covers \Lcobucci\JWT\Signer\Ecdsa - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ +#[PHPUnit\CoversClass(Ecdsa::class)] +#[PHPUnit\CoversClass(Ecdsa\MultibyteStringConverter::class)] +#[PHPUnit\CoversClass(Ecdsa\Sha384::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\UsesClass(Key\InMemory::class)] final class Sha384Test extends EcdsaTestCase { protected function algorithm(): Ecdsa diff --git a/tests/Signer/Ecdsa/Sha512Test.php b/tests/Signer/Ecdsa/Sha512Test.php index 19a45047..8ea9b720 100644 --- a/tests/Signer/Ecdsa/Sha512Test.php +++ b/tests/Signer/Ecdsa/Sha512Test.php @@ -5,19 +5,19 @@ use Lcobucci\JWT\Signer\Ecdsa; use Lcobucci\JWT\Signer\Ecdsa\Sha512; +use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key; +use Lcobucci\JWT\Signer\OpenSSL; +use PHPUnit\Framework\Attributes as PHPUnit; use const OPENSSL_ALGO_SHA512; -/** - * @covers \Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter - * @covers \Lcobucci\JWT\Signer\Ecdsa\Sha512 - * @covers \Lcobucci\JWT\Signer\Ecdsa - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ +#[PHPUnit\CoversClass(Ecdsa::class)] +#[PHPUnit\CoversClass(Ecdsa\MultibyteStringConverter::class)] +#[PHPUnit\CoversClass(Ecdsa\Sha512::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\UsesClass(Key\InMemory::class)] final class Sha512Test extends EcdsaTestCase { protected function algorithm(): Ecdsa diff --git a/tests/Signer/EddsaTest.php b/tests/Signer/EddsaTest.php index 3d130b09..d5148a32 100644 --- a/tests/Signer/EddsaTest.php +++ b/tests/Signer/EddsaTest.php @@ -7,34 +7,29 @@ use Lcobucci\JWT\Signer\Eddsa; use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\SodiumBase64Polyfill; use Lcobucci\JWT\Tests\Keys; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function sodium_crypto_sign_detached; use function sodium_crypto_sign_verify_detached; -/** @coversDefaultClass \Lcobucci\JWT\Signer\Eddsa */ +#[PHPUnit\CoversClass(Eddsa::class)] +#[PHPUnit\UsesClass(InMemory::class)] +#[PHPUnit\UsesClass(JoseEncoder::class)] +#[PHPUnit\UsesClass(SodiumBase64Polyfill::class)] final class EddsaTest extends TestCase { use Keys; - /** - * @test - * - * @covers ::algorithmId - */ + #[PHPUnit\Test] public function algorithmIdMustBeCorrect(): void { self::assertSame('EdDSA', (new Eddsa())->algorithmId()); } - /** - * @test - * - * @covers ::sign - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ + #[PHPUnit\Test] public function signShouldReturnAValidEddsaSignature(): void { $payload = 'testing'; @@ -47,13 +42,7 @@ public function signShouldReturnAValidEddsaSignature(): void self::assertTrue(sodium_crypto_sign_verify_detached($signature, $payload, $publicKey)); } - /** - * @test - * - * @covers ::sign - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ + #[PHPUnit\Test] public function signShouldRaiseAnExceptionWhenKeyIsInvalid(): void { $signer = new Eddsa(); @@ -65,13 +54,7 @@ public function signShouldRaiseAnExceptionWhenKeyIsInvalid(): void $signer->sign('testing', InMemory::plainText('tooshort')); } - /** - * @test - * - * @covers ::verify - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ + #[PHPUnit\Test] public function verifyShouldReturnTrueWhenSignatureIsValid(): void { $payload = 'testing'; @@ -81,13 +64,7 @@ public function verifyShouldReturnTrueWhenSignatureIsValid(): void self::assertTrue($signer->verify($signature, $payload, self::$eddsaKeys['public1'])); } - /** - * @test - * - * @covers ::verify - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ + #[PHPUnit\Test] public function verifyShouldRaiseAnExceptionWhenKeyIsNotParseable(): void { $signer = new Eddsa(); @@ -99,18 +76,8 @@ public function verifyShouldRaiseAnExceptionWhenKeyIsNotParseable(): void $signer->verify('testing', 'testing', InMemory::plainText('blablabla')); } - /** - * @see https://tools.ietf.org/html/rfc8037#appendix-A.4 - * - * @test - * - * @covers ::sign - * - * @uses \Lcobucci\JWT\Encoding\JoseEncoder - * @uses \Lcobucci\JWT\Signer\Key\InMemory - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::base642bin() - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::bin2base64() - */ + /** @see https://tools.ietf.org/html/rfc8037#appendix-A.4 */ + #[PHPUnit\Test] public function signatureOfRfcExample(): void { $signer = new Eddsa(); @@ -133,17 +100,8 @@ public function signatureOfRfcExample(): void ); } - /** - * @see https://tools.ietf.org/html/rfc8037#appendix-A.5 - * - * @test - * - * @covers ::verify - * - * @uses \Lcobucci\JWT\Encoding\JoseEncoder - * @uses \Lcobucci\JWT\Signer\Key\InMemory - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::base642bin() - */ + /** @see https://tools.ietf.org/html/rfc8037#appendix-A.5 */ + #[PHPUnit\Test] public function verificationOfRfcExample(): void { $signer = new Eddsa(); diff --git a/tests/Signer/Hmac/HmacTestCase.php b/tests/Signer/Hmac/HmacTestCase.php index 3e175325..668418d4 100644 --- a/tests/Signer/Hmac/HmacTestCase.php +++ b/tests/Signer/Hmac/HmacTestCase.php @@ -6,6 +6,7 @@ use Lcobucci\JWT\Signer\Hmac; use Lcobucci\JWT\Signer\InvalidKeyProvided; use Lcobucci\JWT\Signer\Key\InMemory; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; @@ -26,13 +27,13 @@ abstract protected function expectedAlgorithmId(): string; abstract protected function expectedMinimumBits(): int; - /** @test */ + #[PHPUnit\Test] public function algorithmIdMustBeCorrect(): void { self::assertEquals($this->expectedAlgorithmId(), $this->algorithm()->algorithmId()); } - /** @test */ + #[PHPUnit\Test] public function signMustReturnAHashAccordingWithTheAlgorithm(): void { $secret = $this->generateSecret(); @@ -43,7 +44,7 @@ public function signMustReturnAHashAccordingWithTheAlgorithm(): void self::assertTrue(hash_equals($expectedHash, $signature)); } - /** @test */ + #[PHPUnit\Test] public function verifyMustReturnTrueWhenContentWasSignedWithTheSameKey(): void { $secret = $this->generateSecret(); @@ -53,7 +54,7 @@ public function verifyMustReturnTrueWhenContentWasSignedWithTheSameKey(): void self::assertTrue($this->algorithm()->verify($signature, 'test', InMemory::plainText($secret))); } - /** @test */ + #[PHPUnit\Test] public function verifyMustReturnTrueWhenContentWasSignedWithADifferentKey(): void { $signature = hash_hmac( @@ -72,11 +73,7 @@ public function verifyMustReturnTrueWhenContentWasSignedWithADifferentKey(): voi ); } - /** - * @test - * - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided::tooShort - */ + #[PHPUnit\Test] public function keyMustFulfillMinimumLengthRequirement(): void { $secret = $this->generateSecret(($this->expectedMinimumBits() / 8) - 1); diff --git a/tests/Signer/Hmac/Sha256Test.php b/tests/Signer/Hmac/Sha256Test.php index de8b5d66..478299e8 100644 --- a/tests/Signer/Hmac/Sha256Test.php +++ b/tests/Signer/Hmac/Sha256Test.php @@ -5,13 +5,14 @@ use Lcobucci\JWT\Signer\Hmac; use Lcobucci\JWT\Signer\Hmac\Sha256; +use Lcobucci\JWT\Signer\InvalidKeyProvided; +use Lcobucci\JWT\Signer\Key\InMemory; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Signer\Hmac - * @covers \Lcobucci\JWT\Signer\Hmac\Sha256 - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ +#[PHPUnit\CoversClass(Hmac::class)] +#[PHPUnit\CoversClass(Sha256::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\UsesClass(InMemory::class)] final class Sha256Test extends HmacTestCase { protected function algorithm(): Hmac diff --git a/tests/Signer/Hmac/Sha384Test.php b/tests/Signer/Hmac/Sha384Test.php index ab09011c..01986c17 100644 --- a/tests/Signer/Hmac/Sha384Test.php +++ b/tests/Signer/Hmac/Sha384Test.php @@ -5,13 +5,14 @@ use Lcobucci\JWT\Signer\Hmac; use Lcobucci\JWT\Signer\Hmac\Sha384; +use Lcobucci\JWT\Signer\InvalidKeyProvided; +use Lcobucci\JWT\Signer\Key\InMemory; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Signer\Hmac - * @covers \Lcobucci\JWT\Signer\Hmac\Sha384 - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ +#[PHPUnit\CoversClass(Hmac::class)] +#[PHPUnit\CoversClass(Sha384::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\UsesClass(InMemory::class)] final class Sha384Test extends HmacTestCase { protected function algorithm(): Hmac diff --git a/tests/Signer/Hmac/Sha512Test.php b/tests/Signer/Hmac/Sha512Test.php index b99c7fd6..87f682bc 100644 --- a/tests/Signer/Hmac/Sha512Test.php +++ b/tests/Signer/Hmac/Sha512Test.php @@ -5,13 +5,14 @@ use Lcobucci\JWT\Signer\Hmac; use Lcobucci\JWT\Signer\Hmac\Sha512; +use Lcobucci\JWT\Signer\InvalidKeyProvided; +use Lcobucci\JWT\Signer\Key\InMemory; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Signer\Hmac - * @covers \Lcobucci\JWT\Signer\Hmac\Sha512 - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ +#[PHPUnit\CoversClass(Hmac::class)] +#[PHPUnit\CoversClass(Sha512::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\UsesClass(InMemory::class)] final class Sha512Test extends HmacTestCase { protected function algorithm(): Hmac diff --git a/tests/Signer/Key/InMemoryTest.php b/tests/Signer/Key/InMemoryTest.php index cef109b0..47d44542 100644 --- a/tests/Signer/Key/InMemoryTest.php +++ b/tests/Signer/Key/InMemoryTest.php @@ -7,21 +7,20 @@ 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; -/** @coversDefaultClass \Lcobucci\JWT\Signer\Key\InMemory */ +#[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 { - /** - * @test - * - * @covers ::base64Encoded - * @covers \Lcobucci\JWT\Encoding\CannotDecodeContent - * - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::base642bin() - */ + #[PHPUnit\Test] public function exceptionShouldBeRaisedWhenInvalidBase64CharsAreUsed(): void { $this->expectException(CannotDecodeContent::class); @@ -30,16 +29,7 @@ public function exceptionShouldBeRaisedWhenInvalidBase64CharsAreUsed(): void InMemory::base64Encoded('ááá'); } - /** - * @test - * - * @covers ::base64Encoded - * @covers ::guardAgainstEmptyKey - * @covers ::__construct - * @covers ::contents - * - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::base642bin() - */ + #[PHPUnit\Test] public function base64EncodedShouldDecodeKeyContents(): void { $key = InMemory::base64Encoded(base64_encode('testing')); @@ -47,14 +37,7 @@ public function base64EncodedShouldDecodeKeyContents(): void self::assertSame('testing', $key->contents()); } - /** - * @test - * - * @covers ::guardAgainstEmptyKey - * @covers ::__construct - * @covers ::file - * @covers \Lcobucci\JWT\Signer\Key\FileCouldNotBeRead - */ + #[PHPUnit\Test] public function exceptionShouldBeRaisedWhenFileDoesNotExists(): void { $path = __DIR__ . '/not-found.pem'; @@ -66,14 +49,7 @@ public function exceptionShouldBeRaisedWhenFileDoesNotExists(): void InMemory::file($path); } - /** - * @test - * - * @covers ::guardAgainstEmptyKey - * @covers ::__construct - * @covers ::file - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided::cannotBeEmpty - */ + #[PHPUnit\Test] public function exceptionShouldBeRaisedWhenFileIsEmpty(): void { $this->expectException(InvalidKeyProvided::class); @@ -82,14 +58,7 @@ public function exceptionShouldBeRaisedWhenFileIsEmpty(): void InMemory::file(__DIR__ . '/empty.pem'); } - /** - * @test - * - * @covers ::guardAgainstEmptyKey - * @covers ::__construct - * @covers ::plainText - * @covers ::contents - */ + #[PHPUnit\Test] public function contentsShouldReturnConfiguredData(): void { $key = InMemory::plainText('testing', 'test'); @@ -97,14 +66,7 @@ public function contentsShouldReturnConfiguredData(): void self::assertSame('testing', $key->contents()); } - /** - * @test - * - * @covers ::guardAgainstEmptyKey - * @covers ::__construct - * @covers ::file - * @covers ::contents - */ + #[PHPUnit\Test] public function contentsShouldReturnFileContentsWhenFilePathHasBeenPassed(): void { $key = InMemory::file(__DIR__ . '/test.pem'); @@ -112,14 +74,7 @@ public function contentsShouldReturnFileContentsWhenFilePathHasBeenPassed(): voi self::assertSame('testing', $key->contents()); } - /** - * @test - * - * @covers ::guardAgainstEmptyKey - * @covers ::__construct - * @covers ::plainText - * @covers ::passphrase - */ + #[PHPUnit\Test] public function passphraseShouldReturnConfiguredData(): void { $key = InMemory::plainText('testing', 'test'); @@ -127,14 +82,7 @@ public function passphraseShouldReturnConfiguredData(): void self::assertSame('test', $key->passphrase()); } - /** - * @test - * - * @covers ::guardAgainstEmptyKey - * @covers ::__construct - * @covers ::plainText - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided::cannotBeEmpty - */ + #[PHPUnit\Test] public function emptyPlainTextContentShouldRaiseException(): void { $this->expectException(InvalidKeyProvided::class); @@ -143,16 +91,7 @@ public function emptyPlainTextContentShouldRaiseException(): void InMemory::plainText(''); } - /** - * @test - * - * @covers ::guardAgainstEmptyKey - * @covers ::__construct - * @covers ::base64Encoded - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided::cannotBeEmpty - * - * @uses \Lcobucci\JWT\SodiumBase64Polyfill::base642bin - */ + #[PHPUnit\Test] public function emptyBase64ContentShouldRaiseException(): void { $this->expectException(InvalidKeyProvided::class); diff --git a/tests/Signer/Rsa/KeyValidationTest.php b/tests/Signer/Rsa/KeyValidationTest.php index a53a0355..3bc99d78 100644 --- a/tests/Signer/Rsa/KeyValidationTest.php +++ b/tests/Signer/Rsa/KeyValidationTest.php @@ -7,6 +7,7 @@ use Lcobucci\JWT\Signer\Key; use Lcobucci\JWT\Signer\Key\InMemory; use Lcobucci\JWT\Signer\OpenSSL; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function openssl_error_string; @@ -14,15 +15,12 @@ use const OPENSSL_ALGO_SHA256; use const PHP_EOL; -/** - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\Signer\CannotSignPayload - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(CannotSignPayload::class)] +#[PHPUnit\UsesClass(InMemory::class)] final class KeyValidationTest extends TestCase { - /** @after */ + #[PHPUnit\After] public function clearOpenSSLErrors(): void { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedWhile @@ -30,7 +28,7 @@ public function clearOpenSSLErrors(): void } } - /** @test */ + #[PHPUnit\Test] public function signShouldRaiseAnExceptionWhenKeyIsInvalid(): void { $key = <<<'KEY' diff --git a/tests/Signer/Rsa/RsaTestCase.php b/tests/Signer/Rsa/RsaTestCase.php index 89f22f5c..8e977826 100644 --- a/tests/Signer/Rsa/RsaTestCase.php +++ b/tests/Signer/Rsa/RsaTestCase.php @@ -8,6 +8,7 @@ use Lcobucci\JWT\Signer\Rsa; use Lcobucci\JWT\Tests\Keys; use OpenSSLAsymmetricKey; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; @@ -29,7 +30,7 @@ abstract protected function algorithmId(): string; abstract protected function signatureAlgorithm(): int; - /** @after */ + #[PHPUnit\After] final public function clearOpenSSLErrors(): void { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedWhile @@ -37,19 +38,19 @@ final public function clearOpenSSLErrors(): void } } - /** @test */ + #[PHPUnit\Test] final public function algorithmIdMustBeCorrect(): void { self::assertSame($this->algorithmId(), $this->algorithm()->algorithmId()); } - /** @test */ + #[PHPUnit\Test] final public function signatureAlgorithmMustBeCorrect(): void { self::assertSame($this->signatureAlgorithm(), $this->algorithm()->algorithm()); } - /** @test */ + #[PHPUnit\Test] public function signShouldReturnAValidOpensslSignature(): void { $payload = 'testing'; @@ -64,7 +65,7 @@ public function signShouldReturnAValidOpensslSignature(): void ); } - /** @test */ + #[PHPUnit\Test] public function signShouldRaiseAnExceptionWhenKeyIsNotParseable(): void { $this->expectException(InvalidKeyProvided::class); @@ -73,7 +74,7 @@ public function signShouldRaiseAnExceptionWhenKeyIsNotParseable(): void $this->algorithm()->sign('testing', InMemory::plainText('blablabla')); } - /** @test */ + #[PHPUnit\Test] public function allOpenSSLErrorsShouldBeOnTheErrorMessage(): void { // Injects a random OpenSSL error message @@ -85,7 +86,7 @@ public function allOpenSSLErrorsShouldBeOnTheErrorMessage(): void $this->algorithm()->sign('testing', InMemory::plainText('blablabla')); } - /** @test */ + #[PHPUnit\Test] public function signShouldRaiseAnExceptionWhenKeyTypeIsNotRsa(): void { $this->expectException(InvalidKeyProvided::class); @@ -94,7 +95,7 @@ public function signShouldRaiseAnExceptionWhenKeyTypeIsNotRsa(): void $this->algorithm()->sign('testing', self::$ecdsaKeys['private']); } - /** @test */ + #[PHPUnit\Test] public function signShouldRaiseAnExceptionWhenKeyLengthIsBelowMinimum(): void { $this->expectException(InvalidKeyProvided::class); @@ -103,7 +104,7 @@ public function signShouldRaiseAnExceptionWhenKeyLengthIsBelowMinimum(): void $this->algorithm()->sign('testing', self::$rsaKeys['private_short']); } - /** @test */ + #[PHPUnit\Test] public function verifyShouldReturnTrueWhenSignatureIsValid(): void { $payload = 'testing'; @@ -116,7 +117,7 @@ public function verifyShouldReturnTrueWhenSignatureIsValid(): void self::assertTrue($this->algorithm()->verify($signature, $payload, self::$rsaKeys['public'])); } - /** @test */ + #[PHPUnit\Test] public function verifyShouldRaiseAnExceptionWhenKeyIsNotParseable(): void { $this->expectException(InvalidKeyProvided::class); @@ -125,7 +126,7 @@ public function verifyShouldRaiseAnExceptionWhenKeyIsNotParseable(): void $this->algorithm()->verify('testing', 'testing', InMemory::plainText('blablabla')); } - /** @test */ + #[PHPUnit\Test] public function verifyShouldRaiseAnExceptionWhenKeyTypeIsNotRsa(): void { $this->expectException(InvalidKeyProvided::class); diff --git a/tests/Signer/Rsa/Sha256Test.php b/tests/Signer/Rsa/Sha256Test.php index d01f8ab1..df25f843 100644 --- a/tests/Signer/Rsa/Sha256Test.php +++ b/tests/Signer/Rsa/Sha256Test.php @@ -3,19 +3,20 @@ namespace Lcobucci\JWT\Tests\Signer\Rsa; +use Lcobucci\JWT\Signer\InvalidKeyProvided; +use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\OpenSSL; use Lcobucci\JWT\Signer\Rsa; use Lcobucci\JWT\Signer\Rsa\Sha256; +use PHPUnit\Framework\Attributes as PHPUnit; use const OPENSSL_ALGO_SHA256; -/** - * @covers \Lcobucci\JWT\Signer\Rsa\Sha256 - * @covers \Lcobucci\JWT\Signer\Rsa - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ +#[PHPUnit\CoversClass(Sha256::class)] +#[PHPUnit\CoversClass(Rsa::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\UsesClass(InMemory::class)] final class Sha256Test extends RsaTestCase { protected function algorithm(): Rsa diff --git a/tests/Signer/Rsa/Sha384Test.php b/tests/Signer/Rsa/Sha384Test.php index 592f8cdb..d1ff3943 100644 --- a/tests/Signer/Rsa/Sha384Test.php +++ b/tests/Signer/Rsa/Sha384Test.php @@ -3,19 +3,20 @@ namespace Lcobucci\JWT\Tests\Signer\Rsa; +use Lcobucci\JWT\Signer\InvalidKeyProvided; +use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\OpenSSL; use Lcobucci\JWT\Signer\Rsa; use Lcobucci\JWT\Signer\Rsa\Sha384; +use PHPUnit\Framework\Attributes as PHPUnit; use const OPENSSL_ALGO_SHA384; -/** - * @covers \Lcobucci\JWT\Signer\Rsa\Sha384 - * @covers \Lcobucci\JWT\Signer\Rsa - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ +#[PHPUnit\CoversClass(Sha384::class)] +#[PHPUnit\CoversClass(Rsa::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\UsesClass(InMemory::class)] final class Sha384Test extends RsaTestCase { protected function algorithm(): Rsa diff --git a/tests/Signer/Rsa/Sha512Test.php b/tests/Signer/Rsa/Sha512Test.php index 7ae01cbe..c72bd58d 100644 --- a/tests/Signer/Rsa/Sha512Test.php +++ b/tests/Signer/Rsa/Sha512Test.php @@ -3,19 +3,20 @@ namespace Lcobucci\JWT\Tests\Signer\Rsa; +use Lcobucci\JWT\Signer\InvalidKeyProvided; +use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\Signer\OpenSSL; use Lcobucci\JWT\Signer\Rsa; use Lcobucci\JWT\Signer\Rsa\Sha512; +use PHPUnit\Framework\Attributes as PHPUnit; use const OPENSSL_ALGO_SHA512; -/** - * @covers \Lcobucci\JWT\Signer\Rsa\Sha512 - * @covers \Lcobucci\JWT\Signer\Rsa - * @covers \Lcobucci\JWT\Signer\OpenSSL - * @covers \Lcobucci\JWT\Signer\InvalidKeyProvided - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - */ +#[PHPUnit\CoversClass(Rsa::class)] +#[PHPUnit\CoversClass(Sha512::class)] +#[PHPUnit\CoversClass(OpenSSL::class)] +#[PHPUnit\CoversClass(InvalidKeyProvided::class)] +#[PHPUnit\UsesClass(InMemory::class)] final class Sha512Test extends RsaTestCase { protected function algorithm(): Rsa diff --git a/tests/SodiumBase64PolyfillTest.php b/tests/SodiumBase64PolyfillTest.php index e8db0c09..fac575e5 100644 --- a/tests/SodiumBase64PolyfillTest.php +++ b/tests/SodiumBase64PolyfillTest.php @@ -5,6 +5,7 @@ use Lcobucci\JWT\Encoding\CannotDecodeContent; use Lcobucci\JWT\SodiumBase64Polyfill; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function rtrim; @@ -15,17 +16,15 @@ use const SODIUM_BASE64_VARIANT_URLSAFE; use const SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING; -/** @coversDefaultClass \Lcobucci\JWT\SodiumBase64Polyfill */ +#[PHPUnit\CoversClass(SodiumBase64Polyfill::class)] +#[PHPUnit\UsesClass(CannotDecodeContent::class)] final class SodiumBase64PolyfillTest extends TestCase { private const B64 = 'I+o2tVq8ynY='; private const B64URL = 'lZ-2HIl9dTz_Oy0nAb-2gvKdG0jhHJ36XB2rWAKj8Uo='; - /** - * @test - * - * @coversNothing - */ + #[PHPUnit\Test] + #[PHPUnit\CoversNothing] public function constantsMatchExtensionOnes(): void { // @phpstan-ignore-next-line @@ -50,26 +49,16 @@ public function constantsMatchExtensionOnes(): void ); } - /** - * @test - * @dataProvider base64Variants - * - * @covers ::bin2base64 - * @covers ::bin2base64Fallback - */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('base64Variants')] public function bin2base64(string $encoded, string $binary, int $variant): void { self::assertSame($encoded, SodiumBase64Polyfill::bin2base64($binary, $variant)); self::assertSame($encoded, SodiumBase64Polyfill::bin2base64Fallback($binary, $variant)); } - /** - * @test - * @dataProvider base64Variants - * - * @covers ::base642bin - * @covers ::base642binFallback - */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('base64Variants')] public function base642binFallback(string $encoded, string $binary, int $variant): void { self::assertSame($binary, SodiumBase64Polyfill::base642bin($encoded, $variant)); @@ -90,14 +79,8 @@ public static function base64Variants(): iterable yield [rtrim(self::B64URL, '='), $urlBinary, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING]; } - /** - * @test - * @dataProvider invalidBase64 - * - * @covers ::base642bin - * - * @uses \Lcobucci\JWT\Encoding\CannotDecodeContent::invalidBase64String() - */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('invalidBase64')] public function sodiumBase642BinRaisesExceptionOnInvalidBase64(string $content, int $variant): void { $this->expectException(CannotDecodeContent::class); @@ -105,14 +88,8 @@ public function sodiumBase642BinRaisesExceptionOnInvalidBase64(string $content, SodiumBase64Polyfill::base642bin($content, $variant); } - /** - * @test - * @dataProvider invalidBase64 - * - * @covers ::base642binFallback - * - * @uses \Lcobucci\JWT\Encoding\CannotDecodeContent::invalidBase64String() - */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('invalidBase64')] public function fallbackBase642BinRaisesExceptionOnInvalidBase64(string $content, int $variant): void { $this->expectException(CannotDecodeContent::class); diff --git a/tests/TimeFractionPrecisionTest.php b/tests/TimeFractionPrecisionTest.php index 80058c7f..996b7f17 100644 --- a/tests/TimeFractionPrecisionTest.php +++ b/tests/TimeFractionPrecisionTest.php @@ -5,35 +5,30 @@ use DateTimeImmutable; use Lcobucci\JWT\Configuration; -use Lcobucci\JWT\Encoding\JoseEncoder; +use Lcobucci\JWT\Encoding; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\SodiumBase64Polyfill; +use Lcobucci\JWT\Token; use Lcobucci\JWT\Token\Plain; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; -/** - * @covers \Lcobucci\JWT\Configuration - * @covers \Lcobucci\JWT\Encoding\JoseEncoder - * @covers \Lcobucci\JWT\Encoding\ChainedFormatter - * @covers \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion - * @covers \Lcobucci\JWT\Encoding\UnifyAudience - * @covers \Lcobucci\JWT\Token\Builder - * @covers \Lcobucci\JWT\Token\Parser - * @covers \Lcobucci\JWT\Token\Plain - * @covers \Lcobucci\JWT\Token\DataSet - * @covers \Lcobucci\JWT\Token\Signature - * @covers \Lcobucci\JWT\Signer\Key\InMemory - * @covers \Lcobucci\JWT\Validation\Validator - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\RequiredConstraintsViolated - * @covers \Lcobucci\JWT\Validation\Constraint\SignedWith - * @covers \Lcobucci\JWT\SodiumBase64Polyfill - */ +#[PHPUnit\CoversClass(Configuration::class)] +#[PHPUnit\CoversClass(Encoding\JoseEncoder::class)] +#[PHPUnit\CoversClass(Encoding\ChainedFormatter::class)] +#[PHPUnit\CoversClass(Encoding\MicrosecondBasedDateConversion::class)] +#[PHPUnit\CoversClass(Encoding\UnifyAudience::class)] +#[PHPUnit\CoversClass(Token\Builder::class)] +#[PHPUnit\CoversClass(Token\Parser::class)] +#[PHPUnit\CoversClass(Token\Plain::class)] +#[PHPUnit\CoversClass(Token\DataSet::class)] +#[PHPUnit\CoversClass(Token\Signature::class)] +#[PHPUnit\CoversClass(InMemory::class)] +#[PHPUnit\CoversClass(SodiumBase64Polyfill::class)] final class TimeFractionPrecisionTest extends TestCase { - /** - * @test - * @dataProvider datesWithPotentialRoundingIssues - */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('datesWithPotentialRoundingIssues')] public function timeFractionsPrecisionsAreRespected(string $timeFraction): void { $config = Configuration::forSymmetricSigner( @@ -65,13 +60,11 @@ public static function datesWithPotentialRoundingIssues(): iterable yield ['1616074725.008455']; } - /** - * @test - * @dataProvider timeFractionConversions - */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('timeFractionConversions')] public function typeConversionDoesNotCauseParsingErrors(float|int|string $issuedAt, string $timeFraction): void { - $encoder = new JoseEncoder(); + $encoder = new Encoding\JoseEncoder(); $headers = $encoder->base64UrlEncode($encoder->jsonEncode(['typ' => 'JWT', 'alg' => 'none'])); $claims = $encoder->base64UrlEncode($encoder->jsonEncode(['iat' => $issuedAt])); @@ -81,7 +74,7 @@ public function typeConversionDoesNotCauseParsingErrors(float|int|string $issued ); $parsedToken = $config->parser()->parse($headers . '.' . $claims . '.cHJpdmF0ZQ'); - self::assertInstanceOf(Plain::class, $parsedToken); + self::assertInstanceOf(Token\Plain::class, $parsedToken); self::assertSame($timeFraction, $parsedToken->claims()->get('iat')->format('U.u')); } diff --git a/tests/Token/BuilderTest.php b/tests/Token/BuilderTest.php index 5aafaeac..9857a7e1 100644 --- a/tests/Token/BuilderTest.php +++ b/tests/Token/BuilderTest.php @@ -9,31 +9,29 @@ use Lcobucci\JWT\Signer; use Lcobucci\JWT\Signer\Key\InMemory; use Lcobucci\JWT\Token\Builder; +use Lcobucci\JWT\Token\DataSet; +use Lcobucci\JWT\Token\Plain; use Lcobucci\JWT\Token\RegisteredClaimGiven; use Lcobucci\JWT\Token\RegisteredClaims; +use Lcobucci\JWT\Token\Signature; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use SplObjectStorage; -/** - * @covers \Lcobucci\JWT\Token\Builder - * @covers \Lcobucci\JWT\Token\RegisteredClaimGiven - * - * @uses \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion - * @uses \Lcobucci\JWT\Signer\Key\InMemory - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - * @uses \Lcobucci\JWT\Token\DataSet - */ +#[PHPUnit\CoversClass(Builder::class)] +#[PHPUnit\CoversClass(RegisteredClaimGiven::class)] +#[PHPUnit\UsesClass(MicrosecondBasedDateConversion::class)] +#[PHPUnit\UsesClass(InMemory::class)] +#[PHPUnit\UsesClass(Plain::class)] +#[PHPUnit\UsesClass(Signature::class)] +#[PHPUnit\UsesClass(DataSet::class)] final class BuilderTest extends TestCase { - /** @var Encoder&MockObject */ - private Encoder $encoder; + private Encoder&MockObject $encoder; + private Signer&MockObject $signer; - /** @var Signer&MockObject */ - private Signer $signer; - - /** @before */ + #[PHPUnit\Before] public function initializeDependencies(): void { $this->encoder = $this->createMock(Encoder::class); @@ -41,7 +39,7 @@ public function initializeDependencies(): void $this->signer->method('algorithmId')->willReturn('RS256'); } - /** @test */ + #[PHPUnit\Test] public function withClaimShouldRaiseExceptionWhenTryingToConfigureARegisteredClaim(): void { $builder = new Builder($this->encoder, new MicrosecondBasedDateConversion()); @@ -55,7 +53,7 @@ public function withClaimShouldRaiseExceptionWhenTryingToConfigureARegisteredCla $builder->withClaim(RegisteredClaims::ISSUER, 'me'); } - /** @test */ + #[PHPUnit\Test] public function getTokenShouldReturnACompletelyConfigureToken(): void { $issuedAt = new DateTimeImmutable('@1487285080'); @@ -73,7 +71,7 @@ public function getTokenShouldReturnACompletelyConfigureToken(): void ->method('base64UrlEncode') ->willReturnArgument(0); - $this->signer->expects(self::once()) + $this->signer->expects($this->once()) ->method('sign') ->with('1.2') ->willReturn('3'); @@ -106,7 +104,7 @@ public function getTokenShouldReturnACompletelyConfigureToken(): void self::assertSame('3', $token->signature()->toString()); } - /** @test */ + #[PHPUnit\Test] public function immutability(): void { $map = new SplObjectStorage(); diff --git a/tests/Token/DataSetTest.php b/tests/Token/DataSetTest.php index 22ed489f..991d0487 100644 --- a/tests/Token/DataSetTest.php +++ b/tests/Token/DataSetTest.php @@ -4,12 +4,13 @@ namespace Lcobucci\JWT\Tests\Token; use Lcobucci\JWT\Token\DataSet; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; -/** @covers \Lcobucci\JWT\Token\DataSet */ +#[PHPUnit\CoversClass(DataSet::class)] final class DataSetTest extends TestCase { - /** @test */ + #[PHPUnit\Test] public function getShouldReturnTheConfiguredValue(): void { $set = new DataSet(['one' => 1], 'one=1'); @@ -17,7 +18,7 @@ public function getShouldReturnTheConfiguredValue(): void self::assertSame(1, $set->get('one')); } - /** @test */ + #[PHPUnit\Test] public function getShouldReturnTheFallbackValueWhenItWasGiven(): void { $set = new DataSet(['one' => 1], 'one=1'); @@ -25,7 +26,7 @@ public function getShouldReturnTheFallbackValueWhenItWasGiven(): void self::assertSame(2, $set->get('two', 2)); } - /** @test */ + #[PHPUnit\Test] public function getShouldReturnNullWhenFallbackValueWasNotGiven(): void { $set = new DataSet(['one' => 1], 'one=1'); @@ -33,7 +34,7 @@ public function getShouldReturnNullWhenFallbackValueWasNotGiven(): void self::assertNull($set->get('two')); } - /** @test */ + #[PHPUnit\Test] public function hasShouldReturnTrueWhenItemWasConfigured(): void { $set = new DataSet(['one' => 1], 'one=1'); @@ -41,7 +42,7 @@ public function hasShouldReturnTrueWhenItemWasConfigured(): void self::assertTrue($set->has('one')); } - /** @test */ + #[PHPUnit\Test] public function hasShouldReturnFalseWhenItemWasNotConfigured(): void { $set = new DataSet(['one' => 1], 'one=1'); @@ -49,7 +50,7 @@ public function hasShouldReturnFalseWhenItemWasNotConfigured(): void self::assertFalse($set->has('two')); } - /** @test */ + #[PHPUnit\Test] public function allShouldReturnAllConfiguredItems(): void { $items = ['one' => 1, 'two' => 2]; @@ -58,7 +59,7 @@ public function allShouldReturnAllConfiguredItems(): void self::assertSame($items, $set->all()); } - /** @test */ + #[PHPUnit\Test] public function toStringShouldReturnTheEncodedData(): void { $set = new DataSet(['one' => 1], 'one=1'); diff --git a/tests/Token/ParserTest.php b/tests/Token/ParserTest.php index 043cda22..adc9a740 100644 --- a/tests/Token/ParserTest.php +++ b/tests/Token/ParserTest.php @@ -12,25 +12,22 @@ use Lcobucci\JWT\Token\RegisteredClaims; use Lcobucci\JWT\Token\Signature; use Lcobucci\JWT\Token\UnsupportedHeaderFound; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use RuntimeException; -/** - * @covers \Lcobucci\JWT\Token\Parser - * @covers \Lcobucci\JWT\Token\InvalidTokenStructure - * @covers \Lcobucci\JWT\Token\UnsupportedHeaderFound - * - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - * @uses \Lcobucci\JWT\Token\DataSet - */ +#[PHPUnit\CoversClass(Parser::class)] +#[PHPUnit\CoversClass(InvalidTokenStructure::class)] +#[PHPUnit\CoversClass(UnsupportedHeaderFound::class)] +#[PHPUnit\UsesClass(Plain::class)] +#[PHPUnit\UsesClass(DataSet::class)] +#[PHPUnit\UsesClass(Signature::class)] final class ParserTest extends TestCase { - /** @var Decoder&MockObject */ - protected Decoder $decoder; + protected Decoder&MockObject $decoder; - /** @before */ + #[PHPUnit\Before] public function createDependencies(): void { $this->decoder = $this->createMock(Decoder::class); @@ -41,7 +38,7 @@ private function createParser(): Parser return new Parser($this->decoder); } - /** @test */ + #[PHPUnit\Test] public function parseMustRaiseExceptionWhenTokenDoesNotHaveThreeParts(): void { $parser = $this->createParser(); @@ -52,7 +49,7 @@ public function parseMustRaiseExceptionWhenTokenDoesNotHaveThreeParts(): void $parser->parse('.'); } - /** @test */ + #[PHPUnit\Test] public function parseMustRaiseExceptionWhenTokenDoesNotHaveHeaders(): void { $parser = $this->createParser(); @@ -63,7 +60,7 @@ public function parseMustRaiseExceptionWhenTokenDoesNotHaveHeaders(): void $parser->parse('.b.c'); } - /** @test */ + #[PHPUnit\Test] public function parseMustRaiseExceptionWhenTokenDoesNotHaveClaims(): void { $parser = $this->createParser(); @@ -74,7 +71,7 @@ public function parseMustRaiseExceptionWhenTokenDoesNotHaveClaims(): void $parser->parse('a..c'); } - /** @test */ + #[PHPUnit\Test] public function parseMustRaiseExceptionWhenTokenDoesNotHaveSignature(): void { $parser = $this->createParser(); @@ -85,7 +82,7 @@ public function parseMustRaiseExceptionWhenTokenDoesNotHaveSignature(): void $parser->parse('a.b.'); } - /** @test */ + #[PHPUnit\Test] public function parseMustRaiseExceptionWhenHeaderCannotBeDecoded(): void { $this->decoder->method('base64UrlDecode') @@ -104,7 +101,7 @@ public function parseMustRaiseExceptionWhenHeaderCannotBeDecoded(): void $parser->parse('a.b.c'); } - /** @test */ + #[PHPUnit\Test] public function parseMustRaiseExceptionWhenDealingWithNonArrayHeaders(): void { $this->decoder->method('jsonDecode') @@ -118,7 +115,7 @@ public function parseMustRaiseExceptionWhenDealingWithNonArrayHeaders(): void $parser->parse('a.a.a'); } - /** @test */ + #[PHPUnit\Test] public function parseMustRaiseExceptionWhenDealingWithHeadersThatHaveEmptyStringKeys(): void { $this->decoder->method('jsonDecode') @@ -132,7 +129,7 @@ public function parseMustRaiseExceptionWhenDealingWithHeadersThatHaveEmptyString $parser->parse('a.a.a'); } - /** @test */ + #[PHPUnit\Test] public function parseMustRaiseExceptionWhenHeaderIsFromAnEncryptedToken(): void { $this->decoder->method('jsonDecode') @@ -146,7 +143,7 @@ public function parseMustRaiseExceptionWhenHeaderIsFromAnEncryptedToken(): void $parser->parse('a.a.a'); } - /** @test */ + #[PHPUnit\Test] public function parseMustRaiseExceptionWhenDealingWithNonArrayClaims(): void { $this->decoder->method('jsonDecode') @@ -160,7 +157,7 @@ public function parseMustRaiseExceptionWhenDealingWithNonArrayClaims(): void $parser->parse('a.a.a'); } - /** @test */ + #[PHPUnit\Test] public function parseMustRaiseExceptionWhenDealingWithClaimsThatHaveEmptyStringKeys(): void { $this->decoder->method('jsonDecode') @@ -174,10 +171,10 @@ public function parseMustRaiseExceptionWhenDealingWithClaimsThatHaveEmptyStringK $parser->parse('a.a.a'); } - /** @test */ + #[PHPUnit\Test] public function parseMustReturnAnUnsecuredTokenWhenSignatureIsNotInformed(): void { - $this->decoder->expects(self::exactly(3)) + $this->decoder->expects($this->exactly(3)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], @@ -185,7 +182,7 @@ public function parseMustReturnAnUnsecuredTokenWhenSignatureIsNotInformed(): voi ['c', 'c_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['typ' => 'JWT', 'alg' => 'none']], @@ -206,10 +203,10 @@ public function parseMustReturnAnUnsecuredTokenWhenSignatureIsNotInformed(): voi self::assertEquals($signature, $token->signature()); } - /** @test */ + #[PHPUnit\Test] public function parseMustConfigureTypeToJWTWhenItIsMissing(): void { - $this->decoder->expects(self::exactly(3)) + $this->decoder->expects($this->exactly(3)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], @@ -217,7 +214,7 @@ public function parseMustConfigureTypeToJWTWhenItIsMissing(): void ['c', 'c_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['alg' => 'none']], @@ -238,10 +235,10 @@ public function parseMustConfigureTypeToJWTWhenItIsMissing(): void self::assertEquals($signature, $token->signature()); } - /** @test */ + #[PHPUnit\Test] public function parseMustNotChangeTypeWhenItIsConfigured(): void { - $this->decoder->expects(self::exactly(3)) + $this->decoder->expects($this->exactly(3)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], @@ -249,7 +246,7 @@ public function parseMustNotChangeTypeWhenItIsConfigured(): void ['c', 'c_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['typ' => 'JWS', 'alg' => 'none']], @@ -270,10 +267,10 @@ public function parseMustNotChangeTypeWhenItIsConfigured(): void self::assertEquals($signature, $token->signature()); } - /** @test */ + #[PHPUnit\Test] public function parseShouldReplicateClaimValueOnHeaderWhenNeeded(): void { - $this->decoder->expects(self::exactly(3)) + $this->decoder->expects($this->exactly(3)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], @@ -281,7 +278,7 @@ public function parseShouldReplicateClaimValueOnHeaderWhenNeeded(): void ['c', 'c_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['typ' => 'JWT', 'alg' => 'none', RegisteredClaims::AUDIENCE => 'test']], @@ -302,10 +299,10 @@ public function parseShouldReplicateClaimValueOnHeaderWhenNeeded(): void self::assertEquals($signature, $token->signature()); } - /** @test */ + #[PHPUnit\Test] public function parseMustReturnANonSignedTokenWhenSignatureAlgorithmIsMissing(): void { - $this->decoder->expects(self::exactly(3)) + $this->decoder->expects($this->exactly(3)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], @@ -313,7 +310,7 @@ public function parseMustReturnANonSignedTokenWhenSignatureAlgorithmIsMissing(): ['c', 'c_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['typ' => 'JWT']], @@ -334,10 +331,10 @@ public function parseMustReturnANonSignedTokenWhenSignatureAlgorithmIsMissing(): self::assertEquals($signature, $token->signature()); } - /** @test */ + #[PHPUnit\Test] public function parseMustReturnANonSignedTokenWhenSignatureAlgorithmIsNone(): void { - $this->decoder->expects(self::exactly(3)) + $this->decoder->expects($this->exactly(3)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], @@ -345,7 +342,7 @@ public function parseMustReturnANonSignedTokenWhenSignatureAlgorithmIsNone(): vo ['c', 'c_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['typ' => 'JWT', 'alg' => 'none']], @@ -366,10 +363,10 @@ public function parseMustReturnANonSignedTokenWhenSignatureAlgorithmIsNone(): vo self::assertEquals($signature, $token->signature()); } - /** @test */ + #[PHPUnit\Test] public function parseMustReturnASignedTokenWhenSignatureIsInformed(): void { - $this->decoder->expects(self::exactly(3)) + $this->decoder->expects($this->exactly(3)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], @@ -377,7 +374,7 @@ public function parseMustReturnASignedTokenWhenSignatureIsInformed(): void ['c', 'c_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['typ' => 'JWT', 'alg' => 'HS256']], @@ -398,7 +395,7 @@ public function parseMustReturnASignedTokenWhenSignatureIsInformed(): void self::assertEquals($signature, $token->signature()); } - /** @test */ + #[PHPUnit\Test] public function parseMustConvertDateClaimsToObjects(): void { $data = [ @@ -406,7 +403,7 @@ public function parseMustConvertDateClaimsToObjects(): void RegisteredClaims::EXPIRATION_TIME => 1486930757.023055, ]; - $this->decoder->expects(self::exactly(3)) + $this->decoder->expects($this->exactly(3)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], @@ -414,7 +411,7 @@ public function parseMustConvertDateClaimsToObjects(): void ['c', 'c_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['typ' => 'JWT', 'alg' => 'HS256']], @@ -437,12 +434,12 @@ public function parseMustConvertDateClaimsToObjects(): void ); } - /** @test */ + #[PHPUnit\Test] public function parseMustConvertStringDates(): void { $data = [RegisteredClaims::NOT_BEFORE => '1486930757.000000']; - $this->decoder->expects(self::exactly(3)) + $this->decoder->expects($this->exactly(3)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], @@ -450,7 +447,7 @@ public function parseMustConvertStringDates(): void ['c', 'c_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['typ' => 'JWT', 'alg' => 'HS256']], @@ -468,19 +465,19 @@ public function parseMustConvertStringDates(): void ); } - /** @test */ + #[PHPUnit\Test] public function parseShouldRaiseExceptionOnInvalidDate(): void { $data = [RegisteredClaims::ISSUED_AT => '14/10/2018 10:50:10.10 UTC']; - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], ['b', 'b_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['typ' => 'JWT', 'alg' => 'HS256']], @@ -492,19 +489,19 @@ public function parseShouldRaiseExceptionOnInvalidDate(): void $this->createParser()->parse('a.b.c'); } - /** @test */ + #[PHPUnit\Test] public function parseShouldRaiseExceptionOnTimestampBeyondDateTimeImmutableRange(): void { $data = [RegisteredClaims::ISSUED_AT => -10000000000 ** 5]; - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('base64UrlDecode') ->willReturnMap([ ['a', 'a_dec'], ['b', 'b_dec'], ]); - $this->decoder->expects(self::exactly(2)) + $this->decoder->expects($this->exactly(2)) ->method('jsonDecode') ->willReturnMap([ ['a_dec', ['typ' => 'JWT', 'alg' => 'HS256']], diff --git a/tests/Token/PlainTest.php b/tests/Token/PlainTest.php index 3554cb44..ebab6c83 100644 --- a/tests/Token/PlainTest.php +++ b/tests/Token/PlainTest.php @@ -8,23 +8,19 @@ use Lcobucci\JWT\Token\Plain; use Lcobucci\JWT\Token\RegisteredClaims; use Lcobucci\JWT\Token\Signature; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; -/** - * @coversDefaultClass \Lcobucci\JWT\Token\Plain - * - * @uses \Lcobucci\JWT\Token\Plain::__construct - * @uses \Lcobucci\JWT\Token\Plain::payload - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(Plain::class)] +#[PHPUnit\UsesClass(DataSet::class)] +#[PHPUnit\UsesClass(Signature::class)] final class PlainTest extends TestCase { private DataSet $headers; private DataSet $claims; private Signature $signature; - /** @before */ + #[PHPUnit\Before] public function createDependencies(): void { $this->headers = new DataSet(['alg' => 'none'], 'headers'); @@ -44,14 +40,7 @@ private function createToken( ); } - /** - * @test - * - * @covers ::__construct - * @covers ::headers - * @covers ::claims - * @covers ::signature - */ + #[PHPUnit\Test] public function signedShouldCreateATokenWithSignature(): void { $token = $this->createToken(); @@ -61,11 +50,7 @@ public function signedShouldCreateATokenWithSignature(): void self::assertSame($this->signature, $token->signature()); } - /** - * @test - * - * @covers ::payload - */ + #[PHPUnit\Test] public function payloadShouldReturnAStringWithTheEncodedHeadersAndClaims(): void { $token = $this->createToken(); @@ -73,11 +58,7 @@ public function payloadShouldReturnAStringWithTheEncodedHeadersAndClaims(): void self::assertSame('headers.claims', $token->payload()); } - /** - * @test - * - * @covers ::isPermittedFor - */ + #[PHPUnit\Test] public function isPermittedForShouldReturnFalseWhenNoAudienceIsConfigured(): void { $token = $this->createToken(); @@ -85,11 +66,7 @@ public function isPermittedForShouldReturnFalseWhenNoAudienceIsConfigured(): voi self::assertFalse($token->isPermittedFor('testing')); } - /** - * @test - * - * @covers ::isPermittedFor - */ + #[PHPUnit\Test] public function isPermittedForShouldReturnFalseWhenAudienceDoesNotMatchAsArray(): void { $token = $this->createToken( @@ -100,11 +77,7 @@ public function isPermittedForShouldReturnFalseWhenAudienceDoesNotMatchAsArray() self::assertFalse($token->isPermittedFor('testing')); } - /** - * @test - * - * @covers ::isPermittedFor - */ + #[PHPUnit\Test] public function isPermittedForShouldReturnFalseWhenAudienceTypeDoesNotMatch(): void { $token = $this->createToken( @@ -115,11 +88,7 @@ public function isPermittedForShouldReturnFalseWhenAudienceTypeDoesNotMatch(): v self::assertFalse($token->isPermittedFor('10')); } - /** - * @test - * - * @covers ::isPermittedFor - */ + #[PHPUnit\Test] public function isPermittedForShouldReturnTrueWhenAudienceMatchesAsArray(): void { $token = $this->createToken( @@ -130,11 +99,7 @@ public function isPermittedForShouldReturnTrueWhenAudienceMatchesAsArray(): void self::assertTrue($token->isPermittedFor('testing')); } - /** - * @test - * - * @covers ::isIdentifiedBy - */ + #[PHPUnit\Test] public function isIdentifiedByShouldReturnFalseWhenNoIdWasConfigured(): void { $token = $this->createToken(); @@ -142,11 +107,7 @@ public function isIdentifiedByShouldReturnFalseWhenNoIdWasConfigured(): void self::assertFalse($token->isIdentifiedBy('test')); } - /** - * @test - * - * @covers ::isIdentifiedBy - */ + #[PHPUnit\Test] public function isIdentifiedByShouldReturnFalseWhenIdDoesNotMatch(): void { $token = $this->createToken( @@ -157,11 +118,7 @@ public function isIdentifiedByShouldReturnFalseWhenIdDoesNotMatch(): void self::assertFalse($token->isIdentifiedBy('test')); } - /** - * @test - * - * @covers ::isIdentifiedBy - */ + #[PHPUnit\Test] public function isIdentifiedByShouldReturnTrueWhenIdMatches(): void { $token = $this->createToken( @@ -172,11 +129,7 @@ public function isIdentifiedByShouldReturnTrueWhenIdMatches(): void self::assertTrue($token->isIdentifiedBy('test')); } - /** - * @test - * - * @covers ::isRelatedTo - */ + #[PHPUnit\Test] public function isRelatedToShouldReturnFalseWhenNoSubjectWasConfigured(): void { $token = $this->createToken(); @@ -184,11 +137,7 @@ public function isRelatedToShouldReturnFalseWhenNoSubjectWasConfigured(): void self::assertFalse($token->isRelatedTo('test')); } - /** - * @test - * - * @covers ::isRelatedTo - */ + #[PHPUnit\Test] public function isRelatedToShouldReturnFalseWhenSubjectDoesNotMatch(): void { $token = $this->createToken( @@ -199,11 +148,7 @@ public function isRelatedToShouldReturnFalseWhenSubjectDoesNotMatch(): void self::assertFalse($token->isRelatedTo('test')); } - /** - * @test - * - * @covers ::isRelatedTo - */ + #[PHPUnit\Test] public function isRelatedToShouldReturnTrueWhenSubjectMatches(): void { $token = $this->createToken( @@ -214,11 +159,7 @@ public function isRelatedToShouldReturnTrueWhenSubjectMatches(): void self::assertTrue($token->isRelatedTo('test')); } - /** - * @test - * - * @covers ::hasBeenIssuedBy - */ + #[PHPUnit\Test] public function hasBeenIssuedByShouldReturnFalseWhenIssuerIsNotConfigured(): void { $token = $this->createToken(); @@ -226,11 +167,7 @@ public function hasBeenIssuedByShouldReturnFalseWhenIssuerIsNotConfigured(): voi self::assertFalse($token->hasBeenIssuedBy('test')); } - /** - * @test - * - * @covers ::hasBeenIssuedBy - */ + #[PHPUnit\Test] public function hasBeenIssuedByShouldReturnFalseWhenIssuerTypeDoesNotMatches(): void { $token = $this->createToken( @@ -241,11 +178,7 @@ public function hasBeenIssuedByShouldReturnFalseWhenIssuerTypeDoesNotMatches(): self::assertFalse($token->hasBeenIssuedBy('10')); } - /** - * @test - * - * @covers ::hasBeenIssuedBy - */ + #[PHPUnit\Test] public function hasBeenIssuedByShouldReturnFalseWhenIssuerIsNotInTheGivenList(): void { $token = $this->createToken( @@ -256,11 +189,7 @@ public function hasBeenIssuedByShouldReturnFalseWhenIssuerIsNotInTheGivenList(): self::assertFalse($token->hasBeenIssuedBy('testing1', 'testing2')); } - /** - * @test - * - * @covers ::hasBeenIssuedBy - */ + #[PHPUnit\Test] public function hasBeenIssuedByShouldReturnTrueWhenIssuerIsInTheGivenList(): void { $token = $this->createToken( @@ -271,11 +200,7 @@ public function hasBeenIssuedByShouldReturnTrueWhenIssuerIsInTheGivenList(): voi self::assertTrue($token->hasBeenIssuedBy('testing1', 'testing2', 'test')); } - /** - * @test - * - * @covers ::hasBeenIssuedBefore - */ + #[PHPUnit\Test] public function hasBeenIssuedBeforeShouldReturnTrueWhenIssueTimeIsNotConfigured(): void { $token = $this->createToken(); @@ -283,11 +208,7 @@ public function hasBeenIssuedBeforeShouldReturnTrueWhenIssueTimeIsNotConfigured( self::assertTrue($token->hasBeenIssuedBefore(new DateTimeImmutable())); } - /** - * @test - * - * @covers ::hasBeenIssuedBefore - */ + #[PHPUnit\Test] public function hasBeenIssuedBeforeShouldReturnTrueWhenIssueTimeIsBeforeThanNow(): void { $now = new DateTimeImmutable(); @@ -299,11 +220,7 @@ public function hasBeenIssuedBeforeShouldReturnTrueWhenIssueTimeIsBeforeThanNow( self::assertTrue($token->hasBeenIssuedBefore($now)); } - /** - * @test - * - * @covers ::hasBeenIssuedBefore - */ + #[PHPUnit\Test] public function hasBeenIssuedBeforeShouldReturnTrueWhenIssueTimeIsEqualsToNow(): void { $now = new DateTimeImmutable(); @@ -315,11 +232,7 @@ public function hasBeenIssuedBeforeShouldReturnTrueWhenIssueTimeIsEqualsToNow(): self::assertTrue($token->hasBeenIssuedBefore($now)); } - /** - * @test - * - * @covers ::hasBeenIssuedBefore - */ + #[PHPUnit\Test] public function hasBeenIssuedBeforeShouldReturnFalseWhenIssueTimeIsGreaterThanNow(): void { $now = new DateTimeImmutable(); @@ -331,11 +244,7 @@ public function hasBeenIssuedBeforeShouldReturnFalseWhenIssueTimeIsGreaterThanNo self::assertFalse($token->hasBeenIssuedBefore($now)); } - /** - * @test - * - * @covers ::isMinimumTimeBefore - */ + #[PHPUnit\Test] public function isMinimumTimeBeforeShouldReturnTrueWhenIssueTimeIsNotConfigured(): void { $token = $this->createToken(); @@ -343,11 +252,7 @@ public function isMinimumTimeBeforeShouldReturnTrueWhenIssueTimeIsNotConfigured( self::assertTrue($token->isMinimumTimeBefore(new DateTimeImmutable())); } - /** - * @test - * - * @covers ::isMinimumTimeBefore - */ + #[PHPUnit\Test] public function isMinimumTimeBeforeShouldReturnTrueWhenNotBeforeClaimIsBeforeThanNow(): void { $now = new DateTimeImmutable(); @@ -359,11 +264,7 @@ public function isMinimumTimeBeforeShouldReturnTrueWhenNotBeforeClaimIsBeforeTha self::assertTrue($token->isMinimumTimeBefore($now)); } - /** - * @test - * - * @covers ::isMinimumTimeBefore - */ + #[PHPUnit\Test] public function isMinimumTimeBeforeShouldReturnTrueWhenNotBeforeClaimIsEqualsToNow(): void { $now = new DateTimeImmutable(); @@ -375,11 +276,7 @@ public function isMinimumTimeBeforeShouldReturnTrueWhenNotBeforeClaimIsEqualsToN self::assertTrue($token->isMinimumTimeBefore($now)); } - /** - * @test - * - * @covers ::isMinimumTimeBefore - */ + #[PHPUnit\Test] public function isMinimumTimeBeforeShouldReturnFalseWhenNotBeforeClaimIsGreaterThanNow(): void { $now = new DateTimeImmutable(); @@ -391,11 +288,7 @@ public function isMinimumTimeBeforeShouldReturnFalseWhenNotBeforeClaimIsGreaterT self::assertFalse($token->isMinimumTimeBefore($now)); } - /** - * @test - * - * @covers ::isExpired - */ + #[PHPUnit\Test] public function isExpiredShouldReturnFalseWhenTokenDoesNotExpires(): void { $token = $this->createToken(); @@ -403,11 +296,7 @@ public function isExpiredShouldReturnFalseWhenTokenDoesNotExpires(): void self::assertFalse($token->isExpired(new DateTimeImmutable())); } - /** - * @test - * - * @covers ::isExpired - */ + #[PHPUnit\Test] public function isExpiredShouldReturnFalseWhenTokenIsNotExpired(): void { $now = new DateTimeImmutable(); @@ -420,11 +309,7 @@ public function isExpiredShouldReturnFalseWhenTokenIsNotExpired(): void self::assertFalse($token->isExpired($now)); } - /** - * @test - * - * @covers ::isExpired - */ + #[PHPUnit\Test] public function isExpiredShouldReturnTrueWhenExpirationIsEqualsToNow(): void { $now = new DateTimeImmutable(); @@ -437,11 +322,7 @@ public function isExpiredShouldReturnTrueWhenExpirationIsEqualsToNow(): void self::assertTrue($token->isExpired($now)); } - /** - * @test - * - * @covers ::isExpired - */ + #[PHPUnit\Test] public function isExpiredShouldReturnTrueAfterTokenExpires(): void { $now = new DateTimeImmutable(); @@ -454,11 +335,7 @@ public function isExpiredShouldReturnTrueAfterTokenExpires(): void self::assertTrue($token->isExpired($now->modify('+10 days'))); } - /** - * @test - * - * @covers ::toString - */ + #[PHPUnit\Test] public function toStringMustReturnEncodedDataWithEmptySignature(): void { $token = $this->createToken(null, null, new Signature('123', '456')); @@ -466,11 +343,7 @@ public function toStringMustReturnEncodedDataWithEmptySignature(): void self::assertSame('headers.claims.456', $token->toString()); } - /** - * @test - * - * @covers ::toString - */ + #[PHPUnit\Test] public function toStringMustReturnEncodedData(): void { $token = $this->createToken(); diff --git a/tests/Token/SignatureTest.php b/tests/Token/SignatureTest.php index 4781a844..c64e5058 100644 --- a/tests/Token/SignatureTest.php +++ b/tests/Token/SignatureTest.php @@ -4,17 +4,13 @@ namespace Lcobucci\JWT\Tests\Token; use Lcobucci\JWT\Token\Signature; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; -/** - * @covers ::__construct - * @covers ::toString - * @covers ::hash - * @coversDefaultClass \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(Signature::class)] final class SignatureTest extends TestCase { - /** @test */ + #[PHPUnit\Test] public function hashShouldReturnTheHash(): void { $signature = new Signature('test', 'encoded'); diff --git a/tests/UnsignedTokenTest.php b/tests/UnsignedTokenTest.php index 3df5ab92..f29f18a1 100644 --- a/tests/UnsignedTokenTest.php +++ b/tests/UnsignedTokenTest.php @@ -6,7 +6,9 @@ use DateTimeImmutable; use Lcobucci\Clock\FrozenClock; use Lcobucci\JWT\Configuration; +use Lcobucci\JWT\Encoding; use Lcobucci\JWT\Signer\Key\InMemory; +use Lcobucci\JWT\SodiumBase64Polyfill; use Lcobucci\JWT\Token; use Lcobucci\JWT\Validation\Constraint; use Lcobucci\JWT\Validation\Constraint\IdentifiedBy; @@ -15,38 +17,38 @@ use Lcobucci\JWT\Validation\Constraint\PermittedFor; use Lcobucci\JWT\Validation\ConstraintViolation; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; +use Lcobucci\JWT\Validation\Validator; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use function assert; -/** - * @covers \Lcobucci\JWT\Configuration - * @covers \Lcobucci\JWT\Encoding\JoseEncoder - * @covers \Lcobucci\JWT\Encoding\ChainedFormatter - * @covers \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion - * @covers \Lcobucci\JWT\Encoding\UnifyAudience - * @covers \Lcobucci\JWT\Token\Builder - * @covers \Lcobucci\JWT\Token\Parser - * @covers \Lcobucci\JWT\Token\Plain - * @covers \Lcobucci\JWT\Token\DataSet - * @covers \Lcobucci\JWT\Token\Signature - * @covers \Lcobucci\JWT\Signer\Key\InMemory - * @covers \Lcobucci\JWT\SodiumBase64Polyfill - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\RequiredConstraintsViolated - * @covers \Lcobucci\JWT\Validation\Validator - * @covers \Lcobucci\JWT\Validation\Constraint\IssuedBy - * @covers \Lcobucci\JWT\Validation\Constraint\PermittedFor - * @covers \Lcobucci\JWT\Validation\Constraint\IdentifiedBy - * @covers \Lcobucci\JWT\Validation\Constraint\LooseValidAt - */ +#[PHPUnit\CoversClass(Configuration::class)] +#[PHPUnit\CoversClass(Encoding\JoseEncoder::class)] +#[PHPUnit\CoversClass(Encoding\ChainedFormatter::class)] +#[PHPUnit\CoversClass(Encoding\MicrosecondBasedDateConversion::class)] +#[PHPUnit\CoversClass(Encoding\UnifyAudience::class)] +#[PHPUnit\CoversClass(Token\Builder::class)] +#[PHPUnit\CoversClass(Token\Parser::class)] +#[PHPUnit\CoversClass(Token\Plain::class)] +#[PHPUnit\CoversClass(Token\DataSet::class)] +#[PHPUnit\CoversClass(Token\Signature::class)] +#[PHPUnit\CoversClass(InMemory::class)] +#[PHPUnit\CoversClass(SodiumBase64Polyfill::class)] +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(RequiredConstraintsViolated::class)] +#[PHPUnit\CoversClass(Validator::class)] +#[PHPUnit\CoversClass(IssuedBy::class)] +#[PHPUnit\CoversClass(PermittedFor::class)] +#[PHPUnit\CoversClass(IdentifiedBy::class)] +#[PHPUnit\CoversClass(LooseValidAt::class)] class UnsignedTokenTest extends TestCase { public const CURRENT_TIME = 100000; private Configuration $config; - /** @before */ + #[PHPUnit\Before] public function createConfiguration(): void { $this->config = Configuration::forSymmetricSigner( @@ -55,7 +57,7 @@ public function createConfiguration(): void ); } - /** @test */ + #[PHPUnit\Test] public function builderCanGenerateAToken(): Token { $user = ['name' => 'testing', 'email' => 'testing@abc.com']; @@ -79,10 +81,8 @@ public function builderCanGenerateAToken(): Token return $token; } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function parserCanReadAToken(Token $generated): void { $read = $this->config->parser()->parse($generated->toString()); @@ -92,10 +92,8 @@ public function parserCanReadAToken(Token $generated): void self::assertSame('testing', $read->claims()->get('user')['name']); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function tokenValidationShouldPassWhenEverythingIsFine(Token $generated): void { $clock = new FrozenClock(new DateTimeImmutable('@' . self::CURRENT_TIME)); @@ -110,19 +108,15 @@ public function tokenValidationShouldPassWhenEverythingIsFine(Token $generated): self::assertTrue($this->config->validator()->validate($generated, ...$constraints)); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function tokenValidationShouldAllowCustomConstraint(Token $generated): void { self::assertTrue($this->config->validator()->validate($generated, $this->validUserConstraint())); } - /** - * @test - * @depends builderCanGenerateAToken - */ + #[PHPUnit\Test] + #[PHPUnit\Depends('builderCanGenerateAToken')] public function tokenAssertionShouldRaiseExceptionWhenOneOfTheConstraintsFails(Token $generated): void { $constraints = [ diff --git a/tests/Validation/Constraint/HasClaimTest.php b/tests/Validation/Constraint/HasClaimTest.php index df9ec51c..058c7291 100644 --- a/tests/Validation/Constraint/HasClaimTest.php +++ b/tests/Validation/Constraint/HasClaimTest.php @@ -7,25 +7,19 @@ use Lcobucci\JWT\Validation\Constraint\CannotValidateARegisteredClaim; use Lcobucci\JWT\Validation\Constraint\HasClaim; use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\Constraint\HasClaim - * - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(HasClaim::class)] +#[PHPUnit\CoversClass(CannotValidateARegisteredClaim::class)] +#[PHPUnit\UsesClass(Token\DataSet::class)] +#[PHPUnit\UsesClass(Token\Plain::class)] +#[PHPUnit\UsesClass(Token\Signature::class)] final class HasClaimTest extends ConstraintTestCase { - /** - * @test - * @dataProvider registeredClaims - * - * @covers \Lcobucci\JWT\Validation\Constraint\CannotValidateARegisteredClaim - * - * @param non-empty-string $claim - */ + /** @param non-empty-string $claim */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('registeredClaims')] public function registeredClaimsCannotBeValidatedUsingThisConstraint(string $claim): void { $this->expectException(CannotValidateARegisteredClaim::class); @@ -44,7 +38,7 @@ public static function registeredClaims(): iterable } } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenClaimIsNotSet(): void { $constraint = new HasClaim('claimId'); @@ -55,7 +49,7 @@ public function assertShouldRaiseExceptionWhenClaimIsNotSet(): void $constraint->assert($this->buildToken()); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenTokenIsNotAPlainToken(): void { $token = $this->createMock(Token::class); @@ -67,7 +61,7 @@ public function assertShouldRaiseExceptionWhenTokenIsNotAPlainToken(): void $constraint->assert($token); } - /** @test */ + #[PHPUnit\Test] public function assertShouldNotRaiseExceptionWhenClaimMatches(): void { $token = $this->buildToken(['claimId' => 'claimValue']); diff --git a/tests/Validation/Constraint/HasClaimWithValueTest.php b/tests/Validation/Constraint/HasClaimWithValueTest.php index bf7a1b78..bbc8aa94 100644 --- a/tests/Validation/Constraint/HasClaimWithValueTest.php +++ b/tests/Validation/Constraint/HasClaimWithValueTest.php @@ -7,25 +7,19 @@ use Lcobucci\JWT\Validation\Constraint\CannotValidateARegisteredClaim; use Lcobucci\JWT\Validation\Constraint\HasClaimWithValue; use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\Constraint\HasClaimWithValue - * - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(HasClaimWithValue::class)] +#[PHPUnit\CoversClass(CannotValidateARegisteredClaim::class)] +#[PHPUnit\UsesClass(Token\DataSet::class)] +#[PHPUnit\UsesClass(Token\Plain::class)] +#[PHPUnit\UsesClass(Token\Signature::class)] final class HasClaimWithValueTest extends ConstraintTestCase { - /** - * @test - * @dataProvider registeredClaims - * - * @covers \Lcobucci\JWT\Validation\Constraint\CannotValidateARegisteredClaim - * - * @param non-empty-string $claim - */ + /** @param non-empty-string $claim */ + #[PHPUnit\Test] + #[PHPUnit\DataProvider('registeredClaims')] public function registeredClaimsCannotBeValidatedUsingThisConstraint(string $claim): void { $this->expectException(CannotValidateARegisteredClaim::class); @@ -44,37 +38,40 @@ public static function registeredClaims(): iterable } } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenClaimIsNotSet(): void { + $constraint = new HasClaimWithValue('claimId', 'claimValue'); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token does not have the claim "claimId"'); - $constraint = new HasClaimWithValue('claimId', 'claimValue'); $constraint->assert($this->buildToken()); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenClaimValueDoesNotMatch(): void { + $constraint = new HasClaimWithValue('claimId', 'claimValue'); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The claim "claimId" does not have the expected value'); - $constraint = new HasClaimWithValue('claimId', 'claimValue'); $constraint->assert($this->buildToken(['claimId' => 'Some wrong value'])); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenTokenIsNotAPlainToken(): void { + $constraint = new HasClaimWithValue('claimId', 'claimValue'); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('You should pass a plain token'); - $constraint = new HasClaimWithValue('claimId', 'claimValue'); $constraint->assert($this->createMock(Token::class)); } - /** @test */ + #[PHPUnit\Test] public function assertShouldNotRaiseExceptionWhenClaimMatches(): void { $token = $this->buildToken(['claimId' => 'claimValue']); diff --git a/tests/Validation/Constraint/IdentifiedByTest.php b/tests/Validation/Constraint/IdentifiedByTest.php index 468c6410..12bf3ced 100644 --- a/tests/Validation/Constraint/IdentifiedByTest.php +++ b/tests/Validation/Constraint/IdentifiedByTest.php @@ -3,41 +3,42 @@ namespace Lcobucci\JWT\Tests\Validation\Constraint; +use Lcobucci\JWT\Token; use Lcobucci\JWT\Token\RegisteredClaims; use Lcobucci\JWT\Validation\Constraint\IdentifiedBy; use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Validation\Constraint\IdentifiedBy - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(IdentifiedBy::class)] +#[PHPUnit\UsesClass(Token\DataSet::class)] +#[PHPUnit\UsesClass(Token\Plain::class)] +#[PHPUnit\UsesClass(Token\Signature::class)] final class IdentifiedByTest extends ConstraintTestCase { - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenIdIsNotSet(): void { + $constraint = new IdentifiedBy('123456'); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token is not identified with the expected ID'); - $constraint = new IdentifiedBy('123456'); $constraint->assert($this->buildToken()); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenIdDoesNotMatch(): void { + $constraint = new IdentifiedBy('123456'); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token is not identified with the expected ID'); - $constraint = new IdentifiedBy('123456'); $constraint->assert($this->buildToken([RegisteredClaims::ID => 15])); } - /** @test */ + #[PHPUnit\Test] public function assertShouldNotRaiseExceptionWhenIdMatches(): void { $token = $this->buildToken([RegisteredClaims::ID => '123456']); diff --git a/tests/Validation/Constraint/IssuedByTest.php b/tests/Validation/Constraint/IssuedByTest.php index e4d975b4..dddf6831 100644 --- a/tests/Validation/Constraint/IssuedByTest.php +++ b/tests/Validation/Constraint/IssuedByTest.php @@ -3,21 +3,20 @@ namespace Lcobucci\JWT\Tests\Validation\Constraint; +use Lcobucci\JWT\Token; use Lcobucci\JWT\Token\RegisteredClaims; use Lcobucci\JWT\Validation\Constraint\IssuedBy; use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Validation\Constraint\IssuedBy - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(IssuedBy::class)] +#[PHPUnit\UsesClass(Token\DataSet::class)] +#[PHPUnit\UsesClass(Token\Plain::class)] +#[PHPUnit\UsesClass(Token\Signature::class)] final class IssuedByTest extends ConstraintTestCase { - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenIssuerIsNotSet(): void { $this->expectException(ConstraintViolation::class); @@ -27,7 +26,7 @@ public function assertShouldRaiseExceptionWhenIssuerIsNotSet(): void $constraint->assert($this->buildToken()); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenIssuerValueDoesNotMatch(): void { $this->expectException(ConstraintViolation::class); @@ -37,7 +36,7 @@ public function assertShouldRaiseExceptionWhenIssuerValueDoesNotMatch(): void $constraint->assert($this->buildToken([RegisteredClaims::ISSUER => 'example.com'])); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenIssuerTypeValueDoesNotMatch(): void { $this->expectException(ConstraintViolation::class); @@ -47,7 +46,7 @@ public function assertShouldRaiseExceptionWhenIssuerTypeValueDoesNotMatch(): voi $constraint->assert($this->buildToken([RegisteredClaims::ISSUER => 123])); } - /** @test */ + #[PHPUnit\Test] public function assertShouldNotRaiseExceptionWhenIssuerMatches(): void { $token = $this->buildToken([RegisteredClaims::ISSUER => 'test.com']); diff --git a/tests/Validation/Constraint/LooseValidAtTest.php b/tests/Validation/Constraint/LooseValidAtTest.php index 4c4643d0..d2c0abfc 100644 --- a/tests/Validation/Constraint/LooseValidAtTest.php +++ b/tests/Validation/Constraint/LooseValidAtTest.php @@ -5,18 +5,18 @@ use DateInterval; use Lcobucci\Clock\Clock; +use Lcobucci\JWT\Token; use Lcobucci\JWT\Validation\Constraint; use Lcobucci\JWT\Validation\Constraint\LooseValidAt; +use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Validation\Constraint\LeewayCannotBeNegative - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\Constraint\LooseValidAt - * - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(Constraint\LeewayCannotBeNegative::class)] +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(LooseValidAt::class)] +#[PHPUnit\UsesClass(Token\DataSet::class)] +#[PHPUnit\UsesClass(Token\Plain::class)] +#[PHPUnit\UsesClass(Token\Signature::class)] final class LooseValidAtTest extends ValidAtTestCase { protected function buildValidAtConstraint(Clock $clock, ?DateInterval $leeway = null): Constraint @@ -24,7 +24,7 @@ protected function buildValidAtConstraint(Clock $clock, ?DateInterval $leeway = return new LooseValidAt($clock, $leeway); } - /** @test */ + #[PHPUnit\Test] public function assertShouldNotRaiseExceptionWhenTokenDoesNotHaveTimeClaims(): void { $token = $this->buildToken(); diff --git a/tests/Validation/Constraint/PermittedForTest.php b/tests/Validation/Constraint/PermittedForTest.php index 9b47acb1..fae96bea 100644 --- a/tests/Validation/Constraint/PermittedForTest.php +++ b/tests/Validation/Constraint/PermittedForTest.php @@ -3,51 +3,53 @@ namespace Lcobucci\JWT\Tests\Validation\Constraint; +use Lcobucci\JWT\Token; use Lcobucci\JWT\Token\RegisteredClaims; use Lcobucci\JWT\Validation\Constraint\PermittedFor; use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Validation\Constraint\PermittedFor - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(PermittedFor::class)] +#[PHPUnit\UsesClass(Token\DataSet::class)] +#[PHPUnit\UsesClass(Token\Plain::class)] +#[PHPUnit\UsesClass(Token\Signature::class)] final class PermittedForTest extends ConstraintTestCase { - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenAudienceIsNotSet(): void { + $constraint = new PermittedFor('test.com'); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token is not allowed to be used by this audience'); - $constraint = new PermittedFor('test.com'); $constraint->assert($this->buildToken()); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenAudienceValueDoesNotMatch(): void { + $constraint = new PermittedFor('test.com'); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token is not allowed to be used by this audience'); - $constraint = new PermittedFor('test.com'); $constraint->assert($this->buildToken([RegisteredClaims::AUDIENCE => ['aa.com']])); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenAudienceTypeDoesNotMatch(): void { + $constraint = new PermittedFor('123'); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token is not allowed to be used by this audience'); - $constraint = new PermittedFor('123'); $constraint->assert($this->buildToken([RegisteredClaims::AUDIENCE => [123]])); } - /** @test */ + #[PHPUnit\Test] public function assertShouldNotRaiseExceptionWhenAudienceMatches(): void { $token = $this->buildToken([RegisteredClaims::AUDIENCE => ['aa.com', 'test.com']]); diff --git a/tests/Validation/Constraint/RelatedToTest.php b/tests/Validation/Constraint/RelatedToTest.php index 98a4763f..eafc671b 100644 --- a/tests/Validation/Constraint/RelatedToTest.php +++ b/tests/Validation/Constraint/RelatedToTest.php @@ -3,41 +3,42 @@ namespace Lcobucci\JWT\Tests\Validation\Constraint; +use Lcobucci\JWT\Token; use Lcobucci\JWT\Token\RegisteredClaims; use Lcobucci\JWT\Validation\Constraint\RelatedTo; use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Validation\Constraint\RelatedTo - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(RelatedTo::class)] +#[PHPUnit\UsesClass(Token\DataSet::class)] +#[PHPUnit\UsesClass(Token\Plain::class)] +#[PHPUnit\UsesClass(Token\Signature::class)] final class RelatedToTest extends ConstraintTestCase { - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenSubjectIsNotSet(): void { + $constraint = new RelatedTo('user-auth'); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token is not related to the expected subject'); - $constraint = new RelatedTo('user-auth'); $constraint->assert($this->buildToken()); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenSubjectDoesNotMatch(): void { + $constraint = new RelatedTo('user-auth'); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token is not related to the expected subject'); - $constraint = new RelatedTo('user-auth'); $constraint->assert($this->buildToken([RegisteredClaims::SUBJECT => 'password-recovery'])); } - /** @test */ + #[PHPUnit\Test] public function assertShouldNotRaiseExceptionWhenSubjectMatches(): void { $token = $this->buildToken([RegisteredClaims::SUBJECT => 'user-auth']); diff --git a/tests/Validation/Constraint/SignedWithTest.php b/tests/Validation/Constraint/SignedWithTest.php index ebbb43b8..87309edd 100644 --- a/tests/Validation/Constraint/SignedWithTest.php +++ b/tests/Validation/Constraint/SignedWithTest.php @@ -8,25 +8,22 @@ use Lcobucci\JWT\Token\Signature; use Lcobucci\JWT\Validation\Constraint\SignedWith; use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\MockObject\MockObject; -/** - * @covers \Lcobucci\JWT\Validation\Constraint\SignedWith - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * - * @uses \Lcobucci\JWT\Signer\Key\InMemory - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(SignedWith::class)] +#[PHPUnit\UsesClass(Signer\Key\InMemory::class)] +#[PHPUnit\UsesClass(Token\DataSet::class)] +#[PHPUnit\UsesClass(Token\Plain::class)] +#[PHPUnit\UsesClass(Token\Signature::class)] final class SignedWithTest extends ConstraintTestCase { - /** @var Signer&MockObject */ - private Signer $signer; + private Signer&MockObject $signer; private Signer\Key $key; private Signature $signature; - /** @before */ + #[PHPUnit\Before] public function createDependencies(): void { $this->signer = $this->createMock(Signer::class); @@ -36,53 +33,56 @@ public function createDependencies(): void $this->signature = new Signature('1234', '5678'); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenTokenIsNotAPlainToken(): void { + $constraint = new SignedWith($this->signer, $this->key); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('You should pass a plain token'); - $constraint = new SignedWith($this->signer, $this->key); $constraint->assert($this->createMock(Token::class)); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenSignerIsNotTheSame(): void { $token = $this->buildToken([], ['alg' => 'test'], $this->signature); - $this->signer->expects(self::never())->method('verify'); + $this->signer->expects($this->never())->method('verify'); + + $constraint = new SignedWith($this->signer, $this->key); $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('Token signer mismatch'); - $constraint = new SignedWith($this->signer, $this->key); $constraint->assert($token); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenSignatureIsInvalid(): void { $token = $this->buildToken([], ['alg' => 'RS256'], $this->signature); - $this->signer->expects(self::once()) + $this->signer->expects($this->once()) ->method('verify') ->with($this->signature->hash(), $token->payload(), $this->key) ->willReturn(false); + $constraint = new SignedWith($this->signer, $this->key); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('Token signature mismatch'); - $constraint = new SignedWith($this->signer, $this->key); $constraint->assert($token); } - /** @test */ + #[PHPUnit\Test] public function assertShouldNotRaiseExceptionWhenSignatureIsValid(): void { $token = $this->buildToken([], ['alg' => 'RS256'], $this->signature); - $this->signer->expects(self::once()) + $this->signer->expects($this->once()) ->method('verify') ->with($this->signature->hash(), $token->payload(), $this->key) ->willReturn(true); diff --git a/tests/Validation/Constraint/SignedWithUntilDateTest.php b/tests/Validation/Constraint/SignedWithUntilDateTest.php index 03980f23..77cdda6d 100644 --- a/tests/Validation/Constraint/SignedWithUntilDateTest.php +++ b/tests/Validation/Constraint/SignedWithUntilDateTest.php @@ -44,9 +44,6 @@ final class SignedWithUntilDateTest extends ConstraintTestCase #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenConstraintUsageIsNotValidAnymore(): void { - $this->expectException(ConstraintViolation::class); - $this->expectExceptionMessage('This constraint was only usable until 2023-11-19T21:45:10+00:00'); - $clock = new FrozenClock(new DateTimeImmutable('2023-11-19 22:45:10')); $constraint = new SignedWithUntilDate( @@ -56,44 +53,50 @@ public function assertShouldRaiseExceptionWhenConstraintUsageIsNotValidAnymore() $clock, ); + $this->expectException(ConstraintViolation::class); + $this->expectExceptionMessage('This constraint was only usable until 2023-11-19T21:45:10+00:00'); + $constraint->assert($this->issueToken(new FakeSigner('1'), InMemory::plainText('a'))); } #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenTokenIsNotAPlainToken(): void { - $this->expectException(ConstraintViolation::class); - $this->expectExceptionMessage('You should pass a plain token'); - $clock = new FrozenClock(new DateTimeImmutable('2023-11-19 22:45:10')); $constraint = new SignedWithUntilDate(new FakeSigner('1'), InMemory::plainText('a'), $clock->now(), $clock); + + $this->expectException(ConstraintViolation::class); + $this->expectExceptionMessage('You should pass a plain token'); + $constraint->assert($this->createMock(Token::class)); } #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenSignerIsNotTheSame(): void { - $this->expectException(ConstraintViolation::class); - $this->expectExceptionMessage('Token signer mismatch'); - $clock = new FrozenClock(new DateTimeImmutable('2023-11-19 22:45:10')); $key = InMemory::plainText('a'); $constraint = new SignedWithUntilDate(new FakeSigner('1'), $key, $clock->now(), $clock); + + $this->expectException(ConstraintViolation::class); + $this->expectExceptionMessage('Token signer mismatch'); + $constraint->assert($this->issueToken(new FakeSigner('2'), $key)); } #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenSignatureIsInvalid(): void { - $this->expectException(ConstraintViolation::class); - $this->expectExceptionMessage('Token signature mismatch'); - $clock = new FrozenClock(new DateTimeImmutable('2023-11-19 22:45:10')); $signer = new FakeSigner('1'); $constraint = new SignedWithUntilDate($signer, InMemory::plainText('a'), $clock->now(), $clock); + + $this->expectException(ConstraintViolation::class); + $this->expectExceptionMessage('Token signature mismatch'); + $constraint->assert($this->issueToken($signer, InMemory::plainText('b'))); } diff --git a/tests/Validation/Constraint/StrictValidAtTest.php b/tests/Validation/Constraint/StrictValidAtTest.php index 8041ff53..d94a639f 100644 --- a/tests/Validation/Constraint/StrictValidAtTest.php +++ b/tests/Validation/Constraint/StrictValidAtTest.php @@ -10,16 +10,14 @@ use Lcobucci\JWT\Validation\Constraint; use Lcobucci\JWT\Validation\Constraint\StrictValidAt; use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; -/** - * @covers \Lcobucci\JWT\Validation\Constraint\LeewayCannotBeNegative - * @covers \Lcobucci\JWT\Validation\ConstraintViolation - * @covers \Lcobucci\JWT\Validation\Constraint\StrictValidAt - * - * @uses \Lcobucci\JWT\Token\DataSet - * @uses \Lcobucci\JWT\Token\Plain - * @uses \Lcobucci\JWT\Token\Signature - */ +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\CoversClass(Constraint\LeewayCannotBeNegative::class)] +#[PHPUnit\CoversClass(StrictValidAt::class)] +#[PHPUnit\UsesClass(Token\DataSet::class)] +#[PHPUnit\UsesClass(Token\Plain::class)] +#[PHPUnit\UsesClass(Token\Signature::class)] final class StrictValidAtTest extends ValidAtTestCase { protected function buildValidAtConstraint(Clock $clock, ?DateInterval $leeway = null): Constraint @@ -27,47 +25,47 @@ protected function buildValidAtConstraint(Clock $clock, ?DateInterval $leeway = return new StrictValidAt($clock, $leeway); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenTokenIsNotAPlainToken(): void { + $constraint = $this->buildValidAtConstraint($this->clock); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('You should pass a plain token'); - $constraint = $this->buildValidAtConstraint($this->clock); $constraint->assert($this->createMock(Token::class)); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenIatClaimIsMissing(): void { + $constraint = $this->buildValidAtConstraint($this->clock); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('"Issued At" claim missing'); - $constraint = $this->buildValidAtConstraint($this->clock); $constraint->assert($this->buildToken()); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenNbfClaimIsMissing(): void { - $this->expectException(ConstraintViolation::class); - $this->expectExceptionMessage('"Not Before" claim missing'); - $now = $this->clock->now(); $claims = [ RegisteredClaims::ISSUED_AT => $now->modify('-5 seconds'), ]; $constraint = $this->buildValidAtConstraint($this->clock); + + $this->expectException(ConstraintViolation::class); + $this->expectExceptionMessage('"Not Before" claim missing'); + $constraint->assert($this->buildToken($claims)); } - /** @test */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenExpClaimIsMissing(): void { - $this->expectException(ConstraintViolation::class); - $this->expectExceptionMessage('"Expiration Time" claim missing'); - $now = $this->clock->now(); $claims = [ RegisteredClaims::ISSUED_AT => $now->modify('-5 seconds'), @@ -75,6 +73,10 @@ public function assertShouldRaiseExceptionWhenExpClaimIsMissing(): void ]; $constraint = $this->buildValidAtConstraint($this->clock); + + $this->expectException(ConstraintViolation::class); + $this->expectExceptionMessage('"Expiration Time" claim missing'); + $constraint->assert($this->buildToken($claims)); } } diff --git a/tests/Validation/Constraint/ValidAtTestCase.php b/tests/Validation/Constraint/ValidAtTestCase.php index 63c5959b..e43d0d27 100644 --- a/tests/Validation/Constraint/ValidAtTestCase.php +++ b/tests/Validation/Constraint/ValidAtTestCase.php @@ -11,12 +11,13 @@ use Lcobucci\JWT\Validation\Constraint; use Lcobucci\JWT\Validation\Constraint\LeewayCannotBeNegative; use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; abstract class ValidAtTestCase extends ConstraintTestCase { protected Clock $clock; - /** @before */ + #[PHPUnit\Before] final public function createDependencies(): void { $this->clock = new FrozenClock(new DateTimeImmutable()); @@ -24,7 +25,7 @@ final public function createDependencies(): void abstract protected function buildValidAtConstraint(Clock $clock, ?DateInterval $leeway = null): Constraint; - /** @test */ + #[PHPUnit\Test] final public function constructShouldRaiseExceptionOnNegativeLeeway(): void { $leeway = new DateInterval('PT30S'); @@ -36,7 +37,7 @@ final public function constructShouldRaiseExceptionOnNegativeLeeway(): void $this->buildValidAtConstraint($this->clock, $leeway); } - /** @test */ + #[PHPUnit\Test] final public function assertShouldRaiseExceptionWhenTokenIsExpired(): void { $now = $this->clock->now(); @@ -47,14 +48,15 @@ final public function assertShouldRaiseExceptionWhenTokenIsExpired(): void RegisteredClaims::EXPIRATION_TIME => $now->modify('-10 seconds'), ]; + $constraint = $this->buildValidAtConstraint($this->clock); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token is expired'); - $constraint = $this->buildValidAtConstraint($this->clock); $constraint->assert($this->buildToken($claims)); } - /** @test */ + #[PHPUnit\Test] final public function assertShouldRaiseExceptionWhenMinimumTimeIsNotMet(): void { $now = $this->clock->now(); @@ -65,14 +67,15 @@ final public function assertShouldRaiseExceptionWhenMinimumTimeIsNotMet(): void RegisteredClaims::EXPIRATION_TIME => $now->modify('+60 seconds'), ]; + $constraint = $this->buildValidAtConstraint($this->clock); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token cannot be used yet'); - $constraint = $this->buildValidAtConstraint($this->clock); $constraint->assert($this->buildToken($claims)); } - /** @test */ + #[PHPUnit\Test] final public function assertShouldRaiseExceptionWhenTokenWasIssuedInTheFuture(): void { $now = $this->clock->now(); @@ -83,14 +86,15 @@ final public function assertShouldRaiseExceptionWhenTokenWasIssuedInTheFuture(): RegisteredClaims::EXPIRATION_TIME => $now->modify('+60 seconds'), ]; + $constraint = $this->buildValidAtConstraint($this->clock); + $this->expectException(ConstraintViolation::class); $this->expectExceptionMessage('The token was issued in the future'); - $constraint = $this->buildValidAtConstraint($this->clock); $constraint->assert($this->buildToken($claims)); } - /** @test */ + #[PHPUnit\Test] final public function assertShouldNotRaiseExceptionWhenLeewayIsUsed(): void { $now = $this->clock->now(); @@ -107,7 +111,7 @@ final public function assertShouldNotRaiseExceptionWhenLeewayIsUsed(): void $this->addToAssertionCount(1); } - /** @test */ + #[PHPUnit\Test] final public function assertShouldNotRaiseExceptionWhenTokenIsUsedInTheRightMoment(): void { $constraint = $this->buildValidAtConstraint($this->clock); diff --git a/tests/Validation/ConstraintViolationTest.php b/tests/Validation/ConstraintViolationTest.php index cd4da00d..c8833ccb 100644 --- a/tests/Validation/ConstraintViolationTest.php +++ b/tests/Validation/ConstraintViolationTest.php @@ -5,19 +5,14 @@ use Lcobucci\JWT\Validation\Constraint\IdentifiedBy; use Lcobucci\JWT\Validation\ConstraintViolation; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; -/** @coversDefaultClass \Lcobucci\JWT\Validation\ConstraintViolation */ +#[PHPUnit\CoversClass(ConstraintViolation::class)] +#[PHPUnit\UsesClass(IdentifiedBy::class)] final class ConstraintViolationTest extends TestCase { - /** - * @test - * - * @covers ::__construct - * @covers ::error - * - * @uses \Lcobucci\JWT\Validation\Constraint\IdentifiedBy - */ + #[PHPUnit\Test] public function errorShouldConfigureMessageAndConstraint(): void { $violation = ConstraintViolation::error('testing', new IdentifiedBy('token id')); diff --git a/tests/Validation/RequiredConstraintsViolatedTest.php b/tests/Validation/RequiredConstraintsViolatedTest.php index 489a7112..ede2e383 100644 --- a/tests/Validation/RequiredConstraintsViolatedTest.php +++ b/tests/Validation/RequiredConstraintsViolatedTest.php @@ -5,21 +5,14 @@ use Lcobucci\JWT\Validation\ConstraintViolation; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; -/** @coversDefaultClass \Lcobucci\JWT\Validation\RequiredConstraintsViolated */ +#[PHPUnit\CoversClass(RequiredConstraintsViolated::class)] +#[PHPUnit\UsesClass(ConstraintViolation::class)] final class RequiredConstraintsViolatedTest extends TestCase { - /** - * @test - * - * @covers ::fromViolations - * @covers ::__construct - * @covers ::buildMessage - * @covers ::violations - * - * @uses \Lcobucci\JWT\Validation\ConstraintViolation - */ + #[PHPUnit\Test] public function fromViolationsShouldConfigureMessageAndViolationList(): void { $violation = new ConstraintViolation('testing'); diff --git a/tests/Validation/ValidatorTest.php b/tests/Validation/ValidatorTest.php index da95a13e..b84d2c90 100644 --- a/tests/Validation/ValidatorTest.php +++ b/tests/Validation/ValidatorTest.php @@ -9,30 +9,24 @@ use Lcobucci\JWT\Validation\NoConstraintsGiven; use Lcobucci\JWT\Validation\RequiredConstraintsViolated; use Lcobucci\JWT\Validation\Validator; +use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -/** - * @coversDefaultClass \Lcobucci\JWT\Validation\Validator - * - * @uses \Lcobucci\JWT\Validation\ConstraintViolation - * @uses \Lcobucci\JWT\Validation\RequiredConstraintsViolated - */ +#[PHPUnit\CoversClass(Validator::class)] +#[PHPUnit\UsesClass(ConstraintViolation::class)] +#[PHPUnit\UsesClass(RequiredConstraintsViolated::class)] final class ValidatorTest extends TestCase { private Token&MockObject $token; - /** @before */ + #[PHPUnit\Before] public function createDependencies(): void { $this->token = $this->createMock(Token::class); } - /** - * @test - * - * @covers ::assert - */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenNoConstraintIsGiven(): void { $validator = new Validator(); @@ -42,22 +36,17 @@ public function assertShouldRaiseExceptionWhenNoConstraintIsGiven(): void $validator->assert($this->token, ...[]); } - /** - * @test - * - * @covers ::assert - * @covers ::checkConstraint - */ + #[PHPUnit\Test] public function assertShouldRaiseExceptionWhenAtLeastOneConstraintFails(): void { $failedConstraint = $this->createMock(Constraint::class); $successfulConstraint = $this->createMock(Constraint::class); - $failedConstraint->expects(self::once()) + $failedConstraint->expects($this->once()) ->method('assert') ->willThrowException(new ConstraintViolation()); - $successfulConstraint->expects(self::once()) + $successfulConstraint->expects($this->once()) ->method('assert'); $validator = new Validator(); @@ -72,16 +61,11 @@ public function assertShouldRaiseExceptionWhenAtLeastOneConstraintFails(): void ); } - /** - * @test - * - * @covers ::assert - * @covers ::checkConstraint - */ + #[PHPUnit\Test] public function assertShouldNotRaiseExceptionWhenNoConstraintFails(): void { $constraint = $this->createMock(Constraint::class); - $constraint->expects(self::once())->method('assert'); + $constraint->expects($this->once())->method('assert'); $validator = new Validator(); @@ -89,35 +73,27 @@ public function assertShouldNotRaiseExceptionWhenNoConstraintFails(): void $this->addToAssertionCount(1); } - /** - * @test - * - * @covers ::validate - */ + #[PHPUnit\Test] public function validateShouldRaiseExceptionWhenNoConstraintIsGiven(): void { $validator = new Validator(); $this->expectException(NoConstraintsGiven::class); - $validator->validate($this->token, ...[]); + $validator->validate($this->token); } - /** - * @test - * - * @covers ::validate - */ + #[PHPUnit\Test] public function validateShouldReturnFalseWhenAtLeastOneConstraintFails(): void { $failedConstraint = $this->createMock(Constraint::class); $successfulConstraint = $this->createMock(Constraint::class); - $failedConstraint->expects(self::once()) + $failedConstraint->expects($this->once()) ->method('assert') ->willThrowException(new ConstraintViolation()); - $successfulConstraint->expects(self::never()) + $successfulConstraint->expects($this->never()) ->method('assert'); $validator = new Validator(); @@ -131,15 +107,11 @@ public function validateShouldReturnFalseWhenAtLeastOneConstraintFails(): void ); } - /** - * @test - * - * @covers ::validate - */ + #[PHPUnit\Test] public function validateShouldReturnTrueWhenNoConstraintFails(): void { $constraint = $this->createMock(Constraint::class); - $constraint->expects(self::once())->method('assert'); + $constraint->expects($this->once())->method('assert'); $validator = new Validator(); self::assertTrue($validator->validate($this->token, $constraint));