AuthenticationTraitTest::buildMockUserSession()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Auth;
3
4
use Fwlib\Auth\AuthenticationInterface;
5
use Fwlib\Auth\AuthenticationTrait;
6
use Fwlib\Auth\UserSessionInterface;
7
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
8
use PHPUnit_Framework_MockObject_MockObject as MockObject;
9
10
/**
11
 * @copyright   Copyright 2014-2015 Fwolf
12
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
13
 */
14
class AuthenticationTraitTest extends PHPUnitTestCase
15
{
16
    /**
17
     * @param   UserSessionInterface    $userSession
18
     * @return  MockObject | AuthenticationInterface
19
     */
20
    protected function buildMock($userSession)
21
    {
22
        $mock = $this->getMockBuilder(
23
            AuthenticationTrait::class
24
        )
25
            ->getMockForTrait();
26
27
        /** @var AuthenticationInterface $mock */
28
        $mock->setUserSession($userSession);
29
30
        return $mock;
31
    }
32
33
34
    /**
35
     * @return  MockObject | UserSessionInterface
36
     */
37
    protected function buildMockUserSession()
38
    {
39
        $userSession = $this->getMockBuilder(
40
            UserSessionInterface::class
41
        )
42
            ->disableOriginalConstructor()
43
            ->getMock();
44
45
        return $userSession;
46
    }
47
48
49
    public function testSetGetUserSession()
50
    {
51
        $authentication = $this->buildMock(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Fwlib\Auth\UserSessionInterface>.

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...
52
53
        $this->assertNull(
54
            $authentication->getUserSession()
55
        );
56
57
58
        $authentication = $this->buildMock($this->buildMockUserSession());
59
60
        $this->assertInstanceOf(
61
            UserSessionInterface::class,
62
            $authentication->getUserSession()
63
        );
64
    }
65
66
67
    public function testGetIdentity()
68
    {
69
        $authentication = $this->buildMock(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Fwlib\Auth\UserSessionInterface>.

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...
70
71
        $this->assertEmpty($authentication->getIdentity());
72
    }
73
}
74