Layout.php 5.41 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) 2012 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
 */

/**
 * Layout configuration constraint
 *
 */
class EcomDev_PHPUnit_Constraint_Config_Layout
    extends EcomDev_PHPUnit_Constraint_Config_Abstract
{
    const XML_PATH_LAYOUT = '%s/layout/updates';

    const TYPE_LAYOUT_DEFINITION = 'layout_definition';
    const TYPE_LAYOUT_FILE = 'layout_file';

    /**
     * Design area (frontend, adminhtml)
     *
     * @var string
     */
    protected $_area = null;

    /**
     * Name of layout update,
     * if specified, constraint
     * will be additionally checked by this parameter
     *
     * @var string
     */
    protected $_layoutUpdate = null;

    /**
     * Restriction by theme name
     *
     * @var string
     */
    protected $_theme = null;

    /**
     * Restriction by design package name
     *
     * @var string
     */
    protected $_designPackage = null;

    /**
     * Model for assertion of the data
     *
     * @var EcomDev_PHPUnit_Constraint_Config_Design_Package_Interface
     */
    protected static $_designPackageModel = null;

    /**
     * Configuration constraint for cheking the existance of
     * layout file in configuration and a particular theme as well
     *
     * @param string $area design area (frontend|adminhtml)
     * @param string $expectedFile layout file name that should be checked
     * @param string $type type of assertion
     * @param string|null $layoutUpdate additional check for layout update name for assertion of configuration
     * @param string|null $theme additional check for layout file existance in a particular theme
     * @param string|null $designPackage additional check for layout file existance in a particular theme
     */
    public function __construct($area, $expectedFile, $type, $layoutUpdate = null,
        $theme = null, $designPackage = null)
    {
        $this->_area = $area;
        $this->_layoutUpdate = $layoutUpdate;
        $this->_designPackage = $designPackage;
        $this->_theme = $theme;

        $this->_expectedValueValidation += array(
            self::TYPE_LAYOUT_FILE => array(true, 'is_string', 'string'),
            self::TYPE_LAYOUT_DEFINITION => array(true, 'is_string', 'string')
        );

        $this->_typesWithDiff[] = self::TYPE_LAYOUT_FILE;

        $nodePath = sprintf(self::XML_PATH_LAYOUT, $area);

        parent::__construct($nodePath, $type, $expectedFile);
    }

    /**
     * Sets design package model for assertions
     *
102
     * @param EcomDev_PHPUnit_Design_Package_Interface $model
103
     */
104
    public static function setDesignPackageModel(EcomDev_PHPUnit_Design_Package_Interface $model)
105 106 107 108 109 110 111
    {
        self::$_designPackageModel = $model;
    }

    /**
     * Retrieves design package model that was set before
     *
112
     * @return EcomDev_PHPUnit_Design_Package_Interface
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
     */
    public static function getDesignPackageModel()
    {
        return self::$_designPackageModel;
    }

    /**
     * Checks layout definititions for expected file defined in the configuration
     *
     * @param Varien_Simplexml_Element $other
     * @return boolean
     */
    protected function evaluateLayoutDefinition($other)
    {
        foreach ($other->children() as $layoutUpdate) {
            if ((string)$layoutUpdate->file === $this->_expectedValue
                && ($this->_layoutUpdate === null || $this->_layoutUpdate === $layoutUpdate->getName())) {
                return true;
            }
        }

        return false;
    }

    /**
     * Layout defition assertion text
     *
     * @return string
     */
142
    protected function textLayoutDefinition()
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    {
        $text = sprintf('file "%s" is defined in configuration for %s area', $this->_expectedValue, $this->_area);

        if ($this->_layoutUpdate !== null) {
            $text .= sprintf(' in "%s" layout update', $this->_layoutUpdate);
        }

        return $text;
    }

    /**
     * Evaluates layout file existance
     *
     * @param Varien_Simplexml_Element $other
     * @return boolean
     */
    protected function evaluateLayoutFile($other)
    {
        $assertion = self::getDesignPackageModel()
            ->getLayoutFileAssertion($this->_expectedValue, $this->_area, $this->_designPackage, $this->_theme);

        $this->setActualValue($assertion['actual']);
165
        $this->_expectedValue = $assertion['expected'];
166

167
        return $this->_actualValue === $this->_expectedValue;
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    }

    /**
     * Text representation of layout file existance constraint
     *
     * @return string
     */
    protected function textLayoutFile()
    {
        return 'file is the same as expected and exists';
    }

    /**
     * Custom failure description for showing config related errors
     * (non-PHPdoc)
     * @see PHPUnit_Framework_Constraint::customFailureDescription()
     */
185
    protected function customFailureDescription($other)
186 187
    {
        return sprintf(
188
            'layout %s.',
189 190 191 192
            $this->toString()
        );
    }
}