Commit 0c1c014f authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

! Added new type of configuration constraint\

# Fixed issue in abstract for default value check
parent 64be18b7
......@@ -63,6 +63,23 @@ abstract class EcomDev_PHPUnit_Test_Case_Config extends EcomDev_PHPUnit_Test_Cas
new EcomDev_PHPUnit_Constraint_Config_Module($moduleName, $type, $expectedValue)
);
}
/**
* A new constraint for checking resources node
*
* @param string $moduleName
* @param string $type
* @param string|null $expectedValue
* @return EcomDev_PHPUnit_Constraint_Config
*/
public static function configResource($moduleName, $type = EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_DEFINED, $expectedValue = null)
{
return self::config(
new EcomDev_PHPUnit_Constraint_Config_Resource($moduleName, $type, self::app()->getConfig()->getModuleDir('', $moduleName), $expectedValue)
);
}
/**
* A new constraint for checking module node
......@@ -134,6 +151,94 @@ abstract class EcomDev_PHPUnit_Test_Case_Config extends EcomDev_PHPUnit_Test_Cas
self::assertThat(Mage::getConfig(), $constraint, $message);
}
/**
* Asserts that config resource for module is defined
*
*
* @param string $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertSetupResourceDefined($moduleName = null, $expectedResourceName = null, $message = '')
{
if ($moduleName === null) {
$moduleName = self::getModuleNameFromCallStack();
}
self::assertThatConfig(
self::configResource($moduleName,
EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_DEFINED,
$expectedResourceName),
$message
);
}
/**
* Asserts that config resource for module is NOT defined
*
*
* @param string $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertSetupResourceNotDefined($moduleName = null, $expectedResourceName = null, $message = '')
{
if ($moduleName === null) {
$moduleName = self::getModuleNameFromCallStack();
}
self::assertThatConfig(
self::logicalNot(
self::configResource($moduleName,
EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_DEFINED,
$expectedResourceName)
),
$message
);
}
/**
* Asserts that config resource for module is defined and directory with the same name exists in module
*
*
* @param string $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertSetupResourceExists($moduleName = null, $expectedResourceName = null, $message = '')
{
if ($moduleName === null) {
$moduleName = self::getModuleNameFromCallStack();
}
self::assertThatConfig(
self::configResource($moduleName,
EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_EXISTS,
$expectedResourceName),
$message
);
}
/**
* Asserts that config resource for module is defined and directory with the same name exists in module
*
*
* @param string $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertSetupResourceNotExists($moduleName = null, $expectedResourceName = null, $message = '')
{
if ($moduleName === null) {
$moduleName = self::getModuleNameFromCallStack();
}
self::assertThatConfig(
self::logicalNot(
self::configResource($moduleName,
EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_EXISTS,
$expectedResourceName)
),
$message
);
}
/**
* Asserts that config node value is equal to the expected value.
*
......
......@@ -109,6 +109,7 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract
// Type check
if (isset($this->_expectedValueValidation[$type][1])
&& $expectedValue !== null
&& !$this->_expectedValueValidation[$type][1]($expectedValue)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, $expectedValueType, $expectedValue);
}
......
<?php
/**
* Setup resources configuration constraint
*
*/
class EcomDev_PHPUnit_Constraint_Config_Resource
extends EcomDev_PHPUnit_Constraint_Config_Abstract
{
const XML_PATH_RESOURCES_NODE = 'global/resources';
const TYPE_SETUP_DEFINED = 'setup_defined';
const TYPE_SETUP_EXISTS = 'setup_exists';
/**
* Name of the module for constraint
*
* @var string
*/
protected $_moduleName = null;
/**
* The module directory for constraint
*
* @var string
*/
protected $_moduleDirectory = null;
/**
* Contraint for evaluation of module config node
*
* @param string $nodePath
* @param string $type
* @param string $moduleDirectory
* @param mixed $expectedValue
*/
public function __construct($moduleName, $type, $moduleDirectory = null, $expectedValue = null)
{
$this->_expectedValueValidation += array(
self::TYPE_SETUP_DEFINED => array(false, 'is_string', 'string'),
self::TYPE_SETUP_EXISTS => array(false, 'is_string', 'string'),
);
$this->_typesWithDiff[] = self::TYPE_SETUP_DEFINED;
$this->_typesWithDiff[] = self::TYPE_SETUP_EXISTS;
parent::__construct(
self::XML_PATH_RESOURCES_NODE,
$type,
$expectedValue
);
$this->_moduleName = $moduleName;
$this->_moduleDirectory = $moduleDirectory;
if ($this->_type === self::TYPE_SETUP_EXISTS
&& !is_dir($moduleDirectory)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'real directory', $moduleDirectory);
}
}
/**
* Returns list of module setup resources
*
* @param Varien_Simplexml_Element $xml
* @return array
*/
protected function getModuleSetupResources(Varien_Simplexml_Element $xml)
{
$resourcesForModule = array();
foreach ($xml->children() as $resourceNode) {
if (isset($resourceNode->setup->module)
&& (string)$resourceNode->setup->module === $this->_moduleName) {
$resourcesForModule[] = $resourceNode->getName();
}
}
return $resourcesForModule;
}
/**
* Checks definition of expected resource name
*
* @param Varien_Simplexml_Element $other
*/
protected function evaluateSetupDefined($other)
{
$moduleResources = $this->getModuleSetupResources($other);
if ($this->_expectedValue === null) {
$this->_expectedValue = empty($moduleResources) ?
strtolower($this->_moduleName) . '_setup' :
current($moduleResources);
}
$this->setActualValue($moduleResources);
return in_array($this->_expectedValue, $this->_actualValue);
}
/**
* Represents contraint for definition of setup resources
*
* @return string
*/
public function textSetupDefined()
{
return sprintf('contains resource definition for %s module with %s name', $this->_moduleName, $this->_expectedValue);
}
/**
* Checks existanse and definition of expected resource name
*
* @param Varien_Simplexml_Element $other
*/
protected function evaluateSetupExists($other)
{
$moduleResources = $this->getModuleSetupResources($other);
if ($this->_expectedValue === null) {
$this->_expectedValue = empty($moduleResources) ?
strtolower($this->_moduleName) . '_setup' :
current($moduleResources);
}
if (!is_dir($this->_moduleDirectory . DIRECTORY_SEPARATOR . 'sql')) {
$this->setActualValue(array());
return false;
}
$dirIterator = new DirectoryIterator($this->_moduleDirectory . DIRECTORY_SEPARATOR . 'sql');
$resourceDirectories = array();
foreach ($dirIterator as $entry) {
if ($entry->isDir() && !$entry->isDot()) {
$resourceDirectories[] = $entry->getBasename();
}
}
$this->setActualValue($resourceDirectories);
return in_array($this->_expectedValue, $moduleResources)
&& in_array($this->_expectedValue, $this->_actualValue);
}
/**
* Represents contraint for definition of setup resources
*
* @return string
*/
public function textSetupExists()
{
return sprintf('are defined or created directory for it in sql one of %s module with %s name', $this->_moduleName, $this->_expectedValue);
}
/**
* 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 setup resources %s.',
$this->toString()
);
}
}
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