1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\InformationCollectionBundle\Tests\Controller; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\ContentView; |
6
|
|
|
use eZ\Publish\Core\Repository\Values\Content\Location; |
7
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; |
8
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\Payload\InformationCollectionStruct; |
9
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Controller\InformationCollectionController; |
10
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Form\Builder\FormBuilder; |
11
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Tests\ContentViewStub; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
use Symfony\Component\Form\Form; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
class InformationCollectionControllerTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var InformationCollectionController |
23
|
|
|
*/ |
24
|
|
|
protected $controller; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
28
|
|
|
*/ |
29
|
|
|
protected $dispatcher; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
33
|
|
|
*/ |
34
|
|
|
protected $builder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
38
|
|
|
*/ |
39
|
|
|
protected $contentView; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
43
|
|
|
*/ |
44
|
|
|
protected $request; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
48
|
|
|
*/ |
49
|
|
|
protected $formBuilder; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
53
|
|
|
*/ |
54
|
|
|
protected $form; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
58
|
|
|
*/ |
59
|
|
|
protected $container; |
60
|
|
|
|
61
|
|
|
public function setUp() |
62
|
|
|
{ |
63
|
|
|
if (!class_exists(ContentView::class)) { |
64
|
|
|
$this->markTestSkipped(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->container = $this->getMockBuilder(ContainerInterface::class) |
68
|
|
|
->disableOriginalConstructor() |
69
|
|
|
->setMethods(array('get', 'getParameter', 'has', 'hasParameter', 'initialized', 'set', 'setParameter', 'addScope', 'enterScope', 'hasScope', 'isScopeActive', 'leaveScope')) |
70
|
|
|
->getMock(); |
71
|
|
|
|
72
|
|
|
$this->builder = $this->getMockBuilder(FormBuilder::class) |
73
|
|
|
->disableOriginalConstructor() |
74
|
|
|
->setMethods(array('createFormForLocation')) |
75
|
|
|
->getMock(); |
76
|
|
|
|
77
|
|
|
$this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class) |
78
|
|
|
->disableOriginalConstructor() |
79
|
|
|
->setMethods(array('dispatch', 'addListener', 'addSubscriber', 'removeListener', 'removeSubscriber', 'getListeners', 'hasListeners', 'getListenerPriority')) |
80
|
|
|
->getMock(); |
81
|
|
|
|
82
|
|
|
$this->contentView = $this->getMockBuilder(ContentView::class) |
83
|
|
|
->disableOriginalConstructor() |
84
|
|
|
->setMethods(array('getLocation', 'addParameters')) |
85
|
|
|
->getMock(); |
86
|
|
|
|
87
|
|
|
$this->request = $this->getMockBuilder(Request::class) |
88
|
|
|
->disableOriginalConstructor() |
89
|
|
|
->setMethods(array()) |
90
|
|
|
->getMock(); |
91
|
|
|
|
92
|
|
|
$this->formBuilder = $this->getMockBuilder(\Symfony\Component\Form\FormBuilder::class) |
93
|
|
|
->disableOriginalConstructor() |
94
|
|
|
->setMethods(array('getForm')) |
95
|
|
|
->getMock(); |
96
|
|
|
|
97
|
|
|
$this->form = $this->getMockBuilder(Form::class) |
98
|
|
|
->disableOriginalConstructor() |
99
|
|
|
->setMethods(array('handleRequest', 'isSubmitted', 'isValid', 'getData', 'createView')) |
100
|
|
|
->getMock(); |
101
|
|
|
|
102
|
|
|
$this->controller = new InformationCollectionController(); |
103
|
|
|
$this->controller->setContainer($this->container); |
104
|
|
|
|
105
|
|
|
parent::setUp(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testInstanceOfContainerAwareInterface() |
109
|
|
|
{ |
110
|
|
|
$this->assertInstanceOf(ContainerAwareInterface::class, $this->controller); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testDisplayAndHandleWithValidFormSubmission() |
114
|
|
|
{ |
115
|
|
|
$location = new Location(); |
116
|
|
|
|
117
|
|
|
$this->container->expects($this->exactly(2)) |
118
|
|
|
->method('get') |
119
|
|
|
->with($this->logicalOr( |
120
|
|
|
$this->equalTo('netgen_information_collection.form.builder'), |
121
|
|
|
$this->equalTo('event_dispatcher') |
122
|
|
|
)) |
123
|
|
|
->will($this->returnCallback(array($this, 'getService'))); |
124
|
|
|
|
125
|
|
|
$this->contentView->expects($this->once()) |
126
|
|
|
->method('getLocation') |
127
|
|
|
->willReturn($location); |
128
|
|
|
|
129
|
|
|
$this->formBuilder->expects($this->once()) |
130
|
|
|
->method('getForm') |
131
|
|
|
->willReturn($this->form); |
132
|
|
|
|
133
|
|
|
$this->builder->expects($this->once()) |
134
|
|
|
->method('createFormForLocation') |
135
|
|
|
->with($location, false) |
136
|
|
|
->willReturn($this->formBuilder); |
137
|
|
|
|
138
|
|
|
$this->form->expects($this->once()) |
139
|
|
|
->method('handleRequest') |
140
|
|
|
->with($this->request); |
141
|
|
|
|
142
|
|
|
$this->form->expects($this->once()) |
143
|
|
|
->method('isSubmitted') |
144
|
|
|
->willReturn(true); |
145
|
|
|
|
146
|
|
|
$this->form->expects($this->once()) |
147
|
|
|
->method('isValid') |
148
|
|
|
->willReturn(true); |
149
|
|
|
|
150
|
|
|
$this->form->expects($this->exactly(2)) |
151
|
|
|
->method('getData') |
152
|
|
|
->willReturn(new DataWrapper(new InformationCollectionStruct())); |
153
|
|
|
|
154
|
|
|
$this->form->expects($this->once()) |
155
|
|
|
->method('createView'); |
156
|
|
|
|
157
|
|
|
$this->dispatcher->expects($this->once()) |
158
|
|
|
->method('dispatch'); |
159
|
|
|
|
160
|
|
|
$this->contentView->expects($this->once()) |
161
|
|
|
->method('addParameters'); |
162
|
|
|
|
163
|
|
|
$this->controller->displayAndHandle($this->contentView, $this->request); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function testDisplayAndHandleWithInvalidFormSubmission() |
167
|
|
|
{ |
168
|
|
|
$location = new Location(); |
169
|
|
|
|
170
|
|
|
$this->container->expects($this->once()) |
171
|
|
|
->method('get') |
172
|
|
|
->with('netgen_information_collection.form.builder') |
173
|
|
|
->willReturn($this->builder); |
174
|
|
|
|
175
|
|
|
$this->contentView->expects($this->once()) |
176
|
|
|
->method('getLocation') |
177
|
|
|
->willReturn($location); |
178
|
|
|
|
179
|
|
|
$this->formBuilder->expects($this->once()) |
180
|
|
|
->method('getForm') |
181
|
|
|
->willReturn($this->form); |
182
|
|
|
|
183
|
|
|
$this->builder->expects($this->once()) |
184
|
|
|
->method('createFormForLocation') |
185
|
|
|
->with($location, false) |
186
|
|
|
->willReturn($this->formBuilder); |
187
|
|
|
|
188
|
|
|
$this->form->expects($this->once()) |
189
|
|
|
->method('handleRequest') |
190
|
|
|
->with($this->request); |
191
|
|
|
|
192
|
|
|
$this->form->expects($this->once()) |
193
|
|
|
->method('isValid') |
194
|
|
|
->willReturn(false); |
195
|
|
|
|
196
|
|
|
$this->form->expects($this->exactly(1)) |
197
|
|
|
->method('getData') |
198
|
|
|
->willReturn(new DataWrapper(new InformationCollectionStruct())); |
199
|
|
|
|
200
|
|
|
$this->dispatcher->expects($this->never()) |
201
|
|
|
->method('dispatch'); |
202
|
|
|
|
203
|
|
|
$this->form->expects($this->once()) |
204
|
|
|
->method('createView'); |
205
|
|
|
|
206
|
|
|
$this->contentView->expects($this->once()) |
207
|
|
|
->method('addParameters'); |
208
|
|
|
|
209
|
|
|
$this->controller->displayAndHandle($this->contentView, $this->request); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @expectedException \BadMethodCallException |
214
|
|
|
* @expectedExceptionMessage eZ view needs to implement LocationValueView interface |
215
|
|
|
*/ |
216
|
|
|
public function testIfLocationValueViewIsNotProvidedThrowBadMethodCallException() |
217
|
|
|
{ |
218
|
|
|
$this->container->expects($this->never()) |
219
|
|
|
->method('get') |
220
|
|
|
->with('netgen_information_collection.form.builder') |
221
|
|
|
->willReturn($this->builder); |
222
|
|
|
|
223
|
|
|
$this->controller->displayAndHandle(new ContentViewStub(), $this->request); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function getService($id) |
227
|
|
|
{ |
228
|
|
|
switch ($id) { |
229
|
|
|
case 'netgen_information_collection.form.builder': |
230
|
|
|
return $this->builder; |
231
|
|
|
case 'event_dispatcher': |
232
|
|
|
return $this->dispatcher; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|