Header.php 2.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * PHP Unit test suite for Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 *
 * @category   EcomDev
 * @package    EcomDev_PHPUnit
14
 * @copyright  Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
 */

/**
 * Constraint for controller response header assertions
 *
 */
class EcomDev_PHPUnit_Constraint_Controller_Response_Header
    extends EcomDev_PHPUnit_Constraint_Controller_Response_Abstract
{
    const TYPE_CONSTRAINT = 'constraint';
    const TYPE_SENT = 'sent';

    /**
     * The name of the header that will be asserted
     *
     * @var string
     */
    protected $_headerName = null;

    /**
     * Response header assertion
     *
     * @param string $headerName
     * @param string $type
     * @param PHPUnit_Framework_Constraint $constraint
     */
    public function __construct($headerName, $type, PHPUnit_Framework_Constraint $constraint = null)
    {
        if (empty($headerName) || !is_string($headerName)) {
            throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string', $headerName);
        }

        $this->_expectedValueValidation += array(
            self::TYPE_CONSTRAINT => array(true, null, 'PHPUnit_Framework_Constraint')
        );

        parent::__construct($type, $constraint);
        $this->_headerName = $headerName;
    }

    /**
     * Evaluates controller response header is sent
     *
     *
     * @param EcomDev_PHPUnit_Controller_Response_Interface $other
     */
    protected function evaluateSent($other)
    {
        $this->setActualValue($other->getSentHeaders());
        return $other->getSentHeader($this->_headerName) !== false;
    }

    /**
     * Text representation of header is sent assertion
     *
     * @return string
     */
    protected function textSent()
    {
        return sprintf('header "%s" is sent', $this->_headerName);
    }

    /**
     * Evaluates controller response header is evaluated by constraint
     *
     *
     * @param EcomDev_PHPUnit_Controller_Response_Interface $other
     */
    protected function evaluateConstraint($other)
    {
        $this->setActualValue($other->getSentHeader($this->_headerName));
88
        return $this->_expectedValue->evaluate($this->_actualValue, '', true);
89 90 91 92 93 94 95 96 97 98 99 100
    }

    /**
     * Text representation of header is evaluated by constraint assertion
     *
     * @return string
     */
    protected function textConstraint()
    {
        return sprintf('header "%s" value %s', $this->_headerName, $this->_expectedValue->toString());
    }
}