Commit 5e093a87 authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

Compatibility with PHPUnit4.0, some changes in Reflection

parent bfa5970e
......@@ -17,7 +17,7 @@ before_script:
# Installing EcomDev_PHPUnit module
- bin/mage-ci install-module $MAGE_DIR $(pwd)
# Configuring EcomDev_PHPUnit module
- bin/mage-ci shell $MAGE_DIR ecomdev-phpunit.php -a magento-config --db-name magento --same-db 1 --base_url http://test.magento.com/
- bin/mage-ci shell $MAGE_DIR ecomdev-phpunit.php -a magento-config --db-name magento --same-db 1 --base-url http://test.magento.com/
# Configuring test suite
- bin/mage-ci shell $MAGE_DIR ecomdev-phpunit.php -a change-status --enable
# Copying phpunit.xml for travis builds
......
......@@ -396,12 +396,19 @@ class EcomDev_PHPUnit_Test_Case_Util
// Try to find the module name by class name
$moduleName = false;
foreach (Mage::getConfig()->getNode('modules')->children() as $module) {
if (strpos($className, $module->getName()) === 0) {
if (strpos($className, $module->getName() . '_') === 0) {
$moduleName = $module->getName();
break;
}
}
// Add a possibility to override a module, for which this test is applicable
$moduleAnnotations = self::getAnnotationByNameFromClass($className, 'module');
if ($moduleAnnotations) {
$moduleName = current($moduleAnnotations);
}
if (!$moduleName) {
throw new RuntimeException('Cannot to find the module name for class name: ' . $className);
}
......
......@@ -263,4 +263,23 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
// No action is required
}
/**
* Risky test.
*
* @param PHPUnit_Framework_Test $test
* @param Exception $e
* @param float $time
* @since Method available since Release 4.0.0
*/
public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
{
Mage::dispatchEvent('phpunit_test_risky', array(
'test' => $test,
'exception' => $e,
'time' => $time,
'listener' => $this
));
// No action required
}
}
\ No newline at end of file
......@@ -5,7 +5,11 @@ if (version_compare(PHP_VERSION, '5.3', '<')) {
exit(1);
}
$_baseDir = getcwd();
if (isset($_SERVER['MAGENTO_DIRECTORY'])) {
$_baseDir = $_SERVER['MAGENTO_DIRECTORY'];
} else {
$_baseDir = getcwd();
}
// Include Mage file by detecting app root
require_once $_baseDir . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'Mage.php';
......
......@@ -23,7 +23,7 @@ class EcomDev_PHPUnitTest_Test_Lib_AbstractConstraint extends PHPUnit_Framework_
);
if (!$expectedResult) {
$this->assertAttributeInstanceOf('PHPUnit_Framework_ComparisonFailure', '_comparisonFailure', $constraint);
$this->assertAttributeInstanceOf('\SebastianBergmann\Comparator\ComparisonFailure', '_comparisonFailure', $constraint);
}
}
......
......@@ -73,6 +73,6 @@ class EcomDev_PHPUnitTest_Test_Lib_Constraint_Config_Node extends EcomDev_PHPUni
$this->assertFalse($constraint->evaluate($actualValue, '', true));
$this->assertAttributeNotEmpty('_comparisonFailure', $constraint);
$this->assertAttributeInstanceOf('PHPUnit_Framework_ComparisonFailure', '_comparisonFailure', $constraint);
$this->assertAttributeInstanceOf('\SebastianBergmann\Comparator\ComparisonFailure', '_comparisonFailure', $constraint);
}
}
\ No newline at end of file
......@@ -22,6 +22,9 @@
<EcomDev_PHPUnitTest>
<codePool>community</codePool>
<active>true</active>
<depends>
<EcomDev_PHPUnit />
</depends>
</EcomDev_PHPUnitTest>
</modules>
</config>
\ No newline at end of file
......@@ -24,6 +24,20 @@
abstract class EcomDev_PHPUnit_AbstractConstraint
extends PHPUnit_Framework_Constraint
{
/**
* Comparator factory
*
* @var \SebastianBergmann\Comparator\Factory
*/
static protected $_comparatorFactory;
/**
* String exporter for variables
*
* @var \SebastianBergmann\Exporter\Exporter
*/
static protected $_exporter;
/**
* List of validation rules for expected value
* It is an associative array with key as type and value
......@@ -76,7 +90,7 @@ abstract class EcomDev_PHPUnit_AbstractConstraint
/**
* Comparison failure for nice failure messages
*
* @var PHPUnit_Framework_ComparisonFailure
* @var \SebastianBergmann\Comparator\ComparisonFailure
*/
protected $_comparisonFailure = null;
......@@ -128,6 +142,35 @@ abstract class EcomDev_PHPUnit_AbstractConstraint
$this->_expectedValue = $expectedValue;
}
/**
* Comparator factory instance
*
* @return \SebastianBergmann\Comparator\Factory
*/
public static function getComparatorFactory()
{
if (self::$_comparatorFactory === null) {
self::$_comparatorFactory = new \SebastianBergmann\Comparator\Factory();
}
return self::$_comparatorFactory;
}
/**
* Exporter instance for variables
*
* @return \SebastianBergmann\Exporter\Exporter
*/
public static function getExporter()
{
if (self::$_exporter === null) {
self::$_exporter = new \SebastianBergmann\Exporter\Exporter();
}
return self::$_exporter;
}
/**
* Set actual value that will be used in the fail message
*
......@@ -190,7 +233,7 @@ abstract class EcomDev_PHPUnit_AbstractConstraint
* (non-PHPdoc)
* @see PHPUnit_Framework_Constraint::fail()
*/
public function fail($other, $description, PHPUnit_Framework_ComparisonFailure $comparisonFailure = NULL)
public function fail($other, $description, \SebastianBergmann\Comparator\ComparisonFailure $comparisonFailure = NULL)
{
$failureDescription = sprintf('Failed asserting that %s', $this->failureDescription($other));
......@@ -269,12 +312,12 @@ abstract class EcomDev_PHPUnit_AbstractConstraint
if (is_array($value) && preg_match('/^\d+$/', implode('', array_keys($value)))) {
$stringValue = '';
foreach ($value as $val) {
$stringValue .= (is_string($val) ? $val : PHPUnit_Util_Type::export($val)) . "\n";
$stringValue .= (is_string($val) ? $val : self::getExporter()->export($val)) . "\n";
}
return $stringValue;
} else {
return PHPUnit_Util_Type::export($value);
return self::getExporter()->export($value);
}
}
......@@ -287,7 +330,7 @@ abstract class EcomDev_PHPUnit_AbstractConstraint
*/
public function compareValues($expectedValue, $actualValue)
{
$comparatorFactory = PHPUnit_Framework_ComparatorFactory::getDefaultInstance();
$comparatorFactory = self::getComparatorFactory();
try {
$comparator = $comparatorFactory->getComparatorFor(
......@@ -300,7 +343,7 @@ abstract class EcomDev_PHPUnit_AbstractConstraint
);
}
catch (PHPUnit_Framework_ComparisonFailure $f) {
catch (\SebastianBergmann\Comparator\ComparisonFailure $f) {
$this->_comparisonFailure = $f;
return false;
}
......@@ -316,7 +359,7 @@ abstract class EcomDev_PHPUnit_AbstractConstraint
* @param mixed $expectedValue
* @param mixed $actualValue
*
* @return PHPUnit_Framework_ComparisonFailure
* @return \SebastianBergmann\Comparator\ComparisonFailure
*/
public function getComparisonFailure($expectedValue, $actualValue)
{
......@@ -326,7 +369,7 @@ abstract class EcomDev_PHPUnit_AbstractConstraint
return $failure;
}
return new PHPUnit_Framework_ComparisonFailure(
return new \SebastianBergmann\Comparator\ComparisonFailure(
$expectedValue,
$actualValue,
$this->exportAsString($expectedValue),
......
......@@ -59,9 +59,9 @@ class EcomDev_PHPUnit_Constraint_Config extends PHPUnit_Framework_Constraint
*
* @param mixed $other
* @param string $description
* @param PHPUnit_Framework_ComparisonFailure $comparisonFailure
* @param \SebastianBergmann\Comparator\ComparisonFailure $comparisonFailure
*/
public function fail($other, $description, PHPUnit_Framework_ComparisonFailure $comparisonFailure = NULL)
public function fail($other, $description, \SebastianBergmann\Comparator\ComparisonFailure $comparisonFailure = NULL)
{
$nodeValue = $this->getNodeValue($other);
......
......@@ -258,7 +258,7 @@ class EcomDev_PHPUnit_Constraint_Config_Node
throw new RuntimeException(sprintf(
'Config node "%s" is not a string of comma separated values, passed expected value: %s',
$this->_nodePath,
PHPUnit_Util_Type::export($this->_expectedValue)
self::getExporter()->export($this->_expectedValue)
));
}
......@@ -279,7 +279,7 @@ class EcomDev_PHPUnit_Constraint_Config_Node
protected function textContainValues()
{
return sprintf('contains "%s" in comma separated value list',
PHPUnit_Util_Type::export($this->_expectedValue));
self::getExporter()->export($this->_expectedValue));
}
/**
......
......@@ -27,7 +27,7 @@ class EcomDev_PHPUnit_Constraint_Exception extends PHPUnit_Framework_Expectation
public function __construct($description, $diff = '', $message = '')
{
if (!$diff instanceof PHPUnit_Framework_ComparisonFailure) {
if (!$diff instanceof \SebastianBergmann\Comparator\ComparisonFailure) {
if (!is_scalar($diff)) {
$diff = print_r($diff, true);
}
......
......@@ -73,7 +73,7 @@ class EcomDev_PHPUnit_Constraint_Json extends EcomDev_PHPUnit_AbstractConstraint
$this->setActualValue($decodedJson);
} catch (Zend_Json_Exception $e) {
$this->setActualValue(
PHPUnit_Util_Type::shortenedString($other)
self::getExporter()->shortenedExport($other)
. "\n" . $e->__toString()
);
return false;
......
......@@ -40,6 +40,13 @@ class EcomDev_Utils_Reflection
}
$reflectionObject = self::getReflection($object);
while (!$reflectionObject->hasProperty($property)) {
if (!$reflectionObject->getParentClass()) {
break;
}
$reflectionObject = $reflectionObject->getParentClass();
}
$reflectionProperty = $reflectionObject->getProperty($property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue((is_string($object) ? null : $object), $value);
......@@ -78,6 +85,15 @@ class EcomDev_Utils_Reflection
}
$reflectionObject = self::getReflection($object);
while (!$reflectionObject->hasProperty($property)) {
if ($reflectionObject->getParentClass()) {
$reflectionObject = $reflectionObject->getParentClass();
} else {
break;
}
}
$reflectionProperty = $reflectionObject->getProperty($property);
$reflectionProperty->setAccessible(true);
return $reflectionProperty->getValue((is_string($object) ? null : $object));
......@@ -155,4 +171,19 @@ class EcomDev_Utils_Reflection
throw new InvalidArgumentException('$object should be a valid class name or object instance');
}
}
/**
* Creates a new instance of object, without calling original constructor
*
* @param string $className
* @param array $properties
* @return object
*/
public static function createObject($className, array $properties = array())
{
$reflection = self::getReflection($className);
$object = $reflection->newInstanceWithoutConstructor();
self::setRestrictedPropertyValues($object, $properties);
return $object;
}
}
\ No newline at end of file
......@@ -35,6 +35,7 @@
<!-- Exclude Varien & Zend libraries -->
<directory suffix=".php">lib/Varien</directory>
<directory suffix=".php">lib/Zend</directory>
<directory suffix=".php">lib/Magento</directory>
</blacklist>
</filter>
<logging>
......
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