'browse', 'coverage' => 'coverage', 'startsession' => 'startsession', 'endsession' => 'endsession', 'cleanupdb' => 'cleanupdb', '$TestCase' => 'only', ); /** * Override the default reporter with a custom configured subclass. * * @param string $reporter */ static function set_reporter($reporter) { if (is_string($reporter)) $reporter = new $reporter; self::$default_reporter = $reporter; } function init() { parent::init(); ManifestBuilder::load_test_manifest(); if (!self::$default_reporter) self::set_reporter(Director::is_cli() ? 'CliDebugView' : 'DebugView'); } public function Link() { return Controller::join_links(Director::absoluteBaseURL(), 'dev/tests/'); } /** * Run all test classes */ function all() { if(hasPhpUnit()) { $tests = ClassInfo::subclassesFor('SapphireTest'); array_shift($tests); unset($tests['FunctionalTest']); $this->runTests($tests); } else { echo "Please install PHPUnit using pear"; } } /** * Browse all enabled test cases in the environment */ function browse() { self::$default_reporter->writeHeader(); self::$default_reporter->writeInfo('Available Tests', false); if(Director::is_cli()) { $tests = ClassInfo::subclassesFor('SapphireTest'); $relativeLink = Director::makeRelative($this->Link()); echo "sake {$relativeLink}all: Run all " . count($tests) . " tests\n"; echo "sake {$relativeLink}coverage: Runs all tests and make test coverage report\n"; foreach ($tests as $test) { echo "sake {$relativeLink}$test: Run $test\n"; } } else { echo '
Coverage reports available here:
"; } else { $suite->run($results); } if(!Director::is_cli()) echo 'You're in the middle of a test session; click here to end it.
"; } else if(!isset($_GET['fixture'])) { $me = Director::baseURL() . "/dev/tests/startsession"; return <<Enter a fixture file name to start a new test session. Don't forget to visit dev/tests/endsession when you're done!
Fixture file:
HTML; } else { $fixtureFile = $_GET['fixture']; // Validate fixture file $realFile = realpath('../' . $fixtureFile); $baseDir = realpath(Director::baseFolder()); if(!$realFile || !file_exists($realFile)) { return "Fixture file doesn't exist
"; } else if(substr($realFile,0,strlen($baseDir)) != $baseDir) { return "Fixture file must be inside $baseDir
"; } else if(substr($realFile,-4) != '.yml') { return "Fixture file must be a .yml file
"; } else if(!preg_match('/^([^\/.][^\/]+)\/tests\//', $fixtureFile)) { return "Fixture file must be inside the tests subfolder of one of your modules.
"; } $dbname = SapphireTest::create_temp_db(); DB::set_alternative_database_name($dbname); $fixture = new YamlFixture($_GET['fixture']); $fixture->saveIntoDatabase(); return "Started testing session with fixture '$fixtureFile'. Time to start testing; where would you like to start?
"; } } else { return "startession can only be used on dev and test sites
"; } } function endsession() { SapphireTest::kill_temp_db(); DB::set_alternative_database_name(null); return "Test session ended.
"; } function setUp() { SapphireTest::create_temp_db(); SSViewer::flush_template_cache(); } function tearDown() { SapphireTest::kill_temp_db(); DB::set_alternative_database_name(null); } } // This class is here to help with documentation. if(!hasPhpUnit()) { /** * PHPUnit is a testing framework that can be installed using PEAR. * It's not bundled with Sapphire, you will need to install it yourself. * * @package sapphire * @subpackage testing */ class PHPUnit_Framework_TestCase { } }