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

Expectation.php 4.39 KB
Newer Older
1 2 3
<?php

class EcomDev_PHPUnit_Model_Expectation
4
    implements EcomDev_PHPUnit_Model_ExpectationInterface
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
{
    /**
     * List of created data object ids by path format
     *
     * @var array
     */
    protected $_dataObjectIds = array();

    /**
     * Loaded data from Yaml files
     *
     * @var Varien_Object
     */
    protected $_loadedData = null;

    /**
     * Data object used for managing
     * expectation data
     *
     * @var string
     */
    protected $_dataObjectClassAlias = 'ecomdev_phpunit/expectation_object';

    /**
     * Returns class alias for fixture data object
     *
     * @return string
     */
    public function getDataObjectClassAlias()
    {
        return $this->_dataObjectClassAlias;
    }

    /**
     * Retrieves data object for a particular path format
     *
41
     * @see EcomDev_PHPUnit_Model_ExpectationInterface::getDataObject()
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
     */
    public function getDataObject($pathFormat = null, $args = array())
    {
        if ($pathFormat === null) {
            return $this->_loadedData;
        }

        $argsHash = $pathFormat . '_' . md5(serialize($args));

        // Check already created objects by path
        if (!isset($this->_dataObjectIds[$argsHash])) {
            if ($args) {
               array_unshift($args, $pathFormat);
               $dataPath = call_user_func_array('sprintf', $args);
            } else {
               $dataPath = $pathFormat;
            }

            $data = $this->_loadedData->getData($dataPath);

            if (!is_array($data)) {
               throw new InvalidArgumentException(
                   'Argument values for specifying special scope of expectations should be presented '
                   . ' in expectation file and should be an associative list (path: "' . $dataPath . '")'
               );
            }

            $this->_dataObjectIds[$argsHash] = Mage::objects()->save(
                Mage::getModel($this->getDataObjectClassAlias(), $data)
            );
        }

        return Mage::objects($this->_dataObjectIds[$argsHash]);
    }

    /**
     * Applies loaded data
     *
80
     * @see EcomDev_PHPUnit_Model_Test_LoadableInterface::apply()
81 82 83 84 85 86 87 88 89 90 91
     */
    public function apply()
    {
        // For now it does nothing :(
        return $this;
    }

    /**
     * Removes objects created in object cache
     * Clears loaded data property
     *
92
     * @see EcomDev_PHPUnit_Model_Test_LoadableInterface::discard()
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
     */
    public function discard()
    {
        foreach ($this->_dataObjectIds as $objectId) {
            Mage::objects()->delete($objectId);
        }

        $this->_dataObjectIds = array();
        $this->_loadedData = null;

        return $this;
    }

    /**
     * Check that expectations is loaded
     *
     * @return boolean
     */
    public function isLoaded()
    {
        return $this->_loadedData !== null;
    }

    /**
     * Loads expected data from test case annotations
     *
119
     * @see EcomDev_PHPUnit_Model_Test_LoadableInterface::loadByTestCase()
120
     */
121
    public function loadByTestCase(PHPUnit_Framework_TestCase $testCase)
122
    {
123 124 125
        $expectations = EcomDev_PHPUnit_Test_Case_Util::getAnnotationByNameFromClass(
            get_class($testCase), 'loadExpectation', array('class', 'method'), $testCase->getName(false)
        );
126 127

        if (!$expectations) {
128
            $expectations[] = $testCase->getName(false);
129 130 131 132 133 134
        }

        $expectationData = array();

        foreach ($expectations as $expectation) {
            if (empty($expectation)) {
135
                $expectation = $testCase->getName(false);
136 137
            }

138 139 140 141
            $expectationFile = EcomDev_PHPUnit_Test_Case_Util::getYamlLoader(get_class($testCase))
                ->resolveFilePath(
                    get_class($testCase), EcomDev_PHPUnit_Model_Yaml_Loader::TYPE_EXPECTATION, $expectation
                );
142 143 144 145 146 147 148 149 150 151

            if (!$expectationFile) {
                $text = 'There was no expectation defined for current test case';
                if ($expectation) {
                    $text = sprintf('Cannot load expectation %s', $expectation);
                }
                throw new RuntimeException($text);
            }

            $expectationData = array_merge_recursive(
152
                $expectationData, EcomDev_PHPUnit_Test_Case_Util::getYamlLoader()->load($expectationFile)
153 154 155 156 157 158 159 160 161
            );
        }

        $this->_loadedData = new Varien_Object($expectationData);
        return $this;
    }


}