PageRenderTime 137ms CodeModel.GetById 30ms app.highlight 90ms RepoModel.GetById 7ms app.codeStats 0ms

/library/Test/PHPUnit/ControllerTestCase.php

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