Commit 975e184b authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

! Some additional refactoring that is touching fixture models

parent 2cde68ad
...@@ -122,7 +122,7 @@ class EcomDev_PHPUnit_Model_Expectation ...@@ -122,7 +122,7 @@ class EcomDev_PHPUnit_Model_Expectation
* *
* @see EcomDev_PHPUnit_Model_Test_Loadable_Interface::loadByTestCase() * @see EcomDev_PHPUnit_Model_Test_Loadable_Interface::loadByTestCase()
*/ */
public function loadByTestCase(EcomDev_PHPUnit_Test_Case $testCase) public function loadByTestCase(PHPUnit_Framework_TestCase $testCase)
{ {
$expectations = $testCase->getAnnotationByName('loadExpectation'); $expectations = $testCase->getAnnotationByName('loadExpectation');
......
...@@ -278,23 +278,22 @@ class EcomDev_PHPUnit_Model_Fixture ...@@ -278,23 +278,22 @@ class EcomDev_PHPUnit_Model_Fixture
/** /**
* Loads fixture files from test case annotations * Loads fixture files from test case annotations
* *
* @param EcomDev_PHPUnit_Test_Case $testCase * @param PHPUnit_Framework_TestCase $testCase
* @return EcomDev_PHPUnit_Model_Fixture * @return PHPUnit_Framework_TestCase
*/ */
public function loadByTestCase(EcomDev_PHPUnit_Test_Case $testCase) public function loadByTestCase(PHPUnit_Framework_TestCase $testCase)
{ {
$fixtures = $testCase->getAnnotationByName( $cacheOptions = EcomDev_PHPUnit_Test_Case_Util::getAnnotationByNameFromClass(
'loadFixture', get_class($testCase), 'cache', 'method', $testCase->getName(false)
array('class', 'method')
); );
$cacheOptions = $testCase->getAnnotationByName('cache', 'method');
$this->_parseCacheOptions($cacheOptions); $this->_parseCacheOptions($cacheOptions);
$this->_loadFixtureFiles($fixtures, $testCase); $fixtures = EcomDev_PHPUnit_Test_Case_Util::getAnnotationByNameFromClass(
get_class($testCase), 'loadFixture', array('class', 'method'), $testCase->getName(false)
);
$this->_loadFixtureFiles($fixtures, $testCase);
return $this; return $this;
} }
...@@ -306,24 +305,16 @@ class EcomDev_PHPUnit_Model_Fixture ...@@ -306,24 +305,16 @@ class EcomDev_PHPUnit_Model_Fixture
*/ */
public function loadForClass($className) public function loadForClass($className)
{ {
$reflection = EcomDev_Utils_Reflection::getRelflection($className); $cacheOptions = EcomDev_PHPUnit_Test_Case_Util::getAnnotationByNameFromClass(
$className, 'cache', 'class'
$method = $reflection->getMethod('getAnnotationByNameFromClass'); );
if (!$method instanceof ReflectionMethod) { $this->_parseCacheOptions($cacheOptions);
throw new RuntimeException('Unable to read class annotations, because it is not extended from EcomDev_PHPUnit_Test_Case');
}
$fixtures = $method->invokeArgs( $fixtures = EcomDev_PHPUnit_Test_Case_Util::getAnnotationByNameFromClass(
null, array($className, 'loadSharedFixture', 'class') $className, 'loadSharedFixture', 'class'
); );
$cacheOptions = $method->invokeArgs(
null, array($className, 'cache', 'class')
);
$this->_parseCacheOptions($cacheOptions);
$this->_loadFixtureFiles($fixtures, $className); $this->_loadFixtureFiles($fixtures, $className);
return $this; return $this;
} }
...@@ -364,25 +355,16 @@ class EcomDev_PHPUnit_Model_Fixture ...@@ -364,25 +355,16 @@ class EcomDev_PHPUnit_Model_Fixture
*/ */
protected function _loadFixtureFiles(array $fixtures, $classOrInstance) protected function _loadFixtureFiles(array $fixtures, $classOrInstance)
{ {
$isShared = ($this->isScopeShared() || !$classOrInstance instanceof EcomDev_PHPUnit_Test_Case); $isShared = ($this->isScopeShared() || !$classOrInstance instanceof PHPUnit_Framework_TestCase);
foreach ($fixtures as $fixture) { foreach ($fixtures as $fixture) {
if (empty($fixture) && $isShared) { if (empty($fixture) && $isShared) {
$fixture = self::DEFAULT_SHARED_FIXTURE_NAME; $fixture = self::DEFAULT_SHARED_FIXTURE_NAME;
} elseif (empty($fixture)) { } elseif (empty($fixture)) {
$fixture = null; $fixture = $classOrInstance->getName(false);
} }
$filePath = false; $className = (is_string($classOrInstance) ? $classOrInstance : get_class($classOrInstance));
$filePath = EcomDev_PHPUnit_Test_Case_Util::getYamlFilePath($className, 'fixtures', $fixture);
if ($isShared) {
$reflection = EcomDev_Utils_Reflection::getRelflection($classOrInstance);
$method = $reflection->getMethod('getYamlFilePathByClass');
if ($method instanceof ReflectionMethod) {
$filePath = $method->invokeArgs(null, array($classOrInstance, 'fixtures', $fixture));
}
} else {
$filePath = $classOrInstance->getYamlFilePath('fixtures', $fixture);
}
if (!$filePath) { if (!$filePath) {
throw new RuntimeException('Unable to load fixture for test'); throw new RuntimeException('Unable to load fixture for test');
......
...@@ -25,10 +25,10 @@ interface EcomDev_PHPUnit_Model_Test_Loadable_Interface ...@@ -25,10 +25,10 @@ interface EcomDev_PHPUnit_Model_Test_Loadable_Interface
/** /**
* Loads external data by test case instance * Loads external data by test case instance
* *
* @param EcomDev_PHPUnit_Test_Case $testCase * @param PHPUnit_Framework_TestCase $testCase
* @return EcomDev_PHPUnit_Model_Test_Loadable_Interface * @return EcomDev_PHPUnit_Model_Test_Loadable_Interface
*/ */
public function loadByTestCase(EcomDev_PHPUnit_Test_Case $testCase); public function loadByTestCase(PHPUnit_Framework_TestCase $testCase);
/** /**
* Applies external data * Applies external data
......
...@@ -643,28 +643,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase ...@@ -643,28 +643,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
*/ */
protected static function getFixture() protected static function getFixture()
{ {
$fixture = Mage::getSingleton( return EcomDev_PHPUnit_Test_Case_Util::getFixture(get_called_class());
self::getLoadableClassAlias(
'fixture',
self::XML_PATH_DEFAULT_FIXTURE_MODEL
)
);
if (!$fixture instanceof EcomDev_PHPUnit_Model_Fixture_Interface) {
throw new RuntimeException('Fixture model should implement EcomDev_PHPUnit_Model_Fixture_Interface interface');
}
$storage = Mage::registry(EcomDev_PHPUnit_Model_App::REGISTRY_PATH_SHARED_STORAGE);
if (!$storage instanceof Varien_Object) {
throw new RuntimeException('Fixture storage object was not initialized during test application setup');
}
$fixture->setStorage(
Mage::registry(EcomDev_PHPUnit_Model_App::REGISTRY_PATH_SHARED_STORAGE)
);
return $fixture;
} }
/** /**
...@@ -722,7 +701,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase ...@@ -722,7 +701,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
$name = $this->getName(false); $name = $this->getName(false);
} }
return self::getYamlFilePathByClass(get_called_class(), $type, $name); return EcomDev_PHPUnit_Test_Case_Util::getYamlFilePath(get_called_class(), $type, $name);
} }
/** /**
...@@ -734,39 +713,11 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase ...@@ -734,39 +713,11 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
* @param string $type type of YAML data (fixtures,expectations,dataproviders) * @param string $type type of YAML data (fixtures,expectations,dataproviders)
* @param string $name the file name for loading * @param string $name the file name for loading
* @return string|boolean * @return string|boolean
* @depracated since 0.3.0
*/ */
public static function getYamlFilePathByClass($className, $type, $name) public static function getYamlFilePathByClass($className, $type, $name)
{ {
if (strrpos($name, '.yaml') !== strlen($name) - 5) { return EcomDev_PHPUnit_Test_Case_Util::getYamlFilePath($className, $type, $name);
$name .= '.yaml';
}
$classFileObject = new SplFileInfo(
EcomDev_Utils_Reflection::getRelflection($className)->getFileName()
);
// When prefixed with ~/ or ~My_Module/, load from the module's Test/<type> directory
if (preg_match('#^~(?<module>[^/]*)/(?<path>.*)$#', $name, $matches)) {
$name = $matches['path'];
if( ! empty($matches['module'])) {
$moduleName = $matches['module'];
} else {
$moduleName = substr($className, 0, strpos($className, '_Test_'));;
}
$filePath = Mage::getModuleDir('', $moduleName) . DS . 'Test' . DS;
}
// Otherwise load from the Class/<type> directory
else {
$filePath = $classFileObject->getPath() . DS
. $classFileObject->getBasename('.php') . DS;
}
$filePath .= $type . DS . $name;
if (file_exists($filePath)) {
return $filePath;
}
return false;
} }
/** /**
...@@ -777,38 +728,10 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase ...@@ -777,38 +728,10 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
*/ */
protected function setUp() protected function setUp()
{ {
self::getFixture()
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_LOCAL)
->loadByTestCase($this);
$annotations = $this->getAnnotations();
self::getFixture()
->setOptions($annotations['method'])
->apply();
$this->app()->resetDispatchedEvents(); $this->app()->resetDispatchedEvents();
parent::setUp(); parent::setUp();
} }
/**
* Initializes test environment for subset of tests
*
*/
public static function setUpBeforeClass()
{
self::getFixture()
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED)
->loadForClass(get_called_class());
$annotations = PHPUnit_Util_Test::parseTestMethodAnnotations(
get_called_class()
);
self::getFixture()
->setOptions($annotations['class'])
->apply();
parent::setUpBeforeClass();
}
/** /**
* Implements default data provider functionality, * Implements default data provider functionality,
* returns array data loaded from Yaml file with the same name as test method * returns array data loaded from Yaml file with the same name as test method
...@@ -859,35 +782,13 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase ...@@ -859,35 +782,13 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
$this->_originalStore = null; $this->_originalStore = null;
} }
if ($this->getExpectation()->isLoaded()) {
$this->getExpectation()->discard();
}
$this->app()->getConfig()->flushReplaceInstanceCreation(); $this->app()->getConfig()->flushReplaceInstanceCreation();
$this->app()->getLayout()->flushReplaceBlockCreation(); $this->app()->getLayout()->flushReplaceBlockCreation();
foreach ($this->_replacedRegistry as $registryPath => $originalValue) { foreach ($this->_replacedRegistry as $registryPath => $originalValue) {
$this->app()->replaceRegistry($registryPath, $originalValue); $this->app()->replaceRegistry($registryPath, $originalValue);
}
self::getFixture()
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_LOCAL)
->discard(); // Clear applied fixture
parent::tearDown(); parent::tearDown();
} }
/**
* Clean up all the shared fixture data
*
* @return void
*/
public static function tearDownAfterClass()
{
self::getFixture()
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED)
->discard();
parent::tearDownAfterClass();
}
} }
...@@ -169,6 +169,50 @@ class EcomDev_PHPUnit_Test_Case_Util ...@@ -169,6 +169,50 @@ class EcomDev_PHPUnit_Test_Case_Util
return $fixture; return $fixture;
} }
/**
* Loads YAML file from directory inside of the unit test class or
* the directory inside the module directory if name is prefixed with ~/
* or from another module if name is prefixed with ~My_Module/
*
* @param string $className class name for looking fixture files
* @param string $type type of YAML data (fixtures,expectations,dataproviders)
* @param string $name the file name for loading
* @return string|boolean
*/
public static function getYamlFilePath($className, $type, $name)
{
if (strrpos($name, '.yaml') !== strlen($name) - 5) {
$name .= '.yaml';
}
$classFileObject = new SplFileInfo(
EcomDev_Utils_Reflection::getRelflection($className)->getFileName()
);
// When prefixed with ~/ or ~My_Module/, load from the module's Test/<type> directory
if (preg_match('#^~(?<module>[^/]*)/(?<path>.*)$#', $name, $matches)) {
$name = $matches['path'];
if( ! empty($matches['module'])) {
$moduleName = $matches['module'];
} else {
$moduleName = substr($className, 0, strpos($className, '_Test_'));;
}
$filePath = Mage::getModuleDir('', $moduleName) . DS . 'Test' . DS;
}
// Otherwise load from the Class/<type> directory
else {
$filePath = $classFileObject->getPath() . DS
. $classFileObject->getBasename('.php') . DS;
}
$filePath .= $type . DS . $name;
if (file_exists($filePath)) {
return $filePath;
}
return false;
}
} }
\ No newline at end of file
...@@ -43,7 +43,17 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener ...@@ -43,7 +43,17 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
} }
if (EcomDev_Utils_Reflection::getRestrictedPropertyValue($suite, 'testCase')) { if (EcomDev_Utils_Reflection::getRestrictedPropertyValue($suite, 'testCase')) {
EcomDev_PHPUnit_Test_Case_Util::getFixture($suite->getName())
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED)
->loadForClass($suite->getName());
$annotations = PHPUnit_Util_Test::parseTestMethodAnnotations(
$suite->getName()
);
EcomDev_PHPUnit_Test_Case_Util::getFixture($suite->getName())
->setOptions($annotations['class'])
->apply();
} }
} }
...@@ -61,6 +71,12 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener ...@@ -61,6 +71,12 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
$this->getAppReflection()->getMethod('discardTestScope')->invoke(null); $this->getAppReflection()->getMethod('discardTestScope')->invoke(null);
} }
} }
if (EcomDev_Utils_Reflection::getRestrictedPropertyValue($suite, 'testCase')) {
EcomDev_PHPUnit_Test_Case_Util::getFixture($suite->getName())
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED)
->discard();
}
} }
/** /**
...@@ -70,7 +86,15 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener ...@@ -70,7 +86,15 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
*/ */
public function startTest(PHPUnit_Framework_Test $test) public function startTest(PHPUnit_Framework_Test $test)
{ {
// No action is required for now if ($test instanceof PHPUnit_Framework_TestCase) {
EcomDev_PHPUnit_Test_Case_Util::getFixture($test->getName())
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_LOCAL)
->loadByTestCase($test);
$annotations = $this->getAnnotations();
self::getFixture()
->setOptions($annotations['method'])
->apply();
}
} }
/** /**
...@@ -81,7 +105,15 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener ...@@ -81,7 +105,15 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
*/ */
public function endTest(PHPUnit_Framework_Test $test, $time) public function endTest(PHPUnit_Framework_Test $test, $time)
{ {
// No action is required for now if ($test instanceof PHPUnit_Framework_TestCase) {
EcomDev_PHPUnit_Test_Case_Util::getFixture($test->getName())
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_LOCAL)
->discard(); // Clear applied fixture
if (EcomDev_PHPUnit_Test_Case_Util::getExpectation($test->getName())->isLoaded()) {
EcomDev_PHPUnit_Test_Case_Util::getExpectation($test->getName())->discard();
}
}
} }
/** /**
......
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