public function collectFromEntityProviders($filePath, $module = null) { $entities = []; // HACK Ugly workaround to avoid "Cannot redeclare class PHPUnit_Framework_TestResult" error // when running text collector with PHPUnit 3.4. There really shouldn't be any dependencies // here, but the class reflection enforces autloading of seemingly unrelated classes. // The main problem here is the CMSMenu class, which iterates through test classes, // which in turn trigger autoloading of PHPUnit. $phpunitwrapper = PhpUnitWrapper::inst(); $phpunitwrapper->init(); $classes = ClassInfo::classes_for_file($filePath); if ($classes) { foreach ($classes as $class) { // Not all classes can be instanciated without mandatory arguments, // so entity collection doesn't work for all SilverStripe classes currently // Requires PHP 5.1+ if (class_exists($class) && in_array('i18nEntityProvider', class_implements($class))) { $reflectionClass = new ReflectionClass($class); if ($reflectionClass->isAbstract()) { continue; } $obj = singleton($class); $entities = array_merge($entities, (array) $obj->provideI18nEntities()); if ($obj->is_a('DataObject')) { foreach (['db', 'has_one', 'has_many', 'many_many', 'belongs_many_many'] as $type) { foreach ((array) Config::inst()->get($obj->class, $type, Config::UNINHERITED) as $name => $spec) { $entities["{$obj->class}.{$type}_{$name}"] = [FormField::name_to_label($name)]; } } } } } } ksort($entities); return $entities; }
/** * @param array $classList * @param boolean $coverage */ public function runTests($classList, $coverage = false) { $startTime = microtime(true); // disable xdebug, as it messes up test execution if (function_exists('xdebug_disable')) { xdebug_disable(); } ini_set('max_execution_time', 0); $this->setUp(); // Optionally skip certain tests $skipTests = array(); if ($this->getRequest()->getVar('SkipTests')) { $skipTests = explode(',', $this->getRequest()->getVar('SkipTests')); } $abstractClasses = array(); foreach ($classList as $className) { // Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this. class_exists($className); $reflection = new ReflectionClass($className); if ($reflection->isAbstract()) { array_push($abstractClasses, $className); } } $classList = array_diff($classList, $skipTests, $abstractClasses); // run tests before outputting anything to the client $suite = new PHPUnit_Framework_TestSuite(); natcasesort($classList); foreach ($classList as $className) { // Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this. class_exists($className); $suite->addTest(new SapphireTestSuite($className)); } // Remove the error handler so that PHPUnit can add its own restore_error_handler(); self::$default_reporter->writeHeader("SilverStripe Test Runner"); if (count($classList) > 1) { self::$default_reporter->writeInfo("All Tests", "Running test cases: ", implode(", ", $classList)); } elseif (count($classList) == 1) { self::$default_reporter->writeInfo(reset($classList), ''); } else { // border case: no tests are available. self::$default_reporter->writeInfo('', ''); } // perform unit tests (use PhpUnitWrapper or derived versions) $phpunitwrapper = PhpUnitWrapper::inst(); $phpunitwrapper->setSuite($suite); $phpunitwrapper->setCoverageStatus($coverage); // Make sure TearDown is called (even in the case of a fatal error) $self = $this; register_shutdown_function(function () use($self) { $self->tearDown(); }); $phpunitwrapper->runTests(); // get results of the PhpUnitWrapper class $reporter = $phpunitwrapper->getReporter(); $results = $phpunitwrapper->getFrameworkTestResults(); if (!Director::is_cli()) { echo '<div class="trace">'; } $reporter->writeResults(); $endTime = microtime(true); if (Director::is_cli()) { echo "\n\nTotal time: " . round($endTime - $startTime, 3) . " seconds\n"; } else { echo "<p class=\"total-time\">Total time: " . round($endTime - $startTime, 3) . " seconds</p>\n"; } if (!Director::is_cli()) { echo '</div>'; } // Put the error handlers back $errorHandler = Injector::inst()->get('ErrorHandler'); $errorHandler->start(); if (!Director::is_cli()) { self::$default_reporter->writeFooter(); } $this->tearDown(); // Todo: we should figure out how to pass this data back through Director more cleanly if (Director::is_cli() && $results->failureCount() + $results->errorCount() > 0) { exit(2); } }
<?php require_once 'TestRunner.php'; PhpUnitWrapper::inst()->init(); /** * Test case class for the Sapphire framework. * Sapphire unit testing is based on PHPUnit, but provides a number of hooks into our data model that make it easier to work with. * * @package sapphire * @subpackage testing */ class SapphireTest extends PHPUnit_Framework_TestCase { /** * Path to fixture data for this test run. * If passed as an array, multiple fixture files will be loaded. * Please note that you won't be able to refer with "=>" notation * between the fixtures, they act independent of each other. * * @var string|array */ static $fixture_file = null; protected $originalMailer; protected $originalMemberPasswordValidator; protected $originalRequirements; protected $originalIsRunningTest; protected $originalTheme; protected $originalNestedURLsState; protected $originalMemoryLimit; protected $mailer; /**
/** * @param array $classList * @param boolean $coverage */ function runTests($classList, $coverage = false) { $startTime = microtime(true); // XDEBUG seem to cause problems with test execution :-( if (function_exists('xdebug_disable')) { xdebug_disable(); } ini_set('max_execution_time', 0); $this->setUp(); // Optionally skip certain tests $skipTests = array(); if ($this->request->getVar('SkipTests')) { $skipTests = explode(',', $this->request->getVar('SkipTests')); } $classList = array_diff($classList, $skipTests); // run tests before outputting anything to the client $suite = new PHPUnit_Framework_TestSuite(); natcasesort($classList); foreach ($classList as $className) { // Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this. class_exists($className); $suite->addTest(new SapphireTestSuite($className)); } // Remove the error handler so that PHPUnit can add its own restore_error_handler(); self::$default_reporter->writeHeader("Sapphire Test Runner"); if (count($classList) > 1) { self::$default_reporter->writeInfo("All Tests", "Running test cases: ", implode(", ", $classList)); } else { if (count($classList) == 1) { self::$default_reporter->writeInfo($classList[0], ""); } else { // border case: no tests are available. self::$default_reporter->writeInfo("", ""); } } // perform unit tests (use PhpUnitWrapper or derived versions) $phpunitwrapper = PhpUnitWrapper::inst(); $phpunitwrapper->setSuite($suite); $phpunitwrapper->setCoverageStatus($coverage); $phpunitwrapper->runTests(); // get results of the PhpUnitWrapper class $reporter = $phpunitwrapper->getReporter(); $results = $phpunitwrapper->getFrameworkTestResults(); if (!Director::is_cli()) { echo '<div class="trace">'; } $reporter->writeResults(); $endTime = microtime(true); if (Director::is_cli()) { echo "\n\nTotal time: " . round($endTime - $startTime, 3) . " seconds\n"; } else { echo "<p>Total time: " . round($endTime - $startTime, 3) . " seconds</p>\n"; } if (!Director::is_cli()) { echo '</div>'; } // Put the error handlers back Debug::loadErrorHandlers(); if (!Director::is_cli()) { self::$default_reporter->writeFooter(); } $this->tearDown(); // Todo: we should figure out how to pass this data back through Director more cleanly if (Director::is_cli() && $results->failureCount() + $results->errorCount() > 0) { exit(2); } }
/** * @uses i18nEntityProvider */ function collectFromEntityProviders($filePath, $module = null) { $entities = array(); // HACK Ugly workaround to avoid "Cannot redeclare class PHPUnit_Framework_TestResult" error // when running text collector with PHPUnit 3.4. There really shouldn't be any dependencies // here, but the class reflection enforces autloading of seemingly unrelated classes. // The main problem here is the CMSMenu class, which iterates through test classes, // which in turn trigger autoloading of PHPUnit. $phpunitwrapper = PhpUnitWrapper::inst(); $phpunitwrapper->init(); $classes = ClassInfo::classes_for_file($filePath); if ($classes) { foreach ($classes as $class) { // Not all classes can be instanciated without mandatory arguments, // so entity collection doesn't work for all SilverStripe classes currently // Requires PHP 5.1+ if (class_exists($class) && in_array('i18nEntityProvider', class_implements($class))) { $reflectionClass = new ReflectionClass($class); if ($reflectionClass->isAbstract()) { continue; } $obj = singleton($class); $entities = array_merge($entities, (array) $obj->provideI18nEntities()); } } } ksort($entities); return $entities; }