PageRenderTime 63ms CodeModel.GetById 39ms app.highlight 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Test/PHPUnit/ControllerTestCase.php

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