/javascript/tests * and includes them as iFrames. * * To create your own tests, please use this template: * * * * * * * * * * *

My Test Name

* *

*
    *
    * * *
    * * @package framework * @subpackage testing */ class JSTestRunner extends Controller { /** @ignore */ private static $default_reporter; static $url_handlers = array( '' => 'browse', '$TestCase' => 'only', ); static $allowed_actions = array( 'index', 'all', 'browse', '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(); if(Director::is_cli()) { echo "Error: JSTestRunner cannot be run in CLI mode\n"; die(); } if (!self::$default_reporter) self::set_reporter('DebugView'); } public function Link() { return Controller::join_links(Director::absoluteBaseURL(), 'dev/jstests/'); } /** * Run all test classes */ function all() { $this->runTests(array_keys($this->getAllTestFiles())); } /** * Browse all enabled test cases in the environment */ function browse() { self::$default_reporter->writeHeader(); echo '
    '; echo '

    Available Tests

    '; echo '
    '; echo '
    '; $tests = $this->getAllTestFiles(); echo "

    Link() . "all\">Run all " . count($tests) . " tests

    "; echo "
    "; foreach ($tests as $testName => $testFilePath) { echo "

    Link() . "$testName\">Run $testName

    "; } echo '
    '; self::$default_reporter->writeFooter(); } /** * Run only a single test class */ function only($request) { $test = $request->param('TestCase'); if ($test == 'all') { $this->all(); } else { $allTests = $this->getAllTestFiles(); if(!array_key_exists($test, $allTests)) { user_error("TestRunner::only(): Invalid TestCase '$className', cannot find matching class", E_USER_ERROR); } $this->runTests(array($test)); } } function runTests($tests) { $this->setUp(); self::$default_reporter->writeHeader("SilverStripe JavaScript Test Runner"); self::$default_reporter->writeInfo("All Tests", "Running test cases: " . implode(", ", $tests)); foreach($tests as $test) { // @todo Integrate output in DebugView $testUrl = $this->urlForTestCase($test); if(!$testUrl) user_error('JSTestRunner::runTests(): Test ' . $test . ' not found', E_USER_ERROR); $absTestUrl = Director::absoluteBaseURL() . $testUrl; echo ''; } $this->tearDown(); } function setUp() { } function tearDown() { } protected function getAllTestFiles() { $testFiles = array(); $baseDir = Director::baseFolder(); $modules = scandir($baseDir); foreach($modules as $moduleFileOrFolder) { if( $moduleFileOrFolder[0] == '.' || !@is_dir("$baseDir/$moduleFileOrFolder") || !file_exists("$baseDir/$moduleFileOrFolder/_config.php") ) { continue; } $testDir = "$baseDir/$moduleFileOrFolder/tests/javascript"; if(@is_dir($testDir)) { $tests = scandir($testDir); foreach($tests as $testFile) { $testFileExt = pathinfo("$testDir/$testFile", PATHINFO_EXTENSION); if(!in_array(strtolower($testFileExt),array('htm','html'))) continue; $testFileNameWithoutExt = substr($testFile, 0,-strlen($testFileExt)-1); $testUrl = Director::makeRelative("$testDir/$testFile"); $testUrl = substr($testUrl, 1); // @todo Limit to html extension with "Test" suffix $testFiles[$testFileNameWithoutExt] = $testUrl; } } } return $testFiles; } /** * Returns the URL for a test case file. * * @return string */ protected function urlForTestCase($testName) { $allTests = $this->getAllTestFiles(); return (array_key_exists($testName, $allTests)) ? $allTests[$testName] : false; } }