Completed
Push — master ( 22d22f...259b7d )
by Gabriel
19:09
created

HasApiTokensTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\Hello\Tests\Models\Traits;
4
5
use ByTIC\Hello\Models\AccessTokens\Token;
6
use ByTIC\Hello\Models\AccessTokens\Tokens;
7
use ByTIC\Hello\Models\Clients\PersonalAccess\TokenFactory;
8
use ByTIC\Hello\Tests\AbstractTest;
9
use ByTIC\Hello\Tests\Fixtures\Models\Users\User;
10
use Mockery as m;
11
use Nip\Container\Container;
12
use Nip\Records\Locator\ModelLocator;
13
14
/**
15
 * Class HasApiTokensTest
16
 * @package ByTIC\Hello\Tests\Models\Traits
17
 */
18
class HasApiTokensTest extends AbstractTest
19
{
20
21
    public function testTokenCanBeCreated()
22
    {
23
        $container = Container::getInstance();
24
25
        $factory = m::mock();
26
        $factory->shouldReceive('make')->once()->with('users|1', 'name', ['scopes']);
27
        $container->share(TokenFactory::class, $factory);
28
29
        $tokens = m::mock(Tokens::class)->makePartial();
30
        $tokens->shouldReceive('getByIdentifier')->andReturn(new Token());
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...
31
        ModelLocator::set(Tokens::class, $tokens);
0 ignored issues
show
Documentation introduced by
$tokens is of type object<Mockery\Mock>, but the function expects a object<Nip\Records\AbstractModels\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...
32
33
        $user = new User();
34
        $user->setIdentifier('users|1');
35
        $user->createToken('name', ['scopes']);
36
    }
37
}
38