ClientsManagerTest::createDataProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 4
c 2
b 0
f 1
dl 0
loc 6
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ByTIC\Hello\Tests\Models\Clients\PersonalAccess;
4
5
use ByTIC\Hello\Models\Clients\Client;
6
use ByTIC\Hello\Models\Clients\Clients;
7
use ByTIC\Hello\Models\Clients\PersonalAccess\ClientsManager;
8
use ByTIC\Hello\Tests\AbstractTest;
9
use ByTIC\Hello\Utility\ClientsHelper;
10
use ByTIC\Hello\Utility\ModelsHelper;
11
use Nip\Collections\Collection;
12
13
/**
14
 * Class ClientsManagerTest
15
 * @package ByTIC\Hello\Tests\Models\Clients\PersonalAccess
16
 */
17
class ClientsManagerTest extends AbstractTest
18
{
19
    public function testGetWithPersonalAccessClientId()
20
    {
21
        $client = new Client();
22
23
        ClientsHelper::personalAccessClientId(99);
24
25
        $clientsManager = \Mockery::mock(Clients::class)->makePartial();
26
        $clientsManager->shouldReceive('findOne')->with(99)->andReturn($client);
27
        ModelsHelper::useClientsManager($clientsManager);
0 ignored issues
show
Bug introduced by
$clientsManager of type Mockery\Mock is incompatible with the type Nip\Records\RecordManager expected by parameter $manager of ByTIC\Hello\Utility\Mode...er::useClientsManager(). ( Ignorable by Annotation )

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

27
        ModelsHelper::useClientsManager(/** @scrutinizer ignore-type */ $clientsManager);
Loading history...
28
29
        $getClient = ClientsManager::get();
30
31
        self::assertSame($getClient, $client);
32
    }
33
34
    public function testGetWithFindByRedirect()
35
    {
36
        $client = new Client();
37
38
        $clientsManager = \Mockery::mock(Clients::class)->makePartial();
39
        $clientsManager->shouldReceive('findByRedirect')
40
            ->with(ClientsHelper::PERSONAL_ACCESS_REDIRECT_URI)
41
            ->andReturn(new Collection([$client]));
42
        ModelsHelper::useClientsManager($clientsManager);
0 ignored issues
show
Bug introduced by
$clientsManager of type Mockery\Mock is incompatible with the type Nip\Records\RecordManager expected by parameter $manager of ByTIC\Hello\Utility\Mode...er::useClientsManager(). ( Ignorable by Annotation )

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

42
        ModelsHelper::useClientsManager(/** @scrutinizer ignore-type */ $clientsManager);
Loading history...
43
44
        $getClient = ClientsManager::get();
45
46
        self::assertSame($getClient, $client);
47
    }
48
49
    public function testGetWithCreate()
50
    {
51
        $clientsManager = \Mockery::mock(Clients::class)->makePartial();
52
        $clientsManager->shouldReceive('getPrimaryKey')->andReturn('id');
53
        $clientsManager->shouldReceive('getModel')->andReturn(Client::class);
54
        $clientsManager->shouldReceive('findByRedirect')
55
            ->with(ClientsHelper::PERSONAL_ACCESS_REDIRECT_URI)
56
            ->andReturn(new Collection());
57
        $clientsManager->shouldReceive('save');
58
59
        ModelsHelper::useClientsManager($clientsManager);
0 ignored issues
show
Bug introduced by
$clientsManager of type Mockery\Mock is incompatible with the type Nip\Records\RecordManager expected by parameter $manager of ByTIC\Hello\Utility\Mode...er::useClientsManager(). ( Ignorable by Annotation )

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

59
        ModelsHelper::useClientsManager(/** @scrutinizer ignore-type */ $clientsManager);
Loading history...
60
61
        $client = ClientsManager::get();
62
63
        self::assertInstanceOf(Client::class, $client);
64
    }
65
66
67
    /**
68
     * @param null|string $name
69
     * @dataProvider createDataProvider
70
     */
71
    public function testCreate($nameIn, $nameOut)
72
    {
73
        $clientsManager = \Mockery::mock(Clients::class)->makePartial();
74
        $clientsManager->shouldReceive('getPrimaryKey')->andReturn('id');
75
        $clientsManager->shouldReceive('getModel')->andReturn(Client::class);
76
        $clientsManager->shouldReceive('save');
77
78
        ModelsHelper::useClientsManager($clientsManager);
0 ignored issues
show
Bug introduced by
$clientsManager of type Mockery\Mock is incompatible with the type Nip\Records\RecordManager expected by parameter $manager of ByTIC\Hello\Utility\Mode...er::useClientsManager(). ( Ignorable by Annotation )

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

78
        ModelsHelper::useClientsManager(/** @scrutinizer ignore-type */ $clientsManager);
Loading history...
79
        $client = ClientsManager::create($nameIn);
80
81
        self::assertInstanceOf(Client::class, $client);
82
        self::assertSame($nameOut, $client->getName());
83
        self::assertSame(32, strlen($client->getIdentifier()));
84
        self::assertGreaterThan(40, strlen($client->getSecret()));
85
        self::assertSame('INTERNAL_API', $client->getRedirectUri());
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function createDataProvider()
92
    {
93
        return [
94
            [null, 'Personal Access Client'],
95
            ['', 'Personal Access Client'],
96
            ['test1', 'test1'],
97
        ];
98
    }
99
100
    public function setUp(): void
101
    {
102
        parent::setUp();
103
104
        ClientsHelper::personalAccessClientId(null);
105
        ClientsManager::resetClient();
106
    }
107
}
108