|
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()); |
|
|
|
|
|
|
75
|
|
|
$this->assertEquals('FooName', $api->get('uname')); |
|
76
|
|
|
$this->assertEquals('FooName', $api->uname()); |
|
|
|
|
|
|
77
|
|
|
$this->assertEquals('[email protected]', $api->get('email')); |
|
78
|
|
|
$this->assertEquals('[email protected]', $api->email()); |
|
|
|
|
|
|
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)); |
|
|
|
|
|
|
83
|
|
|
$this->assertEquals($attributes, $api->get('attributes')); |
|
84
|
|
|
$this->assertNull($api->get('foo')); |
|
85
|
|
|
$this->assertNull($api->foo()); |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
94
|
|
|
$this->assertNull($api->get('uname')); |
|
95
|
|
|
$this->assertNull($api->uname()); |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: