PageRenderTime 30ms CodeModel.GetById 19ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Test/PHPUnit/Constraint/Redirect.php

https://bitbucket.org/hamidrezas/melobit
PHP | 282 lines | 140 code | 30 blank | 112 comment | 17 complexity | 27aefd6d7075cd9e49fa60ecc4ccbc5f MD5 | raw file
Possible License(s): AGPL-1.0
  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 * @subpackage PHPUnit
 18 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Redirect.php 24594 2012-01-05 21:27:01Z matthew $
 21 */
 22
 23/** @see PHPUnit_Framework_Constraint */
 24require_once 'PHPUnit/Framework/Constraint.php';
 25
 26/**
 27 * Redirection constraints
 28 *
 29 * @uses       PHPUnit_Framework_Constraint
 30 * @category   Zend
 31 * @package    Zend_Test
 32 * @subpackage PHPUnit
 33 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 34 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 35 */
 36class Zend_Test_PHPUnit_Constraint_Redirect extends PHPUnit_Framework_Constraint
 37{
 38    /**#@+
 39     * Assertion type constants
 40     */
 41    const ASSERT_REDIRECT       = 'assertRedirect';
 42    const ASSERT_REDIRECT_TO    = 'assertRedirectTo';
 43    const ASSERT_REDIRECT_REGEX = 'assertRedirectRegex';
 44    /**#@-*/
 45
 46    /**
 47     * Current assertion type
 48     * @var string
 49     */
 50    protected $_assertType      = null;
 51
 52    /**
 53     * Available assertion types
 54     * @var array
 55     */
 56    protected $_assertTypes     = array(
 57        self::ASSERT_REDIRECT,
 58        self::ASSERT_REDIRECT_TO,
 59        self::ASSERT_REDIRECT_REGEX,
 60    );
 61
 62    /**
 63     * Pattern to match against
 64     * @var string
 65     */
 66    protected $_match             = null;
 67
 68    /**
 69     * Whether or not assertion is negated
 70     * @var bool
 71     */
 72    protected $_negate            = false;
 73
 74    /**
 75     * Constructor; setup constraint state
 76     *
 77     * @return void
 78     */
 79    public function __construct()
 80    {
 81    }
 82
 83    /**
 84     * Indicate negative match
 85     *
 86     * @param  bool $flag
 87     * @return void
 88     */
 89    public function setNegate($flag = true)
 90    {
 91        $this->_negate = $flag;
 92    }
 93
 94    /**
 95     * Evaluate an object to see if it fits the constraints
 96     *
 97     * @param  string $other String to examine
 98     * @param  null|string Assertion type
 99     * @return bool
100     */
101    public function evaluate($other, $assertType = null)
102    {
103        if (!$other instanceof Zend_Controller_Response_Abstract) {
104            require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
105            throw new Zend_Test_PHPUnit_Constraint_Exception('Redirect constraint assertions require a response object');
106        }
107
108        if (strstr($assertType, 'Not')) {
109            $this->setNegate(true);
110            $assertType = str_replace('Not', '', $assertType);
111        }
112
113        if (!in_array($assertType, $this->_assertTypes)) {
114            require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
115            throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
116        }
117
118        $this->_assertType = $assertType;
119
120        $response = $other;
121        $argv     = func_get_args();
122        $argc     = func_num_args();
123
124        switch ($assertType) {
125            case self::ASSERT_REDIRECT_TO:
126                if (3 > $argc) {
127                    require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
128                    throw new Zend_Test_PHPUnit_Constraint_Exception('No redirect URL provided against which to match');
129                }
130                $this->_match = $match = $argv[2];
131                return ($this->_negate)
132                    ? $this->_notMatch($response, $match)
133                    : $this->_match($response, $match);
134            case self::ASSERT_REDIRECT_REGEX:
135                if (3 > $argc) {
136                    require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
137                    throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match redirect');
138                }
139                $this->_match = $match = $argv[2];
140                return ($this->_negate)
141                    ? $this->_notRegex($response, $match)
142                    : $this->_regex($response, $match);
143            case self::ASSERT_REDIRECT:
144            default:
145                return ($this->_negate) ? !$response->isRedirect() : $response->isRedirect();
146        }
147    }
148
149    /**
150     * Report Failure
151     *
152     * @see    PHPUnit_Framework_Constraint for implementation details
153     * @param  mixed $other
154     * @param  string $description Additional message to display
155     * @param  bool $not
156     * @return void
157     * @throws PHPUnit_Framework_ExpectationFailedException
158     */
159    public function fail($other, $description, $not = false)
160    {
161        require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
162        switch ($this->_assertType) {
163            case self::ASSERT_REDIRECT_TO:
164                $failure = 'Failed asserting response redirects to "%s"';
165                if ($this->_negate) {
166                    $failure = 'Failed asserting response DOES NOT redirect to "%s"';
167                }
168                $failure = sprintf($failure, $this->_match);
169                break;
170            case self::ASSERT_REDIRECT_REGEX:
171                $failure = 'Failed asserting response redirects to URL MATCHING "%s"';
172                if ($this->_negate) {
173                    $failure = 'Failed asserting response DOES NOT redirect to URL MATCHING "%s"';
174                }
175                $failure = sprintf($failure, $this->_match);
176                break;
177            case self::ASSERT_REDIRECT:
178            default:
179                $failure = 'Failed asserting response is a redirect';
180                if ($this->_negate) {
181                    $failure = 'Failed asserting response is NOT a redirect';
182                }
183                break;
184        }
185
186        if (!empty($description)) {
187            $failure = $description . "\n" . $failure;
188        }
189
190        throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
191    }
192
193    /**
194     * Complete implementation
195     *
196     * @return string
197     */
198    public function toString()
199    {
200        return '';
201    }
202
203    /**
204     * Check to see if content is matched in selected nodes
205     *
206     * @param  Zend_Controller_Response_HttpTestCase $response
207     * @param  string $match Content to match
208     * @return bool
209     */
210    protected function _match($response, $match)
211    {
212        if (!$response->isRedirect()) {
213            return false;
214        }
215
216        $headers  = $response->sendHeaders();
217        $redirect = $headers['location'];
218        $redirect = str_replace('Location: ', '', $redirect);
219
220        return ($redirect == $match);
221    }
222
223    /**
224     * Check to see if content is NOT matched in selected nodes
225     *
226     * @param  Zend_Controller_Response_HttpTestCase $response
227     * @param  string $match
228     * @return bool
229     */
230    protected function _notMatch($response, $match)
231    {
232        if (!$response->isRedirect()) {
233            return true;
234        }
235
236        $headers  = $response->sendHeaders();
237        $redirect = $headers['location'];
238        $redirect = str_replace('Location: ', '', $redirect);
239
240        return ($redirect != $match);
241    }
242
243    /**
244     * Check to see if content is matched by regex in selected nodes
245     *
246     * @param  Zend_Controller_Response_HttpTestCase $response
247     * @param  string $pattern
248     * @return bool
249     */
250    protected function _regex($response, $pattern)
251    {
252        if (!$response->isRedirect()) {
253            return false;
254        }
255
256        $headers  = $response->sendHeaders();
257        $redirect = $headers['location'];
258        $redirect = str_replace('Location: ', '', $redirect);
259
260        return preg_match($pattern, $redirect);
261    }
262
263    /**
264     * Check to see if content is NOT matched by regex in selected nodes
265     *
266     * @param  Zend_Controller_Response_HttpTestCase $response
267     * @param  string $pattern
268     * @return bool
269     */
270    protected function _notRegex($response, $pattern)
271    {
272        if (!$response->isRedirect()) {
273            return true;
274        }
275
276        $headers  = $response->sendHeaders();
277        $redirect = $headers['location'];
278        $redirect = str_replace('Location: ', '', $redirect);
279
280        return !preg_match($pattern, $redirect);
281    }
282}