Commit 68f9fe81 authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

+ New assertion for configuration tests.

parent 00008320
...@@ -32,6 +32,9 @@ class EcomDev_PHPUnit_Model_App extends Mage_Core_Model_App ...@@ -32,6 +32,9 @@ class EcomDev_PHPUnit_Model_App extends Mage_Core_Model_App
// Admin store code // Admin store code
const ADMIN_STORE_CODE = 'admin'; const ADMIN_STORE_CODE = 'admin';
const AREA_ADMINHTML = 'adminhtml';
const AREA_FRONTEND = 'frontend';
const REGISTRY_PATH_LAYOUT_SINGLETON = '_singleton/core/layout'; const REGISTRY_PATH_LAYOUT_SINGLETON = '_singleton/core/layout';
const XML_PATH_LAYOUT_MODEL_FOR_TEST = 'phpunit/suite/layout/model'; const XML_PATH_LAYOUT_MODEL_FOR_TEST = 'phpunit/suite/layout/model';
......
...@@ -110,6 +110,26 @@ class EcomDev_PHPUnit_Model_Config extends Mage_Core_Model_Config ...@@ -110,6 +110,26 @@ class EcomDev_PHPUnit_Model_Config extends Mage_Core_Model_Config
return $this->_replaceInstanceCreation['resource_model'][$modelClass]; return $this->_replaceInstanceCreation['resource_model'][$modelClass];
} }
/**
* Retrieves real resource model class alias
*
* @param string $classAlias
* @return string
*/
public function getRealResourceModelClassAlias($classAlias)
{
list($classAliasPrefix,) = explode('/', $classAlias, 2);
if (isset($this->_xml->global->models->$classAliasPrefix->resourceModel)) {
$realClassAliasPrefix = $this->_xml->global->models->$classAliasPrefix->resourceModel;
$classAlias = $realClassAliasPrefix . substr(
$classAlias, strlen($classAliasPrefix)
);
}
return $classAlias;
}
/** /**
* Loads scope snapshot * Loads scope snapshot
* *
......
<?php
class EcomDev_PHPUnit_Model_Design_Package
extends Mage_Core_Model_Design_Package
implements EcomDev_PHPUnit_Constraint_Config_Layout_Design_Package_Interface
{
/**
* Asserts layout file existance in design packages,
* and returns actual and expected filenames as result
*
* @param string $fileName
* @param string $area
* @param string|null $designPackage if not specified any theme will be used
* @param string|null $theme if not specified any theme will be used
* @return array of 'expected' and 'actual' file names
*/
public function getLayoutFileAssertion($fileName, $area, $designPackage = null, $theme = null)
{
$this->reset();
$this->setArea($area);
if ($area == EcomDev_PHPUnit_Model_App::AREA_ADMINHTML) {
$this->setStore(EcomDev_PHPUnit_Model_App::ADMIN_STORE_CODE);
} else {
$this->setStore(Mage::app()->getAnyStoreView()->getCode());
}
$this->setPackageName($designPackage);
$this->setTheme($theme);
$actualFileName = $this->getLayoutFilename($fileName, $params);
if ($theme !== null || $designPackage !== null) {
$expectedTheme = $this->getTheme('layout');
$expectedDesignPackage = $this->getPackageName();
$params = array(
'_type' => 'layout',
'_theme' => $expectedTheme,
'_package' => $expectedDesignPackage
);
$expectedFileName = Mage::getBaseDir('design') . DS . $this->_renderFilename($fileName, $params);
} else {
$expectedFileName = $actualFileName;
if (!file_exists($actualFileName)) {
$actualFileName = false;
}
}
return array(
'actual' => $actualFileName,
'expected' => $expectedFileName
);
}
/**
* Resets design data
*
* @return EcomDev_PHPUnit_Model_Design_Package
*/
public function reset()
{
$this->_store = null;
$this->_area = null;
$this->_name = null;
$this->_theme = null;
$this->_config = null;
$this->_rootDir = null;
$this->_callbackFileDir = null;
return $this;
}
}
<?php <?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) 2011 Ecommerce Developers (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>
*/
/** /**
* Constraint for testing the configuration values * Constraint for testing the configuration values
......
<?php <?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) 2011 Ecommerce Developers (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 for constraints based on configuration * Abstract class for constraints based on configuration
...@@ -200,8 +216,14 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract ...@@ -200,8 +216,14 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract
$description $description
); );
} else { } else {
if (!$this->_useActualValue) {
$actualValue = $other->asNiceXml();
} else {
$actualValue = $this->_actualValue;
}
throw new EcomDev_PHPUnit_Constraint_Exception( throw new EcomDev_PHPUnit_Constraint_Exception(
$failureDescription, $other->asNiceXml(), $description $failureDescription, $actualValue, $description
); );
} }
} }
......
<?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()
*/
protected function customFailureDescription($other, $description, $not)
{
return sprintf(
'Failed asserting that %s "%s/%s" %s.',
$this->_textByGroup[$this->_group],
$this->_classAliasPrefix, $this->_classAliasName,
$this->toString()
);
}
}
<?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) 2011 Ecommerce Developers (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>
*/
/**
* Constraint for testing event observer definition
* in the configuration
*
*/
class EcomDev_PHPUnit_Constraint_Config_EventObserver
extends EcomDev_PHPUnit_Constraint_Config_Abstract
{
const XML_PATH_EVENTS = '%s/events';
const TYPE_DEFINDED = 'defined';
const OBSERVER_TYPE_DISABLED = 'disabled';
const OBSERVER_TYPE_SINGLETON = 'singleton';
const OBSERVER_TYPE_MODEL = 'model';
/**
* Event area (frontend, adminhtml, global, cron)
*
* @var string
*/
protected $_area = null;
/**
* Observer name for additional restriction
*
* @var string|null
*/
protected $_observerName = null;
/**
* Observer class alias
*
* @var string
*/
protected $_observerClassAlias = null;
/**
* Observer method
*
* @var string
*/
protected $_observerMethod = null;
/**
* Name of the event that should be observed
*
* @var string
*/
protected $_eventName = null;
/**
* Constraint for testing observer
* event definitions in configuration
*
* @param string $area
* @param string $eventName
* @param string $observerClassAlias
* @param string $observerMethod
* @param string|null $observerName
*/
public function __construct($area, $eventName, $observerClassAlias, $observerMethod, $type = self::TYPE_DEFINDED, $observerName = null)
{
if (empty($area) || !is_string($area)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string', $area);
}
if (empty($eventName) || !is_string($eventName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string', $eventName);
}
if (empty($observerClassAlias) || !is_string($observerClassAlias)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'string', $observerClassAlias);
}
if (empty($observerMethod) || !is_string($observerMethod)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(4, 'string', $observerMethod);
}
if ($observerName !== null && !is_string($observerName)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(6, 'string', $observerName);
}
$this->_area = $area;
$this->_eventName = $eventName;
$this->_observerClassAlias = $observerClassAlias;
$this->_observerMethod = $observerMethod;
$this->_observerName = $observerName;
$expectedValue = $this->_observerClassAlias . '::' . $this->_observerMethod;
$nodePath = sprintf(self::XML_PATH_EVENTS, $this->_area, $this->_eventName);
parent::__construct($nodePath, $type, $expectedValue);
}
/**
* Evaluates that observer is defined and enabled
*
* @param Varien_Simplexml_Element $other
* @return boolean
*/
protected function evaluateDefined($other)
{
$this->setActualValue(false);
if (!isset($other->{$this->_eventName}->observers)) {
return false;
}
$observers = $other->{$this->_eventName}->observers;
foreach ($observers->children() as $observer) {
if ((string)$observer->type === self::OBSERVER_TYPE_DISABLED ||
($this->_observerName !== null && $this->_observerName !== $observer->getName())) {
continue;
}
$classAlias = (isset($observer->class) ? (string) $observer->class : $observer->getClassName());
$method = (string)$observer->method;
if ($classAlias === $this->_observerClassAlias && $method == $this->_observerMethod) {
$this->setActualValue($classAlias . '::' . $method);
return true;
}
}
return false;
}
/**
* Text representation of event observer definition evaluation
*
* @return string
*/
protected function textDefined()
{
$text = 'is defined';
if ($this->_observerName !== null) {
$text .= sprintf(' as %s observer name', $this->_observerName);
}
return $text;
}
/**
* Custom failure description for showing config related errors
* (non-PHPdoc)
* @see PHPUnit_Framework_Constraint::customFailureDescription()
*/
protected function customFailureDescription($other, $description, $not)
{
return sprintf(
'Failed asserting that %s "%s" event observer %s.',
$this->_area,
$this->_expectedValue,
$this->toString()
);
}
}
<?php <?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) 2011 Ecommerce Developers (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 for configuration constraints * Interface for configuration constraints
......
<?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) 2011 Ecommerce Developers (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>
*/
/**
* Layout configuration constraint
*
*/
class EcomDev_PHPUnit_Constraint_Config_Layout
extends EcomDev_PHPUnit_Constraint_Config_Abstract
{
const XML_PATH_LAYOUT = '%s/layout/updates';
const TYPE_LAYOUT_DEFINITION = 'layout_definition';
const TYPE_LAYOUT_FILE = 'layout_file';
/**
* Design area (frontend, adminhtml)
*
* @var string
*/
protected $_area = null;
/**
* Name of layout update,
* if specified, constraint
* will be additionally checked by this parameter
*
* @var string
*/
protected $_layoutUpdate = null;
/**
* Restriction by theme name
*
* @var string
*/
protected $_theme = null;
/**
* Restriction by design package name
*
* @var string
*/
protected $_designPackage = null;
/**
* Model for assertion of the data
*
* @var EcomDev_PHPUnit_Constraint_Config_Design_Package_Interface
*/
protected static $_designPackageModel = null;
/**
* Configuration constraint for cheking the existance of
* layout file in configuration and a particular theme as well
*
* @param string $area design area (frontend|adminhtml)
* @param string $expectedFile layout file name that should be checked
* @param string $type type of assertion
* @param string|null $layoutUpdate additional check for layout update name for assertion of configuration
* @param string|null $theme additional check for layout file existance in a particular theme
* @param string|null $designPackage additional check for layout file existance in a particular theme
*/
public function __construct($area, $expectedFile, $type, $layoutUpdate = null,
$theme = null, $designPackage = null)
{
$this->_area = $area;
$this->_layoutUpdate = $layoutUpdate;
$this->_designPackage = $designPackage;
$this->_theme = $theme;
$this->_expectedValueValidation += array(
self::TYPE_LAYOUT_FILE => array(true, 'is_string', 'string'),
self::TYPE_LAYOUT_DEFINITION => array(true, 'is_string', 'string')
);
$this->_typesWithDiff[] = self::TYPE_LAYOUT_FILE;
$nodePath = sprintf(self::XML_PATH_LAYOUT, $area);
parent::__construct($nodePath, $type, $expectedFile);
}
/**
* Sets design package model for assertions
*
* @param EcomDev_PHPUnit_Constraint_Config_Design_Package_Interface $model
*/
public static function setDesignPackageModel(EcomDev_PHPUnit_Constraint_Config_Design_Package_Interface $model)
{
self::$_designPackageModel = $model;
}
/**
* Retrieves design package model that was set before
*
* @return EcomDev_PHPUnit_Constraint_Config_Design_Package_Interface
*/
public static function getDesignPackageModel()
{
return self::$_designPackageModel;
}
/**
* Checks layout definititions for expected file defined in the configuration
*
* @param Varien_Simplexml_Element $other
* @return boolean
*/
protected function evaluateLayoutDefinition($other)
{
foreach ($other->children() as $layoutUpdate) {
if ((string)$layoutUpdate->file === $this->_expectedValue
&& ($this->_layoutUpdate === null || $this->_layoutUpdate === $layoutUpdate->getName())) {
return true;
}
}
return false;
}
/**
* Layout defition assertion text
*
* @return string
*/
protected function textLayoutDefition()
{
$text = sprintf('file "%s" is defined in configuration for %s area', $this->_expectedValue, $this->_area);
if ($this->_layoutUpdate !== null) {
$text .= sprintf(' in "%s" layout update', $this->_layoutUpdate);
}
return $text;
}
/**
* Evaluates layout file existance
*
* @param Varien_Simplexml_Element $other
* @return boolean
*/
protected function evaluateLayoutFile($other)
{
$assertion = self::getDesignPackageModel()
->getLayoutFileAssertion($this->_expectedValue, $this->_area, $this->_designPackage, $this->_theme);
$this->setActualValue($assertion['actual']);
$this->_expectedValue = $assertin['expected'];
return $this->_actualValue !== $this->_expectedValue;
}
/**
* Text representation of layout file existance constraint
*
* @return string
*/
protected function textLayoutFile()
{
return 'file is the same as expected and exists';
}
/**
* Custom failure description for showing config related errors
* (non-PHPdoc)
* @see PHPUnit_Framework_Constraint::customFailureDescription()
*/
protected function customFailureDescription($other, $description, $not)
{
return sprintf(
'Failed asserting that layout %s.',
$this->toString()
);
}
}
<?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) 2011 Ecommerce Developers (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 for assertions in layout configuration
*
*/
interface EcomDev_PHPUnit_Constraint_Config_Layout_Design_Package_Interface
{
/**
* Asserts layout file existance in design packages,
* and returns actual and expected filenames as result
*
* @param string $fileName
* @param string $area
* @param string|null $designPackage if not specified any theme will be used
* @param string|null $theme if not specified any theme will be used
* @return array of 'expected' and 'actual' file names
*/
public function getLayoutFileAssertion($fileName, $area, $designPackage = null, $theme = null);
}
<?php <?php
/**
class EcomDev_PHPUnit_Constraint_Config_Module extends EcomDev_PHPUnit_Constraint_Config_Abstract * 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) 2011 Ecommerce Developers (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>
*/
/**
* Module configuration constraint
*
*/
class EcomDev_PHPUnit_Constraint_Config_Module
extends EcomDev_PHPUnit_Constraint_Config_Abstract
{ {
const XML_PATH_MODULE_NODE = 'modules/%s'; const XML_PATH_MODULE_NODE = 'modules/%s';
...@@ -19,7 +40,7 @@ class EcomDev_PHPUnit_Constraint_Config_Module extends EcomDev_PHPUnit_Constrain ...@@ -19,7 +40,7 @@ class EcomDev_PHPUnit_Constraint_Config_Module extends EcomDev_PHPUnit_Constrain
protected $_moduleName = null; protected $_moduleName = null;
/** /**
* Contraint for evaluation of config node * Contraint for evaluation of module config node
* *
* @param string $nodePath * @param string $nodePath
* @param string $type * @param string $type
......
<?php <?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) 2011 Ecommerce Developers (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>
*/
/**
* Configuration node constaint
*
*/
class EcomDev_PHPUnit_Constraint_Config_Node class EcomDev_PHPUnit_Constraint_Config_Node
extends EcomDev_PHPUnit_Constraint_Config_Abstract extends EcomDev_PHPUnit_Constraint_Config_Abstract
{ {
......
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