UserCallableTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zenify\DoctrineBehaviors\Tests\Blameable;
6
7
use Nette\Security\User;
8
use PHPUnit\Framework\TestCase;
9
use Prophecy\Prophecy\ObjectProphecy;
10
use Zenify\DoctrineBehaviors\Blameable\UserCallable;
11
12
13
final class UserCallableTest extends TestCase
14
{
15
16
	/**
17
	 * @var ObjectProphecy|User
18
	 */
19
	private $userMock;
20
21
	/**
22
	 * @var UserCallable
23
	 */
24
	private $userCallable;
25
26
27
	protected function setUp()
28
	{
29
		$this->userMock = $this->prophesize(User::class);
30
		$this->userMock->getId()->willReturn(1);
31
		$this->userMock->isLoggedIn()->willReturn(TRUE);
32
		$this->userCallable = new UserCallable($this->userMock->reveal());
33
	}
34
35
36
	public function testInvoke()
37
	{
38
		$this->assertSame(1, call_user_func($this->userCallable));
39
40
		$this->userMock->isLoggedIn()->willReturn(FALSE);
0 ignored issues
show
Bug introduced by
The method isLoggedIn does only exist in Nette\Security\User, but not in Prophecy\Prophecy\ObjectProphecy.

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...
41
		$this->assertSame(NULL, call_user_func($this->userCallable));
42
	}
43
44
}
45