We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

Resource.php 7.38 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13
/**
 * 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
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author     Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
 */

19 20 21 22 23 24 25 26 27 28
/**
 * Setup resources configuration constraint
 *
 */
class EcomDev_PHPUnit_Constraint_Config_Resource
    extends EcomDev_PHPUnit_Constraint_Config_Abstract
{
    const XML_PATH_RESOURCES_NODE = 'global/resources';

    const TYPE_SETUP_DEFINED = 'setup_defined';
29 30
    const TYPE_SETUP_SCHEME_EXISTS = 'setup_scheme_exists';
    const TYPE_SETUP_DATA_EXISTS = 'setup_data_exists';
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

    /**
     * Name of the module for constraint
     *
     * @var string
     */
    protected $_moduleName = null;
    
    /**
     * The module directory for constraint
     *
     * @var string
     */
    protected $_moduleDirectory = null;

    /**
47
     * Constraint for evaluation of module config node
48 49 50 51 52 53 54 55 56 57
     *
     * @param string $nodePath
     * @param string $type
     * @param string $moduleDirectory
     * @param mixed $expectedValue
     */
    public function __construct($moduleName, $type, $moduleDirectory = null, $expectedValue = null)
    {
        $this->_expectedValueValidation += array(
            self::TYPE_SETUP_DEFINED => array(false, 'is_string', 'string'),
58 59
            self::TYPE_SETUP_SCHEME_EXISTS => array(false, 'is_string', 'string'),
            self::TYPE_SETUP_DATA_EXISTS => array(false, 'is_string', 'string'),
60 61 62
        );

        $this->_typesWithDiff[] = self::TYPE_SETUP_DEFINED;
63 64
        $this->_typesWithDiff[] = self::TYPE_SETUP_SCHEME_EXISTS;
        $this->_typesWithDiff[] = self::TYPE_SETUP_DATA_EXISTS;
65 66 67 68 69 70 71 72 73 74

        parent::__construct(
            self::XML_PATH_RESOURCES_NODE,
            $type,
            $expectedValue
        );

        $this->_moduleName = $moduleName;
        $this->_moduleDirectory = $moduleDirectory;
        
75
        if (($this->_type === self::TYPE_SETUP_SCHEME_EXISTS || $this->_type === self::TYPE_SETUP_DATA_EXISTS)
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
            && !is_dir($moduleDirectory)) {
            throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'real directory', $moduleDirectory);
        }
    }
    
    /**
     * Returns list of module setup resources
     * 
     * @param Varien_Simplexml_Element $xml
     * @return array
     */
    protected function getModuleSetupResources(Varien_Simplexml_Element $xml)
    {
        $resourcesForModule = array(); 
        foreach ($xml->children() as $resourceNode) {
            if (isset($resourceNode->setup->module) 
                && (string)$resourceNode->setup->module === $this->_moduleName) {
                $resourcesForModule[] = $resourceNode->getName();
            }
        }
        
        return $resourcesForModule;
    }
    
    /**
     * Checks definition of expected resource name
     *
     * @param Varien_Simplexml_Element $other
     */
    protected function evaluateSetupDefined($other)
    {
        $moduleResources = $this->getModuleSetupResources($other);
        
        if ($this->_expectedValue === null) {
            $this->_expectedValue = empty($moduleResources) ? 
                                    strtolower($this->_moduleName) . '_setup' : 
                                    current($moduleResources);
        }
        
        $this->setActualValue($moduleResources);
        
        return in_array($this->_expectedValue, $this->_actualValue);
    }
    
    /**
121
     * Represents constraint for definition of setup resources
122 123 124 125 126
     * 
     * @return string
     */
    public function textSetupDefined()
    {
127 128
        return sprintf('contains resource definition for %s module with %s name',
                       $this->_moduleName, $this->_expectedValue);
129 130 131
    }
    
    /**
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
     * Set actual value for comparison from module sql/data directories
     * 
     * @param string $type
     * @return EcomDev_PHPUnit_Constraint_Config_Resource
     */
    protected function setActualValueFromResourceDirectories($type = 'sql')
    {
        if (!is_dir($this->_moduleDirectory . DIRECTORY_SEPARATOR . $type)) {
            $this->setActualValue(array());
            return $this;
        }
        
        $dirIterator = new DirectoryIterator($this->_moduleDirectory . DIRECTORY_SEPARATOR . $type);
        
        $resourceDirectories = array();

        foreach ($dirIterator as $entry) {
            /* @var $entry DirectoryIterator */
            if ($entry->isDir() && !$entry->isDot()) {
                $resourceDirectories[] = $entry->getBasename();
            }
        }
        
        $this->setActualValue($resourceDirectories);
        
        return $this;
    }
    
    /**
     * Checks existence and definition of expected resource name schema directory
162 163 164
     *
     * @param Varien_Simplexml_Element $other
     */
165
    protected function evaluateSetupSchemeExists($other)
166 167 168 169 170 171 172 173 174
    {
        $moduleResources = $this->getModuleSetupResources($other);
        
        if ($this->_expectedValue === null) {
            $this->_expectedValue = empty($moduleResources) ? 
                                    strtolower($this->_moduleName) . '_setup' : 
                                    current($moduleResources);
        }
        
175
        $this->setActualValueFromResourceDirectories('sql');
176
        
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
        return in_array($this->_expectedValue, $moduleResources) 
               && in_array($this->_expectedValue, $this->_actualValue);
    }
    
    /**
     * Represents constraint for definition of setup resources
     * 
     * @return string
     */
    public function textSetupSchemeExists()
    {
        return sprintf(' schema directory is created for %s module with %s name',
                       $this->_moduleName, $this->_expectedValue);
    }
    
    /**
     * Checks existence and definition of expected resource name data directory
     *
     * @param Varien_Simplexml_Element $other
     */
    protected function evaluateSetupDataExists($other)
    {
        $moduleResources = $this->getModuleSetupResources($other);
200
        
201 202 203 204
        if ($this->_expectedValue === null) {
            $this->_expectedValue = empty($moduleResources) ? 
                                    strtolower($this->_moduleName) . '_setup' : 
                                    current($moduleResources);
205 206
        }
        
207
        $this->setActualValueFromResourceDirectories('data');
208 209 210 211 212 213
        
        return in_array($this->_expectedValue, $moduleResources) 
               && in_array($this->_expectedValue, $this->_actualValue);
    }
    
    /**
214
     * Represents constraint for definition of setup resources
215 216 217
     * 
     * @return string
     */
218
    public function textSetupDataExists()
219
    {
220
        return sprintf(' data directory is created for %s module with %s name', $this->_moduleName, $this->_expectedValue);
221 222 223 224 225 226 227
    }
    
    /**
     * Custom failure description for showing config related errors
     * (non-PHPdoc)
     * @see PHPUnit_Framework_Constraint::customFailureDescription()
     */
228
    protected function customFailureDescription($other)
229 230
    {
        return sprintf(
231
            'setup resources %s.',
232 233 234 235
            $this->toString()
        );
    }
}
236