mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FEATURE Added JavaScript unit tests with jQuery QUnit. Can be viewed similiarly to PHPUnit tests through dev/jstests URL. Uses an <iframe> to include all tests.
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@64877 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
c013b02dc8
commit
1c435dfaee
@ -21,6 +21,8 @@ class DevelopmentAdmin extends Controller {
|
|||||||
"build" => "Build/rebuild this environment (formerly db/build). Call this whenever you have updated your project sources",
|
"build" => "Build/rebuild this environment (formerly db/build). Call this whenever you have updated your project sources",
|
||||||
"tests" => "See a list of unit tests to run",
|
"tests" => "See a list of unit tests to run",
|
||||||
"tests/all" => "Run all tests",
|
"tests/all" => "Run all tests",
|
||||||
|
"jstests" => "See a list of JavaScript tests to run",
|
||||||
|
"jstests/all" => "Run all JavaScript tests",
|
||||||
"modules/add" => "Add a module, for example, 'sake dev/modules/add ecommerce'",
|
"modules/add" => "Add a module, for example, 'sake dev/modules/add ecommerce'",
|
||||||
"tasks" => "See a list of build tasks to run",
|
"tasks" => "See a list of build tasks to run",
|
||||||
"viewcode" => "Read source code in a literate programming style",
|
"viewcode" => "Read source code in a literate programming style",
|
||||||
@ -58,6 +60,10 @@ class DevelopmentAdmin extends Controller {
|
|||||||
return new TestRunner();
|
return new TestRunner();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function jstests($request) {
|
||||||
|
return new JSTestRunner();
|
||||||
|
}
|
||||||
|
|
||||||
function tasks() {
|
function tasks() {
|
||||||
return new TaskRunner();
|
return new TaskRunner();
|
||||||
}
|
}
|
||||||
|
179
dev/JSTestRunner.php
Normal file
179
dev/JSTestRunner.php
Normal file
@ -0,0 +1,179 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Controller that executes QUnit tests via jQuery.
|
||||||
|
* Finds all htm/html files located in <yourmodule>/javascript/tests
|
||||||
|
* and includes them as iFrames.
|
||||||
|
*
|
||||||
|
* To create your own tests, please use this template:
|
||||||
|
* <example>
|
||||||
|
* <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||||
|
* <html>
|
||||||
|
* <head>
|
||||||
|
* <script src="http://code.jquery.com/jquery-latest.js"></script>
|
||||||
|
* <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/qunit/testsuite.css" type="text/css" media="screen" />
|
||||||
|
* <script>
|
||||||
|
* $(document).ready(function(){
|
||||||
|
* test("test my feature", function() {
|
||||||
|
* ok('mytest');
|
||||||
|
* });
|
||||||
|
* });
|
||||||
|
* </script>
|
||||||
|
* </head>
|
||||||
|
* <body>
|
||||||
|
* <script type="text/javascript" src="http://jqueryjs.googlecode.com/svn/trunk/qunit/testrunner.js"></script>
|
||||||
|
* <h1>My Test Name</h1>
|
||||||
|
* <h2 id="banner"></h2>
|
||||||
|
* <h2 id="userAgent"></h2>
|
||||||
|
* <ol id="tests"></ol>
|
||||||
|
* <div id="main"></div>
|
||||||
|
* </body>
|
||||||
|
* </html>
|
||||||
|
* </example>
|
||||||
|
*
|
||||||
|
* @package sapphire
|
||||||
|
* @subpackage testing
|
||||||
|
*/
|
||||||
|
class JSTestRunner extends Controller {
|
||||||
|
/** @ignore */
|
||||||
|
private static $default_reporter;
|
||||||
|
|
||||||
|
static $url_handlers = array(
|
||||||
|
'' => 'browse',
|
||||||
|
'$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();
|
||||||
|
|
||||||
|
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 '<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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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("Sapphire 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 '<iframe src="' . $absTestUrl . '" width="800" height="300"></iframe>';
|
||||||
|
}
|
||||||
|
|
||||||
|
$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/javascript/tests";
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user