Skip to content

Commit

Permalink
Merge pull request #13 from Messhias/issues/fix-units-tests
Browse files Browse the repository at this point in the history
Fix unit tests
  • Loading branch information
eschricker authored Sep 21, 2021
2 parents c5d7538 + f312dce commit 9dc6001
Show file tree
Hide file tree
Showing 25 changed files with 222 additions and 205 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.lock
build
coverage.xml
.phpunit.result.cache
.idea
8 changes: 5 additions & 3 deletions tests/BlacklistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PHPOpenSourceSaver\JWTAuth\Test;

use Mockery;
use Mockery\MockInterface;
use PHPOpenSourceSaver\JWTAuth\Blacklist;
use PHPOpenSourceSaver\JWTAuth\Claims\Collection;
use PHPOpenSourceSaver\JWTAuth\Claims\Expiration;
Expand All @@ -23,21 +24,22 @@
use PHPOpenSourceSaver\JWTAuth\Contracts\Providers\Storage;
use PHPOpenSourceSaver\JWTAuth\Payload;
use PHPOpenSourceSaver\JWTAuth\Validators\PayloadValidator;
use PHPOpenSourceSaver\JWTAuth\Validators\Validator;

class BlacklistTest extends AbstractTestCase
{
/**
* @var \PHPOpenSourceSaver\JWTAuth\Contracts\Providers\Storage|\Mockery\MockInterface
* @var Storage|MockInterface
*/
protected $storage;

/**
* @var \PHPOpenSourceSaver\JWTAuth\Blacklist
* @var Blacklist
*/
protected $blacklist;

/**
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Validators\Validator
* @var MockInterface|Validator
*/
protected $validator;

Expand Down
4 changes: 2 additions & 2 deletions tests/Claims/ClaimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class ClaimTest extends AbstractTestCase
{
/**
* @var \PHPOpenSourceSaver\JWTAuth\Claims\Expiration
* @var Expiration
*/
protected $claim;

Expand Down Expand Up @@ -48,7 +48,7 @@ public function it_should_convert_the_claim_to_an_array()
/** @test */
public function it_should_get_the_claim_as_a_string()
{
$this->assertJsonStringEqualsJsonString((string) $this->claim, $this->claim->toJson());
$this->assertJsonStringEqualsJsonString((string)$this->claim, $this->claim->toJson());
}

/** @test */
Expand Down
16 changes: 8 additions & 8 deletions tests/Claims/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@

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

$this->assertSame(array_keys($collection->toArray()), ['sub', 'iss', 'exp', 'nbf', 'iat', 'jti']);
}

private function getCollection()
{
$claims = [
Expand All @@ -36,14 +44,6 @@ private function getCollection()
return new Collection($claims);
}

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

$this->assertSame(array_keys($collection->toArray()), ['sub', 'iss', 'exp', 'nbf', 'iat', 'jti']);
}

/** @test */
public function it_should_determine_if_a_collection_contains_all_the_given_claims()
{
Expand Down
5 changes: 3 additions & 2 deletions tests/Claims/DatetimeClaimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use DateTimeImmutable;
use DateTimeInterface;
use Mockery;
use Mockery\MockInterface;
use PHPOpenSourceSaver\JWTAuth\Claims\Collection;
use PHPOpenSourceSaver\JWTAuth\Claims\Expiration;
use PHPOpenSourceSaver\JWTAuth\Claims\IssuedAt;
Expand All @@ -31,7 +32,7 @@
class DatetimeClaimTest extends AbstractTestCase
{
/**
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Validators\PayloadValidator
* @var MockInterface|PayloadValidator
*/
protected $validator;

Expand Down Expand Up @@ -109,7 +110,7 @@ public function it_should_handle_datetime_claims()
/** @test */
public function it_should_handle_datetime_immutable_claims()
{
$testDateTimeImmutable = DateTimeImmutable::createFromFormat('U', (string) $this->testNowTimestamp);
$testDateTimeImmutable = DateTimeImmutable::createFromFormat('U', (string)$this->testNowTimestamp);

$this->assertInstanceOf(DateTimeImmutable::class, $testDateTimeImmutable);
$this->assertInstanceOf(DatetimeInterface::class, $testDateTimeImmutable);
Expand Down
2 changes: 1 addition & 1 deletion tests/Claims/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class FactoryTest extends AbstractTestCase
{
/**
* @var \PHPOpenSourceSaver\JWTAuth\Claims\Factory
* @var Factory
*/
protected $factory;

Expand Down
7 changes: 4 additions & 3 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PHPOpenSourceSaver\JWTAuth\Test;

use Mockery;
use Mockery\MockInterface;
use PHPOpenSourceSaver\JWTAuth\Claims\Collection;
use PHPOpenSourceSaver\JWTAuth\Claims\Custom;
use PHPOpenSourceSaver\JWTAuth\Claims\Expiration;
Expand All @@ -28,17 +29,17 @@
class FactoryTest extends AbstractTestCase
{
/**
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Claims\Factory
* @var MockInterface|ClaimFactory
*/
protected $claimFactory;

/**
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Validators\PayloadValidator
* @var MockInterface|PayloadValidator
*/
protected $validator;

/**
* @var \PHPOpenSourceSaver\JWTAuth\Factory
* @var Factory
*/
protected $factory;

Expand Down
20 changes: 10 additions & 10 deletions tests/Http/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,15 @@ public function it_should_return_the_token_from_route()
$this->assertTrue($parser->hasToken());
}

protected function getRouteMock($expectedParameterValue = null, $expectedParameterName = 'token')
{
return Mockery::mock(Route::class)
->shouldReceive('parameter')
->with($expectedParameterName)
->andReturn($expectedParameterValue)
->getMock();
}

/** @test */
public function it_should_return_the_token_from_route_with_a_custom_param()
{
Expand Down Expand Up @@ -460,7 +469,7 @@ public function it_should_retrieve_the_chain_with_alias()
new RouteParams,
];

/* @var \Illuminate\Http\Request $request */
/* @var Request $request */
$request = Mockery::mock(Request::class);

$parser = new Parser($request);
Expand Down Expand Up @@ -508,13 +517,4 @@ public function it_should_add_multiple_custom_parser()
$this->assertSame($parser->parseToken(), 'foobar');
$this->assertTrue($parser->hasToken());
}

protected function getRouteMock($expectedParameterValue = null, $expectedParameterName = 'token')
{
return Mockery::mock(Route::class)
->shouldReceive('parameter')
->with($expectedParameterName)
->andReturn($expectedParameterValue)
->getMock();
}
}
41 changes: 21 additions & 20 deletions tests/JWTAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Illuminate\Http\Request;
use Mockery;
use stdClass;
use Mockery\MockInterface;
use PHPOpenSourceSaver\JWTAuth\Contracts\Providers\Auth;
use PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException;
use PHPOpenSourceSaver\JWTAuth\Exceptions\TokenInvalidException;
Expand All @@ -24,26 +24,27 @@
use PHPOpenSourceSaver\JWTAuth\Payload;
use PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub;
use PHPOpenSourceSaver\JWTAuth\Token;
use stdClass;

class JWTAuthTest extends AbstractTestCase
{
/**
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Manager
* @var MockInterface|Manager
*/
protected $manager;

/**
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Contracts\Providers\Auth
* @var MockInterface|Auth
*/
protected $auth;

/**
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Http\Parser\Parser
* @var MockInterface|Parser
*/
protected $parser;

/**
* @var \PHPOpenSourceSaver\JWTAuth\JWTAuth
* @var JWTAuth
*/
protected $jwtAuth;

Expand All @@ -62,10 +63,10 @@ public function it_should_return_a_token_when_passing_a_user()
$payloadFactory->shouldReceive('make')->andReturn(Mockery::mock(Payload::class));

$this->manager
->shouldReceive('getPayloadFactory->customClaims')
->once()
->with(['sub' => 1, 'prv' => sha1('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub'), 'foo' => 'bar', 'role' => 'admin'])
->andReturn($payloadFactory);
->shouldReceive('getPayloadFactory->customClaims')
->once()
->with(['sub' => 1, 'prv' => sha1('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub'), 'foo' => 'bar', 'role' => 'admin'])
->andReturn($payloadFactory);

$this->manager->shouldReceive('encode->get')->once()->andReturn('foo.bar.baz');

Expand All @@ -80,8 +81,8 @@ public function it_should_pass_provider_check_if_hash_matches()
$payloadFactory = Mockery::mock(Factory::class);
$payloadFactory->shouldReceive('make')->andReturn(Mockery::mock(Payload::class));
$payloadFactory->shouldReceive('get')
->with('prv')
->andReturn(sha1('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub'));
->with('prv')
->andReturn(sha1('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub'));

$this->manager->shouldReceive('decode')->once()->andReturn($payloadFactory);

Expand All @@ -94,8 +95,8 @@ public function it_should_pass_provider_check_if_hash_matches_when_provider_is_n
$payloadFactory = Mockery::mock(Factory::class);
$payloadFactory->shouldReceive('make')->andReturn(Mockery::mock(Payload::class));
$payloadFactory->shouldReceive('get')
->with('prv')
->andReturnNull();
->with('prv')
->andReturnNull();

$this->manager->shouldReceive('decode')->once()->andReturn($payloadFactory);

Expand All @@ -108,8 +109,8 @@ public function it_should_not_pass_provider_check_if_hash_not_match()
$payloadFactory = Mockery::mock(Factory::class);
$payloadFactory->shouldReceive('make')->andReturn(Mockery::mock(Payload::class));
$payloadFactory->shouldReceive('get')
->with('prv')
->andReturn(sha1('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub1'));
->with('prv')
->andReturn(sha1('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub1'));

$this->manager->shouldReceive('decode')->once()->andReturn($payloadFactory);

Expand All @@ -123,10 +124,10 @@ public function it_should_return_a_token_when_passing_valid_credentials_to_attem
$payloadFactory->shouldReceive('make')->andReturn(Mockery::mock(Payload::class));

$this->manager
->shouldReceive('getPayloadFactory->customClaims')
->once()
->with(['sub' => 1, 'prv' => sha1('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub'), 'foo' => 'bar', 'role' => 'admin'])
->andReturn($payloadFactory);
->shouldReceive('getPayloadFactory->customClaims')
->once()
->with(['sub' => 1, 'prv' => sha1('PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub'), 'foo' => 'bar', 'role' => 'admin'])
->andReturn($payloadFactory);

$this->manager->shouldReceive('encode->get')->once()->andReturn('foo.bar.baz');

Expand Down Expand Up @@ -168,7 +169,7 @@ public function it_should_return_the_owning_user_from_a_token_containing_an_exis
$this->manager->shouldReceive('decode')->once()->andReturn($payload);

$this->auth->shouldReceive('byId')->once()->with(1)->andReturn(true);
$this->auth->shouldReceive('user')->once()->andReturn((object) ['id' => 1]);
$this->auth->shouldReceive('user')->once()->andReturn((object)['id' => 1]);

$user = $this->jwtAuth->setToken('foo.bar.baz')->customClaims(['foo' => 'bar'])->authenticate();

Expand Down
Loading

0 comments on commit 9dc6001

Please sign in to comment.