diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 88ff0504..23013c32 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -10,7 +10,7 @@ on: jobs: php-tests: - runs-on: ${{ matrix.os }} + runs-on: ubuntu-latest strategy: fail-fast: false @@ -18,19 +18,17 @@ jobs: php: [8.1, 8.2, 8.3] laravel: [10.*, 11.*] stability: [prefer-lowest, prefer-stable] - os: [ubuntu-latest] coverage: [none] exclude: - laravel: 11.* php: 8.1 include: - - php: 8.1 - laravel: 10.* + - php: 8.3 + laravel: 11.* stability: prefer-stable - os: ubuntu-latest coverage: xdebug - name: '[PHP ${{ matrix.php }}] [Laravel ${{ matrix.laravel }} - ${{ matrix.stability }}] [${{ matrix.coverage }} coverage]' + name: 'P${{ matrix.php }} L${{ matrix.laravel }} ${{ matrix.stability }} c:${{ matrix.coverage }}' steps: - name: Checkout code @@ -41,7 +39,7 @@ jobs: with: php-version: ${{ matrix.php }} extensions: mbstring json openssl - coverage: xdebug + coverage: ${{ matrix.coverage }} ini-values: memory_limit=-1 - name: Setup Problem Matchers @@ -53,14 +51,6 @@ jobs: id: composer-cache run: echo "dir=$(composer config cache-dir)" >> $GITHUB_OUTPUT - - name: Set phpunit for PHP >= 8 but Laravel < 11 - run: composer require phpunit/phpunit:^9.4 --no-interaction --no-progress --no-update - if: matrix.php >= 8 || matrix.laravel != '11.*' - - - name: Set phpunit for Laravel >= 11 - run: composer require phpunit/phpunit:^10 --no-interaction --no-progress --no-update - if: matrix.laravel == '11.*' - - name: Set Minimum Laravel ${{ matrix.laravel }} Versions run: | composer require "illuminate/contracts:${{ matrix.laravel }}" --no-interaction --no-progress --no-update @@ -79,7 +69,12 @@ jobs: - name: Install dependencies run: composer update --no-progress --${{ matrix.stability }} --prefer-dist --no-interaction --no-suggest - - name: Execute tests + - name: Execute tests (without coverage) + if: matrix.coverage != 'xdebug' + run: composer test -- --no-coverage + + - name: Execute tests (with coverage) + if: matrix.coverage == 'xdebug' run: composer test:ci - name: Upload coverage diff --git a/composer.json b/composer.json index 48fcba46..e5438999 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,7 @@ "orchestra/testbench": "^8|^9", "mockery/mockery": "^1.4.4", "phpstan/phpstan": "^1", - "phpunit/phpunit": "^9.4|^10" + "phpunit/phpunit": "^10.5|^11" }, "autoload": { "psr-4": { diff --git a/tests/BlacklistTest.php b/tests/BlacklistTest.php index 76ac86d9..aaea1adb 100644 --- a/tests/BlacklistTest.php +++ b/tests/BlacklistTest.php @@ -24,6 +24,7 @@ use PHPOpenSourceSaver\JWTAuth\Contracts\Providers\Storage; use PHPOpenSourceSaver\JWTAuth\Payload; use PHPOpenSourceSaver\JWTAuth\Validators\PayloadValidator; +use PHPUnit\Framework\Attributes\DataProvider; class BlacklistTest extends AbstractTestCase { @@ -42,8 +43,7 @@ public function setUp(): void $this->validator = \Mockery::mock(PayloadValidator::class); } - /** @test */ - public function itShouldAddAValidTokenToTheBlacklist() + public function testItShouldAddAValidTokenToTheBlacklist() { $claims = [ new Subject(1), @@ -74,8 +74,7 @@ public function itShouldAddAValidTokenToTheBlacklist() $this->blacklist->setRefreshTTL($refreshTTL)->add($payload); } - /** @test */ - public function itShouldAddATokenWithNoExpToTheBlacklistForever() + public function testItShouldAddATokenWithNoExpToTheBlacklistForever() { $claims = [ new Subject(1), @@ -94,8 +93,7 @@ public function itShouldAddATokenWithNoExpToTheBlacklistForever() $this->blacklist->add($payload); } - /** @test */ - public function itShouldReturnTrueWhenAddingAnExpiredTokenToTheBlacklist() + public function testItShouldReturnTrueWhenAddingAnExpiredTokenToTheBlacklist() { $claims = [ new Subject(1), @@ -125,8 +123,7 @@ public function itShouldReturnTrueWhenAddingAnExpiredTokenToTheBlacklist() $this->assertTrue($this->blacklist->setRefreshTTL($refreshTTL)->add($payload)); } - /** @test */ - public function itShouldReturnTrueEarlyWhenAddingAnItemAndItAlreadyExists() + public function testItShouldReturnTrueEarlyWhenAddingAnItemAndItAlreadyExists() { $claims = [ new Subject(1), @@ -156,8 +153,7 @@ public function itShouldReturnTrueEarlyWhenAddingAnItemAndItAlreadyExists() $this->assertTrue($this->blacklist->setRefreshTTL($refreshTTL)->add($payload)); } - /** @test */ - public function itShouldCheckWhetherATokenHasBeenBlacklisted() + public function testItShouldCheckWhetherATokenHasBeenBlacklisted() { $claims = [ new Subject(1), @@ -190,12 +186,8 @@ public static function blacklist_provider() ]; } - /** - * @test - * - * @dataProvider blacklist_provider - */ - public function itShouldCheckWhetherATokenHasNotBeenBlacklisted($result) + #[DataProvider('blacklist_provider')] + public function testItShouldCheckWhetherATokenHasNotBeenBlacklisted($result) { $claims = [ new Subject(1), @@ -216,8 +208,7 @@ public function itShouldCheckWhetherATokenHasNotBeenBlacklisted($result) $this->assertFalse($this->blacklist->has($payload)); } - /** @test */ - public function itShouldCheckWhetherATokenHasBeenBlacklistedForever() + public function testItShouldCheckWhetherATokenHasBeenBlacklistedForever() { $claims = [ new Subject(1), @@ -238,8 +229,7 @@ public function itShouldCheckWhetherATokenHasBeenBlacklistedForever() $this->assertTrue($this->blacklist->has($payload)); } - /** @test */ - public function itShouldCheckWhetherATokenHasBeenBlacklistedWhenTheTokenIsNotBlacklisted() + public function testItShouldCheckWhetherATokenHasBeenBlacklistedWhenTheTokenIsNotBlacklisted() { $claims = [ new Subject(1), @@ -260,8 +250,7 @@ public function itShouldCheckWhetherATokenHasBeenBlacklistedWhenTheTokenIsNotBla $this->assertFalse($this->blacklist->has($payload)); } - /** @test */ - public function itShouldRemoveATokenFromTheBlacklist() + public function testItShouldRemoveATokenFromTheBlacklist() { $claims = [ new Subject(1), @@ -281,8 +270,7 @@ public function itShouldRemoveATokenFromTheBlacklist() $this->assertTrue($this->blacklist->remove($payload)); } - /** @test */ - public function itShouldSetACustomUniqueKeyForTheBlacklist() + public function testItShouldSetACustomUniqueKeyForTheBlacklist() { $claims = [ new Subject(1), @@ -304,22 +292,19 @@ public function itShouldSetACustomUniqueKeyForTheBlacklist() $this->assertSame(1, $this->blacklist->getKey($payload)); } - /** @test */ - public function itShouldEmptyTheBlacklist() + public function testItShouldEmptyTheBlacklist() { $this->storage->shouldReceive('flush'); $this->assertTrue($this->blacklist->clear()); } - /** @test */ - public function itShouldSetAndGetTheBlacklistGracePeriod() + public function testItShouldSetAndGetTheBlacklistGracePeriod() { $this->assertInstanceOf(Blacklist::class, $this->blacklist->setGracePeriod(15)); $this->assertSame(15, $this->blacklist->getGracePeriod()); } - /** @test */ - public function itShouldSetAndGetTheBlacklistRefreshTtl() + public function testItShouldSetAndGetTheBlacklistRefreshTtl() { $this->assertInstanceOf(Blacklist::class, $this->blacklist->setRefreshTTL(15)); $this->assertSame(15, $this->blacklist->getRefreshTTL()); diff --git a/tests/Claims/ClaimTest.php b/tests/Claims/ClaimTest.php index 4a0bc736..bca55536 100644 --- a/tests/Claims/ClaimTest.php +++ b/tests/Claims/ClaimTest.php @@ -31,8 +31,7 @@ public function setUp(): void $this->claim = new Expiration($this->testNowTimestamp); } - /** @test */ - public function itShouldThrowAnExceptionWhenPassingAnInvalidValue() + public function testItShouldThrowAnExceptionWhenPassingAnInvalidValue() { $this->expectException(InvalidClaimException::class); $this->expectExceptionMessage('Invalid value provided for claim [exp]'); @@ -40,26 +39,22 @@ public function itShouldThrowAnExceptionWhenPassingAnInvalidValue() $this->claim->setValue('foo'); } - /** @test */ - public function itShouldConvertTheClaimToAnArray() + public function testItShouldConvertTheClaimToAnArray() { $this->assertSame(['exp' => $this->testNowTimestamp], $this->claim->toArray()); } - /** @test */ - public function itShouldGetTheClaimAsAString() + public function testItShouldGetTheClaimAsAString() { $this->assertJsonStringEqualsJsonString((string) $this->claim, $this->claim->toJson()); } - /** @test */ - public function itShouldGetTheObjectAsJson() + public function testItShouldGetTheObjectAsJson() { $this->assertJsonStringEqualsJsonString(json_encode($this->claim), $this->claim->toJson()); } - /** @test */ - public function itShouldImplementArrayable() + public function testItShouldImplementArrayable() { $this->assertInstanceOf(Arrayable::class, $this->claim); } diff --git a/tests/Claims/CollectionTest.php b/tests/Claims/CollectionTest.php index 602e09e0..082e4aa9 100644 --- a/tests/Claims/CollectionTest.php +++ b/tests/Claims/CollectionTest.php @@ -23,8 +23,7 @@ class CollectionTest extends AbstractTestCase { - /** @test */ - public function itShouldSanitizeTheClaimsToAssociativeArray() + public function testItShouldSanitizeTheClaimsToAssociativeArray() { $collection = $this->getCollection(); @@ -45,8 +44,7 @@ private function getCollection() return new Collection($claims); } - /** @test */ - public function itShouldDetermineIfACollectionContainsAllTheGivenClaims() + public function testItShouldDetermineIfACollectionContainsAllTheGivenClaims() { $collection = $this->getCollection(); @@ -58,8 +56,7 @@ public function itShouldDetermineIfACollectionContainsAllTheGivenClaims() $this->assertTrue($collection->hasAllClaims(['sub', 'iss', 'exp', 'nbf', 'iat', 'jti'])); } - /** @test */ - public function itShouldGetAClaimInstanceByName() + public function testItShouldGetAClaimInstanceByName() { $collection = $this->getCollection(); diff --git a/tests/Claims/DatetimeClaimTest.php b/tests/Claims/DatetimeClaimTest.php index 52475822..52a2e8c7 100644 --- a/tests/Claims/DatetimeClaimTest.php +++ b/tests/Claims/DatetimeClaimTest.php @@ -52,10 +52,10 @@ public function setUp(): void ]; } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldHandleCarbonClaims() + public function testItShouldHandleCarbonClaims() { $testCarbon = Carbon::createFromTimestampUTC($this->testNowTimestamp); $testCarbonCopy = clone $testCarbon; @@ -79,8 +79,7 @@ public function itShouldHandleCarbonClaims() $this->assertEquals($payloadTimestamp, $payloadDatetime); } - /** @test */ - public function itShouldHandleDatetimeClaims() + public function testItShouldHandleDatetimeClaims() { $testDateTime = \DateTime::createFromFormat('U', $this->testNowTimestamp); $testDateTimeCopy = clone $testDateTime; @@ -103,8 +102,7 @@ public function itShouldHandleDatetimeClaims() $this->assertEquals($payloadTimestamp, $payloadDatetime); } - /** @test */ - public function itShouldHandleDatetimeImmutableClaims() + public function testItShouldHandleDatetimeImmutableClaims() { $testDateTimeImmutable = \DateTimeImmutable::createFromFormat('U', (string) $this->testNowTimestamp); @@ -126,10 +124,10 @@ public function itShouldHandleDatetimeImmutableClaims() $this->assertEquals($payloadTimestamp, $payloadDatetime); } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldHandleDatetintervalClaims() + public function testItShouldHandleDatetintervalClaims() { $testDateInterval = new \DateInterval('PT1H'); diff --git a/tests/Claims/FactoryTest.php b/tests/Claims/FactoryTest.php index 21bcb338..d0a1c3f4 100644 --- a/tests/Claims/FactoryTest.php +++ b/tests/Claims/FactoryTest.php @@ -35,28 +35,24 @@ public function setUp(): void $this->factory = new Factory(Request::create('/foo', 'GET')); } - /** @test */ - public function itShouldSetTheRequest() + public function testItShouldSetTheRequest() { $factory = $this->factory->setRequest(Request::create('/bar', 'GET')); $this->assertInstanceOf(Factory::class, $factory); } - /** @test */ - public function itShouldSetTheTtl() + public function testItShouldSetTheTtl() { $this->assertInstanceOf(Factory::class, $this->factory->setTTL(30)); } - /** @test */ - public function itShouldGetTheTtl() + public function testItShouldGetTheTtl() { $this->factory->setTTL($ttl = 30); $this->assertSame($ttl, $this->factory->getTTL()); } - /** @test */ - public function itShouldGetADefinedClaimInstanceWhenPassingANameAndValue() + public function testItShouldGetADefinedClaimInstanceWhenPassingANameAndValue() { $this->assertInstanceOf(Subject::class, $this->factory->get('sub', 1)); $this->assertInstanceOf(Issuer::class, $this->factory->get('iss', 'http://example.com')); @@ -66,14 +62,12 @@ public function itShouldGetADefinedClaimInstanceWhenPassingANameAndValue() $this->assertInstanceOf(JwtId::class, $this->factory->get('jti', 'foo')); } - /** @test */ - public function itShouldGetACustomClaimInstanceWhenPassingANonDefinedNameAndValue() + public function testItShouldGetACustomClaimInstanceWhenPassingANonDefinedNameAndValue() { $this->assertInstanceOf(Custom::class, $this->factory->get('foo', ['bar'])); } - /** @test */ - public function itShouldMakeAClaimInstanceWithAValue() + public function testItShouldMakeAClaimInstanceWithAValue() { $iat = $this->factory->make('iat'); $this->assertSame($iat->getValue(), $this->testNowTimestamp); @@ -95,8 +89,7 @@ public function itShouldMakeAClaimInstanceWithAValue() $this->assertInstanceOf(JwtId::class, $jti); } - /** @test */ - public function itShouldExtendClaimFactoryToAddACustomClaim() + public function testItShouldExtendClaimFactoryToAddACustomClaim() { $this->factory->extend('foo', Foo::class); diff --git a/tests/Claims/IssuedAtTest.php b/tests/Claims/IssuedAtTest.php index 7b44b988..8e981cfd 100644 --- a/tests/Claims/IssuedAtTest.php +++ b/tests/Claims/IssuedAtTest.php @@ -18,8 +18,7 @@ class IssuedAtTest extends AbstractTestCase { - /** @test */ - public function itShouldThrowAnExceptionWhenPassingAFutureTimestamp() + public function testItShouldThrowAnExceptionWhenPassingAFutureTimestamp() { $this->expectException(InvalidClaimException::class); $this->expectExceptionMessage('Invalid value provided for claim [iat]'); diff --git a/tests/Claims/NotBeforeTest.php b/tests/Claims/NotBeforeTest.php index 10983b46..a90daf65 100644 --- a/tests/Claims/NotBeforeTest.php +++ b/tests/Claims/NotBeforeTest.php @@ -18,8 +18,7 @@ class NotBeforeTest extends AbstractTestCase { - /** @test */ - public function itShouldThrowAnExceptionWhenPassingAnInvalidValue() + public function testItShouldThrowAnExceptionWhenPassingAnInvalidValue() { $this->expectException(InvalidClaimException::class); $this->expectExceptionMessage('Invalid value provided for claim [nbf]'); diff --git a/tests/DefaultConfigValuesTest.php b/tests/DefaultConfigValuesTest.php index ea4620fb..92c2476c 100644 --- a/tests/DefaultConfigValuesTest.php +++ b/tests/DefaultConfigValuesTest.php @@ -27,40 +27,34 @@ public function setUp(): void $this->configuration = include __DIR__.'/../config/config.php'; } - /** @test */ - public function secretShouldBeNull() + public function testSecretShouldBeNull() { $this->assertNull($this->configuration['secret']); } - /** @test */ - public function keysShouldBeNull() + public function testKeysShouldBeNull() { $this->assertNull($this->configuration['keys']['public']); $this->assertNull($this->configuration['keys']['private']); $this->assertNull($this->configuration['keys']['passphrase']); } - /** @test */ - public function ttlShouldBeSet() + public function testTtlShouldBeSet() { $this->assertEquals(60, $this->configuration['ttl']); } - /** @test */ - public function refreshTtlShouldBeSet() + public function testRefreshTtlShouldBeSet() { $this->assertEquals(20160, $this->configuration['refresh_ttl']); } - /** @test */ - public function algoShouldBeHs256() + public function testAlgoShouldBeHs256() { $this->assertEquals('HS256', $this->configuration['algo']); } - /** @test */ - public function requiredClaimsShouldBeSet() + public function testRequiredClaimsShouldBeSet() { $this->assertIsArray($this->configuration['required_claims']); $this->assertCount(6, $this->configuration['required_claims']); @@ -73,51 +67,43 @@ public function requiredClaimsShouldBeSet() $this->assertTrue(in_array('jti', $this->configuration['required_claims'])); } - /** @test */ - public function persistedClaimsShouldBeEmpty() + public function testPersistedClaimsShouldBeEmpty() { $this->assertIsArray($this->configuration['persistent_claims']); $this->assertCount(0, $this->configuration['persistent_claims']); } - /** @test */ - public function subjectShouldBeLocked() + public function testSubjectShouldBeLocked() { $this->assertTrue($this->configuration['lock_subject']); } - /** @test */ - public function leewayShouldBeSet() + public function testLeewayShouldBeSet() { $this->assertEquals(0, $this->configuration['leeway']); } - /** @test */ - public function blacklistShouldBeEnabled() + public function testBlacklistShouldBeEnabled() { $this->assertTrue($this->configuration['blacklist_enabled']); } - /** @test */ - public function blacklistGracePeriodShouldBeSet() + public function testBlacklistGracePeriodShouldBeSet() { $this->assertEquals(0, $this->configuration['blacklist_grace_period']); } - /** @test */ - public function showBlackListExceptionShouldBeDisabled() + public function testShowBlackListExceptionShouldBeDisabled() { $this->assertTrue($this->configuration['show_black_list_exception']); } - /** @test */ - public function decryptCookiesShouldBeDisabled() + public function testDecryptCookiesShouldBeDisabled() { $this->assertFalse($this->configuration['decrypt_cookies']); } - /** @test */ - public function providersShouldBeSet() + public function testProvidersShouldBeSet() { $this->assertEquals(Lcobucci::class, $this->configuration['providers']['jwt']); $this->assertEquals(AuthIlluminate::class, $this->configuration['providers']['auth']); diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php index 277bcfe8..d40dca8a 100644 --- a/tests/FactoryTest.php +++ b/tests/FactoryTest.php @@ -44,8 +44,7 @@ public function setUp(): void $this->factory = new Factory($this->claimFactory, $this->validator); } - /** @test */ - public function itShouldReturnAPayloadWhenPassingAnArrayOfClaims() + public function testItShouldReturnAPayloadWhenPassingAnArrayOfClaims() { $expTime = $this->testNowTimestamp + 3600; @@ -85,10 +84,10 @@ public function itShouldReturnAPayloadWhenPassingAnArrayOfClaims() $this->assertInstanceOf(Payload::class, $payload); } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldReturnAPayloadWhenChainingClaimMethods() + public function testItShouldReturnAPayloadWhenChainingClaimMethods() { $this->claimFactory->shouldReceive('get')->twice()->with('sub', 1)->andReturn(new Subject(1)); $this->claimFactory->shouldReceive('get')->twice()->with('foo', 'baz')->andReturn(new Custom('foo', 'baz')); @@ -116,10 +115,10 @@ public function itShouldReturnAPayloadWhenChainingClaimMethods() $this->assertInstanceOf(Payload::class, $payload); } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldReturnAPayloadWhenPassingMiltidimensionalArrayAsCustomClaimToMakeMethod() + public function testItShouldReturnAPayloadWhenPassingMiltidimensionalArrayAsCustomClaimToMakeMethod() { // these are added from default claims $this->claimFactory->shouldReceive('make')->twice()->with('iss')->andReturn(new Issuer('/foo')); @@ -150,10 +149,10 @@ public function itShouldReturnAPayloadWhenPassingMiltidimensionalArrayAsCustomCl $this->assertInstanceOf(Payload::class, $payload); } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldExcludeTheExpClaimWhenSettingTtlToNull() + public function testItShouldExcludeTheExpClaimWhenSettingTtlToNull() { // these are added from default claims $this->claimFactory->shouldReceive('make')->twice()->with('iss')->andReturn(new Issuer('/foo')); @@ -180,8 +179,7 @@ public function itShouldExcludeTheExpClaimWhenSettingTtlToNull() $this->assertInstanceOf(Payload::class, $payload); } - /** @test */ - public function itShouldExcludeClaimsFromPreviousPayloads() + public function testItShouldExcludeClaimsFromPreviousPayloads() { $validator = new PayloadValidator(); $factory = new Factory($this->claimFactory, $validator); @@ -209,18 +207,17 @@ public function itShouldExcludeClaimsFromPreviousPayloads() $this->assertFalse($payload->hasKey('baz')); } - /** @test */ - public function itShouldSetTheDefaultClaims() + public function testItShouldSetTheDefaultClaims() { $this->factory->setDefaultClaims(['sub', 'iat']); $this->assertSame($this->factory->getDefaultClaims(), ['sub', 'iat']); } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldGetPayloadWithAPredefinedCollectionOfClaims() + public function testItShouldGetPayloadWithAPredefinedCollectionOfClaims() { $claims = [ new Subject(1), @@ -240,8 +237,7 @@ public function itShouldGetPayloadWithAPredefinedCollectionOfClaims() $this->assertSame($payload->get('sub'), 1); } - /** @test */ - public function itShouldGetTheValidator() + public function testItShouldGetTheValidator() { $this->assertInstanceOf(PayloadValidator::class, $this->factory->validator()); } diff --git a/tests/Http/ParserTest.php b/tests/Http/ParserTest.php index 2a36c9ba..c896079e 100644 --- a/tests/Http/ParserTest.php +++ b/tests/Http/ParserTest.php @@ -26,11 +26,11 @@ use PHPOpenSourceSaver\JWTAuth\Http\Parser\QueryString; use PHPOpenSourceSaver\JWTAuth\Http\Parser\RouteParams; use PHPOpenSourceSaver\JWTAuth\Test\AbstractTestCase; +use PHPUnit\Framework\Attributes\DataProvider; class ParserTest extends AbstractTestCase { - /** @test */ - public function itShouldReturnTheTokenFromTheAuthorizationHeader() + public function testItShouldReturnTheTokenFromTheAuthorizationHeader() { $request = Request::create('foo', 'POST'); $request->headers->set('Authorization', 'Bearer foobar'); @@ -48,8 +48,7 @@ public function itShouldReturnTheTokenFromTheAuthorizationHeader() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnTheTokenFromThePrefixedAuthenticationHeader() + public function testItShouldReturnTheTokenFromThePrefixedAuthenticationHeader() { $request = Request::create('foo', 'POST'); $request->headers->set('Authorization', 'Custom foobar'); @@ -67,8 +66,7 @@ public function itShouldReturnTheTokenFromThePrefixedAuthenticationHeader() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnTheTokenFromTheCustomAuthenticationHeader() + public function testItShouldReturnTheTokenFromTheCustomAuthenticationHeader() { $request = Request::create('foo', 'POST'); $request->headers->set('custom_authorization', 'Bearer foobar'); @@ -86,8 +84,7 @@ public function itShouldReturnTheTokenFromTheCustomAuthenticationHeader() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnTheTokenFromTheAltAuthorizationHeaders() + public function testItShouldReturnTheTokenFromTheAltAuthorizationHeaders() { $request1 = Request::create('foo', 'POST'); $request1->server->set('HTTP_AUTHORIZATION', 'Bearer foobar'); @@ -110,8 +107,7 @@ public function itShouldReturnTheTokenFromTheAltAuthorizationHeaders() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldNotStripTrailingHyphensFromTheAuthorizationHeader() + public function testItShouldNotStripTrailingHyphensFromTheAuthorizationHeader() { $request = Request::create('foo', 'POST'); $request->headers->set('Authorization', 'Bearer foobar--'); @@ -129,12 +125,8 @@ public function itShouldNotStripTrailingHyphensFromTheAuthorizationHeader() $this->assertTrue($parser->hasToken()); } - /** - * @test - * - * @dataProvider whitespaceProvider - */ - public function itShouldHandleExcessWhitespaceFromTheAuthorizationHeader($whitespace) + #[DataProvider('whitespaceProvider')] + public function testItShouldHandleExcessWhitespaceFromTheAuthorizationHeader($whitespace) { $request = Request::create('foo', 'POST'); $request->headers->set('Authorization', "Bearer{$whitespace}foobar{$whitespace}"); @@ -167,8 +159,7 @@ public static function whitespaceProvider() ]; } - /** @test */ - public function itShouldReturnTheTokenFromQueryString() + public function testItShouldReturnTheTokenFromQueryString() { $request = Request::create('foo', 'GET', ['token' => 'foobar']); @@ -184,8 +175,7 @@ public function itShouldReturnTheTokenFromQueryString() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnTheTokenFromTheCustomQueryString() + public function testItShouldReturnTheTokenFromTheCustomQueryString() { $request = Request::create('foo', 'GET', ['custom_token_key' => 'foobar']); @@ -201,8 +191,7 @@ public function itShouldReturnTheTokenFromTheCustomQueryString() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnTheTokenFromTheQueryStringNotTheInputSource() + public function testItShouldReturnTheTokenFromTheQueryStringNotTheInputSource() { $request = Request::create('foo?token=foobar', 'POST', [], [], [], [], json_encode(['token' => 'foobarbaz'])); @@ -218,8 +207,7 @@ public function itShouldReturnTheTokenFromTheQueryStringNotTheInputSource() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnTheTokenFromTheCustomQueryStringNotTheCustomInputSource() + public function testItShouldReturnTheTokenFromTheCustomQueryStringNotTheCustomInputSource() { $request = Request::create('foo?custom_token_key=foobar', 'POST', [], [], [], [], json_encode(['custom_token_key' => 'foobarbaz'])); @@ -235,8 +223,7 @@ public function itShouldReturnTheTokenFromTheCustomQueryStringNotTheCustomInputS $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnTheTokenFromInputSource() + public function testItShouldReturnTheTokenFromInputSource() { $request = Request::create('foo', 'POST', [], [], [], [], json_encode(['token' => 'foobar'])); $request->headers->set('Content-Type', 'application/json'); @@ -253,8 +240,7 @@ public function itShouldReturnTheTokenFromInputSource() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnTheTokenFromTheCustomInputSource() + public function testItShouldReturnTheTokenFromTheCustomInputSource() { $request = Request::create('foo', 'POST', [], [], [], [], json_encode(['custom_token_key' => 'foobar'])); $request->headers->set('Content-Type', 'application/json'); @@ -271,8 +257,7 @@ public function itShouldReturnTheTokenFromTheCustomInputSource() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnTheTokenFromAnUnencryptedCookie() + public function testItShouldReturnTheTokenFromAnUnencryptedCookie() { $request = Request::create('foo', 'POST', [], ['token' => 'foobar']); @@ -289,8 +274,7 @@ public function itShouldReturnTheTokenFromAnUnencryptedCookie() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnTheTokenFromACryptedCookie() + public function testItShouldReturnTheTokenFromACryptedCookie() { Crypt::shouldReceive('encrypt') ->with('foobar') @@ -317,8 +301,7 @@ public function itShouldReturnTheTokenFromACryptedCookie() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldThrowTokenInvalidExceptionFromAInvalidEncryptedCookie() + public function testItShouldThrowTokenInvalidExceptionFromAInvalidEncryptedCookie() { $request = Request::create('foo', 'POST', [], ['token' => 'foobar']); @@ -340,8 +323,7 @@ public function itShouldThrowTokenInvalidExceptionFromAInvalidEncryptedCookie() $parser->parseToken(); } - /** @test */ - public function itShouldReturnTheTokenFromRoute() + public function testItShouldReturnTheTokenFromRoute() { $request = Request::create('foo', 'GET', ['foo' => 'bar']); $request->setRouteResolver(fn () => $this->getRouteMock('foobar')); @@ -367,8 +349,7 @@ protected function getRouteMock($expectedParameterValue = null, $expectedParamet ->getMock(); } - /** @test */ - public function itShouldReturnTheTokenFromRouteWithACustomParam() + public function testItShouldReturnTheTokenFromRouteWithACustomParam() { $request = Request::create('foo', 'GET', ['foo' => 'bar']); $request->setRouteResolver(fn () => $this->getRouteMock('foobar', 'custom_route_param')); @@ -385,8 +366,7 @@ public function itShouldReturnTheTokenFromRouteWithACustomParam() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldIgnoreRoutelessRequests() + public function testItShouldIgnoreRoutelessRequests() { $request = Request::create('foo', 'GET', ['foo' => 'bar']); $request->setRouteResolver(function () { @@ -404,8 +384,7 @@ public function itShouldIgnoreRoutelessRequests() $this->assertFalse($parser->hasToken()); } - /** @test */ - public function itShouldIgnoreLumenRequestArrays() + public function testItShouldIgnoreLumenRequestArrays() { $request = Request::create('foo', 'GET', ['foo' => 'bar']); $request->setRouteResolver(fn () => [false, ['uses' => 'someController'], ['token' => 'foobar']]); @@ -422,8 +401,7 @@ public function itShouldIgnoreLumenRequestArrays() $this->assertFalse($parser->hasToken()); } - /** @test */ - public function itShouldAcceptLumenRequestArraysWithSpecialClass() + public function testItShouldAcceptLumenRequestArraysWithSpecialClass() { $request = Request::create('foo', 'GET', ['foo' => 'bar']); $request->setRouteResolver(fn () => [false, ['uses' => 'someController'], ['token' => 'foo.bar.baz']]); @@ -440,8 +418,7 @@ public function itShouldAcceptLumenRequestArraysWithSpecialClass() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldReturnNullIfNoTokenInRequest() + public function testItShouldReturnNullIfNoTokenInRequest() { $request = Request::create('foo', 'GET', ['foo' => 'bar']); $request->setRouteResolver(fn () => $this->getRouteMock()); @@ -458,8 +435,7 @@ public function itShouldReturnNullIfNoTokenInRequest() $this->assertFalse($parser->hasToken()); } - /** @test */ - public function itShouldRetrieveTheChain() + public function testItShouldRetrieveTheChain() { $chain = [ new AuthHeaders(), @@ -474,8 +450,7 @@ public function itShouldRetrieveTheChain() $this->assertSame($parser->getChain(), $chain); } - /** @test */ - public function itShouldRetrieveTheChainWithAlias() + public function testItShouldRetrieveTheChainWithAlias() { $chain = [ new AuthHeaders(), @@ -493,15 +468,13 @@ public function itShouldRetrieveTheChainWithAlias() $this->assertSame($parser->getChain(), $chain); } - /** @test */ - public function itShouldSetTheCookieKey() + public function testItShouldSetTheCookieKey() { $cookies = (new Cookies())->setKey('test'); $this->assertInstanceOf(Cookies::class, $cookies); } - /** @test */ - public function itShouldAddCustomParser() + public function testItShouldAddCustomParser() { $request = Request::create('foo', 'GET', ['foo' => 'bar']); @@ -515,8 +488,7 @@ public function itShouldAddCustomParser() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldAddMultipleCustomParser() + public function testItShouldAddMultipleCustomParser() { $request = Request::create('foo', 'GET', ['foo' => 'bar']); @@ -533,8 +505,7 @@ public function itShouldAddMultipleCustomParser() $this->assertTrue($parser->hasToken()); } - /** @test */ - public function itShouldIgnoreNonBearerTokens() + public function testItShouldIgnoreNonBearerTokens() { $request = Request::create('foo', 'POST'); $request->headers->set('Authorization', 'Basic OnBhc3N3b3Jk'); @@ -552,8 +523,7 @@ public function itShouldIgnoreNonBearerTokens() $this->assertFalse($parser->hasToken()); } - /** @test */ - public function itShouldIgnoreTokensWithoutPrefixes() + public function testItShouldIgnoreTokensWithoutPrefixes() { $request = Request::create('foo', 'POST'); $request->headers->set('Authorization', 'eyJhbGciOiJIUzI1NiIsInR5'); @@ -571,8 +541,7 @@ public function itShouldIgnoreTokensWithoutPrefixes() $this->assertFalse($parser->hasToken()); } - /** @test */ - public function itShouldParseMultipleAuthHeaders() + public function testItShouldParseMultipleAuthHeaders() { $request = Request::create('foo', 'POST'); $request->headers->set('Authorization', 'Bearer eyJhbGciOiJIUzI1NiIsInR5, Basic OnBhc3N3b3Jk'); diff --git a/tests/JWTAuthTest.php b/tests/JWTAuthTest.php index 6e4d62fa..d428bf64 100644 --- a/tests/JWTAuthTest.php +++ b/tests/JWTAuthTest.php @@ -43,8 +43,7 @@ public function setUp(): void $this->jwtAuth = new JWTAuth($this->manager, $this->auth, $this->parser); } - /** @test */ - public function itShouldReturnATokenWhenPassingAUser() + public function testItShouldReturnATokenWhenPassingAUser() { $payloadFactory = \Mockery::mock(Factory::class); $payloadFactory->shouldReceive('make')->andReturn(\Mockery::mock(Payload::class)); @@ -62,8 +61,7 @@ public function itShouldReturnATokenWhenPassingAUser() $this->assertSame($token, 'foo.bar.baz'); } - /** @test */ - public function itShouldPassProviderCheckIfHashMatches() + public function testItShouldPassProviderCheckIfHashMatches() { $payloadFactory = \Mockery::mock(Factory::class); $payloadFactory->shouldReceive('make')->andReturn(\Mockery::mock(Payload::class)); @@ -76,8 +74,7 @@ public function itShouldPassProviderCheckIfHashMatches() $this->assertTrue($this->jwtAuth->setToken('foo.bar.baz')->checkSubjectModel('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub')); } - /** @test */ - public function itShouldPassProviderCheckIfHashMatchesWhenProviderIsNull() + public function testItShouldPassProviderCheckIfHashMatchesWhenProviderIsNull() { $payloadFactory = \Mockery::mock(Factory::class); $payloadFactory->shouldReceive('make')->andReturn(\Mockery::mock(Payload::class)); @@ -90,8 +87,7 @@ public function itShouldPassProviderCheckIfHashMatchesWhenProviderIsNull() $this->assertTrue($this->jwtAuth->setToken('foo.bar.baz')->checkSubjectModel('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub')); } - /** @test */ - public function itShouldNotPassProviderCheckIfHashNotMatch() + public function testItShouldNotPassProviderCheckIfHashNotMatch() { $payloadFactory = \Mockery::mock(Factory::class); $payloadFactory->shouldReceive('make')->andReturn(\Mockery::mock(Payload::class)); @@ -104,8 +100,7 @@ public function itShouldNotPassProviderCheckIfHashNotMatch() $this->assertFalse($this->jwtAuth->setToken('foo.bar.baz')->checkSubjectModel('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub')); } - /** @test */ - public function itShouldReturnATokenWhenPassingValidCredentialsToAttemptMethod() + public function testItShouldReturnATokenWhenPassingValidCredentialsToAttemptMethod() { $payloadFactory = \Mockery::mock(Factory::class); $payloadFactory->shouldReceive('make')->andReturn(\Mockery::mock(Payload::class)); @@ -126,8 +121,7 @@ public function itShouldReturnATokenWhenPassingValidCredentialsToAttemptMethod() $this->assertSame($token, 'foo.bar.baz'); } - /** @test */ - public function itShouldReturnFalseWhenPassingInvalidCredentialsToAttemptMethod() + public function testItShouldReturnFalseWhenPassingInvalidCredentialsToAttemptMethod() { $this->manager->shouldReceive('encode->get')->never(); $this->auth->shouldReceive('byCredentials')->once()->andReturn(false); @@ -138,8 +132,7 @@ public function itShouldReturnFalseWhenPassingInvalidCredentialsToAttemptMethod( $this->assertFalse($token); } - /** @test */ - public function itShouldThrowAnExceptionWhenNotProvidingAToken() + public function testItShouldThrowAnExceptionWhenNotProvidingAToken() { $this->expectException(JWTException::class); $this->expectExceptionMessage('A token is required'); @@ -147,8 +140,7 @@ public function itShouldThrowAnExceptionWhenNotProvidingAToken() $this->jwtAuth->toUser(); } - /** @test */ - public function itShouldReturnTheOwningUserFromATokenContainingAnExistingUser() + public function testItShouldReturnTheOwningUserFromATokenContainingAnExistingUser() { $payload = \Mockery::mock(Payload::class); $payload->shouldReceive('get')->once()->with('sub')->andReturn(1); @@ -163,8 +155,7 @@ public function itShouldReturnTheOwningUserFromATokenContainingAnExistingUser() $this->assertSame($user->id, 1); } - /** @test */ - public function itShouldReturnFalseWhenPassingATokenNotContainingAnExistingUser() + public function testItShouldReturnFalseWhenPassingATokenNotContainingAnExistingUser() { $payload = \Mockery::mock(Payload::class); $payload->shouldReceive('get')->once()->with('sub')->andReturn(1); @@ -179,8 +170,7 @@ public function itShouldReturnFalseWhenPassingATokenNotContainingAnExistingUser( $this->assertFalse($user); } - /** @test */ - public function itShouldRefreshAToken() + public function testItShouldRefreshAToken() { $newToken = \Mockery::mock(Token::class); $newToken->shouldReceive('get')->once()->andReturn('baz.bar.foo'); @@ -192,8 +182,7 @@ public function itShouldRefreshAToken() $this->assertSame($result, 'baz.bar.foo'); } - /** @test */ - public function itShouldInvalidateAToken() + public function testItShouldInvalidateAToken() { $token = new Token('foo.bar.baz'); @@ -202,8 +191,7 @@ public function itShouldInvalidateAToken() $this->jwtAuth->setToken($token)->invalidate(); } - /** @test */ - public function itShouldForceInvalidateATokenForever() + public function testItShouldForceInvalidateATokenForever() { $token = new Token('foo.bar.baz'); @@ -212,8 +200,7 @@ public function itShouldForceInvalidateATokenForever() $this->jwtAuth->setToken($token)->invalidate(true); } - /** @test */ - public function itShouldRetrieveTheTokenFromTheRequest() + public function testItShouldRetrieveTheTokenFromTheRequest() { $this->parser->shouldReceive('parseToken')->andReturn('foo.bar.baz'); @@ -221,15 +208,13 @@ public function itShouldRetrieveTheTokenFromTheRequest() $this->assertEquals($this->jwtAuth->getToken(), 'foo.bar.baz'); } - /** @test */ - public function itShouldGetTheAuthenticatedUser() + public function testItShouldGetTheAuthenticatedUser() { $manager = $this->jwtAuth->manager(); $this->assertInstanceOf(Manager::class, $manager); } - /** @test */ - public function itShouldReturnFalseIfTheTokenIsInvalid() + public function testItShouldReturnFalseIfTheTokenIsInvalid() { $this->parser->shouldReceive('parseToken')->andReturn('foo.bar.baz'); $this->manager->shouldReceive('decode')->once()->andThrow(new TokenInvalidException()); @@ -237,8 +222,7 @@ public function itShouldReturnFalseIfTheTokenIsInvalid() $this->assertFalse($this->jwtAuth->parseToken()->check()); } - /** @test */ - public function itShouldReturnTrueIfTheTokenIsValid() + public function testItShouldReturnTrueIfTheTokenIsValid() { $payload = \Mockery::mock(Payload::class); @@ -248,8 +232,7 @@ public function itShouldReturnTrueIfTheTokenIsValid() $this->assertTrue($this->jwtAuth->parseToken()->check()); } - /** @test */ - public function itShouldThrowAnExceptionWhenTokenNotPresentInRequest() + public function testItShouldThrowAnExceptionWhenTokenNotPresentInRequest() { $this->expectException(JWTException::class); $this->expectExceptionMessage('The token could not be parsed from the request'); @@ -259,16 +242,14 @@ public function itShouldThrowAnExceptionWhenTokenNotPresentInRequest() $this->jwtAuth->parseToken(); } - /** @test */ - public function itShouldReturnFalseWhenNoTokenIsSet() + public function testItShouldReturnFalseWhenNoTokenIsSet() { $this->parser->shouldReceive('parseToken')->andReturn(false); $this->assertNull($this->jwtAuth->getToken()); } - /** @test */ - public function itShouldMagicallyCallTheManager() + public function testItShouldMagicallyCallTheManager() { $this->manager->shouldReceive('getBlacklist')->andReturn(new \stdClass()); @@ -277,8 +258,7 @@ public function itShouldMagicallyCallTheManager() $this->assertInstanceOf(\stdClass::class, $blacklist); } - /** @test */ - public function itShouldSetTheRequest() + public function testItShouldSetTheRequest() { $request = Request::create('/foo', 'GET', ['token' => 'some.random.token']); @@ -290,8 +270,7 @@ public function itShouldSetTheRequest() $this->assertEquals('some.random.token', $token); } - /** @test */ - public function itShouldUnsetTheToken() + public function testItShouldUnsetTheToken() { $this->parser->shouldReceive('parseToken')->andThrow(new JWTException()); $token = new Token('foo.bar.baz'); @@ -302,22 +281,19 @@ public function itShouldUnsetTheToken() $this->assertNull($this->jwtAuth->getToken()); } - /** @test */ - public function itShouldGetTheManagerInstance() + public function testItShouldGetTheManagerInstance() { $manager = $this->jwtAuth->manager(); $this->assertInstanceOf(Manager::class, $manager); } - /** @test */ - public function itShouldGetTheParserInstance() + public function testItShouldGetTheParserInstance() { $parser = $this->jwtAuth->parser(); $this->assertInstanceOf(Parser::class, $parser); } - /** @test */ - public function itShouldGetAClaimValue() + public function testItShouldGetAClaimValue() { $payload = \Mockery::mock(Payload::class); $payload->shouldReceive('get')->once()->with('sub')->andReturn(1); diff --git a/tests/JWTGuardTest.php b/tests/JWTGuardTest.php index 5f6e0442..0c452a5e 100644 --- a/tests/JWTGuardTest.php +++ b/tests/JWTGuardTest.php @@ -54,14 +54,12 @@ public function setUp(): void ); } - /** @test */ - public function itShouldGetTheRequest() + public function testItShouldGetTheRequest() { $this->assertInstanceOf(Request::class, $this->guard->getRequest()); } - /** @test */ - public function itShouldGetTheAuthenticatedUserIfAValidTokenIsProvided() + public function testItShouldGetTheAuthenticatedUserIfAValidTokenIsProvided() { $payload = \Mockery::mock(Payload::class); $payload->shouldReceive('offsetGet')->once()->with('sub')->andReturn(1); @@ -92,8 +90,7 @@ public function itShouldGetTheAuthenticatedUserIfAValidTokenIsProvided() $this->assertSame(1, $this->guard->userOrFail()->id); } - /** @test */ - public function itShouldGetTheAuthenticatedUserIfAValidTokenIsProvidedAndNotThrowAnException() + public function testItShouldGetTheAuthenticatedUserIfAValidTokenIsProvidedAndNotThrowAnException() { $payload = \Mockery::mock(Payload::class); $payload->shouldReceive('offsetGet')->once()->with('sub')->andReturn(1); @@ -121,8 +118,7 @@ public function itShouldGetTheAuthenticatedUserIfAValidTokenIsProvidedAndNotThro $this->assertTrue($this->guard->check()); } - /** @test */ - public function itShouldReturnNullIfAnInvalidTokenIsProvided() + public function testItShouldReturnNullIfAnInvalidTokenIsProvided() { $this->jwt->shouldReceive('setRequest')->andReturn($this->jwt); $this->jwt->shouldReceive('getToken')->twice()->andReturn('invalid.token.here'); @@ -134,8 +130,7 @@ public function itShouldReturnNullIfAnInvalidTokenIsProvided() $this->assertFalse($this->guard->check()); // twice } - /** @test */ - public function itShouldReturnNullIfNoTokenIsProvided() + public function testItShouldReturnNullIfNoTokenIsProvided() { $this->jwt->shouldReceive('setRequest')->andReturn($this->jwt); $this->jwt->shouldReceive('getToken')->andReturn(false); @@ -147,8 +142,7 @@ public function itShouldReturnNullIfNoTokenIsProvided() $this->assertFalse($this->guard->check()); } - /** @test */ - public function itShouldThrowAnExceptionIfAnInvalidTokenIsProvided() + public function testItShouldThrowAnExceptionIfAnInvalidTokenIsProvided() { $this->expectException(UserNotDefinedException::class); $this->expectExceptionMessage('An error occurred'); @@ -163,8 +157,7 @@ public function itShouldThrowAnExceptionIfAnInvalidTokenIsProvided() $this->guard->userOrFail(); // twice, throws the exception } - /** @test */ - public function itShouldThrowAnExceptionIfNoTokenIsProvided() + public function testItShouldThrowAnExceptionIfNoTokenIsProvided() { $this->expectException(UserNotDefinedException::class); $this->expectExceptionMessage('An error occurred'); @@ -179,8 +172,7 @@ public function itShouldThrowAnExceptionIfNoTokenIsProvided() $this->guard->userOrFail(); // throws the exception } - /** @test */ - public function itShouldReturnATokenIfCredentialsAreOkAndUserIsFound() + public function testItShouldReturnATokenIfCredentialsAreOkAndUserIsFound() { $credentials = ['foo' => 'bar', 'baz' => 'bob']; $user = new LaravelUserStub(); @@ -234,8 +226,7 @@ public function itShouldReturnATokenIfCredentialsAreOkAndUserIsFound() $this->assertSame($token, 'foo.bar.baz'); } - /** @test */ - public function itShouldReturnTrueIfCredentialsAreOkAndUserIsFoundWhenChoosingNotToLogin() + public function testItShouldReturnTrueIfCredentialsAreOkAndUserIsFoundWhenChoosingNotToLogin() { $credentials = ['foo' => 'bar', 'baz' => 'bob']; $user = new LaravelUserStub(); @@ -272,8 +263,7 @@ public function itShouldReturnTrueIfCredentialsAreOkAndUserIsFoundWhenChoosingNo $this->assertTrue($this->guard->validate($credentials)); // twice } - /** @test */ - public function itShouldReturnFalseIfCredentialsAreInvalid() + public function testItShouldReturnFalseIfCredentialsAreInvalid() { $credentials = ['foo' => 'bar', 'baz' => 'bob']; $user = new LaravelUserStub(); @@ -307,15 +297,13 @@ public function itShouldReturnFalseIfCredentialsAreInvalid() $this->assertFalse($this->guard->attempt($credentials)); } - /** @test */ - public function itShouldMagicallyCallTheJwtInstance() + public function testItShouldMagicallyCallTheJwtInstance() { $this->jwt->shouldReceive('factory')->andReturn(\Mockery::mock(Factory::class)); $this->assertInstanceOf(Factory::class, $this->guard->factory()); } - /** @test */ - public function itShouldLogoutTheUserByInvalidatingTheToken() + public function testItShouldLogoutTheUserByInvalidatingTheToken() { $this->jwt->shouldReceive('setRequest')->andReturn($this->jwt); $this->jwt->shouldReceive('getToken')->once()->andReturn(true); @@ -334,8 +322,7 @@ public function itShouldLogoutTheUserByInvalidatingTheToken() $this->assertNull($this->guard->getUser()); } - /** @test */ - public function itShouldRefreshTheToken() + public function testItShouldRefreshTheToken() { $this->jwt->shouldReceive('setRequest')->andReturn($this->jwt); $this->jwt->shouldReceive('getToken')->once()->andReturn(true); @@ -344,8 +331,7 @@ public function itShouldRefreshTheToken() $this->assertSame($this->guard->refresh(), 'foo.bar.baz'); } - /** @test */ - public function itShouldInvalidateTheToken() + public function testItShouldInvalidateTheToken() { $this->jwt->shouldReceive('setRequest')->andReturn($this->jwt); $this->jwt->shouldReceive('getToken')->once()->andReturn(true); @@ -354,8 +340,7 @@ public function itShouldInvalidateTheToken() $this->assertTrue($this->guard->invalidate()); } - /** @test */ - public function itShouldThrowAnExceptionIfThereIsNoTokenPresentWhenRequired() + public function testItShouldThrowAnExceptionIfThereIsNoTokenPresentWhenRequired() { $this->expectException(JWTException::class); $this->expectExceptionMessage('Token could not be parsed from the request.'); @@ -367,8 +352,7 @@ public function itShouldThrowAnExceptionIfThereIsNoTokenPresentWhenRequired() $this->guard->refresh(); } - /** @test */ - public function itShouldGenerateATokenById() + public function testItShouldGenerateATokenById() { $user = new LaravelUserStub(); @@ -393,8 +377,7 @@ public function itShouldGenerateATokenById() $this->assertSame('foo.bar.baz', $this->guard->tokenById(1)); } - /** @test */ - public function itShouldNotGenerateATokenById() + public function testItShouldNotGenerateATokenById() { $this->provider->shouldReceive('retrieveById') ->once() @@ -404,8 +387,7 @@ public function itShouldNotGenerateATokenById() $this->assertNull($this->guard->tokenById(1)); } - /** @test */ - public function itShouldAuthenticateTheUserByCredentialsAndReturnTrueIfValid() + public function testItShouldAuthenticateTheUserByCredentialsAndReturnTrueIfValid() { $credentials = ['foo' => 'bar', 'baz' => 'bob']; $user = new LaravelUserStub(); @@ -441,8 +423,7 @@ public function itShouldAuthenticateTheUserByCredentialsAndReturnTrueIfValid() $this->assertTrue($this->guard->once($credentials)); } - /** @test */ - public function itShouldAttemptToAuthenticateTheUserByCredentialsAndReturnFalseIfInvalid() + public function testItShouldAttemptToAuthenticateTheUserByCredentialsAndReturnFalseIfInvalid() { $credentials = ['foo' => 'bar', 'baz' => 'bob']; $user = new LaravelUserStub(); @@ -468,8 +449,7 @@ public function itShouldAttemptToAuthenticateTheUserByCredentialsAndReturnFalseI $this->assertFalse($this->guard->once($credentials)); } - /** @test */ - public function itShouldAuthenticateTheUserByIdAndReturnBoolean() + public function testItShouldAuthenticateTheUserByIdAndReturnBoolean() { $user = new LaravelUserStub(); @@ -486,8 +466,7 @@ public function itShouldAuthenticateTheUserByIdAndReturnBoolean() $this->assertTrue($this->guard->byId(1)); // twice } - /** @test */ - public function itShouldNotAuthenticateTheUserByIdAndReturnFalse() + public function testItShouldNotAuthenticateTheUserByIdAndReturnFalse() { $this->provider->shouldReceive('retrieveById') ->twice() @@ -498,8 +477,7 @@ public function itShouldNotAuthenticateTheUserByIdAndReturnFalse() $this->assertFalse($this->guard->byId(1)); // twice } - /** @test */ - public function itShouldCreateATokenFromAUserObject() + public function testItShouldCreateATokenFromAUserObject() { $user = new LaravelUserStub(); @@ -526,8 +504,7 @@ public function itShouldCreateATokenFromAUserObject() $this->assertSame('foo.bar.baz', $token); } - /** @test */ - public function itShouldGetThePayload() + public function testItShouldGetThePayload() { $this->jwt->shouldReceive('setRequest')->andReturn($this->jwt); $this->jwt->shouldReceive('getToken')->once()->andReturn('foo.bar.baz'); @@ -535,8 +512,7 @@ public function itShouldGetThePayload() $this->assertInstanceOf(Payload::class, $this->guard->payload()); } - /** @test */ - public function itShouldBeMacroable() + public function testItShouldBeMacroable() { $this->guard->macro('foo', fn () => 'bar'); diff --git a/tests/ManagerTest.php b/tests/ManagerTest.php index 79d41b14..2e29e5e5 100644 --- a/tests/ManagerTest.php +++ b/tests/ManagerTest.php @@ -54,10 +54,10 @@ public function setUp(): void $this->validator = \Mockery::mock(PayloadValidator::class); } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldEncodeAPayload() + public function testItShouldEncodeAPayload() { $claims = [ new Subject(1), @@ -80,10 +80,10 @@ public function itShouldEncodeAPayload() $this->assertEquals($token, 'foo.bar.baz'); } - /** @test + /** * @throws InvalidClaimException|TokenBlacklistedException */ - public function itShouldDecodeAToken() + public function testItShouldDecodeAToken() { $claims = [ new Subject(1), @@ -114,10 +114,10 @@ public function itShouldDecodeAToken() $this->assertSame($payload->count(), 6); } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldThrowExceptionWhenTokenIsBlacklisted() + public function testItShouldThrowExceptionWhenTokenIsBlacklisted() { $this->expectException(TokenBlacklistedException::class); $this->expectExceptionMessage('The token has been blacklisted'); @@ -147,10 +147,10 @@ public function itShouldThrowExceptionWhenTokenIsBlacklisted() $this->manager->decode($token); } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldRefreshAToken() + public function testItShouldRefreshAToken() { $claims = [ new Subject(1), @@ -183,10 +183,10 @@ public function itShouldRefreshAToken() $this->assertEquals('baz.bar.foo', $token); } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldInvalidateAToken() + public function testItShouldInvalidateAToken() { $claims = [ new Subject(1), @@ -215,10 +215,10 @@ public function itShouldInvalidateAToken() $this->manager->invalidate($token); } - /** @test + /** * @throws InvalidClaimException */ - public function itShouldForceInvalidateATokenForever() + public function testItShouldForceInvalidateATokenForever() { $claims = [ new Subject(1), @@ -247,8 +247,7 @@ public function itShouldForceInvalidateATokenForever() $this->manager->invalidate($token, true); } - /** @test */ - public function itShouldThrowAnExceptionWhenEnableBlacklistIsSetToFalse() + public function testItShouldThrowAnExceptionWhenEnableBlacklistIsSetToFalse() { $this->expectException(JWTException::class); $this->expectExceptionMessage('You must have the blacklist enabled to invalidate a token.'); @@ -258,25 +257,21 @@ public function itShouldThrowAnExceptionWhenEnableBlacklistIsSetToFalse() $this->manager->setBlacklistEnabled(false)->invalidate($token); } - /** @test */ - public function itShouldGetThePayloadFactory() + public function testItShouldGetThePayloadFactory() { $this->assertInstanceOf(Factory::class, $this->manager->getPayloadFactory()); } - /** @test */ - public function itShouldGetTheJwtProvider() + public function testItShouldGetTheJwtProvider() { $this->assertInstanceOf(JWT::class, $this->manager->getJWTProvider()); } - /** @test */ - public function itShouldGetTheBlacklist() + public function testItShouldGetTheBlacklist() { $this->assertInstanceOf(Blacklist::class, $this->manager->getBlacklist()); } - /** @test */ public function testIfShowBlacklistedExceptionConfigurationIsEnabled() { $this->manager->setBlackListExceptionEnabled(true); @@ -284,7 +279,6 @@ public function testIfShowBlacklistedExceptionConfigurationIsEnabled() $this->assertIsBool($this->manager->getBlackListExceptionEnabled()); } - /** @test */ public function testIfBlackListedExceptionIsSetToTrue() { $this->manager->setBlackListExceptionEnabled(true); @@ -292,7 +286,6 @@ public function testIfBlackListedExceptionIsSetToTrue() $this->assertTrue($this->manager->getBlackListExceptionEnabled()); } - /** @test */ public function testIfBlackListedExceptionIsSetToFalse() { $this->manager->setBlackListExceptionEnabled(false); diff --git a/tests/Middleware/AuthenticateAndRenewTest.php b/tests/Middleware/AuthenticateAndRenewTest.php index 33991140..7533844e 100644 --- a/tests/Middleware/AuthenticateAndRenewTest.php +++ b/tests/Middleware/AuthenticateAndRenewTest.php @@ -34,8 +34,7 @@ public function setUp(): void $this->middleware = new AuthenticateAndRenew($this->auth); } - /** @test */ - public function itShouldAuthenticateAUserAndReturnANewToken() + public function testItShouldAuthenticateAUserAndReturnANewToken() { $parser = \Mockery::mock(Parser::class); $parser->shouldReceive('hasToken')->once()->andReturn(true); @@ -53,8 +52,7 @@ public function itShouldAuthenticateAUserAndReturnANewToken() $this->assertSame($response->headers->get('authorization'), 'Bearer foo.bar.baz'); } - /** @test */ - public function itShouldThrowAnUnauthorizedExceptionIfTokenNotProvided() + public function testItShouldThrowAnUnauthorizedExceptionIfTokenNotProvided() { $this->expectException(UnauthorizedHttpException::class); @@ -68,8 +66,7 @@ public function itShouldThrowAnUnauthorizedExceptionIfTokenNotProvided() }); } - /** @test */ - public function itShouldThrowAnUnauthorizedExceptionIfTokenInvalid() + public function testItShouldThrowAnUnauthorizedExceptionIfTokenInvalid() { $this->expectException(UnauthorizedHttpException::class); diff --git a/tests/Middleware/AuthenticateTest.php b/tests/Middleware/AuthenticateTest.php index 016679af..fdcb95ae 100644 --- a/tests/Middleware/AuthenticateTest.php +++ b/tests/Middleware/AuthenticateTest.php @@ -32,8 +32,7 @@ public function setUp(): void $this->middleware = new Authenticate($this->auth); } - /** @test */ - public function itShouldAuthenticateAUser() + public function testItShouldAuthenticateAUser() { $parser = \Mockery::mock(Parser::class); $parser->shouldReceive('hasToken')->once()->andReturn(true); @@ -47,8 +46,7 @@ public function itShouldAuthenticateAUser() }); } - /** @test */ - public function itShouldThrowAnUnauthorizedExceptionIfTokenNotProvided() + public function testItShouldThrowAnUnauthorizedExceptionIfTokenNotProvided() { $this->expectException(UnauthorizedHttpException::class); @@ -62,8 +60,7 @@ public function itShouldThrowAnUnauthorizedExceptionIfTokenNotProvided() }); } - /** @test */ - public function itShouldThrowAnUnauthorizedExceptionIfTokenInvalid() + public function testItShouldThrowAnUnauthorizedExceptionIfTokenInvalid() { $this->expectException(UnauthorizedHttpException::class); @@ -79,8 +76,7 @@ public function itShouldThrowAnUnauthorizedExceptionIfTokenInvalid() }); } - /** @test */ - public function itShouldThrowAnUnauthorizedExceptionIfUserNotFound() + public function testItShouldThrowAnUnauthorizedExceptionIfUserNotFound() { $this->expectException(UnauthorizedHttpException::class); diff --git a/tests/Middleware/CheckTest.php b/tests/Middleware/CheckTest.php index e43fa920..e115d398 100644 --- a/tests/Middleware/CheckTest.php +++ b/tests/Middleware/CheckTest.php @@ -31,8 +31,7 @@ public function setUp(): void $this->middleware = new Check($this->auth); } - /** @test */ - public function itShouldAuthenticateAUserIfATokenIsPresent() + public function testItShouldAuthenticateAUserIfATokenIsPresent() { $parser = \Mockery::mock(Parser::class); $parser->shouldReceive('hasToken')->once()->andReturn(true); @@ -46,8 +45,7 @@ public function itShouldAuthenticateAUserIfATokenIsPresent() }); } - /** @test */ - public function itShouldUnsetTheExceptionIfATokenIsPresent() + public function testItShouldUnsetTheExceptionIfATokenIsPresent() { $parser = \Mockery::mock(Parser::class); $parser->shouldReceive('hasToken')->once()->andReturn(true); @@ -61,8 +59,7 @@ public function itShouldUnsetTheExceptionIfATokenIsPresent() }); } - /** @test */ - public function itShouldDoNothingIfATokenIsNotPresent() + public function testItShouldDoNothingIfATokenIsNotPresent() { $parser = \Mockery::mock(Parser::class); $parser->shouldReceive('hasToken')->once()->andReturn(false); diff --git a/tests/Middleware/RefreshTokenTest.php b/tests/Middleware/RefreshTokenTest.php index 38563372..258d35f5 100644 --- a/tests/Middleware/RefreshTokenTest.php +++ b/tests/Middleware/RefreshTokenTest.php @@ -32,8 +32,7 @@ public function setUp(): void $this->middleware = new RefreshToken($this->auth); } - /** @test */ - public function itShouldRefreshAToken() + public function testItShouldRefreshAToken() { $parser = \Mockery::mock(Parser::class); $parser->shouldReceive('hasToken')->once()->andReturn(true); @@ -50,8 +49,7 @@ public function itShouldRefreshAToken() $this->assertSame($response->headers->get('authorization'), 'Bearer foo.bar.baz'); } - /** @test */ - public function itShouldThrowAnUnauthorizedExceptionIfTokenNotProvided() + public function testItShouldThrowAnUnauthorizedExceptionIfTokenNotProvided() { $this->expectException(UnauthorizedHttpException::class); @@ -65,8 +63,7 @@ public function itShouldThrowAnUnauthorizedExceptionIfTokenNotProvided() }); } - /** @test */ - public function itShouldThrowAnUnauthorizedExceptionIfTokenInvalid() + public function testItShouldThrowAnUnauthorizedExceptionIfTokenInvalid() { $this->expectException(UnauthorizedHttpException::class); diff --git a/tests/PayloadTest.php b/tests/PayloadTest.php index ebc5bee6..8dae0afc 100644 --- a/tests/PayloadTest.php +++ b/tests/PayloadTest.php @@ -44,11 +44,9 @@ public function setUp(): void } /** - * @return Payload - * * @throws InvalidClaimException */ - private function getTestPayload(array $extraClaims = []) + private function getTestPayload(array $extraClaims = []): Payload { $claims = [ new Subject(1), @@ -71,8 +69,7 @@ private function getTestPayload(array $extraClaims = []) return new Payload($collection, $this->validator); } - /** @test */ - public function itShouldThrowAnExceptionWhenTryingToAddToThePayload() + public function testItShouldThrowAnExceptionWhenTryingToAddToThePayload() { $this->expectException(PayloadException::class); $this->expectExceptionMessage('The payload is immutable'); @@ -80,8 +77,7 @@ public function itShouldThrowAnExceptionWhenTryingToAddToThePayload() $this->payload['foo'] = 'bar'; } - /** @test */ - public function itShouldThrowAnExceptionWhenTryingToRemoveAKeyFromThePayload() + public function testItShouldThrowAnExceptionWhenTryingToRemoveAKeyFromThePayload() { $this->expectException(PayloadException::class); $this->expectExceptionMessage('The payload is immutable'); @@ -89,23 +85,20 @@ public function itShouldThrowAnExceptionWhenTryingToRemoveAKeyFromThePayload() unset($this->payload['foo']); } - /** @test */ - public function itShouldCastThePayloadToAStringAsJson() + public function testItShouldCastThePayloadToAStringAsJson() { $this->assertSame((string) $this->payload, json_encode($this->payload->get(), JSON_UNESCAPED_SLASHES)); $this->assertJsonStringEqualsJsonString((string) $this->payload, json_encode($this->payload->get())); } - /** @test */ - public function itShouldAllowArrayAccessOnThePayload() + public function testItShouldAllowArrayAccessOnThePayload() { $this->assertTrue(isset($this->payload['iat'])); $this->assertSame($this->payload['sub'], 1); $this->assertArrayHasKey('exp', $this->payload); } - /** @test */ - public function itShouldGetPropertiesOfPayloadViaGetMethod() + public function testItShouldGetPropertiesOfPayloadViaGetMethod() { $this->assertIsArray($this->payload->get()); $this->assertSame($this->payload->get('sub'), 1); @@ -116,8 +109,7 @@ public function itShouldGetPropertiesOfPayloadViaGetMethod() ); } - /** @test */ - public function itShouldGetMultiplePropertiesWhenPassingAnArrayToTheGetMethod() + public function testItShouldGetMultiplePropertiesWhenPassingAnArrayToTheGetMethod() { $values = $this->payload->get(['sub', 'jti']); @@ -129,15 +121,13 @@ public function itShouldGetMultiplePropertiesWhenPassingAnArrayToTheGetMethod() $this->assertSame($jti, 'foo'); } - /** @test */ - public function itShouldDetermineWhetherThePayloadHasAClaim() + public function testItShouldDetermineWhetherThePayloadHasAClaim() { $this->assertTrue($this->payload->has(new Subject(1))); $this->assertFalse($this->payload->has(new Audience(1))); } - /** @test */ - public function itShouldMagicallyGetAProperty() + public function testItShouldMagicallyGetAProperty() { $sub = $this->payload->getSubject(); $jti = $this->payload->getJwtId(); @@ -148,8 +138,7 @@ public function itShouldMagicallyGetAProperty() $this->assertSame($iss, 'http://example.com'); } - /** @test */ - public function itShouldInvokeTheInstanceAsACallable() + public function testItShouldInvokeTheInstanceAsACallable() { $payload = $this->payload; @@ -164,8 +153,7 @@ public function itShouldInvokeTheInstanceAsACallable() $this->assertSame($payload(), $this->payload->toArray()); } - /** @test */ - public function itShouldThrowAnExceptionWhenMagicallyGettingAPropertyThatDoesNotExist() + public function testItShouldThrowAnExceptionWhenMagicallyGettingAPropertyThatDoesNotExist() { $this->expectException(\BadMethodCallException::class); $this->expectExceptionMessage('The claim [Foo] does not exist on the payload'); @@ -173,8 +161,7 @@ public function itShouldThrowAnExceptionWhenMagicallyGettingAPropertyThatDoesNot $this->payload->getFoo(); } - /** @test */ - public function itShouldGetTheClaims() + public function testItShouldGetTheClaims() { $claims = $this->payload->getClaims(); @@ -185,21 +172,18 @@ public function itShouldGetTheClaims() $this->assertContainsOnlyInstancesOf(Claim::class, $claims); } - /** @test */ - public function itShouldGetTheObjectAsJson() + public function testItShouldGetTheObjectAsJson() { $this->assertJsonStringEqualsJsonString(json_encode($this->payload), $this->payload->toJson()); } - /** @test */ - public function itShouldCountTheClaims() + public function testItShouldCountTheClaims() { $this->assertSame(6, $this->payload->count()); $this->assertCount(6, $this->payload); } - /** @test */ - public function itShouldMatchValues() + public function testItShouldMatchValues() { $values = $this->payload->toArray(); $values['sub'] = (string) $values['sub']; @@ -207,8 +191,7 @@ public function itShouldMatchValues() $this->assertTrue($this->payload->matches($values)); } - /** @test */ - public function itShouldMatchStrictValues() + public function testItShouldMatchStrictValues() { $values = $this->payload->toArray(); @@ -216,14 +199,12 @@ public function itShouldMatchStrictValues() $this->assertTrue($this->payload->matches($values, true)); } - /** @test */ - public function itShouldNotMatchEmptyValues() + public function testItShouldNotMatchEmptyValues() { $this->assertFalse($this->payload->matches([])); } - /** @test */ - public function itShouldNotMatchValues() + public function testItShouldNotMatchValues() { $values = $this->payload->toArray(); $values['sub'] = 'dummy_subject'; @@ -231,8 +212,7 @@ public function itShouldNotMatchValues() $this->assertFalse($this->payload->matches($values)); } - /** @test */ - public function itShouldNotMatchStrictValues() + public function testItShouldNotMatchStrictValues() { $values = $this->payload->toArray(); $values['sub'] = (string) $values['sub']; @@ -241,8 +221,7 @@ public function itShouldNotMatchStrictValues() $this->assertFalse($this->payload->matches($values, true)); } - /** @test */ - public function itShouldNotMatchANonExistingClaim() + public function testItShouldNotMatchANonExistingClaim() { $values = ['foo' => 'bar']; diff --git a/tests/Providers/Auth/IlluminateTest.php b/tests/Providers/Auth/IlluminateTest.php index 566dfe1d..8587f4da 100644 --- a/tests/Providers/Auth/IlluminateTest.php +++ b/tests/Providers/Auth/IlluminateTest.php @@ -37,29 +37,25 @@ public function setUp(): void $this->auth = new Auth($this->authManager); } - /** @test */ - public function itShouldReturnTrueIfCredentialsAreValid() + public function testItShouldReturnTrueIfCredentialsAreValid() { $this->authManager->shouldReceive('once')->once()->with(['email' => 'foo@bar.com', 'password' => 'foobar'])->andReturn(true); $this->assertTrue($this->auth->byCredentials(['email' => 'foo@bar.com', 'password' => 'foobar'])); } - /** @test */ - public function itShouldReturnTrueIfUserIsFound() + public function testItShouldReturnTrueIfUserIsFound() { $this->authManager->shouldReceive('onceUsingId')->once()->with(123)->andReturn(true); $this->assertTrue($this->auth->byId(123)); } - /** @test */ - public function itShouldReturnFalseIfUserIsNotFound() + public function testItShouldReturnFalseIfUserIsNotFound() { $this->authManager->shouldReceive('onceUsingId')->once()->with(123)->andReturn(false); $this->assertFalse($this->auth->byId(123)); } - /** @test */ - public function itShouldReturnTheCurrentlyAuthenticatedUser() + public function testItShouldReturnTheCurrentlyAuthenticatedUser() { $this->authManager->shouldReceive('user')->once()->andReturn((object) ['id' => 1]); $this->assertSame($this->auth->user()->id, 1); diff --git a/tests/Providers/JWT/LcobucciTest.php b/tests/Providers/JWT/LcobucciTest.php index 31b1716e..b960ee92 100644 --- a/tests/Providers/JWT/LcobucciTest.php +++ b/tests/Providers/JWT/LcobucciTest.php @@ -20,6 +20,7 @@ use Lcobucci\JWT\Token; use Lcobucci\JWT\Token\DataSet; use Lcobucci\JWT\Validation\Constraint; +use Lcobucci\JWT\Validator; use Mockery\MockInterface; use PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException; use PHPOpenSourceSaver\JWTAuth\Exceptions\TokenInvalidException; @@ -58,8 +59,7 @@ public function setUp(): void $this->parser = \Mockery::mock(Parser::class); } - /** @test */ - public function itShouldReturnTheTokenWhenPassingAValidPayloadToEncode() + public function testItShouldReturnTheTokenWhenPassingAValidPayloadToEncode() { $payload = ['sub' => 1, 'exp' => $this->testNowTimestamp + 3600, 'iat' => $this->testNowTimestamp, 'iss' => '/foo']; @@ -81,8 +81,7 @@ public function itShouldReturnTheTokenWhenPassingAValidPayloadToEncode() $this->assertSame('header.payload.signature', $token); } - /** @test */ - public function itShouldThrowAnInvalidExceptionWhenThePayloadCouldNotBeEncoded() + public function testItShouldThrowAnInvalidExceptionWhenThePayloadCouldNotBeEncoded() { $this->expectException(JWTException::class); $this->expectExceptionMessage('Could not create token:'); @@ -102,8 +101,7 @@ public function itShouldThrowAnInvalidExceptionWhenThePayloadCouldNotBeEncoded() $this->getProvider('secret', 'HS256')->encode($payload); } - /** @test */ - public function itShouldReturnThePayloadWhenPassingAValidTokenToDecode() + public function testItShouldReturnThePayloadWhenPassingAValidTokenToDecode() { $payload = ['sub' => 1, 'exp' => $this->testNowTimestamp + 3600, 'iat' => $this->testNowTimestamp, 'iss' => '/foo']; @@ -120,8 +118,7 @@ public function itShouldReturnThePayloadWhenPassingAValidTokenToDecode() $this->assertSame($payload, $provider->decode('foo.bar.baz')); } - /** @test */ - public function itShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecodedDueToABadSignature() + public function testItShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecodedDueToABadSignature() { $token = \Mockery::mock(Token::class); $dataSet = \Mockery::mock(new DataSet(['pay', 'load'], 'payload')); @@ -139,8 +136,7 @@ public function itShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecoded $provider->decode('foo.bar.baz'); } - /** @test */ - public function itShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecoded() + public function testItShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecoded() { $this->expectException(TokenInvalidException::class); $this->expectExceptionMessage('Could not decode token:'); @@ -152,8 +148,7 @@ public function itShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecoded $this->getProvider('secret', 'HS256')->decode('foo.bar.baz'); } - /** @test */ - public function itShouldGenerateATokenWhenUsingAnRsaAlgorithm() + public function testItShouldGenerateATokenWhenUsingAnRsaAlgorithm() { $dummyPrivateKey = $this->getDummyPrivateKey(); $dummyPublicKey = $this->getDummyPublicKey(); @@ -183,8 +178,7 @@ public function itShouldGenerateATokenWhenUsingAnRsaAlgorithm() $this->assertSame('header.payload.signature', $token); } - /** @test */ - public function itShouldThrowAExceptionWhenTheAlgorithmPassedIsInvalid() + public function testItShouldThrowAExceptionWhenTheAlgorithmPassedIsInvalid() { $this->expectException(JWTException::class); $this->expectExceptionMessage('The given algorithm could not be found'); @@ -195,8 +189,7 @@ public function itShouldThrowAExceptionWhenTheAlgorithmPassedIsInvalid() $this->getProvider('secret', 'AlgorithmWrong')->decode('foo.bar.baz'); } - /** @test */ - public function itShouldReturnThePublicKey() + public function testItShouldReturnThePublicKey() { $provider = $this->getProvider( 'does_not_matter', @@ -207,8 +200,7 @@ public function itShouldReturnThePublicKey() $this->assertSame($keys['public'], $provider->getPublicKey()); } - /** @test */ - public function itShouldReturnTheKeys() + public function testItShouldReturnTheKeys() { $provider = $this->getProvider( 'does_not_matter', @@ -219,8 +211,7 @@ public function itShouldReturnTheKeys() $this->assertSame($keys, $provider->getKeys()); } - /** @test */ - public function itShouldCorrectlyInstantiateAnEcdsaSigner() + public function testItShouldCorrectlyInstantiateAnEcdsaSigner() { $provider = new Lcobucci( 'does_not_matter', @@ -277,7 +268,7 @@ public function getProvider($secret, $algo, array $keys = []) { $provider = new Lcobucci($secret, $algo, $keys); - $this->validator = \Mockery::mock(\Lcobucci\JWT\Validator::class); + $this->validator = \Mockery::mock(Validator::class); $this->config = \Mockery::mock($provider->getConfig()); $provider = new Lcobucci($secret, $algo, $keys, $this->config); diff --git a/tests/Providers/JWT/NamshiTest.php b/tests/Providers/JWT/NamshiTest.php index 36f83e04..0876a02a 100644 --- a/tests/Providers/JWT/NamshiTest.php +++ b/tests/Providers/JWT/NamshiTest.php @@ -38,8 +38,7 @@ public function setUp(): void $this->jws = \Mockery::mock(JWS::class); } - /** @test */ - public function itShouldReturnTheTokenWhenPassingAValidPayloadToEncode() + public function testItShouldReturnTheTokenWhenPassingAValidPayloadToEncode() { $payload = ['sub' => 1, 'exp' => $this->testNowTimestamp + 3600, 'iat' => $this->testNowTimestamp, 'iss' => '/foo']; @@ -57,8 +56,7 @@ public function getProvider($secret, $algo, array $keys = []) return new Namshi($this->jws, $secret, $algo, $keys); } - /** @test */ - public function itShouldThrowAnInvalidExceptionWhenThePayloadCouldNotBeEncoded() + public function testItShouldThrowAnInvalidExceptionWhenThePayloadCouldNotBeEncoded() { $this->expectException(JWTException::class); $this->expectExceptionMessage('Could not create token:'); @@ -71,8 +69,7 @@ public function itShouldThrowAnInvalidExceptionWhenThePayloadCouldNotBeEncoded() $this->getProvider('secret', 'HS256')->encode($payload); } - /** @test */ - public function itShouldReturnThePayloadWhenPassingAValidTokenToDecode() + public function testItShouldReturnThePayloadWhenPassingAValidTokenToDecode() { $payload = ['sub' => 1, 'exp' => $this->testNowTimestamp + 3600, 'iat' => $this->testNowTimestamp, 'iss' => '/foo']; @@ -83,8 +80,7 @@ public function itShouldReturnThePayloadWhenPassingAValidTokenToDecode() $this->assertSame($payload, $this->getProvider('secret', 'HS256')->decode('foo.bar.baz')); } - /** @test */ - public function itShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecodedDueToABadSignature() + public function testItShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecodedDueToABadSignature() { $this->expectException(TokenInvalidException::class); $this->expectExceptionMessage('Token Signature could not be verified.'); @@ -96,8 +92,7 @@ public function itShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecoded $this->getProvider('secret', 'HS256')->decode('foo.bar.baz'); } - /** @test */ - public function itShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecoded() + public function testItShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecoded() { $this->expectException(TokenInvalidException::class); $this->expectExceptionMessage('Could not decode token:'); @@ -109,8 +104,7 @@ public function itShouldThrowATokenInvalidExceptionWhenTheTokenCouldNotBeDecoded $this->getProvider('secret', 'HS256')->decode('foo.bar.baz'); } - /** @test */ - public function itShouldGenerateATokenWhenUsingAnRsaAlgorithm() + public function testItShouldGenerateATokenWhenUsingAnRsaAlgorithm() { $provider = $this->getProvider( 'does_not_matter', @@ -139,8 +133,7 @@ public function getDummyPublicKey() return file_get_contents(__DIR__.'/../Keys/id_rsa.pub'); } - /** @test */ - public function itShouldGenerateATokenWhenUsingAnEcdsaAlgorithm() + public function testItShouldGenerateATokenWhenUsingAnEcdsaAlgorithm() { $provider = $this->getProvider( 'does_not_matter', @@ -159,8 +152,7 @@ public function itShouldGenerateATokenWhenUsingAnEcdsaAlgorithm() $this->assertSame('foo.bar.baz', $token); } - /** @test */ - public function itShouldDecodeATokenWhenUsingAnRsaAlgorithm() + public function testItShouldDecodeATokenWhenUsingAnRsaAlgorithm() { $provider = $this->getProvider( 'does_not_matter', @@ -179,8 +171,7 @@ public function itShouldDecodeATokenWhenUsingAnRsaAlgorithm() $this->assertSame('foo.bar.baz', $token); } - /** @test */ - public function itShouldThrowAExceptionWhenTheAlgorithmPassedIsInvalid() + public function testItShouldThrowAExceptionWhenTheAlgorithmPassedIsInvalid() { $this->expectException(JWTException::class); $this->expectExceptionMessage('The given algorithm could not be found'); @@ -191,8 +182,7 @@ public function itShouldThrowAExceptionWhenTheAlgorithmPassedIsInvalid() $this->getProvider('secret', 'AlgorithmWrong')->decode('foo.bar.baz'); } - /** @test */ - public function itShouldReturnThePublicKey() + public function testItShouldReturnThePublicKey() { $provider = $this->getProvider( 'does_not_matter', @@ -203,8 +193,7 @@ public function itShouldReturnThePublicKey() $this->assertSame($keys['public'], $provider->getPublicKey()); } - /** @test */ - public function itShouldReturnTheKeys() + public function testItShouldReturnTheKeys() { $provider = $this->getProvider( 'does_not_matter', diff --git a/tests/Providers/JWT/ProviderEmptySecretTest.php b/tests/Providers/JWT/ProviderEmptySecretTest.php index d2c39e14..754f60b9 100644 --- a/tests/Providers/JWT/ProviderEmptySecretTest.php +++ b/tests/Providers/JWT/ProviderEmptySecretTest.php @@ -23,16 +23,14 @@ class ProviderEmptySecretTest extends AbstractTestCase */ protected $provider; - /** @test */ - public function asymmetricNoSecret() + public function testAsymmetricNoSecret() { $this->provider = new JWTProviderStub(null, 'RS256', ['public' => '123', 'private' => '456']); $this->assertSame(null, $this->provider->getSecret()); } - /** @test */ - public function asymmetricPublicMissing() + public function testAsymmetricPublicMissing() { $this->expectException(SecretMissingException::class); $this->provider = new JWTProviderStub(null, 'RS256', ['public' => null, 'private' => '456']); @@ -40,8 +38,7 @@ public function asymmetricPublicMissing() $this->assertSame(null, $this->provider->getSecret()); } - /** @test */ - public function asymmetricPrivateMissing() + public function testAsymmetricPrivateMissing() { $this->expectException(SecretMissingException::class); $this->provider = new JWTProviderStub(null, 'RS256', ['public' => '123', 'private' => null]); @@ -49,8 +46,7 @@ public function asymmetricPrivateMissing() $this->assertSame(null, $this->provider->getSecret()); } - /** @test */ - public function symmetricKeyMissing() + public function testSymmetricKeyMissing() { $this->expectException(SecretMissingException::class); $this->provider = new JWTProviderStub(null, 'RS256', ['public' => null, 'private' => null]); diff --git a/tests/Providers/JWT/ProviderTest.php b/tests/Providers/JWT/ProviderTest.php index be1df7cc..69dca45f 100644 --- a/tests/Providers/JWT/ProviderTest.php +++ b/tests/Providers/JWT/ProviderTest.php @@ -29,16 +29,14 @@ public function setUp(): void $this->provider = new JWTProviderStub('secret', 'HS256', []); } - /** @test */ - public function itShouldSetTheAlgo() + public function testItShouldSetTheAlgo() { $this->provider->setAlgo('HS512'); $this->assertSame('HS512', $this->provider->getAlgo()); } - /** @test */ - public function itShouldSetTheSecret() + public function testItShouldSetTheSecret() { $this->provider->setSecret('foo'); diff --git a/tests/Providers/Storage/IlluminateTest.php b/tests/Providers/Storage/IlluminateTest.php index 6e548c86..0c2efe86 100644 --- a/tests/Providers/Storage/IlluminateTest.php +++ b/tests/Providers/Storage/IlluminateTest.php @@ -38,40 +38,35 @@ public function setUp(): void $this->storage = new Storage($this->cache); } - /** @test */ - public function itShouldAddTheItemToStorage() + public function testItShouldAddTheItemToStorage() { $this->cache->shouldReceive('put')->with('foo', 'bar', 10)->once(); $this->storage->add('foo', 'bar', 10); } - /** @test */ - public function itShouldAddTheItemToStorageForever() + public function testItShouldAddTheItemToStorageForever() { $this->cache->shouldReceive('forever')->with('foo', 'bar')->once(); $this->storage->forever('foo', 'bar'); } - /** @test */ - public function itShouldGetAnItemFromStorage() + public function testItShouldGetAnItemFromStorage() { $this->cache->shouldReceive('get')->with('foo')->once()->andReturn(['foo' => 'bar']); $this->assertSame(['foo' => 'bar'], $this->storage->get('foo')); } - /** @test */ - public function itShouldRemoveTheItemFromStorage() + public function testItShouldRemoveTheItemFromStorage() { $this->cache->shouldReceive('forget')->with('foo')->once()->andReturn(true); $this->assertTrue($this->storage->destroy('foo')); } - /** @test */ - public function itShouldRemoveAllItemsFromStorage() + public function testItShouldRemoveAllItemsFromStorage() { $this->cache->shouldReceive('flush')->withNoArgs()->once(); @@ -80,8 +75,7 @@ public function itShouldRemoveAllItemsFromStorage() // Duplicate tests for tagged storage -------------------- - /** @test */ - public function itShouldAddTheItemToTaggedStorage() + public function testItShouldAddTheItemToTaggedStorage() { $this->emulateTags(); $this->cache->shouldReceive('put')->with('foo', 'bar', 10)->once(); @@ -92,8 +86,6 @@ public function itShouldAddTheItemToTaggedStorage() /** * Replace the storage with our one above that overrides the tag flag, and * define expectations for tags() method. - * - * @return void */ private function emulateTags() { @@ -102,8 +94,7 @@ private function emulateTags() $this->cache->shouldReceive('tags')->with('tymon.jwt')->once()->andReturn(\Mockery::self()); } - /** @test */ - public function itShouldAddTheItemToTaggedStorageForever() + public function testItShouldAddTheItemToTaggedStorageForever() { $this->emulateTags(); $this->cache->shouldReceive('forever')->with('foo', 'bar')->once(); @@ -111,8 +102,7 @@ public function itShouldAddTheItemToTaggedStorageForever() $this->storage->forever('foo', 'bar'); } - /** @test */ - public function itShouldGetAnItemFromTaggedStorage() + public function testItShouldGetAnItemFromTaggedStorage() { $this->emulateTags(); $this->cache->shouldReceive('get')->with('foo')->once()->andReturn(['foo' => 'bar']); @@ -120,8 +110,7 @@ public function itShouldGetAnItemFromTaggedStorage() $this->assertSame(['foo' => 'bar'], $this->storage->get('foo')); } - /** @test */ - public function itShouldRemoveTheItemFromTaggedStorage() + public function testItShouldRemoveTheItemFromTaggedStorage() { $this->emulateTags(); $this->cache->shouldReceive('forget')->with('foo')->once()->andReturn(true); @@ -129,8 +118,7 @@ public function itShouldRemoveTheItemFromTaggedStorage() $this->assertTrue($this->storage->destroy('foo')); } - /** @test */ - public function itShouldRemoveAllTaggedItemsFromStorage() + public function testItShouldRemoveAllTaggedItemsFromStorage() { $this->emulateTags(); $this->cache->shouldReceive('flush')->withNoArgs()->once(); diff --git a/tests/TokenTest.php b/tests/TokenTest.php index c4779a53..94c3c0b9 100644 --- a/tests/TokenTest.php +++ b/tests/TokenTest.php @@ -28,14 +28,12 @@ public function setUp(): void $this->token = new Token('foo.bar.baz'); } - /** @test */ - public function itShouldReturnTheTokenWhenCastingToAString() + public function testItShouldReturnTheTokenWhenCastingToAString() { $this->assertEquals((string) $this->token, $this->token); } - /** @test */ - public function itShouldReturnTheTokenWhenCallingGetMethod() + public function testItShouldReturnTheTokenWhenCallingGetMethod() { $this->assertIsString($this->token->get()); } diff --git a/tests/Validators/PayloadValidatorTest.php b/tests/Validators/PayloadValidatorTest.php index 6fbf0813..34d8276a 100644 --- a/tests/Validators/PayloadValidatorTest.php +++ b/tests/Validators/PayloadValidatorTest.php @@ -37,11 +37,9 @@ public function setUp(): void } /** - * @test - * * @throws InvalidClaimException */ - public function itShouldReturnTrueWhenProvidingAValidPayload() + public function testItShouldReturnTrueWhenProvidingAValidPayload() { $claims = [ new Subject(1), @@ -58,11 +56,9 @@ public function itShouldReturnTrueWhenProvidingAValidPayload() } /** - * @test - * * @throws InvalidClaimException */ - public function itShouldThrowAnExceptionWhenProvidingAnExpiredPayload() + public function testItShouldThrowAnExceptionWhenProvidingAnExpiredPayload() { $this->expectException(TokenExpiredException::class); $this->expectExceptionMessage('Token has expired'); @@ -82,11 +78,9 @@ public function itShouldThrowAnExceptionWhenProvidingAnExpiredPayload() } /** - * @test - * * @throws InvalidClaimException */ - public function itShouldThrowAnExceptionWhenProvidingAnInvalidNbfClaim() + public function testItShouldThrowAnExceptionWhenProvidingAnInvalidNbfClaim() { $this->expectException(TokenInvalidException::class); $this->expectExceptionMessage('Not Before (nbf) timestamp cannot be in the future'); @@ -105,8 +99,7 @@ public function itShouldThrowAnExceptionWhenProvidingAnInvalidNbfClaim() $this->validator->check($collection); } - /** @test */ - public function itShouldThrowAnExceptionWhenProvidingAnInvalidIatClaim() + public function testItShouldThrowAnExceptionWhenProvidingAnInvalidIatClaim() { $this->expectException(InvalidClaimException::class); $this->expectExceptionMessage('Invalid value provided for claim [iat]'); @@ -126,11 +119,9 @@ public function itShouldThrowAnExceptionWhenProvidingAnInvalidIatClaim() } /** - * @test - * * @throws InvalidClaimException */ - public function itShouldThrowAnExceptionWhenProvidingAnInvalidPayload() + public function testItShouldThrowAnExceptionWhenProvidingAnInvalidPayload() { $this->expectException(TokenInvalidException::class); $this->expectExceptionMessage('JWT payload does not contain the required claims'); @@ -145,8 +136,7 @@ public function itShouldThrowAnExceptionWhenProvidingAnInvalidPayload() $this->validator->check($collection); } - /** @test */ - public function itShouldThrowAnExceptionWhenProvidingAnInvalidExpiry() + public function testItShouldThrowAnExceptionWhenProvidingAnInvalidExpiry() { $this->expectException(InvalidClaimException::class); $this->expectExceptionMessage('Invalid value provided for claim [exp]'); @@ -166,11 +156,9 @@ public function itShouldThrowAnExceptionWhenProvidingAnInvalidExpiry() } /** - * @test - * * @throws InvalidClaimException */ - public function itShouldSetTheRequiredClaims() + public function testItShouldSetTheRequiredClaims() { $claims = [ new Subject(1), @@ -183,11 +171,9 @@ public function itShouldSetTheRequiredClaims() } /** - * @test - * * @throws InvalidClaimException */ - public function itShouldCheckTheTokenInTheRefreshContext() + public function testItShouldCheckTheTokenInTheRefreshContext() { $claims = [ new Subject(1), @@ -206,11 +192,9 @@ public function itShouldCheckTheTokenInTheRefreshContext() } /** - * @test - * * @throws InvalidClaimException */ - public function itShouldReturnTrueIfTheRefreshTtlIsNull() + public function testItShouldReturnTrueIfTheRefreshTtlIsNull() { $claims = [ new Subject(1), @@ -229,11 +213,9 @@ public function itShouldReturnTrueIfTheRefreshTtlIsNull() } /** - * @test - * * @throws InvalidClaimException */ - public function itShouldThrowAnExceptionIfTheTokenCannotBeRefreshed() + public function testItShouldThrowAnExceptionIfTheTokenCannotBeRefreshed() { $this->expectException(TokenExpiredException::class); $this->expectExceptionMessage('Token has expired and can no longer be refreshed'); diff --git a/tests/Validators/TokenValidatorTest.php b/tests/Validators/TokenValidatorTest.php index 9880c07a..5fee8ef7 100644 --- a/tests/Validators/TokenValidatorTest.php +++ b/tests/Validators/TokenValidatorTest.php @@ -15,6 +15,7 @@ use PHPOpenSourceSaver\JWTAuth\Exceptions\TokenInvalidException; use PHPOpenSourceSaver\JWTAuth\Test\AbstractTestCase; use PHPOpenSourceSaver\JWTAuth\Validators\TokenValidator; +use PHPUnit\Framework\Attributes\DataProviderExternal; class TokenValidatorTest extends AbstractTestCase { @@ -30,30 +31,19 @@ public function setUp(): void $this->validator = new TokenValidator(); } - /** @test */ - public function itShouldReturnTrueWhenProvidingAWellFormedToken() + public function testItShouldReturnTrueWhenProvidingAWellFormedToken() { $this->assertTrue($this->validator->isValid('one.two.three')); } - /** - * @test - * - * @dataProvider \PHPOpenSourceSaver\JWTAuth\Test\Validators\TokenValidatorTest::dataProviderMalformedTokens - * - * @param string $token - */ - public function itShouldReturnFalseWhenProvidingAMalformedToken($token) + #[DataProviderExternal(TokenValidatorTest::class, 'dataProviderMalformedTokens')] + public function testItShouldReturnFalseWhenProvidingAMalformedToken(string $token) { $this->assertFalse($this->validator->isValid($token)); } - /** - * @test - * - * @dataProvider \PHPOpenSourceSaver\JWTAuth\Test\Validators\TokenValidatorTest::dataProviderMalformedTokens - */ - public function itShouldThrowAnExceptionWhenProvidingAMalformedToken($token) + #[DataProviderExternal(TokenValidatorTest::class, 'dataProviderMalformedTokens')] + public function testItShouldThrowAnExceptionWhenProvidingAMalformedToken($token) { $this->expectException(TokenInvalidException::class); $this->expectExceptionMessage('Malformed token'); @@ -61,22 +51,14 @@ public function itShouldThrowAnExceptionWhenProvidingAMalformedToken($token) $this->validator->check($token); } - /** - * @test - * - * @dataProvider \PHPOpenSourceSaver\JWTAuth\Test\Validators\TokenValidatorTest::dataProviderTokensWithWrongSegmentsNumber - */ - public function itShouldReturnFalseWhenProvidingATokenWithWrongSegmentsNumber($token) + #[DataProviderExternal(TokenValidatorTest::class, 'dataProviderTokensWithWrongSegmentsNumber')] + public function testItShouldReturnFalseWhenProvidingATokenWithWrongSegmentsNumber($token) { $this->assertFalse($this->validator->isValid($token)); } - /** - * @test - * - * @dataProvider \PHPOpenSourceSaver\JWTAuth\Test\Validators\TokenValidatorTest::dataProviderTokensWithWrongSegmentsNumber - */ - public function itShouldThrowAnExceptionWhenProvidingAMalformedTokenWithWrongSegmentsNumber($token) + #[DataProviderExternal(TokenValidatorTest::class, 'dataProviderTokensWithWrongSegmentsNumber')] + public function testItShouldThrowAnExceptionWhenProvidingAMalformedTokenWithWrongSegmentsNumber($token) { $this->expectException(TokenInvalidException::class); $this->expectExceptionMessage('Wrong number of segments');