PHPUnit supports the declaration of explicit dependencies between test methods. It is not necessary to determine the order.
Class ParentTestCase extends PHPUnit_Framework_TestCase { public function testOne() { self::assertTrue(true); } /** * @depends testOne */ public function testTwo() { self::assertTrue(true); } }
class ChildTestCase extends ParentTestCase { /** * @depends testTwo */ public function testThree() { self::assertTrue(true); } }
class MagicTestCase extends PHPUnit_Framework_TestCase { /** * @var array */ protected $order = array(); /** * Sets the orderSet of a TestCase. * * @param array $orderSet */ public function setOrderSet(array $orderSet) { $this->order = $orderSet; } /** * Get the orderSet of a TestCase. * * @return array $order */ public function getOrderSet() { return $this->order; } }
$test->setOrderSet(PHPUnit_Util_Test::getDependencies($class->getName(), $name));
foreach($this->tests as $test) { $test->setOrderSet( array_unique($this->getRecursiveOrderSet($test, $test->getName())) ); } usort($this->tests, array("MagicUtilTest ", "compareTestOrder"));
$this->addTest(new MagicTestSuite($testClass));
class MagicTestSuite extends PHPUnit_Framework_TestSuite { /** * Constructs a new TestSuite: * * @param mixed $theClass * @param string $name * @throws InvalidArgumentException */ public function __construct($theClass = '', $name = '') { parent::__construct($theClass, $name); foreach($this->tests as $test) { $test->setOrderSet( array_unique($this->getRecursiveOrderSet($test, $test->getName())) ); } usort($this->tests, array("MagicUtilTest ", "compareTestOrder")); } /** * @param $object * @param $methodName * @return array */ protected function getRecursiveOrderSet($object, $methodName) { $orderSet = array(); foreach($this->tests as $test) { if ($test->getName() == $methodName && get_class($object) == get_class($test)) { $testOrderSet = $test->getOrderSet(); if (!empty($testOrderSet)) { foreach($testOrderSet as $orderMethodName) { if(!in_array($orderMethodName, $orderSet)) { $orderResult = $this->getRecursiveOrderSet($test, $orderMethodName); $orderSet = array_merge($orderSet, $orderResult); } } } $orderSet = array_merge($orderSet, $testOrderSet); } } return $orderSet; } /** * @param ReflectionClass $class * @param ReflectionMethod $method */ protected function addTestMethod(ReflectionClass $class, ReflectionMethod $method) { $name = $method->getName(); if ($this->isPublicTestMethod($method)) { $test = self::createTest($class, $name); if ($test instanceof PHPUnit_Framework_TestCase || $test instanceof PHPUnit_Framework_TestSuite_DataProvider) { $test->setDependencies( PHPUnit_Util_Test::getDependencies($class->getName(), $name) ); } $test->setOrderSet(PHPUnit_Util_Test::getDependencies($class->getName(), $name)); $this->addTest($test, PHPUnit_Util_Test::getGroups( $class->getName(), $name) ); } else if ($this->isTestMethod($method)) { $this->addTest( self::warning( sprintf( 'Test method "%s" is not public.', $name ) ) ); } /** * Adds the tests from the given class to the suite. * * @param mixed $testClass * @throws InvalidArgumentException */ public function addTestSuite($testClass) { if (is_string($testClass) && class_exists($testClass)) { $testClass = new ReflectionClass($testClass); } if (!is_object($testClass)) { throw PHPUnit_Util_InvalidArgumentHelper::factory( 1, 'class name or object' ); } if ($testClass instanceof PHPUnit_Framework_TestSuite) { $this->addTest($testClass); } else if ($testClass instanceof ReflectionClass) { $suiteMethod = FALSE; if (!$testClass->isAbstract()) { if ($testClass->hasMethod(PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME)) { $method = $testClass->getMethod( PHPUnit_Runner_BaseTestRunner::SUITE_METHODNAME ); if ($method->isStatic()) { $this->addTest( $method->invoke(NULL, $testClass->getName()) ); $suiteMethod = TRUE; } } } if (!$suiteMethod && !$testClass->isAbstract()) { $this->addTest(new MagicTestSuite($testClass)); } } else { throw new InvalidArgumentException; } } }
class MagicUtilTest { /** * @static * @param PHPUnit_Framework_TestCase $object1 * @param PHPUnit_Framework_TestCase $object2 * @return int */ public static function compareTestOrder(PHPUnit_Framework_TestCase $object1, PHPUnit_Framework_TestCase $object2) { if (in_array($object2->getName(), $object1->getOrderSet())) { return 1; } if (in_array($object1->getName(), $object2->getOrderSet())) { return -1; } return 0; } }
require dirname(__FILE__) . DIRECTORY_SEPARATOR.' runSuite.php'; PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT'); require_once 'PHPUnit/TextUI/Command.php'; $tests= runSuite::suite(); PHPUnit_TextUI_TestRunner::run($tests); exit;
require_once 'MagicTestSuite.php'; class runSuite extends MagicTestSuite { public static function suite() { $suite = new self(); $suite->addTestSuite(“ChildTestCase”); } }
require_once 'MagicTestCase.php'; class ParentTestCase extends MagicTestCase { public function testOne() { self::assertTrue(true); } /** * @depends testOne */ public function testTwo() { self::assertTrue(true); } }
require_once 'ParentTestCase.php'; class ChildTestCase extends ParentTestCase { /** * @depends testTwo */ public function testThree() { self::assertTrue(true); } }
Source: https://habr.com/ru/post/116896/
All Articles