Commit 051a6efd authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

! Fix constraints to make proper constraints validation

parent fee5cb31
<?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
......@@ -20,7 +20,6 @@
* Abstract constraint for EcomDev_PHPUnit constraints
* Contains flexible constaint types implementation
*
* @todo refactor failures for being 100% compatible with PHPUnit 3.6
*/
abstract class EcomDev_PHPUnit_Constraint_Abstract
extends PHPUnit_Framework_Constraint
......@@ -74,12 +73,21 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract
*/
protected $_useActualValue = false;
/**
* Comparison failure for nice failure messages
*
* @var PHPUnit_Framework_ComparisonFailure
*/
protected $_comparisonFailure = null;
/**
* Abstract cnstraint constructor,
* provides unified interface for working with multiple types of evalation
*
* @param string $type
* @param mixed $expectedValue
*
* @throws PHPUnit_Framework_Exception
*/
public function __construct($type, $expectedValue = null)
{
......@@ -98,7 +106,7 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract
if (isset($this->_expectedValueValidation[$type])) {
$expectedValueType = (isset($this->_expectedValueValidation[$type][2]) ?
isset($this->_expectedValueValidation[$type][2]) :
$this->_expectedValueValidation[$type][2] :
'');
// Mandatory check
......@@ -186,17 +194,9 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract
$failureDescription = sprintf('Failed asserting that %s', $this->failureDescription($other));
if (in_array($this->_type, $this->_typesWithDiff)) {
$actual = $this->getActualValue($other);
$expected = $this->getExpectedValue();
throw new EcomDev_PHPUnit_Constraint_Exception(
$failureDescription,
new PHPUnit_Framework_ComparisonFailure(
$expected,
$actual,
$this->exportAsString($expected),
$this->exportAsString($actual)
),
$this->getComparisonFailure($this->getExpectedValue(), $this->getActualValue($other)),
$description
);
} else {
......@@ -278,4 +278,60 @@ abstract class EcomDev_PHPUnit_Constraint_Abstract
return PHPUnit_Util_Type::export($value);
}
}
/**
* Compares two values by using correct comparator for two types
*
* @param mixed $expectedValue
* @param mixed $actualValue
* @return bool
*/
public function compareValues($expectedValue, $actualValue)
{
$comparatorFactory = PHPUnit_Framework_ComparatorFactory::getDefaultInstance();
try {
$comparator = $comparatorFactory->getComparatorFor(
$expectedValue, $actualValue
);
$comparator->assertEquals(
$expectedValue,
$actualValue
);
}
catch (PHPUnit_Framework_ComparisonFailure $f) {
$this->_comparisonFailure = $f;
return false;
}
return true;
}
/**
* Retrieve comparison failure exception.
*
* Is used for generation of the failure messages
*
* @param mixed $actualValue
* @param mixed $expectedValue
*
* @return PHPUnit_Framework_ComparisonFailure
*/
public function getComparisonFailure($actualValue, $expectedValue)
{
if ($this->_comparisonFailure !== null) {
$failure = $this->_comparisonFailure;
$this->_comparisonFailure = null;
return $failure;
}
return new PHPUnit_Framework_ComparisonFailure(
$expectedValue,
$actualValue,
$this->exportAsString($expectedValue),
$this->exportAsString($actualValue)
);
}
}
......@@ -86,7 +86,7 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract
protected function getActualValue($other = null)
{
if (!$this->_useActualValue && $other->hasChildren()) {
return $other->asNiceXml();
return $this->getXmlAsDom($this->_expectedValue);
} elseif (!$this->_useActualValue) {
return (string) $other;
}
......@@ -102,9 +102,28 @@ abstract class EcomDev_PHPUnit_Constraint_Config_Abstract
protected function getExpectedValue()
{
if ($this->_expectedValue instanceof Varien_Simplexml_Element) {
return $this->_expectedValue->asNiceXml();
return $this->getXmlAsDom($this->_expectedValue);
}
return parent::getExpectedValue();
}
/**
* Converts xml to dom object
*
* @param $xmlValue
* @return DOMDocument
*/
protected function getXmlAsDom($xmlValue)
{
if ($xmlValue instanceof SimpleXMLElement) {
$xmlValue = $xmlValue->asXML();
}
$domValue = new DOMDocument;
$domValue->preserveWhiteSpace = FALSE;
$domValue->loadXML($xmlValue);
return $domValue;
}
}
\ No newline at end of file
......@@ -90,6 +90,7 @@ class EcomDev_PHPUnit_Constraint_Config_Layout
);
$this->_typesWithDiff[] = self::TYPE_LAYOUT_FILE;
$this->_typesWithDiff[] = self::TYPE_LAYOUT_DEFINITION;
$nodePath = sprintf(self::XML_PATH_LAYOUT, $area);
......@@ -131,7 +132,18 @@ class EcomDev_PHPUnit_Constraint_Config_Layout
}
}
return false;
if ($this->_layoutUpdate === null) {
$this->_layoutUpdate = 'your_module';
}
$expected = clone $other;
$expected->addChild($this->_layoutUpdate)
->addChild('file', htmlspecialchars($this->_expectedValue));
return $this->compareValues(
$this->getXmlAsDom($expected),
$this->getXmlAsDom($other)
);
}
/**
......@@ -156,15 +168,12 @@ class EcomDev_PHPUnit_Constraint_Config_Layout
* @param Varien_Simplexml_Element $other
* @return boolean
*/
protected function evaluateLayoutFile($other)
protected function evaluateLayoutFile()
{
$assertion = self::getDesignPackageModel()
->getLayoutFileAssertion($this->_expectedValue, $this->_area, $this->_designPackage, $this->_theme);
$this->setActualValue($assertion['actual']);
$this->_expectedValue = $assertion['expected'];
return $this->_actualValue === $this->_expectedValue;
return $this->compareValues($assertion['expected'], $assertion['actual']);
}
/**
......
......@@ -99,9 +99,7 @@ class EcomDev_PHPUnit_Constraint_Config_Module
*/
protected function evaluateCodePool($other)
{
$this->setActualValue((string)$other->codePool);
return $this->_actualValue === $this->_expectedValue;
return $this->compareValues($this->_expectedValue, (string)$other->codePool);
}
/**
......
......@@ -43,7 +43,7 @@ class EcomDev_PHPUnit_Constraint_Config_Node
public function __construct($nodePath, $type, $expectedValue = null)
{
$this->_expectedValueValidation += array(
self::TYPE_EQUALS_STRING => array(true, 'is_string', 'string'),
self::TYPE_EQUALS_STRING => array(true, 'is_scalar', 'scalar'),
self::TYPE_EQUALS_NUMBER => array(true, 'is_numeric', 'numeric'),
self::TYPE_LESS_THAN => array(true, 'is_numeric', 'numeric'),
self::TYPE_GREATER_THAN => array(true, 'is_numeric', 'numeric'),
......@@ -68,7 +68,7 @@ class EcomDev_PHPUnit_Constraint_Config_Node
*/
protected function evaluateEqualsString($other)
{
return (string)$other === $this->_expectedValue;
return $this->compareValues($this->_expectedValue, (string)$other);
}
/**
......@@ -88,7 +88,7 @@ class EcomDev_PHPUnit_Constraint_Config_Node
*/
protected function evaluateEqualsNumber($other)
{
return (float)$other == $this->_expectedValue;
return $this->compareValues($this->_expectedValue, (float)$other);
}
/**
......@@ -171,27 +171,10 @@ class EcomDev_PHPUnit_Constraint_Config_Node
*/
protected function evaluateEqualsXml($other)
{
// Normalize expected XML for matching appropriate xml structure without
// whitespaces, etc
if (!$this->_expectedValue instanceof Varien_Simplexml_Element) {
try {
if ($other instanceof SimpleXMLElement) {
$other = $other->asXML();
}
$expectedXml = new Varien_Simplexml_Element($this->_expectedValue);
} catch (Exception $e) {
throw new RuntimeException(sprintf(
'Expected value is not an xml string for node %s, passed expected value: %s, parsing error: %s',
$this->_nodePath,
PHPUnit_Util_Type::export($this->_expectedValue),
$e->getMessage()
));
}
$this->_expectedValue = $expectedXml;
}
$expectedValue = $this->getXmlAsDom($this->_expectedValue);
$other = $this->getXmlAsDom($other);
return $this->_expectedValue->asNiceXml() == $other->asNiceXml();
return $this->compareValues($expectedValue, $other);
}
......
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