2008-10-29 14:19:01 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Controller that executes QUnit tests via jQuery.
|
|
|
|
* Finds all htm/html files located in <yourmodule>/javascript/tests
|
|
|
|
* and includes them as iFrames.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-29 14:19:01 +01:00
|
|
|
* To create your own tests, please use this template:
|
2009-03-22 23:59:14 +01:00
|
|
|
* <code>
|
2014-02-06 22:40:28 +01:00
|
|
|
* <!DOCTYPE html>
|
|
|
|
* <html id="html">
|
|
|
|
* <head>
|
|
|
|
* <title>jQuery - Validation Test Suite</title>
|
2014-08-15 08:53:05 +02:00
|
|
|
* <link rel="Stylesheet" media="screen"
|
2014-02-06 22:40:28 +01:00
|
|
|
* href="thirdparty/jquery-validate/test/qunit/qunit.css" />
|
2014-08-15 08:53:05 +02:00
|
|
|
* <script type="text/javascript"
|
2014-02-06 22:40:28 +01:00
|
|
|
* src="thirdparty/jquery-validate/lib/jquery.js"></script>
|
|
|
|
* <script type="text/javascript"
|
|
|
|
* src="thirdparty/jquery-validate/test/qunit/qunit.js"></script>
|
|
|
|
* <script>
|
|
|
|
* $(document).ready(function(){
|
|
|
|
* test("test my feature", function() {
|
|
|
|
* ok('mytest');
|
|
|
|
* });
|
|
|
|
* });
|
|
|
|
* </script>
|
|
|
|
* </head>
|
|
|
|
* <body id="body">
|
|
|
|
* <h1 id="qunit-header">
|
|
|
|
* <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">
|
|
|
|
* jQuery Validation Plugin</a> Test Suite</h1>
|
|
|
|
* <h2 id="qunit-banner"></h2>
|
|
|
|
* <div id="qunit-testrunner-toolbar"></div>
|
|
|
|
* <h2 id="qunit-userAgent"></h2>
|
|
|
|
* <ol id="qunit-tests"></ol>
|
|
|
|
* </body>
|
2008-10-29 14:19:01 +01:00
|
|
|
* </html>
|
2009-03-22 23:59:14 +01:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-10-29 14:19:01 +01:00
|
|
|
* @subpackage testing
|
|
|
|
*/
|
|
|
|
class JSTestRunner extends Controller {
|
|
|
|
/** @ignore */
|
|
|
|
private static $default_reporter;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2008-10-29 14:19:01 +01:00
|
|
|
'' => 'browse',
|
|
|
|
'$TestCase' => 'only',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2011-02-13 23:14:51 +01:00
|
|
|
'index',
|
|
|
|
'all',
|
|
|
|
'browse',
|
|
|
|
'only'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
/**
|
|
|
|
* Override the default reporter with a custom configured subclass.
|
|
|
|
*
|
|
|
|
* @param string $reporter
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_reporter($reporter) {
|
2008-10-29 14:19:01 +01:00
|
|
|
if (is_string($reporter)) $reporter = new $reporter;
|
|
|
|
self::$default_reporter = $reporter;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function init() {
|
2008-10-29 14:19:01 +01:00
|
|
|
parent::init();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
if(Director::is_cli()) {
|
|
|
|
echo "Error: JSTestRunner cannot be run in CLI mode\n";
|
|
|
|
die();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
if (!self::$default_reporter) self::set_reporter('DebugView');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
public function Link() {
|
|
|
|
return Controller::join_links(Director::absoluteBaseURL(), 'dev/jstests/');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
/**
|
|
|
|
* Run all test classes
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function all() {
|
2008-10-29 14:19:01 +01:00
|
|
|
$this->runTests(array_keys($this->getAllTestFiles()));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
/**
|
|
|
|
* Browse all enabled test cases in the environment
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function browse() {
|
2008-10-29 14:19:01 +01:00
|
|
|
self::$default_reporter->writeHeader();
|
|
|
|
echo '<div class="info">';
|
|
|
|
echo '<h1>Available Tests</h1>';
|
|
|
|
echo '</div>';
|
|
|
|
echo '<div class="trace">';
|
|
|
|
$tests = $this->getAllTestFiles();
|
|
|
|
echo "<h3><a href=\"" . $this->Link() . "all\">Run all " . count($tests) . " tests</a></h3>";
|
|
|
|
echo "<hr />";
|
|
|
|
foreach ($tests as $testName => $testFilePath) {
|
|
|
|
echo "<h3><a href=\"" . $this->Link() . "$testName\">Run $testName</a></h3>";
|
|
|
|
}
|
|
|
|
echo '</div>';
|
|
|
|
self::$default_reporter->writeFooter();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
/**
|
|
|
|
* Run only a single test class
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function only($request) {
|
2008-10-29 14:19:01 +01:00
|
|
|
$test = $request->param('TestCase');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
if ($test == 'all') {
|
|
|
|
$this->all();
|
|
|
|
} else {
|
|
|
|
$allTests = $this->getAllTestFiles();
|
|
|
|
if(!array_key_exists($test, $allTests)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("TestRunner::only(): Invalid TestCase '$className', cannot find matching class",
|
|
|
|
E_USER_ERROR);
|
2008-10-29 14:19:01 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
$this->runTests(array($test));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function runTests($tests) {
|
2008-10-29 14:19:01 +01:00
|
|
|
$this->setUp();
|
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
self::$default_reporter->writeHeader("SilverStripe JavaScript Test Runner");
|
2008-10-29 14:19:01 +01:00
|
|
|
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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
echo '<iframe src="' . $absTestUrl . '" width="800" height="300"></iframe>';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
$this->tearDown();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2008-10-29 14:19:01 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function tearDown() {
|
2008-10-29 14:19:01 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
protected function getAllTestFiles() {
|
|
|
|
$testFiles = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
$baseDir = Director::baseFolder();
|
|
|
|
$modules = scandir($baseDir);
|
|
|
|
foreach($modules as $moduleFileOrFolder) {
|
|
|
|
if(
|
2014-08-15 08:53:05 +02:00
|
|
|
$moduleFileOrFolder[0] == '.'
|
|
|
|
|| !@is_dir("$baseDir/$moduleFileOrFolder")
|
2008-10-29 14:19:01 +01:00
|
|
|
|| !file_exists("$baseDir/$moduleFileOrFolder/_config.php")
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-01 12:05:38 +01:00
|
|
|
$testDir = "$baseDir/$moduleFileOrFolder/tests/javascript";
|
2008-10-29 14:19:01 +01:00
|
|
|
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;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 14:19:01 +01:00
|
|
|
/**
|
|
|
|
* Returns the URL for a test case file.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-29 14:19:01 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function urlForTestCase($testName) {
|
|
|
|
$allTests = $this->getAllTestFiles();
|
|
|
|
return (array_key_exists($testName, $allTests)) ? $allTests[$testName] : false;
|
|
|
|
}
|
|
|
|
}
|