Completed
Push — master ( 55311e...acc5b2 )
by Craig
06:34
created

CurrentUserApiTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 27 1
A testIsLoggedIn() 0 18 1
A testIsNotLoggedIn() 0 9 1
A getApi() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\UsersModule\Tests\Api;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Symfony\Component\HttpFoundation\Session\SessionInterface;
16
use Zikula\UsersModule\Api\CurrentUserApi;
17
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
18
use Zikula\UsersModule\Entity\UserAttributeEntity;
19
use Zikula\UsersModule\Entity\UserEntity;
20
21
class CurrentUserApiTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var \PHPUnit_Framework_MockObject_Builder_InvocationMocker
25
     */
26
    private $session;
27
28
    /**
29
     * @var UserEntity
30
     */
31
    private $user;
32
33
    /**
34
     * @var \PHPUnit_Framework_MockObject_Builder_InvocationMocker
35
     */
36
    private $userRepo;
37
38
    /**
39
     * CurrentUserApiTest setUp.
40
     */
41
    public function setUp()
42
    {
43
        $this->user = new UserEntity();
44
        $this->user->setUname('FooName');
45
        $this->user->setEmail('[email protected]');
46
        $this->user->setActivated(1);
47
        $this->user->setAttribute('legs', 2);
48
49
        $this->userRepo = $this
50
            ->getMockBuilder(UserRepositoryInterface::class)
51
            ->disableOriginalConstructor()
52
            ->getMock();
53
        $this->userRepo
54
            ->method('find')
55
            ->with($this->logicalNot($this->isNull()))
56
            ->will($this->returnCallback(function ($uid) {
57
                $this->user->setUid($uid);
58
59
                return $this->user;
60
            }));
61
        $this->session = $this
62
            ->getMockBuilder(SessionInterface::class)
63
            ->disableOriginalConstructor()
64
            ->getMock();
65
        $this->session
66
            ->method('start')->willReturn(true);
67
    }
68
69
    public function testIsLoggedIn()
70
    {
71
        $api = $this->getApi(42);
72
        $this->assertTrue($api->isLoggedIn());
73
        $this->assertEquals(42, $api->get('uid'));
74
        $this->assertEquals(42, $api->uid());
0 ignored issues
show
Documentation Bug introduced by
The method uid does not exist on object<Zikula\UsersModule\Api\CurrentUserApi>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
        $this->assertEquals('FooName', $api->get('uname'));
76
        $this->assertEquals('FooName', $api->uname());
0 ignored issues
show
Documentation Bug introduced by
The method uname does not exist on object<Zikula\UsersModule\Api\CurrentUserApi>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
77
        $this->assertEquals('[email protected]', $api->get('email'));
78
        $this->assertEquals('[email protected]', $api->email());
0 ignored issues
show
Documentation Bug introduced by
The method email does not exist on object<Zikula\UsersModule\Api\CurrentUserApi>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
79
        $this->assertEquals(1, $api->get('activated'));
80
        $attributes = new ArrayCollection();
81
        $this->user->setUid(42);
82
        $attributes->set('legs', new UserAttributeEntity($this->user, 'legs', 2));
0 ignored issues
show
Documentation introduced by
$this->user is of type object<Zikula\UsersModule\Entity\UserEntity>, but the function expects a integer.

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...
83
        $this->assertEquals($attributes, $api->get('attributes'));
84
        $this->assertNull($api->get('foo'));
85
        $this->assertNull($api->foo());
0 ignored issues
show
Documentation Bug introduced by
The method foo does not exist on object<Zikula\UsersModule\Api\CurrentUserApi>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
86
    }
87
88
    public function testIsNotLoggedIn()
89
    {
90
        $api = $this->getApi();
91
        $this->assertFalse($api->isLoggedIn());
92
        $this->assertNull($api->get('uid'));
93
        $this->assertNull($api->uid());
0 ignored issues
show
Documentation Bug introduced by
The method uid does not exist on object<Zikula\UsersModule\Api\CurrentUserApi>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
94
        $this->assertNull($api->get('uname'));
95
        $this->assertNull($api->uname());
0 ignored issues
show
Documentation Bug introduced by
The method uname does not exist on object<Zikula\UsersModule\Api\CurrentUserApi>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
96
    }
97
98
    private function getApi($uid = null)
99
    {
100
        $this->session->method('get')->with('uid')->willReturn($uid);
101
102
        return new CurrentUserApi($this->session, $this->userRepo);
0 ignored issues
show
Documentation introduced by
$this->session is of type object<PHPUnit_Framework...ilder_InvocationMocker>, but the function expects a object<Symfony\Component...ssion\SessionInterface>.

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...
Compatibility introduced by
$this->userRepo of type object<PHPUnit_Framework...ilder_InvocationMocker> is not a sub-type of object<Zikula\UsersModul...serRepositoryInterface>. It seems like you assume a child interface of the class PHPUnit_Framework_MockOb...uilder_InvocationMocker to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
103
    }
104
}
105