|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\OpenIDBundle\Tests\EventListener; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\CoreBundle\Entity\Person; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Helper\SecurityHelper; |
|
15
|
|
|
use LoginCidadao\OAuthBundle\Entity\Client; |
|
16
|
|
|
use LoginCidadao\OpenIDBundle\EventListener\TaskSubscriber; |
|
17
|
|
|
use LoginCidadao\OpenIDBundle\Manager\ClientManager; |
|
18
|
|
|
use LoginCidadao\OpenIDBundle\Task\CompleteUserInfoTaskValidator; |
|
19
|
|
|
use LoginCidadao\TaskStackBundle\Event\GetTasksEvent; |
|
20
|
|
|
use LoginCidadao\TaskStackBundle\TaskStackEvents; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
22
|
|
|
|
|
23
|
|
|
class TaskSubscriberTest extends \PHPUnit_Framework_TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
public function testGetSubscribedEvents() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->assertSame([ |
|
28
|
|
|
TaskStackEvents::GET_TASKS => ['onGetTasks', 50], |
|
29
|
|
|
], TaskSubscriber::getSubscribedEvents()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testOnGetTasksNoPersonOrClient() |
|
33
|
|
|
{ |
|
34
|
|
|
$securityHelper = $this->getSecurityHelper(); |
|
35
|
|
|
$clientManager = $this->getClientManager(); |
|
36
|
|
|
$taskValidator = $this->getCompleteUserInfoTaskValidator(); |
|
37
|
|
|
|
|
38
|
|
|
$request = $this->getRequest(); |
|
39
|
|
|
$this->requestExpectsClientId($request); |
|
40
|
|
|
|
|
41
|
|
|
$event = $this->getEvent(); |
|
42
|
|
|
$event->expects($this->once()) |
|
43
|
|
|
->method('getRequest')->willReturn($request); |
|
44
|
|
|
|
|
45
|
|
|
$subscriber = new TaskSubscriber($securityHelper, $clientManager, $taskValidator); |
|
46
|
|
|
$subscriber->onGetTasks($event); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testOnGetTasksWithPersonAndClient() |
|
50
|
|
|
{ |
|
51
|
|
|
$client = new Client(); |
|
52
|
|
|
$person = new Person(); |
|
53
|
|
|
|
|
54
|
|
|
$task = $this->getMockBuilder('LoginCidadao\OpenIDBundle\Task\CompleteUserInfoTask') |
|
55
|
|
|
->disableOriginalConstructor()->getMock(); |
|
56
|
|
|
|
|
57
|
|
|
$securityHelper = $this->getSecurityHelper(); |
|
58
|
|
|
$securityHelper->expects($this->once()) |
|
59
|
|
|
->method('getUser')->willReturn($person); |
|
60
|
|
|
|
|
61
|
|
|
$clientManager = $this->getClientManager(); |
|
62
|
|
|
$clientManager->expects($this->once()) |
|
63
|
|
|
->method('getClientById')->with('client_id') |
|
64
|
|
|
->willReturn($client); |
|
65
|
|
|
|
|
66
|
|
|
$request = $this->getRequest(); |
|
67
|
|
|
$this->requestExpectsClientId($request); |
|
68
|
|
|
|
|
69
|
|
|
$taskValidator = $this->getCompleteUserInfoTaskValidator(); |
|
70
|
|
|
$taskValidator->expects($this->once()) |
|
71
|
|
|
->method('getCompleteUserInfoTask')->with($person, $client, $request) |
|
72
|
|
|
->willReturn($task); |
|
73
|
|
|
|
|
74
|
|
|
$event = $this->getEvent(); |
|
75
|
|
|
$event->expects($this->once()) |
|
76
|
|
|
->method('getRequest')->willReturn($request); |
|
77
|
|
|
$event->expects($this->once()) |
|
78
|
|
|
->method('addTask')->with($task); |
|
79
|
|
|
|
|
80
|
|
|
$subscriber = new TaskSubscriber($securityHelper, $clientManager, $taskValidator); |
|
81
|
|
|
$subscriber->onGetTasks($event); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return SecurityHelper|\PHPUnit_Framework_MockObject_MockObject |
|
86
|
|
|
*/ |
|
87
|
|
|
private function getSecurityHelper() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->getMockBuilder('LoginCidadao\CoreBundle\Helper\SecurityHelper') |
|
90
|
|
|
->disableOriginalConstructor()->getMock(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return ClientManager|\PHPUnit_Framework_MockObject_MockObject |
|
95
|
|
|
*/ |
|
96
|
|
|
private function getClientManager() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->getMockBuilder('LoginCidadao\OpenIDBundle\Manager\ClientManager') |
|
99
|
|
|
->disableOriginalConstructor()->getMock(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return CompleteUserInfoTaskValidator|\PHPUnit_Framework_MockObject_MockObject |
|
104
|
|
|
*/ |
|
105
|
|
|
private function getCompleteUserInfoTaskValidator() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->getMockBuilder('LoginCidadao\OpenIDBundle\Task\CompleteUserInfoTaskValidator') |
|
108
|
|
|
->disableOriginalConstructor()->getMock(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Request|\PHPUnit_Framework_MockObject_MockObject |
|
113
|
|
|
*/ |
|
114
|
|
|
private function getRequest() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->getMockBuilder('Symfony\Component\HttpFoundation\Request') |
|
117
|
|
|
->disableOriginalConstructor()->getMock(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function requestExpectsClientId(\PHPUnit_Framework_MockObject_MockObject $request) |
|
121
|
|
|
{ |
|
122
|
|
|
$params = $this->logicalOr($this->equalTo('client_id'), $this->equalTo('clientId')); |
|
123
|
|
|
$request->expects($this->exactly(2)) |
|
124
|
|
|
->method('get')->with($params) |
|
125
|
|
|
->willReturn('client_id'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return GetTasksEvent|\PHPUnit_Framework_MockObject_MockObject |
|
130
|
|
|
*/ |
|
131
|
|
|
private function getEvent() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->getMockBuilder('LoginCidadao\TaskStackBundle\Event\GetTasksEvent') |
|
134
|
|
|
->disableOriginalConstructor()->getMock(); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|