TokenTest::test_getClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 19
rs 9.8333
cc 1
nc 1
nop 0
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 Mockery\Mock;
11
use Nip\Collections\Collection;
12
use Nip\Records\Locator\ModelLocator;
13
14
/**
15
 * Class TokenTest
16
 * @package ByTIC\Hello\Tests\Models\AccessTokens
17
 */
18
class TokenTest extends AbstractTest
19
{
20
    public function test_setIdentifier()
21
    {
22
        /** @var Clients|Mock $tokens */
23
        $tokens = \Mockery::mock(Tokens::class)->makePartial();
24
        $tokens->shouldReceive('getFields')->andReturn(['id', 'identifier', 'secret']);
25
        $tokens->shouldReceive('getPrimaryKey')->andReturn('id');
26
        $tokens->shouldReceive('getModel')->andReturn(Token::class);
27
28
        $token = $tokens->getNew();
29
        self::assertNull($token->getIdentifier());
0 ignored issues
show
Bug introduced by
The method getIdentifier() does not exist on Mockery\LegacyMockInterface. It seems like you code against a sub-type of Mockery\LegacyMockInterface such as Mockery\Mock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        self::assertNull($token->/** @scrutinizer ignore-call */ getIdentifier());
Loading history...
30
31
        $token->setIdentifier(99);
0 ignored issues
show
Bug introduced by
The method setIdentifier() does not exist on Mockery\LegacyMockInterface. It seems like you code against a sub-type of Mockery\LegacyMockInterface such as Mockery\Mock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        $token->/** @scrutinizer ignore-call */ 
32
                setIdentifier(99);
Loading history...
32
        self::assertSame(99, $token->getIdentifier());
33
34
        $data = $tokens->getQueryModelData($token);
35
        self::assertArrayHasKey('identifier', $data);
36
    }
37
38
    public function test_setUserIdentifier_property()
39
    {
40
        $token = new Token();
41
        $token->user_id = 3;
42
43
        self::assertSame(3, $token->getUserIdentifier());
44
        self::assertSame(3, $token->getAttribute('user_id'));
45
    }
46
47
    public function test_setUserIdentifier_writeData()
48
    {
49
        $token = new Token();
50
        $token->writeData(['user_id' => 3]);
51
52
        self::assertSame(3, $token->getUserIdentifier());
53
        self::assertSame(3, $token->getAttribute('user_id'));
54
    }
55
56
    public function test_setExpiresAt_property()
57
    {
58
        $token = new Token();
59
        $token->expires_at = '2030-09-09';
60
61
        self::assertSame('2030-09-09', $token->getExpiryDateTime()->format('Y-m-d'));
62
        self::assertSame('2030-09-09', $token->getAttribute('expires_at'));
63
    }
64
65
    public function test_setExpiresAt_writeData()
66
    {
67
        $token = new Token();
68
        $token->writeData(['expires_at' => '2030-09-09']);
69
70
        self::assertSame('2030-09-09', $token->getExpiryDateTime()->format('Y-m-d'));
71
        self::assertSame('2030-09-09', $token->getAttribute('expires_at'));
72
    }
73
74
    public function test_getClient()
75
    {
76
        /** @var Tokens|Mock $tokens */
77
        $tokens = \Mockery::mock(Tokens::class)->makePartial();
78
        $tokens->shouldReceive('getPrimaryKey')->andReturn('id');
79
        $tokens->shouldReceive('getModel')->andReturn(Token::class);
80
81
        $clients = \Mockery::mock(Clients::class)->makePartial();
82
        $clients->shouldReceive('findByField')->withArgs([
83
            'identifier',
84
            '999999'
85
        ])->andReturn(new Collection([new Client()]));
86
        ModelLocator::set(Clients::class, $clients);
0 ignored issues
show
Bug introduced by
$clients of type Mockery\Mock is incompatible with the type Nip\Records\AbstractModels\RecordManager expected by parameter $entityManager of Nip\Records\Locator\ModelLocator::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
        ModelLocator::set(Clients::class, /** @scrutinizer ignore-type */ $clients);
Loading history...
87
88
        $token = $tokens->getNew();
89
        $token->client_id = '999999';
90
91
        $client = $token->getClient();
0 ignored issues
show
Bug introduced by
The method getClient() does not exist on Mockery\LegacyMockInterface. It seems like you code against a sub-type of Mockery\LegacyMockInterface such as Mockery\Mock. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        /** @scrutinizer ignore-call */ 
92
        $client = $token->getClient();
Loading history...
92
        self::assertInstanceOf(Client::class, $client);
93
    }
94
95
    public function test_getScopes()
96
    {
97
        $token = new Token();
98
        $scopes = $token->getScopes();
99
        self::assertIsArray($scopes);
100
        self::assertCount(0, $scopes);
101
102
        $token->writeData(['scopes' => '']);
103
        $scopes = $token->getScopes();
104
        self::assertIsArray($scopes);
105
        self::assertCount(0, $scopes);
106
    }
107
}
108