1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Hello\Tests\Models\AccessTokens; |
4
|
|
|
|
5
|
|
|
use ByTIC\Hello\Models\AccessTokens\Token; |
6
|
|
|
use ByTIC\Hello\Models\AccessTokens\Tokens; |
7
|
|
|
use ByTIC\Hello\Models\Clients\Client; |
8
|
|
|
use ByTIC\Hello\Models\Clients\Clients; |
9
|
|
|
use ByTIC\Hello\Tests\AbstractTest; |
10
|
|
|
use Nip\Collections\Collection; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class TokenTest |
14
|
|
|
* @package ByTIC\Hello\Tests\Models\AccessTokens |
15
|
|
|
*/ |
16
|
|
|
class TokenTest extends AbstractTest |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
View Code Duplication |
public function testSetIdentifier() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** @var Clients $tokens */ |
22
|
|
|
$tokens = \Mockery::mock(Tokens::class)->makePartial(); |
23
|
|
|
$tokens->shouldReceive('getFields')->andReturn(['id', 'identifier', 'secret']); |
|
|
|
|
24
|
|
|
$tokens->shouldReceive('getPrimaryKey')->andReturn('id'); |
|
|
|
|
25
|
|
|
$tokens->shouldReceive('getModel')->andReturn(Token::class); |
|
|
|
|
26
|
|
|
|
27
|
|
|
$token = $tokens->getNew(); |
28
|
|
|
self::assertNull($token->getIdentifier()); |
29
|
|
|
|
30
|
|
|
$token->setIdentifier(99); |
31
|
|
|
self::assertSame(99, $token->getIdentifier()); |
32
|
|
|
|
33
|
|
|
$data = $tokens->getQueryModelData($token); |
34
|
|
|
self::assertArrayHasKey('identifier', $data); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testGetClient() |
38
|
|
|
{ |
39
|
|
|
/** @var Tokens $tokens */ |
40
|
|
|
$tokens = \Mockery::mock(Tokens::class)->makePartial(); |
41
|
|
|
$tokens->shouldReceive('getPrimaryKey')->andReturn('id'); |
|
|
|
|
42
|
|
|
$tokens->shouldReceive('getModel')->andReturn(Token::class); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$clients = \Mockery::mock(Clients::class)->makePartial(); |
45
|
|
|
$clients->shouldReceive('findByField')->withArgs([ |
46
|
|
|
'identifier', |
47
|
|
|
'999999' |
48
|
|
|
])->andReturn(new Collection([new Client()])); |
49
|
|
|
$tokens->getRelation('Client')->setWith($clients); |
50
|
|
|
|
51
|
|
|
$token = $tokens->getNew(); |
52
|
|
|
$token->client_id = '999999'; |
53
|
|
|
|
54
|
|
|
$client = $token->getClient(); |
55
|
|
|
self::assertInstanceOf(Client::class, $client); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testGetUserIdentifierFromData() |
59
|
|
|
{ |
60
|
|
|
$token = new Token(); |
61
|
|
|
$token->writeData(['user_id' => 99]); |
|
|
|
|
62
|
|
|
|
63
|
|
|
self::assertEquals(99, $token->getUserIdentifier()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.