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
*
* @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');
......
......@@ -278,23 +278,22 @@ class EcomDev_PHPUnit_Model_Fixture
/**
* Loads fixture files from test case annotations
*
* @param EcomDev_PHPUnit_Test_Case $testCase
* @return EcomDev_PHPUnit_Model_Fixture
* @param PHPUnit_Framework_TestCase $testCase
* @return PHPUnit_Framework_TestCase
*/
public function loadByTestCase(EcomDev_PHPUnit_Test_Case $testCase)
public function loadByTestCase(PHPUnit_Framework_TestCase $testCase)
{
$fixtures = $testCase->getAnnotationByName(
'loadFixture',
array('class', 'method')
$cacheOptions = EcomDev_PHPUnit_Test_Case_Util::getAnnotationByNameFromClass(
get_class($testCase), 'cache', 'method', $testCase->getName(false)
);
$cacheOptions = $testCase->getAnnotationByName('cache', 'method');
$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;
}
......@@ -306,24 +305,16 @@ class EcomDev_PHPUnit_Model_Fixture
*/
public function loadForClass($className)
{
$reflection = EcomDev_Utils_Reflection::getRelflection($className);
$method = $reflection->getMethod('getAnnotationByNameFromClass');
if (!$method instanceof ReflectionMethod) {
throw new RuntimeException('Unable to read class annotations, because it is not extended from EcomDev_PHPUnit_Test_Case');
}
$fixtures = $method->invokeArgs(
null, array($className, 'loadSharedFixture', 'class')
);
$cacheOptions = $method->invokeArgs(
null, array($className, 'cache', 'class')
$cacheOptions = EcomDev_PHPUnit_Test_Case_Util::getAnnotationByNameFromClass(
$className, 'cache', 'class'
);
$this->_parseCacheOptions($cacheOptions);
$fixtures = EcomDev_PHPUnit_Test_Case_Util::getAnnotationByNameFromClass(
$className, 'loadSharedFixture', 'class'
);
$this->_loadFixtureFiles($fixtures, $className);
return $this;
}
......@@ -364,25 +355,16 @@ class EcomDev_PHPUnit_Model_Fixture
*/
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) {
if (empty($fixture) && $isShared) {
$fixture = self::DEFAULT_SHARED_FIXTURE_NAME;
} elseif (empty($fixture)) {
$fixture = null;
$fixture = $classOrInstance->getName(false);
}
$filePath = false;
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);
}
$className = (is_string($classOrInstance) ? $classOrInstance : get_class($classOrInstance));
$filePath = EcomDev_PHPUnit_Test_Case_Util::getYamlFilePath($className, 'fixtures', $fixture);
if (!$filePath) {
throw new RuntimeException('Unable to load fixture for test');
......
......@@ -25,10 +25,10 @@ interface EcomDev_PHPUnit_Model_Test_Loadable_Interface
/**
* 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
*/
public function loadByTestCase(EcomDev_PHPUnit_Test_Case $testCase);
public function loadByTestCase(PHPUnit_Framework_TestCase $testCase);
/**
* Applies external data
......
......@@ -643,28 +643,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
*/
protected static function getFixture()
{
$fixture = Mage::getSingleton(
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;
return EcomDev_PHPUnit_Test_Case_Util::getFixture(get_called_class());
}
/**
......@@ -722,7 +701,7 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
$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
* @param string $type type of YAML data (fixtures,expectations,dataproviders)
* @param string $name the file name for loading
* @return string|boolean
* @depracated since 0.3.0
*/
public static function getYamlFilePathByClass($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;
return EcomDev_PHPUnit_Test_Case_Util::getYamlFilePath($className, $type, $name);
}
/**
......@@ -777,38 +728,10 @@ abstract class EcomDev_PHPUnit_Test_Case extends PHPUnit_Framework_TestCase
*/
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();
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,
* 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
$this->_originalStore = null;
}
if ($this->getExpectation()->isLoaded()) {
$this->getExpectation()->discard();
}
$this->app()->getConfig()->flushReplaceInstanceCreation();
$this->app()->getLayout()->flushReplaceBlockCreation();
foreach ($this->_replacedRegistry as $registryPath => $originalValue) {
$this->app()->replaceRegistry($registryPath, $originalValue);
}
self::getFixture()
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_LOCAL)
->discard(); // Clear applied fixture
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
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
}
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
$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
*/
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
*/
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