ClassAlias.php 3.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
<?php

/**
 * Class alias constraint
 *
 */
class EcomDev_PHPUnit_Constraint_Config_ClassAlias
    extends EcomDev_PHPUnit_Constraint_Config_Abstract
{
    const XML_PATH_CLASS_ALIAS = 'global/%s/%s';

    const GROUP_BLOCK = 'blocks';
    const GROUP_MODEL = 'models';
    const GROUP_HELPER = 'helpers';

    const TYPE_CLASS_ALIAS = 'class_alias';

    /**
     * Mapping of the text that represents
     * class alias group in fail message
     *
     * @var array
     */
    protected $_textByGroup = array(
        self::GROUP_BLOCK => 'block alias',
        self::GROUP_MODEL => 'model alias',
        self::GROUP_HELPER => 'helper alias'
    );

    /**
     * Class alias name, e.g. the name of the model
     *
     * @var string
     */
    protected $_classAliasName = null;

    /**
     * Class alias prefix,
     * e.g. the prefix for models of a particular module
     *
     * @var string
     */
    protected $_classAliasPrefix = null;

    /**
     * Class alias group
     *
     * @var string
     */
    protected $_group = null;

    /**
     * Constraint for evaluation of grouped class alias (block, model, helper)
     *
     * @param string $group
     * @param string $classAlias
     * @param string $expectedClassName
     * @param string $type
     */
    public function __construct($group, $classAlias, $expectedClassName, $type = self::TYPE_CLASS_ALIAS)
    {
        if (!isset($this->_textByGroup[$group])) {
            throw PHPUnit_Util_InvalidArgumentHelper::factory(
                1,
                implode(
                    '|',
                    array_keys($this->_textByGroup)
                ),
                $group
            );
        }

        $this->_group = $group;

        if ($group === self::GROUP_HELPER && strpos($classAlias, '/') === false) {
            $classAlias .= '/data';
        }

        if (!strpos($classAlias, '/')) {
            throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'class/alias', $classAlias);
        }

        list($this->_classAliasPrefix, $this->_classAliasName) = explode('/', $classAlias, 2);

        $nodePath = sprintf(self::XML_PATH_CLASS_ALIAS, $group, $this->_classAliasPrefix);

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

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

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

    /**
     * Evaluates class alias is mapped to expected class name
     *
     * @param Varien_Simplexml_Element $other
     * @return boolean
     */
    protected function evaluateClassAlias($other)
    {
        $classPrefix = $other->class;

        if (isset($other->rewrite->{$this->_classAliasName})) {
            $className = (string)$other->rewrite->{$this->_classAliasName};
        } else {
            $className = $classPrefix . '_' . uc_words($this->_classAliasName);
        }

        $this->setActualValue($className);
        return $this->_actualValue === $this->_expectedValue;
    }

    /**
     * Text representation of class alias constaint
     *
     * @return string
     */
    protected function textClassAlias()
    {
        return 'is mapped to expected class name';
    }

    /**
     * Custom failure description for showing config related errors
     * (non-PHPdoc)
     * @see PHPUnit_Framework_Constraint::customFailureDescription()
     */
131
    protected function customFailureDescription($other)
132 133
    {
        return sprintf(
134
            '%s "%s/%s" %s.',
135 136 137 138 139 140
            $this->_textByGroup[$this->_group],
            $this->_classAliasPrefix, $this->_classAliasName,
            $this->toString()
        );
    }
}