~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Test/PHPUnit/ControllerTestCase.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/** PHPUnit_Framework_TestCase */
 
4
require_once 'PHPUnit/Framework/TestCase.php';
 
5
 
 
6
/** PHPUnit_Runner_Version */
 
7
require_once 'PHPUnit/Runner/Version.php';
 
8
 
 
9
/** Zend_Controller_Front */
 
10
require_once 'Zend/Controller/Front.php';
 
11
 
 
12
/** Zend_Controller_Action_HelperBroker */
 
13
require_once 'Zend/Controller/Action/HelperBroker.php';
 
14
 
 
15
/** Zend_Layout */
 
16
require_once 'Zend/Layout.php';
 
17
 
 
18
/** Zend_Session */
 
19
require_once 'Zend/Session.php';
 
20
 
 
21
/** Zend_Registry */
 
22
require_once 'Zend/Registry.php';
 
23
 
 
24
/**
 
25
 * Functional testing scaffold for MVC applications
 
26
 * 
 
27
 * @uses       PHPUnit_Framework_TestCase
 
28
 * @package    Zend_Test
 
29
 * @subpackage PHPUnit
 
30
 * @copyright  Copyright (C) 2008 - Present, Zend Technologies, Inc.
 
31
 * @license    New BSD {@link http://framework.zend.com/license/new-bsd}
 
32
 */
 
33
abstract class Zend_Test_PHPUnit_ControllerTestCase extends PHPUnit_Framework_TestCase
 
34
{
 
35
    /**
 
36
     * @var mixed Bootstrap file path or callback
 
37
     */
 
38
    public $bootstrap;
 
39
 
 
40
    /**
 
41
     * @var Zend_Controller_Front
 
42
     */
 
43
    protected $_frontController;
 
44
 
 
45
    /**
 
46
     * @var Zend_Dom_Query
 
47
     */
 
48
    protected $_query;
 
49
 
 
50
    /**
 
51
     * @var Zend_Controller_Request_Abstract
 
52
     */
 
53
    protected $_request;
 
54
    
 
55
    /**
 
56
     * @var Zend_Controller_Response_Abstract
 
57
     */
 
58
    protected $_response;
 
59
 
 
60
    /**
 
61
     * Overlaoding: prevent overloading to special properties
 
62
     * 
 
63
     * @param  string $name 
 
64
     * @param  mixed $value 
 
65
     * @return void
 
66
     */
 
67
    public function __set($name, $value)
 
68
    {
 
69
        if (in_array($name, array('request', 'response', 'frontController'))) {
 
70
            throw new Zend_Exception(sprintf('Setting %s object manually is not allowed', $name));
 
71
        }
 
72
        $this->$name = $value;
 
73
    }
 
74
 
 
75
    /**
 
76
     * Overloading for common properties
 
77
     *
 
78
     * Provides overloading for request, response, and frontController objects.
 
79
     * 
 
80
     * @param mixed $name 
 
81
     * @return void
 
82
     */
 
83
    public function __get($name)
 
84
    {
 
85
        switch ($name) {
 
86
            case 'request':
 
87
                return $this->getRequest();
 
88
            case 'response':
 
89
                return $this->getResponse();
 
90
            case 'frontController':
 
91
                return $this->getFrontController();
 
92
        }
 
93
 
 
94
        return null;
 
95
    }
 
96
 
 
97
    /**
 
98
     * Set up MVC app
 
99
     *
 
100
     * Calls {@link bootstrap()} by default
 
101
     * 
 
102
     * @return void
 
103
     */
 
104
    protected function setUp()
 
105
    {
 
106
        $this->bootstrap();
 
107
    }
 
108
 
 
109
    /**
 
110
     * Bootstrap the front controller
 
111
     *
 
112
     * Resets the front controller, and then bootstraps it.
 
113
     *
 
114
     * If {@link $bootstrap} is a callback, executes it; if it is a file, it include's 
 
115
     * it. When done, sets the test case request and response objects into the 
 
116
     * front controller.
 
117
     * 
 
118
     * @return void
 
119
     */
 
120
    final public function bootstrap()
 
121
    {
 
122
        $this->reset();
 
123
        if (null !== $this->bootstrap) {
 
124
            if (is_callable($this->bootstrap)) {
 
125
                call_user_func($this->bootstrap);
 
126
            } elseif (is_string($this->bootstrap)) {
 
127
                require_once 'Zend/Loader.php';
 
128
                if (Zend_Loader::isReadable($this->bootstrap)) {
 
129
                    include $this->bootstrap;
 
130
                }
 
131
            }
 
132
        }
 
133
        $this->frontController
 
134
             ->setRequest($this->getRequest())
 
135
             ->setResponse($this->getResponse());
 
136
    }
 
137
 
 
138
    /**
 
139
     * Dispatch the MVC
 
140
     *
 
141
     * If a URL is provided, sets it as the request URI in the request object. 
 
142
     * Then sets test case request and response objects in front controller, 
 
143
     * disables throwing exceptions, and disables returning the response.
 
144
     * Finally, dispatches the front controller.
 
145
     * 
 
146
     * @param  string|null $url 
 
147
     * @return void
 
148
     */
 
149
    public function dispatch($url = null)
 
150
    {
 
151
        // redirector should not exit
 
152
        $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
 
153
        $redirector->setExit(false);
 
154
 
 
155
        // json helper should not exit
 
156
        $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
 
157
        $json->suppressExit = true;
 
158
 
 
159
        $request    = $this->getRequest();
 
160
        if (null !== $url) {
 
161
            $request->setRequestUri($url);
 
162
        }
 
163
        $request->setPathInfo(null);
 
164
        $controller = $this->getFrontController();
 
165
        $this->frontController
 
166
             ->setRequest($request)
 
167
             ->setResponse($this->getResponse())
 
168
             ->throwExceptions(false)
 
169
             ->returnResponse(false);
 
170
        $this->frontController->dispatch();
 
171
    }
 
172
 
 
173
    /**
 
174
     * Reset MVC state
 
175
     * 
 
176
     * Creates new request/response objects, resets the front controller 
 
177
     * instance, and resets the action helper broker.
 
178
     *
 
179
     * @todo   Need to update Zend_Layout to add a resetInstance() method
 
180
     * @return void
 
181
     */
 
182
    public function reset()
 
183
    {
 
184
        $_SESSION = array();
 
185
        $_GET     = array();
 
186
        $_POST    = array();
 
187
        $_COOKIE  = array();
 
188
        $this->resetRequest();
 
189
        $this->resetResponse();
 
190
        Zend_Layout::resetMvcInstance();
 
191
        Zend_Controller_Action_HelperBroker::resetHelpers();
 
192
        $this->frontController->resetInstance();
 
193
        Zend_Session::$_unitTestEnabled = true;
 
194
    }
 
195
 
 
196
    /**
 
197
     * Rest all view placeholders
 
198
     * 
 
199
     * @return void
 
200
     */
 
201
    protected function _resetPlaceholders()
 
202
    {
 
203
        $registry = Zend_Registry::getInstance();
 
204
        $remove   = array();
 
205
        foreach ($registry as $key => $value) {
 
206
            if (strstr($key, '_View_')) {
 
207
                $remove[] = $key;
 
208
            }
 
209
        }
 
210
 
 
211
        foreach ($remove as $key) {
 
212
            unset($registry[$key]);
 
213
        }
 
214
    }
 
215
 
 
216
    /**
 
217
     * Reset the request object
 
218
     *
 
219
     * Useful for test cases that need to test multiple trips to the server.
 
220
     * 
 
221
     * @return Zend_Test_PHPUnit_ControllerTestCase
 
222
     */
 
223
    public function resetRequest()
 
224
    {
 
225
        $this->_request = null;
 
226
        return $this;
 
227
    }
 
228
 
 
229
    /**
 
230
     * Reset the response object
 
231
     *
 
232
     * Useful for test cases that need to test multiple trips to the server.
 
233
     * 
 
234
     * @return Zend_Test_PHPUnit_ControllerTestCase
 
235
     */
 
236
    public function resetResponse()
 
237
    {
 
238
        $this->_response = null;
 
239
        $this->_resetPlaceholders();
 
240
        return $this;
 
241
    }
 
242
 
 
243
    /**
 
244
     * Assert against DOM selection
 
245
     * 
 
246
     * @param  string $path CSS selector path
 
247
     * @param  string $message
 
248
     * @return void
 
249
     */
 
250
    public function assertQuery($path, $message = '')
 
251
    {
 
252
        $this->_incrementAssertionCount();
 
253
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
254
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
255
        $content    = $this->response->outputBody();
 
256
        if (!$constraint->evaluate($content, __FUNCTION__)) {
 
257
            $constraint->fail($path, $message);
 
258
        }
 
259
    }
 
260
 
 
261
    /**
 
262
     * Assert against DOM selection
 
263
     * 
 
264
     * @param  string $path CSS selector path
 
265
     * @param  string $message
 
266
     * @return void
 
267
     */
 
268
    public function assertNotQuery($path, $message = '')
 
269
    {
 
270
        $this->_incrementAssertionCount();
 
271
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
272
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
273
        $content    = $this->response->outputBody();
 
274
        if (!$constraint->evaluate($content, __FUNCTION__)) {
 
275
            $constraint->fail($path, $message);
 
276
        }
 
277
    }
 
278
 
 
279
    /**
 
280
     * Assert against DOM selection; node should contain content
 
281
     * 
 
282
     * @param  string $path CSS selector path
 
283
     * @param  string $match content that should be contained in matched nodes
 
284
     * @param  string $message
 
285
     * @return void
 
286
     */
 
287
    public function assertQueryContentContains($path, $match, $message = '')
 
288
    {
 
289
        $this->_incrementAssertionCount();
 
290
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
291
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
292
        $content    = $this->response->outputBody();
 
293
        if (!$constraint->evaluate($content, __FUNCTION__, $match)) {
 
294
            $constraint->fail($path, $message);
 
295
        }
 
296
    }
 
297
 
 
298
    /**
 
299
     * Assert against DOM selection; node should NOT contain content
 
300
     * 
 
301
     * @param  string $path CSS selector path
 
302
     * @param  string $match content that should NOT be contained in matched nodes
 
303
     * @param  string $message
 
304
     * @return void
 
305
     */
 
306
    public function assertNotQueryContentContains($path, $match, $message = '')
 
307
    {
 
308
        $this->_incrementAssertionCount();
 
309
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
310
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
311
        $content    = $this->response->outputBody();
 
312
        if (!$constraint->evaluate($content, __FUNCTION__, $match)) {
 
313
            $constraint->fail($path, $message);
 
314
        }
 
315
    }
 
316
 
 
317
    /**
 
318
     * Assert against DOM selection; node should match content
 
319
     * 
 
320
     * @param  string $path CSS selector path
 
321
     * @param  string $pattern Pattern that should be contained in matched nodes
 
322
     * @param  string $message
 
323
     * @return void
 
324
     */
 
325
    public function assertQueryContentRegex($path, $pattern, $message = '')
 
326
    {
 
327
        $this->_incrementAssertionCount();
 
328
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
329
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
330
        $content    = $this->response->outputBody();
 
331
        if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) {
 
332
            $constraint->fail($path, $message);
 
333
        }
 
334
    }
 
335
 
 
336
    /**
 
337
     * Assert against DOM selection; node should NOT match content
 
338
     * 
 
339
     * @param  string $path CSS selector path
 
340
     * @param  string $pattern pattern that should NOT be contained in matched nodes
 
341
     * @param  string $message
 
342
     * @return void
 
343
     */
 
344
    public function assertNotQueryContentRegex($path, $pattern, $message = '')
 
345
    {
 
346
        $this->_incrementAssertionCount();
 
347
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
348
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
349
        $content    = $this->response->outputBody();
 
350
        if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) {
 
351
            $constraint->fail($path, $message);
 
352
        }
 
353
    }
 
354
 
 
355
    /**
 
356
     * Assert against DOM selection; should contain exact number of nodes
 
357
     * 
 
358
     * @param  string $path CSS selector path
 
359
     * @param  string $count Number of nodes that should match
 
360
     * @param  string $message
 
361
     * @return void
 
362
     */
 
363
    public function assertQueryCount($path, $count, $message = '')
 
364
    {
 
365
        $this->_incrementAssertionCount();
 
366
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
367
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
368
        $content    = $this->response->outputBody();
 
369
        if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
 
370
            $constraint->fail($path, $message);
 
371
        }
 
372
    }
 
373
 
 
374
    /**
 
375
     * Assert against DOM selection; should NOT contain exact number of nodes
 
376
     * 
 
377
     * @param  string $path CSS selector path
 
378
     * @param  string $count Number of nodes that should NOT match
 
379
     * @param  string $message
 
380
     * @return void
 
381
     */
 
382
    public function assertNotQueryCount($path, $count, $message = '')
 
383
    {
 
384
        $this->_incrementAssertionCount();
 
385
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
386
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
387
        $content    = $this->response->outputBody();
 
388
        if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
 
389
            $constraint->fail($path, $message);
 
390
        }
 
391
    }
 
392
 
 
393
    /**
 
394
     * Assert against DOM selection; should contain at least this number of nodes
 
395
     * 
 
396
     * @param  string $path CSS selector path
 
397
     * @param  string $count Minimum number of nodes that should match
 
398
     * @param  string $message
 
399
     * @return void
 
400
     */
 
401
    public function assertQueryCountMin($path, $count, $message = '')
 
402
    {
 
403
        $this->_incrementAssertionCount();
 
404
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
405
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
406
        $content    = $this->response->outputBody();
 
407
        if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
 
408
            $constraint->fail($path, $message);
 
409
        }
 
410
    }
 
411
 
 
412
    /**
 
413
     * Assert against DOM selection; should contain no more than this number of nodes
 
414
     * 
 
415
     * @param  string $path CSS selector path
 
416
     * @param  string $count Maximum number of nodes that should match
 
417
     * @param  string $message
 
418
     * @return void
 
419
     */
 
420
    public function assertQueryCountMax($path, $count, $message = '')
 
421
    {
 
422
        $this->_incrementAssertionCount();
 
423
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
424
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
425
        $content    = $this->response->outputBody();
 
426
        if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
 
427
            $constraint->fail($path, $message);
 
428
        }
 
429
    }
 
430
 
 
431
    /**
 
432
     * Assert against XPath selection
 
433
     * 
 
434
     * @param  string $path XPath path
 
435
     * @param  string $message
 
436
     * @return void
 
437
     */
 
438
    public function assertXpath($path, $message = '')
 
439
    {
 
440
        $this->_incrementAssertionCount();
 
441
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
442
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
443
        $content    = $this->response->outputBody();
 
444
        if (!$constraint->evaluate($content, __FUNCTION__)) {
 
445
            $constraint->fail($path, $message);
 
446
        }
 
447
    }
 
448
 
 
449
    /**
 
450
     * Assert against XPath selection
 
451
     * 
 
452
     * @param  string $path XPath path
 
453
     * @param  string $message
 
454
     * @return void
 
455
     */
 
456
    public function assertNotXpath($path, $message = '')
 
457
    {
 
458
        $this->_incrementAssertionCount();
 
459
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
460
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
461
        $content    = $this->response->outputBody();
 
462
        if (!$constraint->evaluate($content, __FUNCTION__)) {
 
463
            $constraint->fail($path, $message);
 
464
        }
 
465
    }
 
466
 
 
467
    /**
 
468
     * Assert against XPath selection; node should contain content
 
469
     * 
 
470
     * @param  string $path XPath path
 
471
     * @param  string $match content that should be contained in matched nodes
 
472
     * @param  string $message
 
473
     * @return void
 
474
     */
 
475
    public function assertXpathContentContains($path, $match, $message = '')
 
476
    {
 
477
        $this->_incrementAssertionCount();
 
478
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
479
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
480
        $content    = $this->response->outputBody();
 
481
        if (!$constraint->evaluate($content, __FUNCTION__, $match)) {
 
482
            $constraint->fail($path, $message);
 
483
        }
 
484
    }
 
485
 
 
486
    /**
 
487
     * Assert against XPath selection; node should NOT contain content
 
488
     * 
 
489
     * @param  string $path XPath path
 
490
     * @param  string $match content that should NOT be contained in matched nodes
 
491
     * @param  string $message
 
492
     * @return void
 
493
     */
 
494
    public function assertNotXpathContentContains($path, $match, $message = '')
 
495
    {
 
496
        $this->_incrementAssertionCount();
 
497
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
498
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
499
        $content    = $this->response->outputBody();
 
500
        if (!$constraint->evaluate($content, __FUNCTION__, $match)) {
 
501
            $constraint->fail($path, $message);
 
502
        }
 
503
    }
 
504
 
 
505
    /**
 
506
     * Assert against XPath selection; node should match content
 
507
     * 
 
508
     * @param  string $path XPath path
 
509
     * @param  string $pattern Pattern that should be contained in matched nodes
 
510
     * @param  string $message
 
511
     * @return void
 
512
     */
 
513
    public function assertXpathContentRegex($path, $pattern, $message = '')
 
514
    {
 
515
        $this->_incrementAssertionCount();
 
516
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
517
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
518
        $content    = $this->response->outputBody();
 
519
        if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) {
 
520
            $constraint->fail($path, $message);
 
521
        }
 
522
    }
 
523
 
 
524
    /**
 
525
     * Assert against XPath selection; node should NOT match content
 
526
     * 
 
527
     * @param  string $path XPath path
 
528
     * @param  string $pattern pattern that should NOT be contained in matched nodes
 
529
     * @param  string $message
 
530
     * @return void
 
531
     */
 
532
    public function assertNotXpathContentRegex($path, $pattern, $message = '')
 
533
    {
 
534
        $this->_incrementAssertionCount();
 
535
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
536
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
537
        $content    = $this->response->outputBody();
 
538
        if (!$constraint->evaluate($content, __FUNCTION__, $pattern)) {
 
539
            $constraint->fail($path, $message);
 
540
        }
 
541
    }
 
542
 
 
543
    /**
 
544
     * Assert against XPath selection; should contain exact number of nodes
 
545
     * 
 
546
     * @param  string $path XPath path
 
547
     * @param  string $count Number of nodes that should match
 
548
     * @param  string $message
 
549
     * @return void
 
550
     */
 
551
    public function assertXpathCount($path, $count, $message = '')
 
552
    {
 
553
        $this->_incrementAssertionCount();
 
554
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
555
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
556
        $content    = $this->response->outputBody();
 
557
        if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
 
558
            $constraint->fail($path, $message);
 
559
        }
 
560
    }
 
561
 
 
562
    /**
 
563
     * Assert against XPath selection; should NOT contain exact number of nodes
 
564
     * 
 
565
     * @param  string $path XPath path
 
566
     * @param  string $count Number of nodes that should NOT match
 
567
     * @param  string $message
 
568
     * @return void
 
569
     */
 
570
    public function assertNotXpathCount($path, $count, $message = '')
 
571
    {
 
572
        $this->_incrementAssertionCount();
 
573
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
574
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
575
        $content    = $this->response->outputBody();
 
576
        if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
 
577
            $constraint->fail($path, $message);
 
578
        }
 
579
    }
 
580
 
 
581
    /**
 
582
     * Assert against XPath selection; should contain at least this number of nodes
 
583
     * 
 
584
     * @param  string $path XPath path
 
585
     * @param  string $count Minimum number of nodes that should match
 
586
     * @param  string $message
 
587
     * @return void
 
588
     */
 
589
    public function assertXpathCountMin($path, $count, $message = '')
 
590
    {
 
591
        $this->_incrementAssertionCount();
 
592
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
593
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
594
        $content    = $this->response->outputBody();
 
595
        if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
 
596
            $constraint->fail($path, $message);
 
597
        }
 
598
    }
 
599
 
 
600
    /**
 
601
     * Assert against XPath selection; should contain no more than this number of nodes
 
602
     * 
 
603
     * @param  string $path XPath path
 
604
     * @param  string $count Maximum number of nodes that should match
 
605
     * @param  string $message
 
606
     * @return void
 
607
     */
 
608
    public function assertXpathCountMax($path, $count, $message = '')
 
609
    {
 
610
        $this->_incrementAssertionCount();
 
611
        require_once 'Zend/Test/PHPUnit/Constraint/DomQuery.php';
 
612
        $constraint = new Zend_Test_PHPUnit_Constraint_DomQuery($path);
 
613
        $content    = $this->response->outputBody();
 
614
        if (!$constraint->evaluate($content, __FUNCTION__, $count)) {
 
615
            $constraint->fail($path, $message);
 
616
        }
 
617
    }
 
618
 
 
619
    /**
 
620
     * Assert that response is a redirect
 
621
     * 
 
622
     * @param  string $message 
 
623
     * @return void
 
624
     */
 
625
    public function assertRedirect($message = '')
 
626
    {
 
627
        $this->_incrementAssertionCount();
 
628
        require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
 
629
        $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
 
630
        $response   = $this->response;
 
631
        if (!$constraint->evaluate($response, __FUNCTION__)) {
 
632
            $constraint->fail($response, $message);
 
633
        }
 
634
    }
 
635
 
 
636
    /**
 
637
     * Assert that response is NOT a redirect
 
638
     * 
 
639
     * @param  string $message 
 
640
     * @return void
 
641
     */
 
642
    public function assertNotRedirect($message = '')
 
643
    {
 
644
        $this->_incrementAssertionCount();
 
645
        require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
 
646
        $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
 
647
        $response   = $this->response;
 
648
        if (!$constraint->evaluate($response, __FUNCTION__)) {
 
649
            $constraint->fail($response, $message);
 
650
        }
 
651
    }
 
652
 
 
653
    /**
 
654
     * Assert that response redirects to given URL
 
655
     * 
 
656
     * @param  string $url 
 
657
     * @param  string $message 
 
658
     * @return void
 
659
     */
 
660
    public function assertRedirectTo($url, $message = '')
 
661
    {
 
662
        $this->_incrementAssertionCount();
 
663
        require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
 
664
        $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
 
665
        $response   = $this->response;
 
666
        if (!$constraint->evaluate($response, __FUNCTION__, $url)) {
 
667
            $constraint->fail($response, $message);
 
668
        }
 
669
    }
 
670
 
 
671
    /**
 
672
     * Assert that response does not redirect to given URL
 
673
     * 
 
674
     * @param  string $url 
 
675
     * @param  string $message 
 
676
     * @return void
 
677
     */
 
678
    public function assertNotRedirectTo($url, $message = '')
 
679
    {
 
680
        $this->_incrementAssertionCount();
 
681
        require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
 
682
        $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
 
683
        $response   = $this->response;
 
684
        if (!$constraint->evaluate($response, __FUNCTION__, $url)) {
 
685
            $constraint->fail($response, $message);
 
686
        }
 
687
    }
 
688
 
 
689
    /**
 
690
     * Assert that redirect location matches pattern
 
691
     * 
 
692
     * @param  string $pattern 
 
693
     * @param  string $message 
 
694
     * @return void
 
695
     */
 
696
    public function assertRedirectRegex($pattern, $message = '')
 
697
    {
 
698
        $this->_incrementAssertionCount();
 
699
        require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
 
700
        $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
 
701
        $response   = $this->response;
 
702
        if (!$constraint->evaluate($response, __FUNCTION__, $pattern)) {
 
703
            $constraint->fail($response, $message);
 
704
        }
 
705
    }
 
706
 
 
707
    /**
 
708
     * Assert that redirect location does not match pattern
 
709
     * 
 
710
     * @param  string $pattern 
 
711
     * @param  string $message 
 
712
     * @return void
 
713
     */
 
714
    public function assertNotRedirectRegex($pattern, $message = '')
 
715
    {
 
716
        $this->_incrementAssertionCount();
 
717
        require_once 'Zend/Test/PHPUnit/Constraint/Redirect.php';
 
718
        $constraint = new Zend_Test_PHPUnit_Constraint_Redirect();
 
719
        $response   = $this->response;
 
720
        if (!$constraint->evaluate($response, __FUNCTION__, $pattern)) {
 
721
            $constraint->fail($response, $message);
 
722
        }
 
723
    }
 
724
 
 
725
    /**
 
726
     * Assert response code
 
727
     * 
 
728
     * @param  int $code 
 
729
     * @param  string $message 
 
730
     * @return void
 
731
     */
 
732
    public function assertResponseCode($code, $message = '')
 
733
    {
 
734
        $this->_incrementAssertionCount();
 
735
        require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
 
736
        $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
 
737
        $response   = $this->response;
 
738
        if (!$constraint->evaluate($response, __FUNCTION__, $code)) {
 
739
            $constraint->fail($response, $message);
 
740
        }
 
741
    }
 
742
 
 
743
    /**
 
744
     * Assert response code
 
745
     * 
 
746
     * @param  int $code 
 
747
     * @param  string $message 
 
748
     * @return void
 
749
     */
 
750
    public function assertNotResponseCode($code, $message = '')
 
751
    {
 
752
        $this->_incrementAssertionCount();
 
753
        require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
 
754
        $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
 
755
        $constraint->setNegate(true);
 
756
        $response   = $this->response;
 
757
        if (!$constraint->evaluate($response, __FUNCTION__, $code)) {
 
758
            $constraint->fail($response, $message);
 
759
        }
 
760
    }
 
761
 
 
762
    /**
 
763
     * Assert response header exists
 
764
     * 
 
765
     * @param  string $header 
 
766
     * @param  string $message 
 
767
     * @return void
 
768
     */
 
769
    public function assertHeader($header, $message = '')
 
770
    {
 
771
        $this->_incrementAssertionCount();
 
772
        require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
 
773
        $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
 
774
        $response   = $this->response;
 
775
        if (!$constraint->evaluate($response, __FUNCTION__, $header)) {
 
776
            $constraint->fail($response, $message);
 
777
        }
 
778
    }
 
779
 
 
780
    /**
 
781
     * Assert response header does not exist
 
782
     * 
 
783
     * @param  string $header 
 
784
     * @param  string $message 
 
785
     * @return void
 
786
     */
 
787
    public function assertNotHeader($header, $message = '')
 
788
    {
 
789
        $this->_incrementAssertionCount();
 
790
        require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
 
791
        $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
 
792
        $constraint->setNegate(true);
 
793
        $response   = $this->response;
 
794
        if (!$constraint->evaluate($response, __FUNCTION__, $header)) {
 
795
            $constraint->fail($response, $message);
 
796
        }
 
797
    }
 
798
 
 
799
    /**
 
800
     * Assert response header exists and contains the given string
 
801
     * 
 
802
     * @param  string $header 
 
803
     * @param  string $match 
 
804
     * @param  string $message 
 
805
     * @return void
 
806
     */
 
807
    public function assertHeaderContains($header, $match, $message = '')
 
808
    {
 
809
        $this->_incrementAssertionCount();
 
810
        require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
 
811
        $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
 
812
        $response   = $this->response;
 
813
        if (!$constraint->evaluate($response, __FUNCTION__, $header, $match)) {
 
814
            $constraint->fail($response, $message);
 
815
        }
 
816
    }
 
817
 
 
818
    /**
 
819
     * Assert response header does not exist and/or does not contain the given string
 
820
     * 
 
821
     * @param  string $header 
 
822
     * @param  string $match
 
823
     * @param  string $message 
 
824
     * @return void
 
825
     */
 
826
    public function assertNotHeaderContains($header, $match, $message = '')
 
827
    {
 
828
        $this->_incrementAssertionCount();
 
829
        require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
 
830
        $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
 
831
        $constraint->setNegate(true);
 
832
        $response   = $this->response;
 
833
        if (!$constraint->evaluate($response, __FUNCTION__, $header, $match)) {
 
834
            $constraint->fail($response, $message);
 
835
        }
 
836
    }
 
837
 
 
838
    /**
 
839
     * Assert response header exists and matches the given pattern
 
840
     * 
 
841
     * @param  string $header 
 
842
     * @param  string $pattern 
 
843
     * @param  string $message 
 
844
     * @return void
 
845
     */
 
846
    public function assertHeaderRegex($header, $pattern, $message = '')
 
847
    {
 
848
        $this->_incrementAssertionCount();
 
849
        require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
 
850
        $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
 
851
        $response   = $this->response;
 
852
        if (!$constraint->evaluate($response, __FUNCTION__, $header, $pattern)) {
 
853
            $constraint->fail($response, $message);
 
854
        }
 
855
    }
 
856
 
 
857
    /**
 
858
     * Assert response header does not exist and/or does not match the given regex
 
859
     * 
 
860
     * @param  string $header 
 
861
     * @param  string $pattern
 
862
     * @param  string $message 
 
863
     * @return void
 
864
     */
 
865
    public function assertNotHeaderRegex($header, $pattern, $message = '')
 
866
    {
 
867
        $this->_incrementAssertionCount();
 
868
        require_once 'Zend/Test/PHPUnit/Constraint/ResponseHeader.php';
 
869
        $constraint = new Zend_Test_PHPUnit_Constraint_ResponseHeader();
 
870
        $constraint->setNegate(true);
 
871
        $response   = $this->response;
 
872
        if (!$constraint->evaluate($response, __FUNCTION__, $header, $pattern)) {
 
873
            $constraint->fail($response, $message);
 
874
        }
 
875
    }
 
876
 
 
877
    /**
 
878
     * Assert that the last handled request used the given module
 
879
     * 
 
880
     * @param  string $module 
 
881
     * @param  string $message 
 
882
     * @return void
 
883
     */
 
884
    public function assertModule($module, $message = '')
 
885
    {
 
886
        $this->_incrementAssertionCount();
 
887
        if ($module != $this->request->getModuleName()) {
 
888
            $msg = sprintf('Failed asserting last module used was "%s"', $module);
 
889
            if (!empty($message)) {
 
890
                $msg = $message . "\n" . $msg;
 
891
            }
 
892
            $this->fail($msg);
 
893
        }
 
894
    }
 
895
 
 
896
    /**
 
897
     * Assert that the last handled request did NOT use the given module
 
898
     * 
 
899
     * @param  string $module 
 
900
     * @param  string $message 
 
901
     * @return void
 
902
     */
 
903
    public function assertNotModule($module, $message = '')
 
904
    {
 
905
        $this->_incrementAssertionCount();
 
906
        if ($module == $this->request->getModuleName()) {
 
907
            $msg = sprintf('Failed asserting last module used was NOT "%s"', $module);
 
908
            if (!empty($message)) {
 
909
                $msg = $message . "\n" . $msg;
 
910
            }
 
911
            $this->fail($msg);
 
912
        }
 
913
    }
 
914
 
 
915
    /**
 
916
     * Assert that the last handled request used the given controller
 
917
     * 
 
918
     * @param  string $controller 
 
919
     * @param  string $message 
 
920
     * @return void
 
921
     */
 
922
    public function assertController($controller, $message = '')
 
923
    {
 
924
        $this->_incrementAssertionCount();
 
925
        if ($controller != $this->request->getControllerName()) {
 
926
            $msg = sprintf('Failed asserting last controller used was "%s"', $controller);
 
927
            if (!empty($message)) {
 
928
                $msg = $message . "\n" . $msg;
 
929
            }
 
930
            $this->fail($msg);
 
931
        }
 
932
    }
 
933
 
 
934
    /**
 
935
     * Assert that the last handled request did NOT use the given controller
 
936
     * 
 
937
     * @param  string $controller 
 
938
     * @param  string $message 
 
939
     * @return void
 
940
     */
 
941
    public function assertNotController($controller, $message = '')
 
942
    {
 
943
        $this->_incrementAssertionCount();
 
944
        if ($controller == $this->request->getControllerName()) {
 
945
            $msg = sprintf('Failed asserting last controller used was NOT "%s"', $controller);
 
946
            if (!empty($message)) {
 
947
                $msg = $message . "\n" . $msg;
 
948
            }
 
949
            $this->fail($msg);
 
950
        }
 
951
    }
 
952
 
 
953
    /**
 
954
     * Assert that the last handled request used the given action
 
955
     * 
 
956
     * @param  string $action 
 
957
     * @param  string $message 
 
958
     * @return void
 
959
     */
 
960
    public function assertAction($action, $message = '')
 
961
    {
 
962
        $this->_incrementAssertionCount();
 
963
        if ($action != $this->request->getActionName()) {
 
964
            $msg = sprintf('Failed asserting last action used was "%s"', $action);
 
965
            if (!empty($message)) {
 
966
                $msg = $message . "\n" . $msg;
 
967
            }
 
968
            $this->fail($msg);
 
969
        }
 
970
    }
 
971
 
 
972
    /**
 
973
     * Assert that the last handled request did NOT use the given action
 
974
     * 
 
975
     * @param  string $action 
 
976
     * @param  string $message 
 
977
     * @return void
 
978
     */
 
979
    public function assertNotAction($action, $message = '')
 
980
    {
 
981
        $this->_incrementAssertionCount();
 
982
        if ($action == $this->request->getActionName()) {
 
983
            $msg = sprintf('Failed asserting last action used was NOT "%s"', $action);
 
984
            if (!empty($message)) {
 
985
                $msg = $message . "\n" . $msg;
 
986
            }
 
987
            $this->fail($msg);
 
988
        }
 
989
    }
 
990
 
 
991
    /**
 
992
     * Assert that the specified route was used
 
993
     * 
 
994
     * @param  string $route 
 
995
     * @param  string $message 
 
996
     * @return void
 
997
     */
 
998
    public function assertRoute($route, $message = '')
 
999
    {
 
1000
        $this->_incrementAssertionCount();
 
1001
        $router = $this->frontController->getRouter();
 
1002
        if ($route != $router->getCurrentRouteName()) {
 
1003
            $msg = sprintf('Failed asserting route matched was "%s"', $route);
 
1004
            if (!empty($message)) {
 
1005
                $msg = $message . "\n" . $msg;
 
1006
            }
 
1007
            $this->fail($msg);
 
1008
        }
 
1009
    }
 
1010
 
 
1011
    /**
 
1012
     * Assert that the route matched is NOT as specified
 
1013
     * 
 
1014
     * @param  string $route 
 
1015
     * @param  string $message 
 
1016
     * @return void
 
1017
     */
 
1018
    public function assertNotRoute($route, $message = '')
 
1019
    {
 
1020
        $this->_incrementAssertionCount();
 
1021
        $router = $this->frontController->getRouter();
 
1022
        if ($route == $router->getCurrentRouteName()) {
 
1023
            $msg = sprintf('Failed asserting route matched was NOT "%s"', $route);
 
1024
            if (!empty($message)) {
 
1025
                $msg = $message . "\n" . $msg;
 
1026
            }
 
1027
            $this->fail($msg);
 
1028
        }
 
1029
    }
 
1030
 
 
1031
    /**
 
1032
     * Retrieve front controller instance
 
1033
     * 
 
1034
     * @return Zend_Controller_Front
 
1035
     */
 
1036
    public function getFrontController()
 
1037
    {
 
1038
        if (null === $this->_frontController) {
 
1039
            $this->_frontController = Zend_Controller_Front::getInstance();
 
1040
        }
 
1041
        return $this->_frontController;
 
1042
    }
 
1043
 
 
1044
    /**
 
1045
     * Retrieve test case request object
 
1046
     * 
 
1047
     * @return Zend_Controller_Request_Abstract
 
1048
     */
 
1049
    public function getRequest()
 
1050
    {
 
1051
        if (null === $this->_request) {
 
1052
            require_once 'Zend/Controller/Request/HttpTestCase.php';
 
1053
            $this->_request = new Zend_Controller_Request_HttpTestCase;
 
1054
        }
 
1055
        return $this->_request;
 
1056
    }
 
1057
 
 
1058
    /**
 
1059
     * Retrieve test case response object 
 
1060
     * 
 
1061
     * @return Zend_Controller_Response_Abstract
 
1062
     */
 
1063
    public function getResponse()
 
1064
    {
 
1065
        if (null === $this->_response) {
 
1066
            require_once 'Zend/Controller/Response/HttpTestCase.php';
 
1067
            $this->_response = new Zend_Controller_Response_HttpTestCase;
 
1068
        }
 
1069
        return $this->_response;
 
1070
    }
 
1071
 
 
1072
    /**
 
1073
     * Retrieve DOM query object
 
1074
     * 
 
1075
     * @return Zend_Dom_Query
 
1076
     */
 
1077
    public function getQuery()
 
1078
    {
 
1079
        if (null === $this->_query) {
 
1080
            require_once 'Zend/Dom/Query.php';
 
1081
            $this->_query = new Zend_Dom_Query;
 
1082
        }
 
1083
        return $this->_query;
 
1084
    }
 
1085
 
 
1086
    /**
 
1087
     * Increment assertion count
 
1088
     * 
 
1089
     * @return void
 
1090
     */
 
1091
    protected function _incrementAssertionCount()
 
1092
    {
 
1093
        $stack = debug_backtrace();
 
1094
        foreach (debug_backtrace() as $step) {
 
1095
            if (isset($step['object']) 
 
1096
                && $step['object'] instanceof PHPUnit_Framework_TestCase
 
1097
            ) {
 
1098
                if (version_compare(PHPUnit_Runner_Version::id(), '3.3.3', 'lt')) {
 
1099
                    $step['object']->incrementAssertionCounter();
 
1100
                } else {
 
1101
                    $step['object']->addToAssertionCount(1);
 
1102
                }
 
1103
                break;
 
1104
            }
 
1105
        }
 
1106
    }
 
1107
}