-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathCreateTopicFlowTest.php
More file actions
357 lines (303 loc) · 15.2 KB
/
Copy pathCreateTopicFlowTest.php
File metadata and controls
357 lines (303 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
namespace Craue\FormFlowBundle\Tests;
use Craue\FormFlowBundle\Tests\IntegrationTestCase;
use Symfony\Component\DomCrawler\Crawler;
/**
* @group integration
*
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2015 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class CreateTopicFlowTest extends IntegrationTestCase {
public function testCreateTopic_dynamicStepNavigation() {
$this->client->followRedirects();
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic'));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertCurrentStepNumber(1, $crawler);
$this->assertCurrentFormData('{"title":null,"description":null,"category":null,"comment":null,"details":null}', $crawler);
$this->assertCount(0, $crawler->filter('#step-list a'));
// bug report -> step 2
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[title]' => 'blah',
'createTopic[category]' => 'BUG_REPORT',
));
$this->assertCurrentStepNumber(2, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":null,"details":null}', $crawler);
$this->assertCount(1, $crawler->filter('#step-list a'));
// comment -> step 3
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[comment]' => 'my comment',
));
$this->assertCurrentStepNumber(3, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":"my comment","details":null}', $crawler);
$this->assertCount(2, $crawler->filter('#step-list a'));
// empty bug details -> step 3 again
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[details]' => '',
));
$this->assertCurrentStepNumber(3, $crawler);
$this->assertContainsFormError('This value should not be blank.', $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":"my comment","details":null}', $crawler);
// bug details -> step 4
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[details]' => 'blah blah',
));
$this->assertCurrentStepNumber(4, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":"my comment","details":"blah blah"}', $crawler);
$this->assertCount(3, $crawler->filter('#step-list a'));
// back to step 1 via DSN
$linkToStep1 = $crawler->filter('#step-list a')->selectLink('basics')->link()->getUri();
$crawler = $this->client->request('GET', $linkToStep1);
$this->assertCurrentStepNumber(1, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":"my comment","details":"blah blah"}', $crawler);
$this->assertCount(3, $crawler->filter('#step-list a'));
// discussion -> step 2
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[title]' => 'blah',
'createTopic[category]' => 'DISCUSSION',
));
$this->assertCurrentStepNumber(2, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"DISCUSSION","comment":"my comment","details":"blah blah"}', $crawler);
$this->assertCount(2, $crawler->filter('#step-list a')); // link the last step as it's been visited already
// keep as is -> step 4
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form);
$this->assertCurrentStepNumber(4, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"DISCUSSION","comment":"my comment","details":null}', $crawler);
$this->assertCount(2, $crawler->filter('#step-list a'));
// finish flow
$form = $crawler->selectButton('finish')->form();
$this->client->submit($form);
$this->assertJsonResponse('{"title":"blah","description":null,"category":"DISCUSSION","comment":"my comment","details":null}');
}
public function testCreateTopic_dynamicStepNavigation_noLinkForNonVisitedStep() {
$this->client->followRedirects();
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic'));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertCurrentStepNumber(1, $crawler);
$this->assertCount(0, $crawler->filter('#step-list a'));
// discussion -> step 2
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[title]' => 'blah',
'createTopic[category]' => 'DISCUSSION',
));
$this->assertCurrentStepNumber(2, $crawler);
$this->assertCount(1, $crawler->filter('#step-list a')); // don't link the last step as it's not been visited yet
// keep as is -> step 4
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form);
$this->assertCurrentStepNumber(4, $crawler);
$this->assertCount(2, $crawler->filter('#step-list a'));
}
public function testCreateTopic_dynamicStepNavigation_preserveDataOnGetRequestWithInstanceId() {
$this->client->followRedirects();
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic'));
// discussion -> step 2
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[title]' => 'blah',
'createTopic[category]' => 'DISCUSSION',
));
// GET request -> step 1 with data preserved
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic', array(
'instance' => $form->get('flow_createTopic_instance')->getValue(),
)));
$this->assertCurrentStepNumber(1, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"DISCUSSION","comment":null,"details":null}', $crawler);
}
public function testCreateTopic_dynamicStepNavigation_newFlowInstanceOnGetRequest() {
$this->client->followRedirects();
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic'));
// discussion -> step 2
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[title]' => 'blah',
'createTopic[category]' => 'DISCUSSION',
));
// GET request -> step 1 with new flow instance
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic'));
$this->assertCurrentStepNumber(1, $crawler);
$this->assertCurrentFormData('{"title":null,"description":null,"category":null,"comment":null,"details":null}', $crawler);
}
public function testCreateTopic_redirectAfterSubmit() {
$this->client->followRedirects();
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic_redirectAfterSubmit'));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertCurrentStepNumber(1, $crawler);
$this->assertCurrentFormData('{"title":null,"description":null,"category":null,"comment":null,"details":null}', $crawler);
$this->assertCount(0, $crawler->filter('#step-list a'));
// reset -> step 1
$form = $crawler->selectButton('start over')->form();
$crawler = $this->client->submit($form);
$this->assertCurrentStepNumber(1, $crawler);
$this->assertCurrentFormData('{"title":null,"description":null,"category":null,"comment":null,"details":null}', $crawler);
$this->assertCount(0, $crawler->filter('#step-list a'));
// make sure redirection was effective after clicking "start over"
$this->assertEquals('GET', $this->client->getRequest()->getMethod());
$this->assertArrayHasKey('instance', $this->client->getRequest()->query->all());
$this->assertEquals(1, $this->client->getRequest()->query->get('step'));
// empty title -> step 1 again
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[title]' => '',
));
$this->assertCurrentStepNumber(1, $crawler);
$this->assertContainsFormError('This value should not be blank.', $crawler);
$this->assertCurrentFormData('{"title":null,"description":null,"category":null,"comment":null,"details":null}', $crawler);
// make sure query parameters are still added in case of form errors
$this->assertEquals('POST', $this->client->getRequest()->getMethod());
$this->assertArrayHasKey('instance', $this->client->getRequest()->query->all());
$this->assertEquals(1, $this->client->getRequest()->query->get('step'));
// bug report -> step 2
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[title]' => 'blah',
'createTopic[category]' => 'BUG_REPORT',
));
$this->assertCurrentStepNumber(2, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":null,"details":null}', $crawler);
$this->assertCount(0, $crawler->filter('#step-list a'));
// make sure redirection was effective after clicking "next"
$this->assertEquals('GET', $this->client->getRequest()->getMethod());
$this->assertArrayHasKey('instance', $this->client->getRequest()->query->all());
$this->assertEquals(2, $this->client->getRequest()->query->get('step'));
// comment -> step 3
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[comment]' => 'my comment',
));
$this->assertCurrentStepNumber(3, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":"my comment","details":null}', $crawler);
$this->assertCount(0, $crawler->filter('#step-list a'));
// empty bug details -> step 3 again
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[details]' => '',
));
$this->assertCurrentStepNumber(3, $crawler);
$this->assertContainsFormError('This value should not be blank.', $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":"my comment","details":null}', $crawler);
// bug details -> step 4
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[details]' => 'blah blah',
));
$this->assertCurrentStepNumber(4, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":"my comment","details":"blah blah"}', $crawler);
$this->assertCount(0, $crawler->filter('#step-list a'));
// back -> step 3
$form = $crawler->selectButton('back')->form();
$crawler = $this->client->submit($form);
$this->assertCurrentStepNumber(3, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":"my comment","details":"blah blah"}', $crawler);
$this->assertCount(0, $crawler->filter('#step-list a'));
// make sure redirection was effective after clicking "back"
$this->assertEquals('GET', $this->client->getRequest()->getMethod());
$this->assertArrayHasKey('instance', $this->client->getRequest()->query->all());
$this->assertEquals(3, $this->client->getRequest()->query->get('step'));
// next -> step 4
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form);
$this->assertCurrentStepNumber(4, $crawler);
$this->assertCurrentFormData('{"title":"blah","description":null,"category":"BUG_REPORT","comment":"my comment","details":"blah blah"}', $crawler);
$this->assertCount(0, $crawler->filter('#step-list a'));
// finish flow
$form = $crawler->selectButton('finish')->form();
$this->client->submit($form);
$this->assertJsonResponse('{"title":"blah","description":null,"category":"BUG_REPORT","comment":"my comment","details":"blah blah"}');
}
public function testCreateTopic_dynamicStepNavigation_invalidInstanceId_onGetRequest() {
$crawler = $this->proceedToStep(2);
$fakeInstanceId = 'invalid instance id';
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic', array(
'instance' => $fakeInstanceId,
'step' => 2,
)));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertCurrentStepNumber(1, $crawler);
$this->assertNotEquals($fakeInstanceId, $crawler->selectButton('next')->form()->get('flow_createTopic_instance')->getValue());
}
public function testCreateTopic_dynamicStepNavigation_invalidInstanceId_onPostRequest() {
$crawler = $this->proceedToStep(2);
$fakeInstanceId = 'invalid instance id';
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'flow_createTopic_instance' => $fakeInstanceId,
));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertCurrentStepNumber(1, $crawler);
$this->assertNotEquals($fakeInstanceId, $crawler->selectButton('next')->form()->get('flow_createTopic_instance')->getValue());
}
public function testCreateTopic_dynamicStepNavigation_invalidStep_exceedLowerLimit() {
$crawler = $this->proceedToStep(2);
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic', array(
'instance' => $form->get('flow_createTopic_instance')->getValue(),
'step' => 0,
)));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertCurrentStepNumber(1, $crawler);
}
public function testCreateTopic_dynamicStepNavigation_invalidStep_exceedUpperLimit() {
$crawler = $this->proceedToStep(4);
$form = $crawler->selectButton('finish')->form();
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic', array(
'instance' => $form->get('flow_createTopic_instance')->getValue(),
'step' => 5,
)));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertCurrentStepNumber(4, $crawler);
}
public function testCreateTopic_dynamicStepNavigation_invalidStep_noInteger() {
$crawler = $this->proceedToStep(2);
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic', array(
'instance' => $form->get('flow_createTopic_instance')->getValue(),
'step' => 'x',
)));
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertCurrentStepNumber(1, $crawler);
}
/**
* Processes through the flow up to the given step by filling out the forms with some valid data.
* @param integer $stepNumber The targeted step number.
* @return Crawler
*/
private function proceedToStep($stepNumber) {
$this->client->followRedirects();
$crawler = $this->client->request('GET', $this->url('_FormFlow_createTopic'));
if ($stepNumber < 2) {
return $crawler;
}
// bug report -> step 2
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[title]' => 'blah',
'createTopic[category]' => 'BUG_REPORT',
));
if ($stepNumber < 3) {
return $crawler;
}
// comment -> step 3
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[comment]' => 'my comment',
));
if ($stepNumber < 4) {
return $crawler;
}
// bug details -> step 4
$form = $crawler->selectButton('next')->form();
$crawler = $this->client->submit($form, array(
'createTopic[details]' => 'blah blah',
));
return $crawler;
}
}