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

ClientsManagerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetWithPersonalAccessClientId() 0 14 1
A testGetWithFindByRedirect() 0 14 1
A testGetWithCreate() 0 16 1
A testCreate() 0 16 1
A createDataProvider() 0 8 1
A setUp() 0 7 1
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
20
    public function testGetWithPersonalAccessClientId()
21
    {
22
        $client = new Client();
23
24
        ClientsHelper::personalAccessClientId(99);
25
26
        $clientsManager = \Mockery::mock(Clients::class)->makePartial();
27
        $clientsManager->shouldReceive('findOne')->with(99)->andReturn($client);
28
        ModelsHelper::useClientsManager($clientsManager);
0 ignored issues
show
Documentation introduced by
$clientsManager is of type object<Mockery\Mock>, but the function expects a object<Nip\Records\RecordManager>.

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...
29
30
        $getClient = ClientsManager::get();
31
32
        self::assertSame($getClient, $client);
33
    }
34
35
    public function testGetWithFindByRedirect()
36
    {
37
        $client = new Client();
38
39
        $clientsManager = \Mockery::mock(Clients::class)->makePartial();
40
        $clientsManager->shouldReceive('findByRedirect')
41
            ->with(ClientsHelper::PERSONAL_ACCESS_REDIRECT_URI)
42
            ->andReturn(new Collection([$client]));
43
        ModelsHelper::useClientsManager($clientsManager);
0 ignored issues
show
Documentation introduced by
$clientsManager is of type object<Mockery\Mock>, but the function expects a object<Nip\Records\RecordManager>.

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...
44
45
        $getClient = ClientsManager::get();
46
47
        self::assertSame($getClient, $client);
48
    }
49
50
    public function testGetWithCreate()
51
    {
52
        $clientsManager = \Mockery::mock(Clients::class)->makePartial();
53
        $clientsManager->shouldReceive('getPrimaryKey')->andReturn('id');
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\CompositeExpectation, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54
        $clientsManager->shouldReceive('getModel')->andReturn(Client::class);
55
        $clientsManager->shouldReceive('findByRedirect')
56
            ->with(ClientsHelper::PERSONAL_ACCESS_REDIRECT_URI)
57
            ->andReturn(new Collection());
58
        $clientsManager->shouldReceive('save');
59
60
        ModelsHelper::useClientsManager($clientsManager);
0 ignored issues
show
Documentation introduced by
$clientsManager is of type object<Mockery\Mock>, but the function expects a object<Nip\Records\RecordManager>.

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...
61
62
        $client = ClientsManager::get();
63
64
        self::assertInstanceOf(Client::class, $client);
65
    }
66
67
68
    /**
69
     * @param null|string $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
70
     * @dataProvider createDataProvider
71
     */
72
    public function testCreate($nameIn, $nameOut)
73
    {
74
        $clientsManager = \Mockery::mock(Clients::class)->makePartial();
75
        $clientsManager->shouldReceive('getPrimaryKey')->andReturn('id');
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\CompositeExpectation, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
76
        $clientsManager->shouldReceive('getModel')->andReturn(Client::class);
77
        $clientsManager->shouldReceive('save');
78
79
        ModelsHelper::useClientsManager($clientsManager);
0 ignored issues
show
Documentation introduced by
$clientsManager is of type object<Mockery\Mock>, but the function expects a object<Nip\Records\RecordManager>.

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