Repository navigation
Expand file tree
/
Copy pathSymfony1.php
More file actions
572 lines (513 loc) · 18.8 KB
/
Copy pathSymfony1.php
File metadata and controls
572 lines (513 loc) · 18.8 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<?php
namespace Codeception\Module;
use Codeception\Module;
use Codeception\Util\Framework as Framework;
/**
* Module that interacts with Symfony 1.4 applications.
*
* Replaces functional testing framework from symfony. Authorization features uses Doctrine and sfDoctrineGuardPlugin.
* Uses native symfony connections and test classes. Provides additional informations on every actions.
*
* If test fails stores last shown page in 'log' dir.
*
* Please note, this module doesn't implement standard frameworks interface.
*
* ## Status
*
* * Maintainer: **davert**
* * Stability: **stable**
* * Contact: codecept@davert.mail.ua
*
* ## Configuration
*
* * app *required* - application you want to test. In most cases it will be 'frontend'
*
* ## Public Properties
* * browser - current instance of sfBrowser class.
*
*/
class Symfony1 extends Module
{
/**
* @api
* @var \sfBrowser
*/
public $browser;
protected $session_id;
protected $config = array('app' => 'frontend');
public function _initialize()
{
if (!file_exists('config/ProjectConfiguration.class.php')) throw new \Codeception\Exception\Module('Symfony1', 'config/ProjectConfiguration.class.php not found. This file is required for running symfony1');
require_once('config/ProjectConfiguration.class.php');
$conf = \ProjectConfiguration::getApplicationConfiguration($this->config['app'], 'test', true);
\sfContext::createInstance($conf, 'default');
\sfContext::switchTo('default');
// chdir(\sfConfig::get('sf_web_dir'));
$this->browser = new \sfBrowser();
$this->browser->get('/');
\sfForm::disableCSRFProtection();
}
public function _cleanup()
{
chdir(\sfConfig::get('sf_web_dir'));
$this->browser->restart();
$this->session_id = $_SERVER['session_id'];
}
public function _before(\Codeception\TestCase $test)
{
$this->browser->get('/');
}
public function _failed(\Codeception\TestCase $test, $fail)
{
$output = \Codeception\Configuration::logDir() . DIRECTORY_SEPARATOR . basename($test->getFileName()) . '.page.debug.html';
file_put_contents($output, $this->browser->getResponse()->getContent());
}
public function _after(\Codeception\TestCase $test)
{
$this->browser->restart();
$this->browser->resetCurrentException();
$this->browser->get('/');
}
protected function call($uri, $method = 'get', $params = array())
{
// multi testguy implementation
$_SERVER['session_id'] = $this->session_id;
if (false === ($empty = $this->browser->checkCurrentExceptionIsEmpty())) {
\PHPUnit_Framework_Assert::fail(sprintf('last request threw an uncaught exception "%s: %s"', get_class($this->browser->getCurrentException()), $this->browser->getCurrentException()->getMessage()));
return;
}
$this->debug('Request (' . $method . '): ' . $uri . ' ' . json_encode($params));
$this->browser->call($uri, $method, $params);
$this->debug('Response code: ' . $this->browser->getResponse()->getStatusCode());
$this->debug('Response message: ' . $this->browser->getResponse()->getStatusText());
if ($form = $this->getForm()) {
foreach ($form->getErrorSchema() as $field => $desc) {
$this->debug("Error in form $field field: '$desc'");
}
}
$this->followRedirect();
}
protected function followRedirect()
{
if ($this->browser->getResponse()->getStatusCode() == 302) {
$this->debug("redirected to " . $this->browser->getResponse()->getHttpHeader('Location'));
$this->call($this->browser->getResponse()->getHttpHeader('Location'));
}
}
/**
* Opens the page.
*
* @param $page
*/
public function amOnPage($page)
{
$this->browser->get($page);
$this->debug('Response code: ' . $this->browser->getResponse()->getStatusCode());
$this->followRedirect();
}
/**
* Click on link or button and move to next page.
* Either link text, css selector, or xpath can be passed
*
* @param $link
*/
public function click($link)
{
$this->browser->click($link);
$this->debug('moved to page ' . $this->browser->fixUri($this->browser->getRequest()->getUri()));
$this->followRedirect();
}
/**
* Check if current page doesn't contain the text specified.
* Specify the css selector to match only specific region.
*
* Examples:
*
* ```php
* <?php
* $I->dontSee('Login'); // I can suppose user is already logged in
* $I->dontSee('Sign Up','h1'); // I can suppose it's not a signup page
*
* ```
*
* @param $text
* @param null $selector
*/
public function dontSee($text, $selector = null)
{
$res = $this->proceedSee($text, $selector);
$this->assertNot($res);
}
/**
* Check if current page contains the text specified.
* Specify the css selector to match only specific region.
*
* Examples:
*
* ``` php
* <?php
* $I->see('Logout'); // I can suppose user is logged in
* $I->see('Sign Up','h1'); // I can suppose it's a signup page
*
* ```
*
* @param $text
* @param null $selector
*/
public function see($text, $selector = null)
{
$res = $this->proceedSee($text, $selector);
$this->assert($res);
}
protected function proceedSee($text, $selector = null)
{
$response = $this->browser->getResponse()->getContent();
if ($selector) {
$nodes = $this->browser->getResponseDomCssSelector()->matchAll($selector);
$values = '';
foreach ($nodes as $node) {
$values .= '<!-- Merged Output -->'.trim($node->nodeValue);
}
$response = Framework::formatResponse($response);
return array('pageContains', $text, $values, "'$selector' on $response");
}
$response = Framework::formatResponse($response);
return array('pageContains', $text, strip_tags($this->browser->getResponse()->getContent()), "on $response.");
}
/**
* Checks if there is a link with text specified.
* Specify url to match link with exact this url.
*
* Examples:
*
* ``` php
* <?php
* $I->seeLink('Logout'); // matches <a href="#">Logout</a>
* $I->seeLink('Logout','/logout'); // matches <a href="/logout">Logout</a>
*
* ```
*
* @param $text
* @param null $url
*/
public function seeLink($text, $url = null)
{
if (!$url) return $this->see($text, 'a');
$nodes = $this->browser->getResponseDomCssSelector()->matchAll('a')->getNodes();
foreach ($nodes as $node) {
if (0 === strrpos($node->getAttribute('href'), $url)) {
return \PHPUnit_Framework_Assert::assertContains($text, $node->nodeValue, "with url '$url'");
}
}
\PHPUnit_Framework_Assert::fail($text, "with url '$url'");
}
/**
* Checks if page doesn't contain the link with text specified.
* Specify url to narrow the results.
*
* Examples:
*
* ``` php
* <?php
* $I->dontSeeLink('Logout'); // I suppose user is not logged in
*
* ```
*
* @param $text
* @param null $url
*/
public function dontSeeLink($text, $url = null)
{
if (!$url) return $this->dontSee($text, 'a');
$nodes = $this->browser->getResponseDomCssSelector()->matchAll('a')->getNodes();
foreach ($nodes as $node) {
if (0 === strrpos($node->getAttribute('href'), $url) && ($node->nodeValue == trim($text))) {
return \PHPUnit_Framework_Assert::assertFalse((0 === strrpos($node->getAttribute('href'), $url) && ($node->nodeValue == trim($text))), "link with url '$url'");
}
}
}
/**
* Assert if the specified checkbox is checked.
* Use css selector or xpath to match.
*
* Example:
*
* ``` php
* <?php
* $I->seeCheckboxIsChecked('#agree'); // I suppose user agreed to terms
* $I->seeCheckboxIsChecked('#signup_form input[type=checkbox]'); // I suppose user agreed to terms, If there is only one checkbox in form.
*
* ```
*
* @param $selector
*/
public function seeCheckboxIsChecked($selector)
{
$res = $this->proceedCheckboxIsChecked($selector);
$this->assert($res);
}
/**
* Assert if the specified checkbox is unchecked.
* Use css selector or xpath to match.
*
* Example:
*
* ``` php
* <?php
* $I->dontSeeCheckboxIsChecked('#agree'); // I suppose user didn't agree to terms
* $I->seeCheckboxIsChecked('#signup_form input[type=checkbox]'); // I suppose user didn't check the first checkbox in form.
*
* ```
*
* @param $selector
*/
public function dontSeeCheckboxIsChecked($selector)
{
$res = $this->proceedCheckboxIsChecked($selector);
$this->assertNot($res);
}
protected function proceedCheckboxIsChecked($selector)
{
$node = $this->browser->getResponseDomCssSelector()->matchSingle($selector)->getNode();
if (!$node) {
$this->fail("there is no element $selector on page");
}
if ($node->getAttribute('type') != 'checkbox') {
$this->fail("there is no checkbox $selector on page");
}
return array('equals', $node->getAttribute('checked'), 'checked', "that checkbox $selector is checked");
}
/**
* Checks if there were at least one email sent through Symfony test mailer.
*/
public function seeEmailReceived()
{
$this->debug('Emails sent: ' . $this->browser->getContext()->getMailer()->getLogger()->countMessages());
\PHPUnit_Framework_Assert::assertGreaterThan(0, $this->browser->getContext()->getMailer()->getLogger()->countMessages(), "");
}
/**
* Submits a form located on page.
* Specify the form by it's css or xpath selector.
* Fill the form fields values as array.
*
* Skipped fields will be filled by their values from page.
* You don't need to click the 'Submit' button afterwards.
* This command itself triggers the request to form's action.
*
* Examples:
*
* ``` php
* <?php
* $I->submitForm('#login', array('login' => 'davert', 'password' => '123456'));
*
* ```
*
* For sample Sign Up form:
*
* ``` html
* <form action="/sign_up">
* Login: <input type="text" name="user[login]" /><br/>
* Password: <input type="password" name="user[password]" /><br/>
* Do you agree to out terms? <input type="checkbox" name="user[agree]" /><br/>
* Select pricing plan <select name="plan"><option value="1">Free</option><option value="2" selected="selected">Paid</option></select>
* <input type="submit" value="Submit" />
* </form>
* ```
* I can write this:
*
* ``` php
* <?php
* $I->submitForm('#userForm', array('user' => array('login' => 'Davert', 'password' => '123456', 'agree' => true)));
*
* ```
* Note, that pricing plan will be set to Paid, as it's selected on page.
*
* @param $selector
* @param $params
*/
public function submitForm($selector, $params)
{
$form = $this->browser->getResponseDomCssSelector()->matchSingle($selector)->getNode();
if (!$form) \PHPUnit_Framework_Assert::fail("Form by selector '$selector' not found");
$fields = $this->browser->getResponseDomCssSelector()->matchAll($selector . ' input')->getNodes();
$url = '';
foreach ($fields as $field) {
if ($field->getAttribute('type') == 'checkbox') continue;
if ($field->getAttribute('type') == 'radio') continue;
$url .= sprintf('%s=%s', $field->getAttribute('name'), $field->getAttribute('value')) . '&';
}
$fields = $this->browser->getResponseDomCssSelector()->matchAll($selector . ' textarea')->getNodes();
foreach ($fields as $field) {
$url .= sprintf('%s=%s', $field->getAttribute('name'), $field->nodeValue) . '&';
}
$fields = $this->browser->getResponseDomCssSelector()->matchAll($selector . ' select')->getNodes();
foreach ($fields as $field) {
foreach ($field->childNodes as $option) {
if ($option->getAttribute('selected') == 'selected') {
$url .= sprintf('%s=%s', $field->getAttribute('name'), $option->getAttribute('value')) . '&';
break;
}
}
}
$url .= '&' . http_build_query($params);
parse_str($url, $params);
$url = $form->getAttribute('action');
$method = $form->getAttribute('method');
$this->call($url, $method, $params);
}
/**
* If your page triggers an ajax request, you can perform it manually.
* This action sends a POST ajax request with specified params.
* Additional params can be passed as array.
*
* Example:
*
* Imagine that by clicking checkbox you trigger ajax request which updates user settings.
* We emulate that click by running this ajax request manually.
*
* ``` php
* <?php
* $I->sendAjaxPostRequest('/updateSettings', array('notifications' => true); // POST
* $I->sendAjaxGetRequest('/updateSettings', array('notifications' => true); // GET
*
* ```
*
* @param $uri
* @param $params
*/
public function sendAjaxPostRequest($uri, $params)
{
$this->browser->setHttpHeader('X-Requested-With', 'XMLHttpRequest');
$this->call($uri, 'post', $params);
$this->debug($this->browser->getResponse()->getContent());
}
/**
* If your page triggers an ajax request, you can perform it manually.
* This action sends a GET ajax request with specified params.
*
* See ->sendAjaxPostRequest for examples.
*
* @param $uri
* @param $params
*/
public function sendAjaxGetRequest($uri, $params)
{
$this->browser->setHttpHeader('X-Requested-With', 'XMLHttpRequest');
$this->call($uri, 'get', $params);
$this->debug($this->browser->getResponse()->getContent());
}
/**
* Emulates click on form's submit button.
* You don't need that action if you fill form by ->submitForm action.
*
* @param $selector
*/
public function clickSubmitButton($selector)
{
$this->browser->click($selector);
}
/**
* Performs validation of Symfony1 form.
* Matches the first sfForm instance from controller and returns isValid() value.
*/
public function seeFormIsValid()
{
$form = $this->getForm();
if (!$form) \PHPUnit_Framework_Assert::fail('as any symfony forms at all');
\PHPUnit_Framework_Assert::isFalse($form->hasErrors(), ', seems like there were errors');
}
/**
* Performs validation of Symfony1 form.
* Matches the first sfForm instance from controller and returns getErrorSchema() values.
* Shows all errors in debug.
*/
public function seeErrorsInForm()
{
$form = $this->getForm();
if (!$form) \PHPUnit_Framework_Assert::fail('as any symfony forms at all');
\PHPUnit_Framework_Assert::isTrue($form->hasErrors(), ', seems like there were no validation errors');
foreach ($form->getErrorSchema() as $field => $desc) {
$this->debug("Error in $field field: '$desc'");
}
}
/**
* Checks for invalid value in Symfony1 form.
* Matches the first sfForm instance from controller and returns getErrorSchema() values.
* Specify field which should contain error message.
*
* @param $field
*/
public function seeErrorInField($field)
{
$form = $this->getForm();
$keys = array();
foreach ($form->getErrorSchema() as $field => $desc) {
$keys[] = $field;
}
$this->assertContains($field, $keys);
}
protected function getForm()
{
$action = $this->browser->getContext()->getActionStack()->getLastEntry()->getActionInstance();
foreach ($action->getVarHolder()->getAll() as $name => $value)
{
if ($value instanceof \sfForm && $value->isBound()) {
$form = $value;
break;
}
}
if (!isset($form)) return null;
return $form;
}
/**
* Sign's user in with sfGuardAuth.
* Uses standard path: /sfGuardAuth/signin for authorization.
* Provide username and password.
*
* @param $username
* @param $password
*/
public function signIn($username, $password)
{
$this->browser->post('/sfGuardAuth/signin', array('signin' => array('username' => $username, 'password' => $password)));
$this->debug('session: ' . json_encode($this->browser->getUser()->getAttributeHolder()->getAll()));
$this->debug('user: ' . json_encode($this->browser->getUser()->getAttributeHolder()->getAll('sfGuardSecurityUser')));
$this->debug('credentials: ' . json_encode($this->browser->getUser()->getCredentials()));
$this->browser->get('/');
$this->followRedirect();
}
/**
* Sign out is performing by triggering '/logout' url.
*
*/
public function signOut()
{
$this->browser->get('/logout');
$this->followRedirect();
}
/**
* Log in as sfDoctrineGuardUser.
* Only name of user should be provided.
* Fetches user by it's username from sfGuardUser table.
*
* @param $name
* @throws \Exception
*/
public function amLoggedAs($name)
{
if (!class_exists('Doctrine')) throw new \Exception('Doctrine is not installed. Consider using \'signIn\' action instead');
$user = \Doctrine::getTable('sfGuardUser')->findOneBy('username', $name);
if (!$user) throw new \Exception("User with name $name was not found in database");
$user->clearRelated();
$browser = $this->browser;
$this->browser->getContext()->getStorage()->initialize($browser->getContext()->getStorage()->getOptions());
$browser->getUser()->signIn($user);
$this->debug('session: ' . json_encode($this->browser->getUser()->getAttributeHolder()->getAll()));
$this->debug('user: ' . json_encode($this->browser->getUser()->getAttributeHolder()->getAll('sfGuardSecurityUser')));
$this->debug('credentials: ' . json_encode($this->browser->getUser()->getCredentials()));
$this->browser->getUser()->shutdown();
$this->browser->getContext()->getStorage()->shutdown();
$browser->get('/');
$this->followRedirect();
}
}