Skip to content

Commit

Permalink
Merge pull request #241 from mfn/mfn-phpunit
Browse files Browse the repository at this point in the history
chore: update phpunit and misc maintenance
  • Loading branch information
Messhias authored Mar 20, 2024
2 parents de4b752 + 35c9461 commit f794dbe
Show file tree
Hide file tree
Showing 29 changed files with 269 additions and 526 deletions.
27 changes: 11 additions & 16 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,25 @@ on:

jobs:
php-tests:
runs-on: ${{ matrix.os }}
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
45 changes: 15 additions & 30 deletions tests/BlacklistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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),
Expand Down Expand Up @@ -74,8 +74,7 @@ public function itShouldAddAValidTokenToTheBlacklist()
$this->blacklist->setRefreshTTL($refreshTTL)->add($payload);
}

/** @test */
public function itShouldAddATokenWithNoExpToTheBlacklistForever()
public function testItShouldAddATokenWithNoExpToTheBlacklistForever()
{
$claims = [
new Subject(1),
Expand All @@ -94,8 +93,7 @@ public function itShouldAddATokenWithNoExpToTheBlacklistForever()
$this->blacklist->add($payload);
}

/** @test */
public function itShouldReturnTrueWhenAddingAnExpiredTokenToTheBlacklist()
public function testItShouldReturnTrueWhenAddingAnExpiredTokenToTheBlacklist()
{
$claims = [
new Subject(1),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -238,8 +229,7 @@ public function itShouldCheckWhetherATokenHasBeenBlacklistedForever()
$this->assertTrue($this->blacklist->has($payload));
}

/** @test */
public function itShouldCheckWhetherATokenHasBeenBlacklistedWhenTheTokenIsNotBlacklisted()
public function testItShouldCheckWhetherATokenHasBeenBlacklistedWhenTheTokenIsNotBlacklisted()
{
$claims = [
new Subject(1),
Expand All @@ -260,8 +250,7 @@ public function itShouldCheckWhetherATokenHasBeenBlacklistedWhenTheTokenIsNotBla
$this->assertFalse($this->blacklist->has($payload));
}

/** @test */
public function itShouldRemoveATokenFromTheBlacklist()
public function testItShouldRemoveATokenFromTheBlacklist()
{
$claims = [
new Subject(1),
Expand All @@ -281,8 +270,7 @@ public function itShouldRemoveATokenFromTheBlacklist()
$this->assertTrue($this->blacklist->remove($payload));
}

/** @test */
public function itShouldSetACustomUniqueKeyForTheBlacklist()
public function testItShouldSetACustomUniqueKeyForTheBlacklist()
{
$claims = [
new Subject(1),
Expand All @@ -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());
Expand Down
15 changes: 5 additions & 10 deletions tests/Claims/ClaimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,30 @@ 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]');

$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);
}
Expand Down
9 changes: 3 additions & 6 deletions tests/Claims/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

class CollectionTest extends AbstractTestCase
{
/** @test */
public function itShouldSanitizeTheClaimsToAssociativeArray()
public function testItShouldSanitizeTheClaimsToAssociativeArray()
{
$collection = $this->getCollection();

Expand All @@ -45,8 +44,7 @@ private function getCollection()
return new Collection($claims);
}

/** @test */
public function itShouldDetermineIfACollectionContainsAllTheGivenClaims()
public function testItShouldDetermineIfACollectionContainsAllTheGivenClaims()
{
$collection = $this->getCollection();

Expand All @@ -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();

Expand Down
14 changes: 6 additions & 8 deletions tests/Claims/DatetimeClaimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -126,10 +124,10 @@ public function itShouldHandleDatetimeImmutableClaims()
$this->assertEquals($payloadTimestamp, $payloadDatetime);
}

/** @test
/**
* @throws InvalidClaimException
*/
public function itShouldHandleDatetintervalClaims()
public function testItShouldHandleDatetintervalClaims()
{
$testDateInterval = new \DateInterval('PT1H');

Expand Down
21 changes: 7 additions & 14 deletions tests/Claims/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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);
Expand All @@ -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);

Expand Down
Loading

0 comments on commit f794dbe

Please sign in to comment.