Test Failed
Push — master ( a6b51e...5fffdb )
by Gabriel
08:05
created

ClientHasGrantsTraitTest::testAddGrants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\Hello\Tests\Models\Clients\Traits;
4
5
use ByTIC\Hello\Models\Clients\Client;
6
use ByTIC\Hello\Models\Clients\Clients;
7
use ByTIC\Hello\Tests\AbstractTest;
8
use ByTIC\Hello\Utility\GrantsHelper;
9
10
/**
11
 * Class ClientHasGrantsTraitTest
12
 * @package ByTIC\Hello\Tests\Models\Clients\Traits
13
 */
14
class ClientHasGrantsTraitTest extends AbstractTest
15
{
16
    public function testNewClientGrants()
17
    {
18
        $client = new Client();
19
        self::assertSame([], $client->getGrants());
20
    }
21
22
    public function testAddGrants()
23
    {
24
        $client = new Client();
25
26
        $client->addGrants(GrantsHelper::GRANT_TYPE_EXTENSIONS);
27
        self::assertSame([GrantsHelper::GRANT_TYPE_EXTENSIONS], $client->getGrants());
28
29
        $client->addGrants(GrantsHelper::GRANT_TYPE_EXTENSIONS);
30
        self::assertSame([GrantsHelper::GRANT_TYPE_EXTENSIONS], $client->getGrants());
31
32
        $client->addGrants(GrantsHelper::GRANT_TYPE_AUTH_CODE);
33
        self::assertSame(
34
            [GrantsHelper::GRANT_TYPE_EXTENSIONS, GrantsHelper::GRANT_TYPE_AUTH_CODE],
35
            $client->getGrants()
36
        );
37
    }
38
39
    public function testHasGrant()
40
    {
41
        $client = new Client();
42
        self::assertFalse($client->hasGrant(GrantsHelper::GRANT_TYPE_AUTH_CODE));
43
44
        $client->addGrants(GrantsHelper::GRANT_TYPE_EXTENSIONS);
45
        self::assertFalse($client->hasGrant(GrantsHelper::GRANT_TYPE_AUTH_CODE));
46
47
        $client->addGrants(GrantsHelper::GRANT_TYPE_AUTH_CODE);
48
        self::assertTrue($client->hasGrant(GrantsHelper::GRANT_TYPE_AUTH_CODE));
49
    }
50
51
    public function testGrantsSave()
52
    {
53
        /** @var Clients $clients */
54
        $clients = \Mockery::mock(Clients::class)->makePartial();
55
        $clients->shouldReceive('getFields')->andReturn(['id', 'identifier', 'grant_types']);
0 ignored issues
show
Documentation Bug introduced by
The method shouldReceive does not exist on object<ByTIC\Hello\Models\Clients\Clients>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
56
        $clients->shouldReceive('getPrimaryKey')->andReturn('id');
0 ignored issues
show
Documentation Bug introduced by
The method shouldReceive does not exist on object<ByTIC\Hello\Models\Clients\Clients>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
57
        $clients->shouldReceive('getModel')->andReturn(Client::class);
0 ignored issues
show
Documentation Bug introduced by
The method shouldReceive does not exist on object<ByTIC\Hello\Models\Clients\Clients>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
58
59
        $client = $clients->getNew();
60
        $client->addGrants([GrantsHelper::GRANT_TYPE_PERSONAL_ACCESS, GrantsHelper::GRANT_TYPE_AUTH_CODE]);
61
62
        $data = $clients->getQueryModelData($client);
63
        self::assertArrayHasKey('grant_types', $data);
64
    }
65
66
    /**
67
     * @dataProvider dataInitFromDB()
68
     * @param $grantDb
69
     * @param $grantArray
70
     */
71
    public function testInitFromDB($grantDb, $grantArray)
72
    {
73
        $client = new Client();
74
        $client->writeData(['id' => 9, 'grant_types' => $grantDb]);
0 ignored issues
show
Documentation introduced by
array('id' => 9, 'grant_types' => $grantDb) is of type array<string,?,{"id":"in...er","grant_types":"?"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
76
        self::assertSame($grantArray, $client->getGrants());
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public static function dataInitFromDB()
83
    {
84
        return [
85
            ['', []],
86
            ['personal_access', ['personal_access']],
87
            ['personal_access,authorization_code', ['personal_access', 'authorization_code']],
88
        ];
89
    }
90
}
91