Commit 6da9ba41 authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

+ Added compatibility with PHPUnit 3.6.x + Added new feature for checking...

+ Added compatibility with PHPUnit 3.6.x + Added new feature for checking setup scripts versions sequance
parent 46e3464e
...@@ -8,7 +8,7 @@ This extension was created especially for resolving this problem and promoting t ...@@ -8,7 +8,7 @@ This extension was created especially for resolving this problem and promoting t
System Requirements System Requirements
------------------- -------------------
* PHP 5.3 or higher * PHP 5.3 or higher
* PHPUnit 3.5 * PHPUnit 3.6.x
* Magento CE1.4.x-1.5.x/PE1.9.x-PE1.10.x/EE1.9.x-1.10.x * Magento CE1.4.x-1.5.x/PE1.9.x-PE1.10.x/EE1.9.x-1.10.x
Documentation Documentation
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
*/ */
// Loading Spyc yaml parser, // Loading Spyc yaml parser,
// becuase Symfony component is not working propertly with nested // because Symfony component is not working properly with nested structures
require_once 'Spyc/spyc.php'; require_once 'Spyc/spyc.php';
/** /**
...@@ -140,7 +140,7 @@ class EcomDev_PHPUnit_Model_Fixture ...@@ -140,7 +140,7 @@ class EcomDev_PHPUnit_Model_Fixture
/** /**
* Model constuctor, just defines wich resource model to use * Model constructor, just defines which resource model to use
* (non-PHPdoc) * (non-PHPdoc)
* @see Varien_Object::_construct() * @see Varien_Object::_construct()
*/ */
...@@ -162,7 +162,7 @@ class EcomDev_PHPUnit_Model_Fixture ...@@ -162,7 +162,7 @@ class EcomDev_PHPUnit_Model_Fixture
} }
/** /**
* Sets storage for fixutures * Sets storage for fixtures
* *
* @param Varien_Object $storage * @param Varien_Object $storage
* @return EcomDev_PHPUnit_Model_Fixture * @return EcomDev_PHPUnit_Model_Fixture
......
...@@ -72,11 +72,35 @@ abstract class EcomDev_PHPUnit_Test_Case_Config extends EcomDev_PHPUnit_Test_Cas ...@@ -72,11 +72,35 @@ abstract class EcomDev_PHPUnit_Test_Case_Config extends EcomDev_PHPUnit_Test_Cas
* @param string|null $expectedValue * @param string|null $expectedValue
* @return EcomDev_PHPUnit_Constraint_Config * @return EcomDev_PHPUnit_Constraint_Config
*/ */
public static function configResource($moduleName, $type = EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_DEFINED, $expectedValue = null) public static function configResource($moduleName,
$type = EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_DEFINED,
$expectedValue = null)
{ {
return self::config( return self::config(
new EcomDev_PHPUnit_Constraint_Config_Resource($moduleName, $type, self::app()->getConfig()->getModuleDir('', $moduleName), $expectedValue) new EcomDev_PHPUnit_Constraint_Config_Resource($moduleName, $type,
self::app()->getConfig()->getModuleDir('', $moduleName),
$expectedValue)
);
}
/**
* A new constraint for checking resource setup scripts consistency
*
* @param string $moduleName
* @param string $type
* @param array|null $expectedValue
* @param string $resourceName
* @return EcomDev_PHPUnit_Constraint_Config
*/
public static function configResourceScript($moduleName,
$type = EcomDev_PHPUnit_Constraint_Config_Resource_Script::TYPE_SCRIPT_SCHEME,
array $expectedValue = null, $resourceName = null)
{
return self::config(
new EcomDev_PHPUnit_Constraint_Config_Resource_Script($moduleName, $type,
self::app()->getConfig()->getModuleDir('', $moduleName),
$resourceName, $expectedValue)
); );
} }
...@@ -219,14 +243,14 @@ abstract class EcomDev_PHPUnit_Test_Case_Config extends EcomDev_PHPUnit_Test_Cas ...@@ -219,14 +243,14 @@ abstract class EcomDev_PHPUnit_Test_Case_Config extends EcomDev_PHPUnit_Test_Cas
* @param mixed $expectedResourceName * @param mixed $expectedResourceName
* @param string $message * @param string $message
*/ */
public static function assertSetupResourceExists($moduleName = null, $expectedResourceName = null, $message = '') public static function assertSchemeSetupExists($moduleName = null, $expectedResourceName = null, $message = '')
{ {
if ($moduleName === null) { if ($moduleName === null) {
$moduleName = self::getModuleNameFromCallStack(); $moduleName = self::getModuleNameFromCallStack();
} }
self::assertThatConfig( self::assertThatConfig(
self::configResource($moduleName, self::configResource($moduleName,
EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_EXISTS, EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_SCHEME_EXISTS,
$expectedResourceName), $expectedResourceName),
$message $message
); );
...@@ -240,7 +264,7 @@ abstract class EcomDev_PHPUnit_Test_Case_Config extends EcomDev_PHPUnit_Test_Cas ...@@ -240,7 +264,7 @@ abstract class EcomDev_PHPUnit_Test_Case_Config extends EcomDev_PHPUnit_Test_Cas
* @param mixed $expectedResourceName * @param mixed $expectedResourceName
* @param string $message * @param string $message
*/ */
public static function assertSetupResourceNotExists($moduleName = null, $expectedResourceName = null, $message = '') public static function assertSchemeSetupNotExists($moduleName = null, $expectedResourceName = null, $message = '')
{ {
if ($moduleName === null) { if ($moduleName === null) {
$moduleName = self::getModuleNameFromCallStack(); $moduleName = self::getModuleNameFromCallStack();
...@@ -248,13 +272,154 @@ abstract class EcomDev_PHPUnit_Test_Case_Config extends EcomDev_PHPUnit_Test_Cas ...@@ -248,13 +272,154 @@ abstract class EcomDev_PHPUnit_Test_Case_Config extends EcomDev_PHPUnit_Test_Cas
self::assertThatConfig( self::assertThatConfig(
self::logicalNot( self::logicalNot(
self::configResource($moduleName, self::configResource($moduleName,
EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_EXISTS, EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_SCHEME_EXISTS,
$expectedResourceName) $expectedResourceName)
), ),
$message $message
); );
} }
/**
* Asserts that config resource for module is defined and directory with the same name exists in module directory
* for data setup scripts
*
*
* @param string $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertDataSetupExists($moduleName = null, $expectedResourceName = null, $message = '')
{
if ($moduleName === null) {
$moduleName = self::getModuleNameFromCallStack();
}
self::assertThatConfig(
self::configResource($moduleName,
EcomDev_PHPUnit_Constraint_Config_Resource::TYPE_SETUP_DATA_EXISTS,
$expectedResourceName),
$message
);
}
/**
* Asserts that config resource for module is defined and directory with the same name exists in module
* directory for data setup scripts
*
* @param string $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertDataSetupNotExists($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_DATA_EXISTS,
$expectedResourceName)
),
$message
);
}
/**
* Asserts that there is defined properly list of data/scheme upgrade scripts
*
* @param string $type
* @param string|null $from
* @param string|null $to
* @param string|null $moduleName
* @param string|null $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertSetupScriptVersions(
$type = EcomDev_PHPUnit_Constraint_Config_Resource_Script::TYPE_SCRIPT_SCHEME, $from = null, $to = null,
$moduleName = null, $resourceName = null,$message = '')
{
if ($moduleName === null) {
$moduleName = self::getModuleNameFromCallStack();
}
if ($to === null) {
$moduleConfig = self::app()->getConfig()->getModuleConfig($moduleName);
if (isset($moduleConfig->version)) {
$to = (string)$moduleConfig->version;
}
}
self::assertThatConfig(
self::configResourceScript($moduleName,
$type,
array($from, $to),
$resourceName
),
$message
);
}
/**
* Asserts that there is defined properly list of scheme upgrade scripts
*
* @param string|null $from
* @param string|null $to
* @param string|null $moduleName
* @param string|null $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertSchemeSetupScriptVersions($from = null, $to = null,
$moduleName = null, $resourceName = null,$message = '')
{
self::assertSetupScriptVersions(EcomDev_PHPUnit_Constraint_Config_Resource_Script::TYPE_SCRIPT_SCHEME, $from,
$to, $moduleName, $resourceName, $message);
}
/**
* Asserts that there is defined properly list of data upgrade scripts
*
* @param string|null $from
* @param string|null $to
* @param string|null $moduleName
* @param string|null $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertDataSetupScriptVersions($from = null, $to = null,
$moduleName = null, $resourceName = null,$message = '')
{
self::assertSetupScriptVersions(EcomDev_PHPUnit_Constraint_Config_Resource_Script::TYPE_SCRIPT_DATA, $from,
$to, $moduleName, $resourceName, $message);
}
/**
* Alias of assertSchemeSetupExists() model
*
*
* @param string $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertSetupResourceExists($moduleName = null, $expectedResourceName = null, $message = '')
{
self::assertSchemeSetupExists($moduleName, $expectedResourceName, $message);
}
/**
* Alias of assertSchemeSetupNotExists() model
*
*
* @param string $moduleName
* @param mixed $expectedResourceName
* @param string $message
*/
public static function assertSetupResourceNotExists($moduleName = null, $expectedResourceName = null, $message = '')
{
self::assertSchemeSetupNotExists($moduleName, $expectedResourceName, $message);
}
/** /**
* Asserts that config node value is equal to the expected value. * Asserts that config node value is equal to the expected value.
* *
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
* Abstract constraint for EcomDev_PHPUnit constraints * Abstract constraint for EcomDev_PHPUnit constraints
* Contains flexible constaint types implementation * Contains flexible constaint types implementation
* *
* @todo refactor failures for being 100% compatible with PHPUnit 3.6
*/ */
abstract class EcomDev_PHPUnit_Constraint_Abstract abstract class EcomDev_PHPUnit_Constraint_Abstract
extends PHPUnit_Framework_Constraint extends PHPUnit_Framework_Constraint
...@@ -187,7 +188,12 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract ...@@ -187,7 +188,12 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract
if (in_array($this->_type, $this->_typesWithDiff)) { if (in_array($this->_type, $this->_typesWithDiff)) {
throw new EcomDev_PHPUnit_Constraint_Exception( throw new EcomDev_PHPUnit_Constraint_Exception(
$failureDescription, $failureDescription,
PHPUnit_Util_Diff::diff($this->getExpectedValue(), $this->getActualValue($other)), new PHPUnit_Framework_ComparisonFailure(
$this->getExpectedValue(),
$this->getActualValue($other),
$this->getExpectedValue(),
$this->getActualValue($other)
),
$description $description
); );
} else { } else {
...@@ -197,6 +203,23 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract ...@@ -197,6 +203,23 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract
} }
} }
/**
* Adds compatibility to PHPUnit 3.6
*
* @param mixed $other
* @param mixed $description (custom description)
* @param boolean $not
* @return string
*/
protected function failureDescription($other, $description, $not)
{
if (method_exists($this, 'customFailureDescription')) {
return $this->customFailureDescription($other, $description, $not);
}
return parent::failureDescription($other, $description, $not);
}
/** /**
* Returns a scalar representation of actual value, * Returns a scalar representation of actual value,
* Returns $other if internal acutal value is not set * Returns $other if internal acutal value is not set
...@@ -216,7 +239,7 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract ...@@ -216,7 +239,7 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract
/** /**
* Returns a scalar representation of expected value * Returns a scalar representation of expected value
* *
* @return string * @return scalar
*/ */
protected function getExpectedValue() protected function getExpectedValue()
{ {
......
...@@ -92,11 +92,11 @@ class EcomDev_PHPUnit_Constraint_Config extends PHPUnit_Framework_Constraint ...@@ -92,11 +92,11 @@ class EcomDev_PHPUnit_Constraint_Config extends PHPUnit_Framework_Constraint
* @param Varien_Simplexml_Config $config * @param Varien_Simplexml_Config $config
* @see PHPUnit_Framework_Constraint::evaluate() * @see PHPUnit_Framework_Constraint::evaluate()
*/ */
public function evaluate($config) public function evaluate($config, $description = '', $returnResult = false)
{ {
$nodeValue = $this->getNodeValue($config); $nodeValue = $this->getNodeValue($config);
return $this->constraint->evaluate($nodeValue); return $this->constraint->evaluate($nodeValue, $description, $returnResult);
} }
/** /**
......
...@@ -65,14 +65,14 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract ...@@ -65,14 +65,14 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract
* (non-PHPdoc) * (non-PHPdoc)
* @see EcomDev_PHPUnit_Constraint_Abstract::evaluate() * @see EcomDev_PHPUnit_Constraint_Abstract::evaluate()
*/ */
public function evaluate($other) public function evaluate($other, $description = '', $returnResult = false)
{ {
if ($other === false) { if ($other === false) {
// If node was not found, than evaluation fails // If node was not found, than evaluation fails
return false; return false;
} }
return parent::evaluate($other); return parent::evaluate($other, $description, $returnResult);
} }
......
<?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>
*/
/** /**
* Setup resources configuration constraint * Setup resources configuration constraint
* *
...@@ -9,7 +26,8 @@ class EcomDev_PHPUnit_Constraint_Config_Resource ...@@ -9,7 +26,8 @@ class EcomDev_PHPUnit_Constraint_Config_Resource
const XML_PATH_RESOURCES_NODE = 'global/resources'; const XML_PATH_RESOURCES_NODE = 'global/resources';
const TYPE_SETUP_DEFINED = 'setup_defined'; const TYPE_SETUP_DEFINED = 'setup_defined';
const TYPE_SETUP_EXISTS = 'setup_exists'; const TYPE_SETUP_SCHEME_EXISTS = 'setup_scheme_exists';
const TYPE_SETUP_DATA_EXISTS = 'setup_data_exists';
/** /**
* Name of the module for constraint * Name of the module for constraint
...@@ -26,7 +44,7 @@ class EcomDev_PHPUnit_Constraint_Config_Resource ...@@ -26,7 +44,7 @@ class EcomDev_PHPUnit_Constraint_Config_Resource
protected $_moduleDirectory = null; protected $_moduleDirectory = null;
/** /**
* Contraint for evaluation of module config node * Constraint for evaluation of module config node
* *
* @param string $nodePath * @param string $nodePath
* @param string $type * @param string $type
...@@ -37,11 +55,13 @@ class EcomDev_PHPUnit_Constraint_Config_Resource ...@@ -37,11 +55,13 @@ class EcomDev_PHPUnit_Constraint_Config_Resource
{ {
$this->_expectedValueValidation += array( $this->_expectedValueValidation += array(
self::TYPE_SETUP_DEFINED => array(false, 'is_string', 'string'), self::TYPE_SETUP_DEFINED => array(false, 'is_string', 'string'),
self::TYPE_SETUP_EXISTS => array(false, 'is_string', 'string'), self::TYPE_SETUP_SCHEME_EXISTS => array(false, 'is_string', 'string'),
self::TYPE_SETUP_DATA_EXISTS => array(false, 'is_string', 'string'),
); );
$this->_typesWithDiff[] = self::TYPE_SETUP_DEFINED; $this->_typesWithDiff[] = self::TYPE_SETUP_DEFINED;
$this->_typesWithDiff[] = self::TYPE_SETUP_EXISTS; $this->_typesWithDiff[] = self::TYPE_SETUP_SCHEME_EXISTS;
$this->_typesWithDiff[] = self::TYPE_SETUP_DATA_EXISTS;
parent::__construct( parent::__construct(
self::XML_PATH_RESOURCES_NODE, self::XML_PATH_RESOURCES_NODE,
...@@ -52,7 +72,7 @@ class EcomDev_PHPUnit_Constraint_Config_Resource ...@@ -52,7 +72,7 @@ class EcomDev_PHPUnit_Constraint_Config_Resource
$this->_moduleName = $moduleName; $this->_moduleName = $moduleName;
$this->_moduleDirectory = $moduleDirectory; $this->_moduleDirectory = $moduleDirectory;
if ($this->_type === self::TYPE_SETUP_EXISTS if (($this->_type === self::TYPE_SETUP_SCHEME_EXISTS || $this->_type === self::TYPE_SETUP_DATA_EXISTS)
&& !is_dir($moduleDirectory)) { && !is_dir($moduleDirectory)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'real directory', $moduleDirectory); throw PHPUnit_Util_InvalidArgumentHelper::factory(3, 'real directory', $moduleDirectory);
} }
...@@ -98,21 +118,51 @@ class EcomDev_PHPUnit_Constraint_Config_Resource ...@@ -98,21 +118,51 @@ class EcomDev_PHPUnit_Constraint_Config_Resource
} }
/** /**
* Represents contraint for definition of setup resources * Represents constraint for definition of setup resources
* *
* @return string * @return string
*/ */
public function textSetupDefined() public function textSetupDefined()
{ {
return sprintf('contains resource definition for %s module with %s name', $this->_moduleName, $this->_expectedValue); return sprintf('contains resource definition for %s module with %s name',
$this->_moduleName, $this->_expectedValue);
}
/**
* Set actual value for comparison from module sql/data directories
*
* @param string $type
* @return EcomDev_PHPUnit_Constraint_Config_Resource
*/
protected function setActualValueFromResourceDirectories($type = 'sql')
{
if (!is_dir($this->_moduleDirectory . DIRECTORY_SEPARATOR . $type)) {
$this->setActualValue(array());
return $this;
}
$dirIterator = new DirectoryIterator($this->_moduleDirectory . DIRECTORY_SEPARATOR . $type);
$resourceDirectories = array();
foreach ($dirIterator as $entry) {
/* @var $entry DirectoryIterator */
if ($entry->isDir() && !$entry->isDot()) {
$resourceDirectories[] = $entry->getBasename();
}
}
$this->setActualValue($resourceDirectories);
return $this;
} }
/** /**
* Checks existanse and definition of expected resource name * Checks existence and definition of expected resource name schema directory
* *
* @param Varien_Simplexml_Element $other * @param Varien_Simplexml_Element $other
*/ */
protected function evaluateSetupExists($other) protected function evaluateSetupSchemeExists($other)
{ {
$moduleResources = $this->getModuleSetupResources($other); $moduleResources = $this->getModuleSetupResources($other);
...@@ -122,35 +172,52 @@ class EcomDev_PHPUnit_Constraint_Config_Resource ...@@ -122,35 +172,52 @@ class EcomDev_PHPUnit_Constraint_Config_Resource
current($moduleResources); current($moduleResources);
} }
if (!is_dir($this->_moduleDirectory . DIRECTORY_SEPARATOR . 'sql')) { $this->setActualValueFromResourceDirectories('sql');
$this->setActualValue(array());
return false; return in_array($this->_expectedValue, $moduleResources)
&& in_array($this->_expectedValue, $this->_actualValue);
} }
$dirIterator = new DirectoryIterator($this->_moduleDirectory . DIRECTORY_SEPARATOR . 'sql'); /**
* Represents constraint for definition of setup resources
*
* @return string
*/
public function textSetupSchemeExists()
{
return sprintf(' schema directory is created for %s module with %s name',
$this->_moduleName, $this->_expectedValue);
}
$resourceDirectories = array(); /**
* Checks existence and definition of expected resource name data directory
*
* @param Varien_Simplexml_Element $other
*/
protected function evaluateSetupDataExists($other)
{
$moduleResources = $this->getModuleSetupResources($other);
foreach ($dirIterator as $entry) { if ($this->_expectedValue === null) {
if ($entry->isDir() && !$entry->isDot()) { $this->_expectedValue = empty($moduleResources) ?
$resourceDirectories[] = $entry->getBasename(); strtolower($this->_moduleName) . '_setup' :
} current($moduleResources);
} }
$this->setActualValue($resourceDirectories); $this->setActualValueFromResourceDirectories('data');
return in_array($this->_expectedValue, $moduleResources) return in_array($this->_expectedValue, $moduleResources)
&& in_array($this->_expectedValue, $this->_actualValue); && in_array($this->_expectedValue, $this->_actualValue);
} }
/** /**
* Represents contraint for definition of setup resources * Represents constraint for definition of setup resources
* *
* @return string * @return string
*/ */
public function textSetupExists() public function textSetupDataExists()
{ {
return sprintf('are defined or created directory for it in sql one of %s module with %s name', $this->_moduleName, $this->_expectedValue); return sprintf(' data directory is created for %s module with %s name', $this->_moduleName, $this->_expectedValue);
} }
/** /**
...@@ -166,3 +233,4 @@ class EcomDev_PHPUnit_Constraint_Config_Resource ...@@ -166,3 +233,4 @@ class EcomDev_PHPUnit_Constraint_Config_Resource
); );
} }
} }
This diff is collapsed.
...@@ -27,12 +27,16 @@ class EcomDev_PHPUnit_Constraint_Exception extends PHPUnit_Framework_Expectation ...@@ -27,12 +27,16 @@ class EcomDev_PHPUnit_Constraint_Exception extends PHPUnit_Framework_Expectation
public function __construct($description, $diff = '', $message = '') public function __construct($description, $diff = '', $message = '')
{ {
if (!$diff instanceof PHPUnit_Framework_ComparisonFailure) {
if (!is_scalar($diff)) { if (!is_scalar($diff)) {
$diff = print_r($diff, true); $diff = print_r($diff, true);
} }
$this->diff = $diff; $this->diff = $diff;
parent::__construct($description, null, $message); $diff = null;
}
parent::__construct($message, $diff);
} }
public function toString() public function 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