Skip to content

Commit e6b1940

Browse files
Fix unit tests
- Replaced class and uses statements for qualified imports.
1 parent 281fd23 commit e6b1940

25 files changed

+222
-205
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ composer.lock
33
build
44
coverage.xml
55
.phpunit.result.cache
6+
.idea

tests/BlacklistTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace PHPOpenSourceSaver\JWTAuth\Test;
1313

1414
use Mockery;
15+
use Mockery\MockInterface;
1516
use PHPOpenSourceSaver\JWTAuth\Blacklist;
1617
use PHPOpenSourceSaver\JWTAuth\Claims\Collection;
1718
use PHPOpenSourceSaver\JWTAuth\Claims\Expiration;
@@ -23,21 +24,22 @@
2324
use PHPOpenSourceSaver\JWTAuth\Contracts\Providers\Storage;
2425
use PHPOpenSourceSaver\JWTAuth\Payload;
2526
use PHPOpenSourceSaver\JWTAuth\Validators\PayloadValidator;
27+
use PHPOpenSourceSaver\JWTAuth\Validators\Validator;
2628

2729
class BlacklistTest extends AbstractTestCase
2830
{
2931
/**
30-
* @var \PHPOpenSourceSaver\JWTAuth\Contracts\Providers\Storage|\Mockery\MockInterface
32+
* @var Storage|MockInterface
3133
*/
3234
protected $storage;
3335

3436
/**
35-
* @var \PHPOpenSourceSaver\JWTAuth\Blacklist
37+
* @var Blacklist
3638
*/
3739
protected $blacklist;
3840

3941
/**
40-
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Validators\Validator
42+
* @var MockInterface|Validator
4143
*/
4244
protected $validator;
4345

tests/Claims/ClaimTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class ClaimTest extends AbstractTestCase
2020
{
2121
/**
22-
* @var \PHPOpenSourceSaver\JWTAuth\Claims\Expiration
22+
* @var Expiration
2323
*/
2424
protected $claim;
2525

@@ -48,7 +48,7 @@ public function it_should_convert_the_claim_to_an_array()
4848
/** @test */
4949
public function it_should_get_the_claim_as_a_string()
5050
{
51-
$this->assertJsonStringEqualsJsonString((string) $this->claim, $this->claim->toJson());
51+
$this->assertJsonStringEqualsJsonString((string)$this->claim, $this->claim->toJson());
5252
}
5353

5454
/** @test */

tests/Claims/CollectionTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222

2323
class CollectionTest extends AbstractTestCase
2424
{
25+
/** @test */
26+
public function it_should_sanitize_the_claims_to_associative_array()
27+
{
28+
$collection = $this->getCollection();
29+
30+
$this->assertSame(array_keys($collection->toArray()), ['sub', 'iss', 'exp', 'nbf', 'iat', 'jti']);
31+
}
32+
2533
private function getCollection()
2634
{
2735
$claims = [
@@ -36,14 +44,6 @@ private function getCollection()
3644
return new Collection($claims);
3745
}
3846

39-
/** @test */
40-
public function it_should_sanitize_the_claims_to_associative_array()
41-
{
42-
$collection = $this->getCollection();
43-
44-
$this->assertSame(array_keys($collection->toArray()), ['sub', 'iss', 'exp', 'nbf', 'iat', 'jti']);
45-
}
46-
4747
/** @test */
4848
public function it_should_determine_if_a_collection_contains_all_the_given_claims()
4949
{

tests/Claims/DatetimeClaimTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use DateTimeImmutable;
1818
use DateTimeInterface;
1919
use Mockery;
20+
use Mockery\MockInterface;
2021
use PHPOpenSourceSaver\JWTAuth\Claims\Collection;
2122
use PHPOpenSourceSaver\JWTAuth\Claims\Expiration;
2223
use PHPOpenSourceSaver\JWTAuth\Claims\IssuedAt;
@@ -31,7 +32,7 @@
3132
class DatetimeClaimTest extends AbstractTestCase
3233
{
3334
/**
34-
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Validators\PayloadValidator
35+
* @var MockInterface|PayloadValidator
3536
*/
3637
protected $validator;
3738

@@ -109,7 +110,7 @@ public function it_should_handle_datetime_claims()
109110
/** @test */
110111
public function it_should_handle_datetime_immutable_claims()
111112
{
112-
$testDateTimeImmutable = DateTimeImmutable::createFromFormat('U', (string) $this->testNowTimestamp);
113+
$testDateTimeImmutable = DateTimeImmutable::createFromFormat('U', (string)$this->testNowTimestamp);
113114

114115
$this->assertInstanceOf(DateTimeImmutable::class, $testDateTimeImmutable);
115116
$this->assertInstanceOf(DatetimeInterface::class, $testDateTimeImmutable);

tests/Claims/FactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class FactoryTest extends AbstractTestCase
2727
{
2828
/**
29-
* @var \PHPOpenSourceSaver\JWTAuth\Claims\Factory
29+
* @var Factory
3030
*/
3131
protected $factory;
3232

tests/FactoryTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace PHPOpenSourceSaver\JWTAuth\Test;
1313

1414
use Mockery;
15+
use Mockery\MockInterface;
1516
use PHPOpenSourceSaver\JWTAuth\Claims\Collection;
1617
use PHPOpenSourceSaver\JWTAuth\Claims\Custom;
1718
use PHPOpenSourceSaver\JWTAuth\Claims\Expiration;
@@ -28,17 +29,17 @@
2829
class FactoryTest extends AbstractTestCase
2930
{
3031
/**
31-
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Claims\Factory
32+
* @var MockInterface|ClaimFactory
3233
*/
3334
protected $claimFactory;
3435

3536
/**
36-
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Validators\PayloadValidator
37+
* @var MockInterface|PayloadValidator
3738
*/
3839
protected $validator;
3940

4041
/**
41-
* @var \PHPOpenSourceSaver\JWTAuth\Factory
42+
* @var Factory
4243
*/
4344
protected $factory;
4445

tests/Http/ParserTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,15 @@ public function it_should_return_the_token_from_route()
334334
$this->assertTrue($parser->hasToken());
335335
}
336336

337+
protected function getRouteMock($expectedParameterValue = null, $expectedParameterName = 'token')
338+
{
339+
return Mockery::mock(Route::class)
340+
->shouldReceive('parameter')
341+
->with($expectedParameterName)
342+
->andReturn($expectedParameterValue)
343+
->getMock();
344+
}
345+
337346
/** @test */
338347
public function it_should_return_the_token_from_route_with_a_custom_param()
339348
{
@@ -460,7 +469,7 @@ public function it_should_retrieve_the_chain_with_alias()
460469
new RouteParams,
461470
];
462471

463-
/* @var \Illuminate\Http\Request $request */
472+
/* @var Request $request */
464473
$request = Mockery::mock(Request::class);
465474

466475
$parser = new Parser($request);
@@ -508,13 +517,4 @@ public function it_should_add_multiple_custom_parser()
508517
$this->assertSame($parser->parseToken(), 'foobar');
509518
$this->assertTrue($parser->hasToken());
510519
}
511-
512-
protected function getRouteMock($expectedParameterValue = null, $expectedParameterName = 'token')
513-
{
514-
return Mockery::mock(Route::class)
515-
->shouldReceive('parameter')
516-
->with($expectedParameterName)
517-
->andReturn($expectedParameterValue)
518-
->getMock();
519-
}
520520
}

tests/JWTAuthTest.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Illuminate\Http\Request;
1515
use Mockery;
16-
use stdClass;
16+
use Mockery\MockInterface;
1717
use PHPOpenSourceSaver\JWTAuth\Contracts\Providers\Auth;
1818
use PHPOpenSourceSaver\JWTAuth\Exceptions\JWTException;
1919
use PHPOpenSourceSaver\JWTAuth\Exceptions\TokenInvalidException;
@@ -24,26 +24,27 @@
2424
use PHPOpenSourceSaver\JWTAuth\Payload;
2525
use PHPOpenSourceSaver\JWTAuth\Test\Stubs\UserStub;
2626
use PHPOpenSourceSaver\JWTAuth\Token;
27+
use stdClass;
2728

2829
class JWTAuthTest extends AbstractTestCase
2930
{
3031
/**
31-
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Manager
32+
* @var MockInterface|Manager
3233
*/
3334
protected $manager;
3435

3536
/**
36-
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Contracts\Providers\Auth
37+
* @var MockInterface|Auth
3738
*/
3839
protected $auth;
3940

4041
/**
41-
* @var \Mockery\MockInterface|\PHPOpenSourceSaver\JWTAuth\Http\Parser\Parser
42+
* @var MockInterface|Parser
4243
*/
4344
protected $parser;
4445

4546
/**
46-
* @var \PHPOpenSourceSaver\JWTAuth\JWTAuth
47+
* @var JWTAuth
4748
*/
4849
protected $jwtAuth;
4950

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

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

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

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

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

@@ -94,8 +95,8 @@ public function it_should_pass_provider_check_if_hash_matches_when_provider_is_n
9495
$payloadFactory = Mockery::mock(Factory::class);
9596
$payloadFactory->shouldReceive('make')->andReturn(Mockery::mock(Payload::class));
9697
$payloadFactory->shouldReceive('get')
97-
->with('prv')
98-
->andReturnNull();
98+
->with('prv')
99+
->andReturnNull();
99100

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)