Passed
Push — master ( e9cdac...1d7587 )
by Gabriel
02:26
created

TokenTest::test_setUserIdentifier_writeData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 4
c 1
b 1
f 0
dl 0
loc 7
rs 10
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 Nip\Collections\Collection;
11
use Nip\Records\Locator\ModelLocator;
12
13
/**
14
 * Class TokenTest
15
 * @package ByTIC\Hello\Tests\Models\AccessTokens
16
 */
17
class TokenTest extends AbstractTest
18
{
19
20
    public function testSetIdentifier()
21
    {
22
        /** @var Clients $tokens */
23
        $tokens = \Mockery::mock(Tokens::class)->makePartial();
24
        $tokens->shouldReceive('getFields')->andReturn(['id', 'identifier', 'secret']);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not exist on ByTIC\Hello\Models\Clients\Clients. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

24
        $tokens->/** @scrutinizer ignore-call */ 
25
                 shouldReceive('getFields')->andReturn(['id', 'identifier', 'secret']);
Loading history...
Bug introduced by
The method andReturn() does not exist on Nip\Records\Collections\Collection. ( Ignorable by Annotation )

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

24
        $tokens->shouldReceive('getFields')->/** @scrutinizer ignore-call */ andReturn(['id', 'identifier', 'secret']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method andReturn() does not exist on ByTIC\Hello\Models\Clients\Clients. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

24
        $tokens->shouldReceive('getFields')->/** @scrutinizer ignore-call */ andReturn(['id', 'identifier', 'secret']);
Loading history...
25
        $tokens->shouldReceive('getPrimaryKey')->andReturn('id');
26
        $tokens->shouldReceive('getModel')->andReturn(Token::class);
27
28
        $token = $tokens->getNew();
29
        self::assertNull($token->getIdentifier());
30
31
        $token->setIdentifier(99);
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 testGetClient()
75
    {
76
        /** @var Tokens $tokens */
77
        $tokens = \Mockery::mock(Tokens::class)->makePartial();
78
        $tokens->shouldReceive('getPrimaryKey')->andReturn('id');
0 ignored issues
show
Bug introduced by
The method andReturn() does not exist on ByTIC\Hello\Models\AccessTokens\Tokens. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

78
        $tokens->shouldReceive('getPrimaryKey')->/** @scrutinizer ignore-call */ andReturn('id');
Loading history...
Bug introduced by
The method shouldReceive() does not exist on ByTIC\Hello\Models\AccessTokens\Tokens. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

78
        $tokens->/** @scrutinizer ignore-call */ 
79
                 shouldReceive('getPrimaryKey')->andReturn('id');
Loading history...
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';
0 ignored issues
show
Documentation Bug introduced by
The property $client_id was declared of type integer, but '999999' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
90
91
        $client = $token->getClient();
92
        self::assertInstanceOf(Client::class, $client);
93
    }
94
}
95