Commit 83c456bc authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

Merge branch 'release/0.3.0'

parents 04d37272 181305ac
......@@ -8,8 +8,8 @@ This extension was created especially for resolving this problem and promoting t
System Requirements
-------------------
* PHP 5.3 or higher
* PHPUnit 3.6.x
* Magento CE1.4.x-1.5.x/PE1.9.x-PE1.10.x/EE1.9.x-1.10.x
* PHPUnit 3.7.x
* Magento CE1.4.x-1.7.x/PE1.9.x-PE1.10.x/EE1.9.x-1.12.x
Documentation
-------------
......@@ -40,31 +40,11 @@ required for proper controller tests.
4. Run the unit tests first time for installing test database. It will take about 3 minutes.
$ phpunit UnitTests.php
$ phpunit
5. If it shows that there was no tests found, it means that extension was successfully
installed. If it shows some errors than it means that your customizations has install
scripts that relay on your current database data so you should fix them.
### Magento Connect
1. Get the extension key from the [extension page](http://www.magentocommerce.com/magento-connect/EcomDev/extension/5717/ecomdev_phpunit) and install it via Magento Connect manager.
2. Open app/etc/local.xml.phpunit in editor that you are comfortable with:
1. Specify database credentials that will be used for test suite in
**global/resources/default_setup/connection** node
2. Specify **base_url** for **secure** and **unsecure** requests in **default/web** node. It is
required for proper controller tests.
3. Run the unit tests first time for installing test database. It will take about 3 minutes.
$ phpunit UnitTests.php
4. If it shows that there was no tests found, it means that extension was successfully
installed. If it shows some errors than it means that your customizations has install
scripts that relay on your current database data so you should fix them.
Issue Tracker
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......@@ -76,7 +76,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
*
* @param string|array $name
* @param string|null $value
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function setCookie($name, $value)
{
......@@ -88,7 +88,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
* Sets more than one cookie
*
* @param array $cookies
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function setCookies(array $cookies)
{
......@@ -99,7 +99,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
/**
* Resets all cookies for the test request
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function resetCookies()
{
......@@ -110,7 +110,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
/**
* Resets query for the current request
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function resetQuery()
{
......@@ -121,7 +121,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
/**
* Resets $_POST superglobal for test request
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function resetPost()
{
......@@ -132,7 +132,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
/**
* Resets user defined request params for test request
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function resetParams()
{
......@@ -143,7 +143,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
/**
* Resets internal properties to its default values
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function resetInternalProperties()
{
......@@ -178,12 +178,24 @@ class EcomDev_PHPUnit_Controller_Request_Http
return $this;
}
/**
* Sets rawBody property to request
*
* @param string $rawBody
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function setRawBody($rawBody)
{
$this->_rawBody = $rawBody;
return $this;
}
/**
* Set custom http header
*
* @param string $name
* @param string $value
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function setHeader($name, $value)
{
......@@ -199,7 +211,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
* headers list is an associative array
*
* @param array $headers
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function setHeaders(array $headers)
{
......@@ -229,7 +241,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
/**
* Resets headers in test request
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function resetHeaders()
{
......@@ -255,7 +267,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
*
* @param string $name
* @param string $value
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function setServer($name, $value)
{
......@@ -272,7 +284,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
* Sets multiple values for $_SERVER superglobal in test request
*
* @param array $values
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function setServers(array $values)
{
......@@ -285,7 +297,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
/**
* Resets $_SERVER superglobal to previous state
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function resetServer()
{
......@@ -307,7 +319,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
* Sets request method for test request
*
* @param string $requestMethod
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function setMethod($requestMethod)
{
......@@ -320,7 +332,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
* accepts boolean flag for HTTPS
*
* @param boolean $flag
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function setIsSecure($flag = true)
{
......@@ -365,7 +377,7 @@ class EcomDev_PHPUnit_Controller_Request_Http
/**
* Resets all request data for test
*
* @return EcomDev_PHPUnit_Controller_Request_Http_Test
* @return EcomDev_PHPUnit_Controller_Request_Http
*/
public function reset()
{
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......@@ -131,6 +131,7 @@ class EcomDev_PHPUnit_Model_App extends Mage_Core_Model_App
* List of module names stored by class name
*
* @var array
* @deprecated since 0.3.0
*/
protected $_moduleNameByClassName = array();
......@@ -402,7 +403,7 @@ class EcomDev_PHPUnit_Model_App extends Mage_Core_Model_App
{
$className = (string)$this->getConfig()->getNode($configPath);
$reflection = EcomDev_Utils_Reflection::getRelflection($className);
$reflection = EcomDev_Utils_Reflection::getReflection($className);
if ($interface !== null && !$reflection->implementsInterface($interface)) {
throw new RuntimeException(
sprintf('Invalid class name defined in configuration path %s, because %s does not implement %s interface',
......@@ -443,31 +444,11 @@ class EcomDev_PHPUnit_Model_App extends Mage_Core_Model_App
* @param string|object $className
* @throws RuntimeException if module name was not found for the passed class name
* @return string
* @deprecated since 0.3.0
*/
public function getModuleNameByClassName($className)
{
if (is_object($className)) {
$className = get_class($className);
}
if (!isset($this->_moduleNameByClassName[$className])) {
// Try to find the module name by class name
$moduleName = false;
foreach ($this->getConfig()->getNode('modules')->children() as $module) {
if (strpos($className, $module->getName()) === 0) {
$moduleName = $module->getName();
break;
}
}
if (!$moduleName) {
throw new RuntimeException('Cannot to find the module name for class name: ' . $className);
}
$this->setModuleNameForClassName($className, $moduleName);
}
return $this->_moduleNameByClassName[$className];
return EcomDev_PHPUnit_Test_Case_Util::getModuleNameByClassName($className);
}
/**
......@@ -478,10 +459,11 @@ class EcomDev_PHPUnit_Model_App extends Mage_Core_Model_App
* @param string $className
* @param string $moduleName
* @return EcomDev_PHPUnit_Model_App
* @deprecated since 0.3.0
*/
public function setModuleNameForClassName($className, $moduleName)
{
$this->_moduleNameByClassName[$className] = $moduleName;
EcomDev_PHPUnit_Test_Case_Util::setModuleNameForClassName($className, $moduleName);
return $this;
}
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
<?php
// Loading Spyc yaml parser,
// becuase Symfony component is not working propertly with nested
require_once 'Spyc/spyc.php';
class EcomDev_PHPUnit_Model_Expectation
implements EcomDev_PHPUnit_Model_Expectation_Interface
{
......@@ -122,22 +118,27 @@ class EcomDev_PHPUnit_Model_Expectation
*
* @see EcomDev_PHPUnit_Model_Test_Loadable_Interface::loadByTestCase()
*/
public function loadByTestCase(EcomDev_PHPUnit_Test_Case $testCase)
public function loadByTestCase(PHPUnit_Framework_TestCase $testCase)
{
$expectations = $testCase->getAnnotationByName('loadExpectation');
$expectations = EcomDev_PHPUnit_Test_Case_Util::getAnnotationByNameFromClass(
get_class($testCase), 'loadExpectation', array('class', 'method'), $testCase->getName(false)
);
if (!$expectations) {
$expectations[] = null;
$expectations[] = $testCase->getName(false);
}
$expectationData = array();
foreach ($expectations as $expectation) {
if (empty($expectation)) {
$expectation = null;
$expectation = $testCase->getName(false);
}
$expectationFile = $testCase->getYamlFilePath('expectations', $expectation);
$expectationFile = EcomDev_PHPUnit_Test_Case_Util::getYamlLoader(get_class($testCase))
->resolveFilePath(
get_class($testCase), EcomDev_PHPUnit_Model_Yaml_Loader::TYPE_EXPECTATION, $expectation
);
if (!$expectationFile) {
$text = 'There was no expectation defined for current test case';
......@@ -148,7 +149,7 @@ class EcomDev_PHPUnit_Model_Expectation
}
$expectationData = array_merge_recursive(
$expectationData, Spyc::YAMLLoad($expectationFile)
$expectationData, EcomDev_PHPUnit_Test_Case_Util::getYamlLoader()->load($expectationFile)
);
}
......
......@@ -12,7 +12,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......@@ -40,6 +40,13 @@ interface EcomDev_PHPUnit_Model_Fixture_Interface extends EcomDev_PHPUnit_Model_
*/
public function setOptions(array $options);
/**
* Sets fixture options
*
* @return array
*/
public function getOptions();
/**
* Sets storage for fixtures
*
......@@ -117,4 +124,30 @@ interface EcomDev_PHPUnit_Model_Fixture_Interface extends EcomDev_PHPUnit_Model_
* @return EcomDev_PHPUnit_Model_Fixture_Interface
*/
public function loadForClass($className);
/**
* Sets fixture value
*
* @param string $key
* @param array[] $value
*
* @return EcomDev_PHPUnit_Model_Fixture_Interface
*/
public function setFixtureValue($key, $value);
/**
* Retrieves fixture value
*
* @param string $key
*
* @return array[]
*/
public function getFixtureValue($key);
/**
* Returns VFS wrapper instance
*
* @return EcomDev_PHPUnit_Model_Fixture_Vfs
*/
public function getVfs();
}
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnit_Model_Fixture_Processor_Cache implements EcomDev_PHPUnit_Model_Fixture_Processor_Interface
{
const STORAGE_KEY = 'cache_options';
/**
* Initializes cache options
*
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Cache
*/
public function initialize(EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$options = $fixture->getOptions();
if (isset($options['cache'])) {
$cacheOptions = array();
foreach ($options['cache'] as $annotation) {
list($action, $cacheType) = preg_split('/\s+/', trim($annotation));
$flag = ($action === 'off' ? 0 : 1);
if ($cacheType === 'all') {
foreach (Mage::app()->getCacheInstance()->getTypes() as $type) {
$cacheOptions[$type->getId()] = $flag;
}
} else {
$cacheOptions[$cacheType] = $flag;
}
}
$fixture->setFixtureValue('cache_options', $cacheOptions);
}
return $this;
}
/**
* Apply cache options from the fixture data
*
* @param array $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Cache
*/
public function apply(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$originalOptions = EcomDev_PHPUnit_Test_Case_Util::app()->getCacheOptions();
$fixture->setStorageData(self::STORAGE_KEY, $originalOptions);
$data += $originalOptions;
EcomDev_PHPUnit_Test_Case_Util::app()->setCacheOptions($data);
return $this;
}
/**
* Discard applied cache options
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Cache
*/
public function discard(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
EcomDev_PHPUnit_Test_Case_Util::app()->setCacheOptions(
$fixture->getStorageData(self::STORAGE_KEY)
);
return $this;
}
}
\ No newline at end of file
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnit_Model_Fixture_Processor_Config implements EcomDev_PHPUnit_Model_Fixture_Processor_Interface
{
/**
* Does nothing
*
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Config
*/
public function initialize(EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
return $this;
}
/**
* Apply cache options from the fixture data
*
* @param array $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Config
*/
public function apply(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$key === 'config_xml' ? $this->_applyConfigXml($data) : $this->_applyConfig($data);
return $this;
}
/**
* Applies fixture configuration values into Mage_Core_Model_Config
*
* @param array $configuration
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Config
* @throws InvalidArgumentException
*/
protected function _applyConfig($configuration)
{
if (!is_array($configuration)) {
throw new InvalidArgumentException('Configuration part should be an associative list');
}
Mage::getConfig()->loadScopeSnapshot();
foreach ($configuration as $path => $value) {
$this->_setConfigNodeValue($path, $value);
}
Mage::getConfig()->loadDb();
// Flush website and store configuration caches
foreach (Mage::app()->getWebsites(true) as $website) {
EcomDev_Utils_Reflection::setRestrictedPropertyValue(
$website, '_configCache', array()
);
}
foreach (Mage::app()->getStores(true) as $store) {
EcomDev_Utils_Reflection::setRestrictedPropertyValue(
$store, '_configCache', array()
);
}
return $this;
}
/**
* Applies raw xml data to config node
*
* @param array $configuration
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Config
* @throws InvalidArgumentException
*/
protected function _applyConfigXml($configuration)
{
if (!is_array($configuration)) {
throw new InvalidArgumentException('Configuration part should be an associative list');
}
foreach ($configuration as $path => $value) {
if (!is_string($value)) {
throw new InvalidArgumentException('Configuration value should be a valid xml string');
}
try {
$xmlElement = new Varien_Simplexml_Element($value);
} catch (Exception $e) {
throw new InvalidArgumentException('Configuration value should be a valid xml string', 0, $e);
}
$node = Mage::getConfig()->getNode($path);
if (!$node) {
throw new InvalidArgumentException('Configuration value should be a valid xml string');
}
$node->extend($xmlElement, true);
}
return $this;
}
/**
* Discard applied cache options
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Config
*/
public function discard(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$this->_restoreConfig();
return $this;
}
/**
* Restores config to a previous configuration scope
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Config
*/
protected function _restoreConfig()
{
Mage::getConfig()->loadScopeSnapshot();
Mage::getConfig()->loadDb();
return $this;
}
/**
* Sets configuration value
*
* @param string $path
* @param string $value
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Config
*/
protected function _setConfigNodeValue($path, $value)
{
if (($originalNode = Mage::getConfig()->getNode($path)) && $originalNode->getAttribute('backend_model')) {
$backendModel = $originalNode->getAttribute('backend_model');
$backend = Mage::getModel((string) $backendModel);
$backend->setPath($path)->setValue($value);
EcomDev_Utils_Reflection::invokeRestrictedMethod($backend, '_beforeSave');
$value = $backend->getValue();
}
Mage::getConfig()->setNode($path, $value);
return $this;
}
}
\ No newline at end of file
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnit_Model_Fixture_Processor_Eav
extends Mage_Core_Model_Abstract
implements EcomDev_PHPUnit_Model_Fixture_Processor_Interface
{
const STORAGE_KEY = 'entities';
// Configuration path for eav loaders
const XML_PATH_FIXTURE_EAV_LOADERS = 'phpunit/suite/fixture/eav';
// Default eav loader class node in loaders configuration
const DEFAULT_EAV_LOADER_NODE = 'default';
// Default eav loader class alias
const DEFAULT_EAV_LOADER_CLASS = 'ecomdev_phpunit/fixture_eav_default';
/**
* Initialize fixture resource model
*/
protected function _construct()
{
$this->_init('ecomdev_phpunit/fixture');
}
/**
* Does nothing
*
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Eav
*/
public function initialize(EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
return $this;
}
/**
* Retrieves eav loader for a particular entity type
*
* @param string $entityType
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Abstract
*/
protected function _getEavLoader($entityType)
{
$loaders = Mage::getConfig()->getNode(self::XML_PATH_FIXTURE_EAV_LOADERS);
if (isset($loaders->$entityType)) {
$classAlias = (string)$loaders->$entityType;
} elseif (isset($loaders->{self::DEFAULT_EAV_LOADER_NODE})) {
$classAlias = (string)$loaders->{self::DEFAULT_EAV_LOADER_NODE};
} else {
$classAlias = self::DEFAULT_EAV_LOADER_CLASS;
}
return Mage::getResourceSingleton($classAlias);
}
/**
* Apply eav records from fixture file
*
* @param array $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Eav
*/
public function apply(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$eavLoaders = array();
$this->getResource()->beginTransaction();
foreach ($data as $entityType => $values) {
$eavLoaders[] = $this->_getEavLoader($entityType)
->setFixture($fixture)
->setOptions($fixture->getOptions())
->loadEntity($entityType, $values);
}
$this->getResource()->commit();
foreach ($eavLoaders as $eavLoader){
$eavLoader->runRequiredIndexers();
}
$fixture->setStorageData(self::STORAGE_KEY, array_keys($data));
return $this;
}
/**
* Discard applied eav records
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Eav
*/
public function discard(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$ignoreCleanUp = array();
// Ignore cleaning of entities if shared fixture loaded something for them
if ($fixture->isScopeLocal()
&& $fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED)) {
$ignoreCleanUp = $fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED);
}
$this->getResource()->beginTransaction();
foreach (array_keys($data) as $entityType) {
if (in_array($entityType, $ignoreCleanUp)) {
continue;
}
$this->_getEavLoader($entityType)
->cleanEntity($entityType);
}
$this->getResource()->commit();
return $this;
}
}
\ No newline at end of file
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
interface EcomDev_PHPUnit_Model_Fixture_Processor_Interface
{
/**
* Applies data from fixture file
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Interface
*/
public function apply(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture);
/**
* Discards data from fixture file
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Interface
*/
public function discard(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture);
/**
* Initializes fixture processor before applying data
*
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Interface
*/
public function initialize(EcomDev_PHPUnit_Model_Fixture_Interface $fixture);
}
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Registry fixture, works great for resetting singletons and other single instances in Magento
*
* Can be used by specifying annotations:
* @singleton catalog/product_type
* @resource catalog/product
* @helper catalog
*
* or by specifying it in Yaml file:
*
* registry:
* singleton:
* - catalog/product_type
* resource:
* - catalog/product
* helper
* - catalog
* - core/url
*
*/
class EcomDev_PHPUnit_Model_Fixture_Processor_Registry implements EcomDev_PHPUnit_Model_Fixture_Processor_Interface
{
const STORAGE_KEY = 'registry';
/**
* Clears singletons before tests
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Registry
* @throws RuntimeException
*/
public function apply(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$typeToKey = array(
'singleton' => '_singleton/',
'resource' => '_resource_singleton/',
'helper' => '_helper/'
);
if ($fixture->getStorageData(self::STORAGE_KEY) !== null) {
throw new RuntimeException('Registry data was not cleared after previous test');
}
$oldRegistry = array();
foreach ($data as $type => $keys) {
if (!isset($typeToKey[$type])) {
continue;
}
foreach ($keys as $key) {
// Preserve old registry value
$oldRegistry[$typeToKey[$type] . $key] = Mage::registry($typeToKey[$type] . $key);
// Set new value to registry
EcomDev_PHPUnit_Test_Case_Util::app()->replaceRegistry($typeToKey[$type] . $key, null);
}
}
$fixture->setStorageData(self::STORAGE_KEY, $oldRegistry);
return $this;
}
/**
* Sets back old singletons after tests
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Interface
*/
public function discard(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
if ($fixture->getStorageData(self::STORAGE_KEY) === null) {
return $this;
}
$oldRegistry = $fixture->getStorageData(self::STORAGE_KEY);
foreach ($oldRegistry as $key => $value) {
// Set old value to registry
EcomDev_PHPUnit_Test_Case_Util::app()->replaceRegistry($key, $value);
}
$fixture->setStorageData(self::STORAGE_KEY, null);
return $this;
}
/**
* Initializes fixture processor before applying data
*
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Registry
*/
public function initialize(EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$options = $fixture->getOptions();
$registry = array();
foreach (array('singleton', 'resource', 'helper') as $type) {
if (!isset($options[$type])) {
continue;
}
foreach ($options[$type] as $name) {
$registry[$type][] = $name;
}
}
if ($registry) {
$fixture->setFixtureValue('registry', $registry);
}
return $this;
}
}
\ No newline at end of file
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnit_Model_Fixture_Processor_Scope
implements EcomDev_PHPUnit_Model_Fixture_Processor_Interface
{
const STORAGE_KEY = 'scope';
/**
* Model aliases by type of scope
*
* @var array
*/
protected $modelByType = array(
'store' => 'core/store',
'group' => 'core/store_group',
'website' => 'core/website'
);
/**
* Does nothing
*
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Scope
*/
public function initialize(EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
return $this;
}
/**
* Handle scope row data
*
* @param string $type
* @param array $row
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
* @return boolean|Mage_Core_Model_Abstract
*/
protected function _handleScopeRow($type, $row, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$previousScope = array();
if ($fixture->isScopeLocal()
&& $fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED) !== null) {
$previousScope = $fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED);
}
if (isset($previousScope[$type][$row[$type . '_id']])) {
return false;
}
$scopeModel = Mage::getModel($this->modelByType[$type]);
$scopeModel->setData($row);
// Change property for saving new objects with specified ids
EcomDev_Utils_Reflection::setRestrictedPropertyValues(
$scopeModel->getResource(),
array(
'_isPkAutoIncrement' => false
)
);
try {
$scopeModel->isObjectNew(true);
$scopeModel->save();
} catch (Exception $e) {
Mage::logException($e);
// Skip duplicated key violation, since it might be a problem
// of previous run with fatal error
// Revert changed property
EcomDev_Utils_Reflection::setRestrictedPropertyValues(
$scopeModel->getResource(),
array(
'_isPkAutoIncrement' => true
)
);
// Load to make possible deletion
$scopeModel->load($row[$type . '_id']);
}
// Revert changed property
EcomDev_Utils_Reflection::setRestrictedPropertyValues(
$scopeModel->getResource(),
array(
'_isPkAutoIncrement' => true
)
);
return $scopeModel;
}
/**
* Validate scope data
*
* @param array $types
* @throws RuntimeException
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Scope
*/
protected function _validateScope($types)
{
foreach ($types as $type => $rows) {
if (!isset($this->modelByType[$type])) {
throw new RuntimeException(sprintf('Unknown "%s" scope type specified', $type));
}
foreach ($rows as $rowNumber => $row) {
if (!isset($row[$type . '_id'])) {
throw new RuntimeException(sprintf('Missing primary key for "%s" scope entity at #%d row', $type, $rowNumber + 1));
}
}
}
return $this;
}
/**
* Apply scoped data (store, website, group)
*
* @param array $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @throws RuntimeException
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Scope
*/
public function apply(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
EcomDev_PHPUnit_Test_Case_Util::app()->disableEvents();
// Validate received fixture data
$this->_validateScope($data);
if ($fixture->getStorageData(self::STORAGE_KEY) !== null) {
throw new RuntimeException('Scope data was not cleared after previous test');
}
$scopeModels = array();
foreach ($data as $type => $rows) {
foreach ($rows as $row) {
$model = $this->_handleScopeRow($type, $row, $fixture);
if ($model) {
$scopeModels[$type][$model->getId()] = $model;
}
}
}
$fixture->setStorageData(self::STORAGE_KEY, $scopeModels);
EcomDev_PHPUnit_Test_Case_Util::app()->enableEvents();
EcomDev_PHPUnit_Test_Case_Util::app()->reinitStores();
return $this;
}
/**
* Discard applied scope models
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Scope
*/
public function discard(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
if ($fixture->getStorageData(self::STORAGE_KEY) === null) {
return $this;
}
EcomDev_PHPUnit_Test_Case_Util::app()->disableEvents();
$scope = array_reverse($fixture->getStorageData(self::STORAGE_KEY));
foreach ($scope as $models) {
foreach ($models as $model) {
$model->delete();
}
}
$fixture->setStorageData(self::STORAGE_KEY, null);
EcomDev_PHPUnit_Test_Case_Util::app()->getCache()->clean(
Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
array(
Mage_Core_Model_Store::CACHE_TAG,
Mage_Core_Model_Store_Group::CACHE_TAG,
Mage_Core_Model_Website::CACHE_TAG
)
);
EcomDev_PHPUnit_Test_Case_Util::app()->enableEvents();
EcomDev_PHPUnit_Test_Case_Util::app()->reinitStores();
return $this;
}
}
\ No newline at end of file
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnit_Model_Fixture_Processor_Tables
extends Mage_Core_Model_Abstract
implements EcomDev_PHPUnit_Model_Fixture_Processor_Interface
{
const STORAGE_KEY = 'tables';
/**
* Initialize fixture resource model
*/
protected function _construct()
{
$this->_init('ecomdev_phpunit/fixture');
}
/**
* Does nothing
*
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Tables
*/
public function initialize(EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
return $this;
}
/**
* Apply tables records from fixture file
*
* @param array $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Tables
*/
public function apply(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$ignoreCleanUp = array();
// Ignore cleaning of tables if shared fixture loaded something
if ($fixture->isScopeLocal()
&& $fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED)) {
$ignoreCleanUp = array_keys($fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED));
}
$this->getResource()->beginTransaction();
foreach ($data as $tableEntity => $tableData) {
if (!in_array($tableEntity, $ignoreCleanUp)) {
$this->getResource()->cleanTable($tableEntity);
}
if (!empty($tableData)) {
$this->getResource()->loadTableData($tableEntity, $tableData);
}
}
$this->getResource()->commit();
$fixture->setStorageData(self::STORAGE_KEY, $data);
return $this;
}
/**
* Discard applied table records
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Tables
*/
public function discard(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$restoreTableData = array();
// Data for tables used in shared fixture
if ($fixture->isScopeLocal()
&& $fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED)) {
$restoreTableData = $fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED);
}
$this->getResource()->beginTransaction();
foreach (array_keys($data) as $tableEntity) {
$this->getResource()->cleanTable($tableEntity);
if (isset($restoreTableData[$tableEntity])) {
$this->getResource()->loadTableData($tableEntity, $restoreTableData[$tableEntity]);
}
}
$this->getResource()->commit();
$fixture->setStorageData(self::STORAGE_KEY, null);
return $this;
}
}
\ No newline at end of file
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnit_Model_Fixture_Processor_Vfs implements EcomDev_PHPUnit_Model_Fixture_Processor_Interface
{
const STORAGE_KEY = 'vfs';
/**
* Does nothing
*
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Cache
*/
public function initialize(EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
return $this;
}
/**
* Apply virtual file system structure
*
* @param array $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Cache
*/
public function apply(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
if ($fixture->isScopeLocal()
&& ($parentData = $fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED))) {
$data = array_merge_recursive($parentData, $data);
}
$fixture->getVfs()->apply($data);
$fixture->setStorageData(self::STORAGE_KEY, $data);
return $this;
}
/**
* Discard applied virtual file system structure
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Cache
*/
public function discard(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$fixture->getVfs()->discard();
$fixture->setStorageData(self::STORAGE_KEY, null);
return $this;
}
}
\ No newline at end of file
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
use org\bovigo\vfs\vfsStream as Stream;
use org\bovigo\vfs\vfsStreamDirectory as StreamDirectory;
use org\bovigo\vfs\vfsStreamWrapper as StreamWrapper;
use org\bovigo\vfs\visitor\vfsStreamStructureVisitor as StreamVisitor;
/**
* VFS library wrapper, to have simple api for manipulation with virtual fs
*
*
*/
class EcomDev_PHPUnit_Model_Fixture_Vfs
{
/**
* Current root directory stack of the VFS before apply process were started
*
* @var StreamDirectory[]
*/
protected $_currentRoot = array();
/**
* Initializes VFS stream wrapper/
*/
public function __construct()
{
Stream::setup();
}
/**
* Applies VFS directory structure
*
* @param $data
* @param bool $cloneCurrent
*
* @return EcomDev_PHPUnit_Model_Fixture_Vfs
*/
public function apply($data)
{
$this->_currentRoot[] = StreamWrapper::getRoot();
Stream::create($data);
return $this;
}
/**
* Discards VFS file system changes
*
* @return EcomDev_PHPUnit_Model_Fixture_Vfs
*/
public function discard()
{
if ($this->_currentRoot) {
StreamWrapper::setRoot(array_pop($this->_currentRoot));
}
return $this;
}
/**
* Dump current files structure
*
* @return array
*/
public function dump()
{
$currentRoot = StreamWrapper::getRoot();
if (!$currentRoot) {
return array();
}
$visitor = new StreamVisitor();
$visitor->visit($currentRoot);
return $visitor->getStructure();
}
/**
* Returns stream wrapper url for operation
* via built-in fs functions
*
* @param string $path
* @return string
*/
public function url($path)
{
return Stream::url($path);
}
}
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
* @author Steve Rice <srice@endertech.com>
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
* @author Steve Rice <srice@endertech.com>
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
* @author Steve Rice <srice@endertech.com>
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......@@ -31,15 +31,45 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Abstract
protected $_requiredIndexers = array();
/**
* Retrieve required indexers for re-building
* Original list of indexers required to build
*
* @var array
*/
protected $_originalIndexers = array();
/**
* Retrieve required indexers for re-building
*
* @return array
*/
public function getRequiredIndexers()
{
return $this->_requiredIndexers;
}
/**
* Run required indexers and reset to original required indexers
*
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Abstract
*/
public function runRequiredIndexers()
{
if (empty($this->_options['doNotIndexAll'])) {
$indexer = Mage::getSingleton('index/indexer');
foreach ($this->getRequiredIndexers() as $indexerCode) {
if (empty($this->_options['doNotIndex'])
|| !in_array($indexerCode, $this->_options['doNotIndex'])) {
$indexer->getProcessByCode($indexerCode)
->reindexAll();
}
}
}
// Restoring original required indexers for making tests isolated
$this->_requiredIndexers = $this->_originalIndexers;
return $this;
}
/**
* Add indexer by specific code to required indexers list
*
......@@ -78,7 +108,7 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Abstract
*/
public function loadEntity($entityType, $values)
{
$originalRequiredIndexers = $this->_requiredIndexers;
$this->_originalIndexers = $this->_requiredIndexers;
if (!empty($this->_options['addRequiredIndex'])) {
foreach ($this->_options['addRequiredIndex'] as $data) {
if (preg_match('/^([a-z0-9_\\-])+\\s+([a-z0-9_\\-])\s*$/i', $data, $match)
......@@ -182,19 +212,6 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Abstract
$this->_customEntityAction($entity, $entityTypeModel);
}
if (empty($this->_options['doNotIndexAll'])) {
$indexer = Mage::getSingleton('index/indexer');
foreach ($this->getRequiredIndexers() as $indexerCode) {
if (empty($this->_options['doNotIndex'])
|| !in_array($indexerCode, $this->_options['doNotIndex'])) {
$indexer->getProcessByCode($indexerCode)
->reindexAll();
}
}
}
// Restoring original required indexers for making tests isolated
$this->_requiredIndexers = $originalRequiredIndexers;
return $this;
}
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......@@ -25,10 +25,10 @@ interface EcomDev_PHPUnit_Model_Test_Loadable_Interface
/**
* Loads external data by test case instance
*
* @param EcomDev_PHPUnit_Test_Case $testCase
* @param PHPUnit_Framework_TestCase $testCase
* @return EcomDev_PHPUnit_Model_Test_Loadable_Interface
*/
public function loadByTestCase(EcomDev_PHPUnit_Test_Case $testCase);
public function loadByTestCase(PHPUnit_Framework_TestCase $testCase);
/**
* Applies external data
......
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
// Loading Spyc yaml parser,
// because Symfony component is not working properly with nested structures
require_once 'Spyc/spyc.php';
class EcomDev_PHPUnit_Model_Yaml_Loader
{
const XML_PATH_YAML_FILE_LOADERS = 'phpunit/suite/yaml/loaders';
const TYPE_FIXTURE = 'fixtures';
const TYPE_PROVIDER = 'providers';
const TYPE_EXPECTATION = 'expectations';
/**
* YAML file loaders
*
* @var EcomDev_PHPUnit_Model_Yaml_Loader_Interface[]
*/
protected $_loaders = array();
/**
* Returns arrays of loaders
*
* @return EcomDev_PHPUnit_Model_Yaml_Loader_Interface[]
*/
public function getLoaders()
{
if (empty($this->_loaders)) {
$this->_initLoaders();
}
return $this->_loaders;
}
/**
* Adds a loader to list of loders
*
* @param EcomDev_PHPUnit_Model_Yaml_Loader_Interface $loader
* @return EcomDev_PHPUnit_Model_Yaml_Loader
*/
public function addLoader(EcomDev_PHPUnit_Model_Yaml_Loader_Interface $loader)
{
$this->_loaders[] = $loader;
return $this;
}
/**
* Initializes current loaders from configuration
*
* @return EcomDev_PHPUnit_Model_Yaml_Loader
*/
protected function _initLoaders()
{
if ($loadersNode = Mage::getConfig()->getNode(self::XML_PATH_YAML_FILE_LOADERS)) {
foreach ($loadersNode->children() as $loader) {
$loaderInstance = Mage::getSingleton((string)$loader);
$this->addLoader($loaderInstance);
}
}
return $this;
}
/**
* Finds YAML file or returns false
*
* It invokes all added loaders for loading a yaml file
*
* @param string $className
* @param string $type
* @param string $name
* @return string|bool
*/
public function resolveFilePath($className, $type, $name)
{
$filePath = false;
foreach ($this->getLoaders() as $loader) {
// Break load file resolve path on first found file
if ($filePath = $loader->resolveFilePath($name, $className, $type)) {
break;
}
}
return $filePath;
}
/**
* Loads YAML file content
*
* @param string $filePath
* @return array
*/
public function load($filePath)
{
return Spyc::YAMLLoad($filePath);
}
}
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
abstract class EcomDev_PHPUnit_Model_Yaml_Loader_Abstract implements EcomDev_PHPUnit_Model_Yaml_Loader_Interface
{
const DATA_DIR = '_data';
protected $_typeMap = array(
'fixtures' => 'fx',
'providers' => 'dp',
'expectations' => 'ex'
);
/**
* Resolves YAML file path based on its filename,
* if file is not found, it should return false
*
* @param string $fileName name of the file
* @param string $relatedClassName class name from which load of yaml file is invoked
* @param string $type type of Yaml file (provider, fixture, expectation)
* @return string|bool
*/
public function resolveFilePath($fileName, $relatedClassName, $type)
{
if (strrpos($fileName, '.yaml') !== strlen($fileName) - 5) {
$fileName .= '.yaml';
}
$filePath = $this->_getFilePath($fileName, $relatedClassName, $type);
if ($filePath && file_exists($filePath)) {
return $filePath;
}
return false;
}
/**
* Returns processed file path
*
* @param string $fileName
* @param string $relatedClassName
* @param string $type
* @return string
*/
abstract protected function _getFilePath($fileName, $relatedClassName, $type);
/**
* Looks in path for possible existent fixture
*
* @param string|array $path
* @param string $fileName
* @param string $type
* @return bool|string
*/
protected function _checkFilePath($path, $fileName, $type)
{
if (is_array($path)) {
foreach ($path as $value) {
if ($filePath = $this->_checkFilePath($value, $fileName, $type)) {
return $filePath;
}
}
return false;
}
if (isset($this->_typeMap[$type])
&& file_exists($filePath = $path . DS . self::DATA_DIR . DS . $this->_typeMap[$type] . '-' . $fileName)) {
return $filePath;
}
if (file_exists($filePath = $path . DS . $type . DS . $fileName)) {
return $filePath;
}
return false;
}
}
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnit_Model_Yaml_Loader_Default extends EcomDev_PHPUnit_Model_Yaml_Loader_Abstract
{
/**
* Returns processed file path
*
* @param string $fileName
* @param string $relatedClassName
* @param string $type
* @return string|bool
*/
protected function _getFilePath($fileName, $relatedClassName, $type)
{
$reflection = EcomDev_Utils_Reflection::getReflection($relatedClassName);
$fileObject = new SplFileInfo($reflection->getFileName());
return $this->_checkFilePath(array(
$fileObject->getPath(),
$fileObject->getPath() . DS . $fileObject->getBasename('.php')
), $fileName, $type);
}
}
\ No newline at end of file
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnit_Model_Yaml_Loader_Global extends EcomDev_PHPUnit_Model_Yaml_Loader_Abstract
{
/**
* Returns processed file path
*
* @param string $fileName
* @param string $relatedClassName
* @param string $type
* @return string|bool
*/
protected function _getFilePath($fileName, $relatedClassName, $type)
{
$reflection = EcomDev_Utils_Reflection::getReflection($relatedClassName);
$fileObject = new SplFileInfo($reflection->getFileName());
$basePath = $fileObject->getPath();
// While base path is within a base directory of Magento installment
while (strpos($basePath, Mage::getBaseDir()) && !is_dir($basePath . DS . $type)) {
$basePath = dirname($basePath);
}
if (basename($basePath)) {
return $basePath . DS . $type . DS .$fileName;
}
return false;
}
}
\ No newline at end of file
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
interface EcomDev_PHPUnit_Model_Yaml_Loader_Interface
{
/**
* Resolves YAML file path based on its filename,
* if file is not found, it should return false
*
* @param string $fileName name of the file
* @param string $relatedClassName class name from which load of yaml file is invoked
* @param string $type type of Yaml file (provider, fixture, expectation)
* @return string|bool
*/
public function resolveFilePath($fileName, $relatedClassName, $type);
}
\ No newline at end of file
<?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
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
* @author Colin Mollenhour <http://colin.mollenhour.com>
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnit_Model_Yaml_Loader_Module extends EcomDev_PHPUnit_Model_Yaml_Loader_Abstract
{
/**
* Returns processed file path based on module test directory
*
* @param string $fileName
* @param string $relatedClassName
* @param string $type
* @return string|bool
*/
protected function _getFilePath($fileName, $relatedClassName, $type)
{
$moduleName = false;
if (preg_match('#^~(?<module>[^/]*)/(?<fileName>.*)$#', $fileName, $matches)) {
$fileName = $matches['fileName'];
if(!empty($matches['module'])) {
$moduleName = $matches['module'];
}
}
if (!$moduleName) {
try {
$moduleName = EcomDev_PHPUnit_Test_Case_Util::getModuleNameByClassName($relatedClassName);
} catch (RuntimeException $e) {
return false;
}
}
$basePath = array();
if ($prefixPosition = strpos($relatedClassName, $moduleName . '_Test_')) {
$testType = substr($relatedClassName, $prefixPosition, strpos($relatedClassName, '_', $prefixPosition));
$basePath[] = Mage::getModuleDir('', $moduleName) . DS . 'Test' . DS . $testType;
}
$basePath[] = Mage::getModuleDir('', $moduleName) . DS . 'Test';
return $this->_checkFilePath($basePath, $fileName, $type);
}
}
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......@@ -149,7 +149,7 @@ abstract class EcomDev_PHPUnit_Test_Case_Controller extends EcomDev_PHPUnit_Test
*
* @param string $type
* @param string|null $expectedValue
* @return string
* @return EcomDev_PHPUnit_Constraint_Controller_Request
*/
public static function request($type, $expectedValue = null)
{
......@@ -162,7 +162,7 @@ abstract class EcomDev_PHPUnit_Test_Case_Controller extends EcomDev_PHPUnit_Test
* @param string $type
* @param string $headerName
* @param PHPUnit_Framework_Constraint|null $constraint
* @return string
* @return EcomDev_PHPUnit_Constraint_Controller_Response_Header
*/
public static function responseHeader($headerName, $type, PHPUnit_Framework_Constraint $constraint = null)
{
......@@ -173,7 +173,7 @@ abstract class EcomDev_PHPUnit_Test_Case_Controller extends EcomDev_PHPUnit_Test
* Controller response body constraint creation
*
* @param PHPUnit_Framework_Constraint $constraint
* @return string
* @return EcomDev_PHPUnit_Constraint_Controller_Response_Body
*/
public static function responseBody(PHPUnit_Framework_Constraint $constraint)
{
......@@ -523,12 +523,12 @@ abstract class EcomDev_PHPUnit_Test_Case_Controller extends EcomDev_PHPUnit_Test
* Assert that response header is equal to expected value
*
* @param string $headerName
* @param mixed $expectedValue
* @param mixed $expectedValue
* @param string $message
* @param float $delta
* @param integer $maxDepth
* @param boolean $canonicalize
* @param boolean $ignoreCase
* @param int $delta
* @param int $maxDepth
* @param bool $canonicalize
* @param bool $ignoreCase
*/
public static function assertResponseHeaderEquals($headerName, $expectedValue, $message = '',
$delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
......@@ -544,12 +544,12 @@ abstract class EcomDev_PHPUnit_Test_Case_Controller extends EcomDev_PHPUnit_Test
* Assert that response header is not equal to expected value
*
* @param string $headerName
* @param mixed $expectedValue
* @param mixed $expectedValue
* @param string $message
* @param float $delta
* @param integer $maxDepth
* @param boolean $canonicalize
* @param boolean $ignoreCase
* @param int $delta
* @param int $maxDepth
* @param bool $canonicalize
* @param bool $ignoreCase
*/
public static function assertResponseHeaderNotEquals($headerName, $expectedValue, $message = '',
$delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
......@@ -633,7 +633,6 @@ abstract class EcomDev_PHPUnit_Test_Case_Controller extends EcomDev_PHPUnit_Test
* @param string $headerName
* @param string $pcrePattern
* @param string $message
* @param boolean $ignoreCase
*/
public static function assertResponseHeaderRegExp($headerName, $pcrePattern, $message = '')
{
......@@ -650,7 +649,6 @@ abstract class EcomDev_PHPUnit_Test_Case_Controller extends EcomDev_PHPUnit_Test
* @param string $headerName
* @param string $pcrePattern
* @param string $message
* @param boolean $ignoreCase
*/
public static function assertResponseHeaderNotRegExp($headerName, $pcrePattern, $message = '')
{
......@@ -871,7 +869,7 @@ abstract class EcomDev_PHPUnit_Test_Case_Controller extends EcomDev_PHPUnit_Test
$params['_store'] = EcomDev_PHPUnit_Model_App::ADMIN_STORE_CODE;
}
if ($params['_store'] === EcomDev_PHPUnit_Model_App::ADMIN_STORE_CODE) {
if (isset($params['_store']) && $params['_store'] === EcomDev_PHPUnit_Model_App::ADMIN_STORE_CODE) {
$urlModel = Mage::getModel('adminhtml/url');
} else {
$urlModel = Mage::getModel('core/url');
......
This diff is collapsed.
<?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
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
{
const XML_PATH_UNIT_TEST_APP = 'phpunit/suite/app/class';
/**
* First level test suite that is used
* for running all the tests
*
* @var PHPUnit_Framework_TestSuite
*/
protected $firstLevelTestSuite = null;
/**
* Returns app reflection instance
*
* @return ReflectionClass|ReflectionObject
*/
protected function getAppReflection()
{
$appClass = (string) Mage::getConfig()->getNode(self::XML_PATH_UNIT_TEST_APP);
$reflectionClass = EcomDev_Utils_Reflection::getReflection($appClass);
return $reflectionClass;
}
/**
* A test suite started.
*
* @param PHPUnit_Framework_TestSuite $suite
*/
public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
{
if ($this->firstLevelTestSuite === null) {
// Apply app substitution for tests
if ($this->getAppReflection()->hasMethod('applyTestScope')) {
$this->getAppReflection()->getMethod('applyTestScope')->invoke(null);
}
$this->firstLevelTestSuite = $suite;
}
if (EcomDev_Utils_Reflection::getRestrictedPropertyValue($suite, 'testCase')) {
EcomDev_PHPUnit_Test_Case_Util::getFixture($suite->getName())
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED)
->loadForClass($suite->getName());
$annotations = PHPUnit_Util_Test::parseTestMethodAnnotations(
$suite->getName()
);
EcomDev_PHPUnit_Test_Case_Util::getFixture($suite->getName())
->setOptions($annotations['class'])
->apply();
}
}
/**
* A test suite ended.
*
* @param PHPUnit_Framework_TestSuite $suite
*/
public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
{
if (EcomDev_Utils_Reflection::getRestrictedPropertyValue($suite, 'testCase')) {
EcomDev_PHPUnit_Test_Case_Util::getFixture($suite->getName())
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED)
->discard();
}
if ($this->firstLevelTestSuite === $suite) {
$this->firstLevelTestSuite = null;
// Discard test scope app
if ($this->getAppReflection()->hasMethod('discardTestScope')) {
$this->getAppReflection()->getMethod('discardTestScope')->invoke(null);
}
}
}
/**
* A test started.
*
* @param PHPUnit_Framework_Test $test
*/
public function startTest(PHPUnit_Framework_Test $test)
{
if ($test instanceof PHPUnit_Framework_TestCase) {
EcomDev_PHPUnit_Test_Case_Util::getFixture(get_class($test))
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_LOCAL)
->loadByTestCase($test);
$annotations = $test->getAnnotations();
EcomDev_PHPUnit_Test_Case_Util::getFixture()
->setOptions($annotations['method'])
->apply();
EcomDev_PHPUnit_Test_Case_Util::setUp();
}
}
/**
* A test ended.
*
* @param PHPUnit_Framework_Test $test
* @param float $time
*/
public function endTest(PHPUnit_Framework_Test $test, $time)
{
if ($test instanceof PHPUnit_Framework_TestCase) {
EcomDev_PHPUnit_Test_Case_Util::getFixture(get_class($test))
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_LOCAL)
->discard(); // Clear applied fixture
if (EcomDev_PHPUnit_Test_Case_Util::getExpectation(get_class($test))->isLoaded()) {
EcomDev_PHPUnit_Test_Case_Util::getExpectation(get_class($test))->discard();
}
EcomDev_PHPUnit_Test_Case_Util::tearDown();
}
}
/**
* An error occurred.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
{
// No action is required
}
/**
* A failure occurred.
*
* @param PHPUnit_Framework_Test $test
* @param PHPUnit_Framework_AssertionFailedError $e
* @param float $time
*/
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
{
// No action is required
}
/**
* Incomplete test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
// No action is required
}
/**
* Skipped test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
*/
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
// No action is required
}
}
\ No newline at end of file
......@@ -11,12 +11,11 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Test suite for Magento
*
......@@ -34,36 +33,6 @@ class EcomDev_PHPUnit_Test_Suite extends PHPUnit_Framework_TestSuite
const CACHE_TAG = 'ECOMDEV_PHPUNIT';
const CACHE_TYPE = 'ecomdev_phpunit';
/**
* Setting up test scope for Magento
* (non-PHPdoc)
* @see PHPUnit_Framework_TestSuite::setUp()
*/
protected function setUp()
{
$appClass = (string) Mage::getConfig()->getNode(self::XML_PATH_UNIT_TEST_APP);
$reflectionClass = EcomDev_Utils_Reflection::getRelflection($appClass);
if ($reflectionClass->hasMethod('applyTestScope')) {
$reflectionClass->getMethod('applyTestScope')->invoke(null);
}
}
/**
* Returning Magento to the state before suite was run
* (non-PHPdoc)
* @see PHPUnit_Framework_TestSuite::tearDown()
*/
protected function tearDown()
{
$appClass = (string) Mage::getConfig()->getNode(self::XML_PATH_UNIT_TEST_APP);
$reflectionClass = EcomDev_Utils_Reflection::getRelflection($appClass);
if ($reflectionClass->hasMethod('discardTestScope')) {
$reflectionClass->getMethod('discardTestScope')->invoke(null);
}
}
/**
* This method loads all available test suites for PHPUnit
*
......@@ -73,7 +42,7 @@ class EcomDev_PHPUnit_Test_Suite extends PHPUnit_Framework_TestSuite
{
$groups = Mage::getConfig()->getNode(self::XML_PATH_UNIT_TEST_GROUPS);
$modules = Mage::getConfig()->getNode(self::XML_PATH_UNIT_TEST_MODULES);
$testSuiteClass = EcomDev_Utils_Reflection::getRelflection((string) Mage::getConfig()->getNode(self::XML_PATH_UNIT_TEST_SUITE));
$testSuiteClass = EcomDev_Utils_Reflection::getReflection((string) Mage::getConfig()->getNode(self::XML_PATH_UNIT_TEST_SUITE));
if (!$testSuiteClass->isSubclassOf('EcomDev_PHPUnit_Test_Suite_Group')) {
new RuntimeException('Test Suite class should be extended from EcomDev_PHPUnit_Test_Suite_Group');
......@@ -153,10 +122,10 @@ class EcomDev_PHPUnit_Test_Suite extends PHPUnit_Framework_TestSuite
$className = uc_words(ltrim($classPath, DS), '_', DS);
// Add unit test case only
// if it is a valid class extended from EcomDev_PHPUnit_Test_Case
// if it is a valid class extended from PHPUnit_Framework_TestCase
if (class_exists($className, true)) {
$reflectionClass = EcomDev_Utils_Reflection::getRelflection($className);
if (!$reflectionClass->isSubclassOf('EcomDev_PHPUnit_Test_Case')
$reflectionClass = EcomDev_Utils_Reflection::getReflection($className);
if (!$reflectionClass->isSubclassOf('PHPUnit_Framework_TestCase')
|| $reflectionClass->isAbstract()) {
continue;
}
......
......@@ -11,7 +11,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......@@ -42,7 +42,7 @@ class EcomDev_PHPUnit_Test_Suite_Group extends PHPUnit_Framework_TestSuite
public function __construct($theClass = '', $groups = array())
{
if (!$theClass instanceof ReflectionClass) {
$theClass = EcomDev_Utils_Reflection::getRelflection($theClass);
$theClass = EcomDev_Utils_Reflection::getReflection($theClass);
}
// Check annotations for test case name
......
<?php
require 'app/Mage.php';
if (version_compare(PHP_VERSION, '5.3', '<')) {
exit('Magento Unit Tests can be runned only on PHP version over 5.3');
echo 'Magento Unit Tests can run only on PHP version higher then 5.3';
exit(1);
}
$_baseDir = getcwd();
// Include Mage file by detecting app root
require_once $_baseDir . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
if (!Mage::isInstalled()) {
exit('Magento Unit Tests can be runned only on installed version');
echo 'Magento Unit Tests can run only on installed version';
exit(1);
}
/* Replace server variables for proper file naming */
$_SERVER['SCRIPT_NAME'] = dirname(__FILE__) . DS . 'index.php';
$_SERVER['SCRIPT_FILENAME'] = dirname(__FILE__) . DS . 'index.php';
$_SERVER['SCRIPT_NAME'] = $_baseDir . DS . 'index.php';
$_SERVER['SCRIPT_FILENAME'] = $_baseDir . DS . 'index.php';
Mage::app('admin');
Mage::getConfig()->init();
class UnitTests extends EcomDev_PHPUnit_Test_Suite
{
}
......@@ -12,7 +12,7 @@
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
......@@ -45,6 +45,14 @@
</global>
<phpunit>
<suite>
<yaml>
<model>ecomdev_phpunit/yaml_loader</model>
<loaders>
<default>ecomdev_phpunit/yaml_loader_default</default>
<module>ecomdev_phpunit/yaml_loader_module</module>
<global>ecomdev_phpunit/yaml_loader_global</global>
</loaders>
</yaml>
<!-- The names of directories inside Test for recognizion of tests per group -->
<groups>
<models>Model</models>
......@@ -70,6 +78,16 @@
<fixture>
<!-- Default model for loading of fixtures -->
<model>ecomdev_phpunit/fixture</model>
<processors>
<registry>ecomdev_phpunit/fixture_processor_registry</registry>
<scope>ecomdev_phpunit/fixture_processor_scope</scope>
<config>ecomdev_phpunit/fixture_processor_config</config>
<config_xml>ecomdev_phpunit/fixture_processor_config</config_xml>
<cache_options>ecomdev_phpunit/fixture_processor_cache</cache_options>
<eav>ecomdev_phpunit/fixture_processor_eav</eav>
<tables>ecomdev_phpunit/fixture_processor_tables</tables>
<vfs>ecomdev_phpunit/fixture_processor_vfs</vfs>
</processors>
<attribute>
<!-- Fixture loaders for EAV attributes
Default can be used but does not support extra attribute table configuration -->
......
<?php
class EcomDev_PHPUnitTest_Test_Lib_Constraint_Abstract extends PHPUnit_Framework_TestCase
{
/**
* Test compare values functionality for constraint
*
* @param mixed $expectedValue
* @param mixed $actualValue
* @param bool $expectedResult
*
* @dataProvider dataProviderForCompareValues
*/
public function testCompareValues($expectedValue, $actualValue, $expectedResult)
{
/**
* @var $constraint EcomDev_PHPUnit_Constraint_Abstract
*/
$constraint = $this->getMockForAbstractClass('EcomDev_PHPUnit_Constraint_Abstract', array(), '', false);
$this->assertSame(
$expectedResult,
$constraint->compareValues($expectedValue, $actualValue)
);
if (!$expectedResult) {
$this->assertAttributeInstanceOf('PHPUnit_Framework_ComparisonFailure', '_comparisonFailure', $constraint);
}
}
/**
* Data provider for checking compare values functionality
*
* @return array
*/
public function dataProviderForCompareValues()
{
return array(
array(
array('value1', 'value2', 'value3'),
array('value1', 'value2', 'value3'),
true
),
array(
array('value1', 'value2', 'value3'),
array('value1', 'value1', 'value3'),
false
),
array(
array('value1', 0, 'value3'),
array('value1', 'value1', 'value3'),
false
),
array(
'1',
1,
true
),
array(
'0',
0,
true
),
array(
'1',
0,
false
),
array(
'',
0,
false
)
);
}
}
\ No newline at end of file
<?php
class EcomDev_PHPUnitTest_Test_Lib_Constraint_Config_Node extends EcomDev_PHPUnit_Test_Case
{
/**
* Creates constraint instance
*
* @param $nodePath
* @param $type
* @param $value
*
* @return EcomDev_PHPUnit_Constraint_Config_Node
*/
protected function _getConstraint($nodePath, $type, $value)
{
return new EcomDev_PHPUnit_Constraint_Config_Node($nodePath, $type, $value);
}
/**
* Test constructor of the node,
*
* @param mixed $expectedValue
* @param string $type
*
* @dataProvider dataProvider
*/
public function testConstructorAccepts($expectedValue, $type)
{
$constraint = $this->_getConstraint('some/dummy/path', $type, $expectedValue);
$this->assertAttributeEquals($expectedValue, '_expectedValue', $constraint);
}
/**
* Tests that particular value equals xml
*
* @param string $actualValue
* @param string $expectedValue
* @dataProvider dataProvider
*/
public function testEqualsXml($actualValue, $expectedValue)
{
$actualValue = new SimpleXMLElement($actualValue);
$expectedValue = new SimpleXMLElement($expectedValue);
$constraint = $this->_getConstraint(
'some/dummy/path',
EcomDev_PHPUnit_Constraint_Config_Node::TYPE_EQUALS_XML,
$expectedValue
);
$this->assertTrue($constraint->evaluate($actualValue, '', true));
$this->assertAttributeEmpty('_comparisonFailure', $constraint);
}
/**
* Tests that particular value equals xml
*
* @param string $actualValue
* @param string $expectedValue
* @dataProvider dataProvider
*/
public function testEqualsXmlFailure($actualValue, $expectedValue)
{
$actualValue = new SimpleXMLElement($actualValue);
$expectedValue = new SimpleXMLElement($expectedValue);
$constraint = $this->_getConstraint(
'some/dummy/path',
EcomDev_PHPUnit_Constraint_Config_Node::TYPE_EQUALS_XML,
$expectedValue
);
$this->assertFalse($constraint->evaluate($actualValue, '', true));
$this->assertAttributeNotEmpty('_comparisonFailure', $constraint);
$this->assertAttributeInstanceOf('PHPUnit_Framework_ComparisonFailure', '_comparisonFailure', $constraint);
}
}
\ No newline at end of file
-
- 1
- equals_string
-
- '0'
- equals_string
-
- 0.01
- equals_string
-
- "0.01"
- equals_decimal
-
- "0"
- equals_decimal
-
- "100.0123"
- equals_decimal
-
actual: |
<config>
<value>1</value>
<value>2</value>
</config>
expected: |
<config>
<value>1</value>
<value>2</value>
</config>
-
actual: |
<config>
<value>1</value>
<value>2</value>
</config>
expected: |
<config>
<value>1</value>
<value>2</value>
</config>
-
actual: |
<config>
<value>1</value>
<value>2</value>
<more_nested><value>1</value><value>2</value></more_nested>
</config>
expected: |
<config>
<value>1</value>
<value>2</value>
<more_nested>
<value>1</value>
<value>2</value>
</more_nested>
</config>
\ No newline at end of file
-
actual: |
<config>
<value>1</value>
<value>2</value>
</config>
expected: |
<config>
<value>2</value>
<value>3</value>
</config>
-
actual: |
<config>
<value>1</value>
<value>2</value>
<more_nested>
<value>1</value>
<value>2</value>
</more_nested>
</config>
expected: |
<config>
<value>1</value>
<value>2</value>
<more_nested>
<value>1</value>
<value>1</value>
</more_nested>
</config>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment