Commit 110f6214 authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

! Added main helper factory

parent 87b5ac9c
......@@ -59,6 +59,8 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
$this->getAppReflection()->getMethod('applyTestScope')->invoke(null);
}
$this->firstLevelTestSuite = $suite;
Mage::dispatchEvent('phpunit_suite_start_after', array(
'suite' => $suite,
......@@ -139,6 +141,7 @@ class EcomDev_PHPUnit_Test_Listener implements PHPUnit_Framework_TestListener
'listener' => $this
));
if ($test instanceof PHPUnit_Framework_TestCase) {
EcomDev_PHPUnit_Helper::setTestCase($test);
EcomDev_PHPUnit_Test_Case_Util::getFixture(get_class($test))
->setScope(EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_LOCAL)
->loadByTestCase($test);
......
This diff is collapsed.
<?php
/**
* Test Helpers Factory
*
*/
class EcomDev_PHPUnit_Helper
{
/**
* Helpers container
*
* @var EcomDev_PHPunit_Helper_Interface[]
*/
protected static $helpers = array();
/**
* Adds a new helper instance to helpers registry
*
* If $position is specified, it will use value
* from before or after key as related helper
*
* @param EcomDev_PHPunit_Helper_Interface $helper
* @param bool|array $position
*
* @throws RuntimeException
*/
public static function add(EcomDev_PHPunit_Helper_Interface $helper, $position = false)
{
if ($position === false) {
self::$helpers[] = $helper;
} elseif (isset($position['after']) || isset($position['before'])) {
$isBefore = isset($position['before']);
$relatedHelper = $isBefore ? $position['before'] : $position['after'];
if (is_string($relatedHelper)) {
// Retrieving of helper by class name
$relatedHelper = current(self::getHelpersByClass($relatedHelper));
}
$helperPosition = array_search($relatedHelper, self::$helpers, true);
if ($helperPosition !== false) {
array_splice(
self::$helpers,
$helperPosition + ($isBefore ? 0 : 1),
null,
array($helper)
);
}
} else {
throw new RuntimeException('Unknown position specified for helper addition');
}
}
/**
* Removes helper by instance
*
* @param EcomDev_PHPUnit_Helper_Interface $helper
*/
public static function remove(EcomDev_PHPUnit_Helper_Interface $helper)
{
$helperPosition = array_search($helper, self::$helpers, true);
if ($helperPosition !== false) {
array_splice(self::$helpers, $helperPosition, 1);
}
}
/**
* Removes all helpers by class name from helpers array
*
* @param string $helperClass
*/
public static function removeByClass($helperClass)
{
$helpersByClass = self::getHelpersByClass($helperClass);
foreach ($helpersByClass as $helper) {
self::remove($helper);
}
}
/**
* Returns helper by action,
* if helper for action was not found it returns false
*
* @param $action
* @return bool|EcomDev_PHPunit_Helper_Interface
*/
public static function getByAction($action)
{
foreach (self::$helpers as $helper) {
if ($helper->has($action)) {
return $helper;
}
}
return false;
}
/**
* Checks existence of a helper for an action
*
* @param string $action
* @return bool
*/
public static function has($action)
{
return self::getByAction($action) !== false;
}
/**
* Invokes a helper action with arguments as an array
*
* @param string $action
* @param array $args
*
* @throws RuntimeException
* @return mixed
*/
public static function invokeArgs($action, array $args)
{
$helper = self::getByAction($action);
if (!$helper) {
throw new RuntimeException(sprintf('Cannot find a helper for action "%s"', $action));
}
return $helper->invoke($action, $args);
}
/**
* Invokes helper action with flat arguments
*
* @param string $action
* @return mixed
*/
public static function invoke($action /*, $arg1, $arg2, $arg3 ... $argN */)
{
$args = func_get_args();
array_shift($args);
return self::invokeArgs($action, $args);
}
/**
* Sets test case to each helper instance
*
* @param PHPUnit_Framework_TestCase $testCase
*/
public static function setTestCase(PHPUnit_Framework_TestCase $testCase)
{
foreach (self::$helpers as $helper) {
$helper->setTestCase($testCase);
}
}
/**
* Finds a helper instance by class name
*
* @param string $className
* @return array
*/
protected static function getHelpersByClass($className)
{
return array_filter(self::$helpers, function ($item) use ($className) {
return get_class($item) === $className;
});
}
}
\ No newline at end of file
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