diff options
author | diosmosis <benakamoorthi@fastmail.fm> | 2014-03-11 18:32:47 +0400 |
---|---|---|
committer | diosmosis <benakamoorthi@fastmail.fm> | 2014-03-11 18:32:47 +0400 |
commit | cf4584b1a00bbeb62c4337db93e96af97adc4322 (patch) | |
tree | 341dad008cd60efe89c173f2f427e5662d9d95ad /tests/lib | |
parent | d075218248175b3bfd5bdfe535ab6aa8d1e2337f (diff) |
Allow screenshot tests to specify their own fixture and update UI submodule.
Diffstat (limited to 'tests/lib')
4 files changed, 106 insertions, 70 deletions
diff --git a/tests/lib/screenshot-testing/support/app.js b/tests/lib/screenshot-testing/support/app.js index c1161a3397..d99a3fa7db 100644 --- a/tests/lib/screenshot-testing/support/app.js +++ b/tests/lib/screenshot-testing/support/app.js @@ -100,6 +100,22 @@ Application.prototype.loadTestModules = function () { return options.tests.indexOf(suite.title) != -1; }); } + + // configure suites (auto-add fixture setup/teardown) + mocha.suite.suites.forEach(function (suite) { + var fixture = suite.fixture || 'UITestFixture'; + + suite.beforeAll(function (done) { + testEnvironment.setupFixture(fixture, done); + }); + + // move to before other hooks + suite._beforeAll.unshift(suite._beforeAll.pop()); + + suite.afterAll(function (done) { + testEnvironment.teardownFixture(fixture, done); + }); + }); }; Application.prototype.runTests = function () { @@ -126,42 +142,7 @@ Application.prototype.runTests = function () { } }); - this.setupDatabase(); -}; - -Application.prototype.setupDatabase = function () { - console.log("Setting up database..."); - - var self = this, - setupFile = path.join("./support", "setupDatabase.php"), - processArgs = [setupFile, "--server=" + JSON.stringify(config.phpServer)]; - - if (options['persist-fixture-data']) { - processArgs.push('--persist-fixture-data'); - } - - if (options['drop']) { - processArgs.push('--drop'); - } - - var child = require('child_process').spawn(config.php, processArgs); - - child.stdout.on("data", function (data) { - fs.write("/dev/stdout", data, "w"); - }); - - child.stderr.on("data", function (data) { - fs.write("/dev/stderr", data, "w"); - }); - - child.on("exit", function (code) { - if (code) { - console.log("\nERROR: Failed to setup database!"); - phantom.exit(-1); - } else { - self.doRunTests(); - } - }); + this.doRunTests(); }; Application.prototype.doRunTests = function () { @@ -186,42 +167,12 @@ Application.prototype.doRunTests = function () { // build diffviewer self.diffViewerGenerator.checkImageMagickCompare(function () { self.diffViewerGenerator.generate(function () { - if (options['persist-fixture-data']) { - self.finish(); - } else { - // teardown database - self.tearDownDatabase(); - } + self.finish(); }); }); }); }; -Application.prototype.tearDownDatabase = function () { - console.log("Tearing down database..."); - - var self = this, - teardownFile = path.join("./support", "teardownDatabase.php"), - child = require('child_process').spawn(config.php, [teardownFile, "--server=" + JSON.stringify(config.phpServer)]); - - child.stdout.on("data", function (data) { - fs.write("/dev/stdout", data, "w"); - }); - - child.stderr.on("data", function (data) { - fs.write("/dev/stderr", data, "w"); - }); - - child.on("exit", function (code) { - if (code) { - console.log("\nERROR: Failed to teardown database!"); - phantom.exit(-2); - } else { - self.finish(); - } - }); -}; - Application.prototype.finish = function () { phantom.exit(this.runner.failures); }; diff --git a/tests/lib/screenshot-testing/support/setupDatabase.php b/tests/lib/screenshot-testing/support/setupDatabase.php index db926e5163..e91e4ffd16 100644 --- a/tests/lib/screenshot-testing/support/setupDatabase.php +++ b/tests/lib/screenshot-testing/support/setupDatabase.php @@ -6,18 +6,25 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ +$fixtureClass = "UITestFixture"; foreach ($argv as $arg) { if (strpos($arg, "--server=") === 0) { $serverStr = substr($arg, strlen("--server=")); $_SERVER = json_decode($serverStr, true); + } else if (strpos($arg, "--fixture=") === 0) { + $fixtureClass = substr($arg, strlen("--fixture=")); } } require_once "PHPUnit/Autoload.php"; require_once dirname(__FILE__) . "/../../../PHPUnit/bootstrap.php"; -$fixture = new \Piwik\Tests\Fixtures\UITestFixture(); +if (!class_exists($fixtureClass)) { + $fixtureClass = "Piwik\\Tests\\Fixtures\\$fixtureClass"; +} + +$fixture = new $fixtureClass(); if (in_array("--persist-fixture-data", $argv)) { $fixture->persistFixtureData = true; } diff --git a/tests/lib/screenshot-testing/support/teardownDatabase.php b/tests/lib/screenshot-testing/support/teardownDatabase.php index 45a07c91aa..3ef3e8e1f7 100644 --- a/tests/lib/screenshot-testing/support/teardownDatabase.php +++ b/tests/lib/screenshot-testing/support/teardownDatabase.php @@ -6,18 +6,25 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ +$fixtureClass = "UITestFixture"; foreach ($argv as $arg) { if (strpos($arg, "--server=") === 0) { $serverStr = substr($arg, strlen("--server=")); - + $_SERVER = json_decode($serverStr, true); + } else if (strpos($arg, "--fixture=") === 0) { + $fixtureClass = substr($arg, strlen("--fixture=")); } } require_once "PHPUnit/Autoload.php"; require_once dirname(__FILE__) . "/../../../PHPUnit/bootstrap.php"; -$fixture = new \Piwik\Tests\Fixtures\UITestFixture(); +if (!class_exists($fixtureClass)) { + $fixtureClass = "Piwik\\Tests\\Fixtures\\$fixtureClass"; +} + +$fixture = new $fixtureClass(); $fixture->dropDatabaseInSetUp = false; $fixture->printToScreen = true; $fixture->performSetUp("", $environmentOnly = true); diff --git a/tests/lib/screenshot-testing/support/test-environment.js b/tests/lib/screenshot-testing/support/test-environment.js index 7130207c38..c371d444e3 100644 --- a/tests/lib/screenshot-testing/support/test-environment.js +++ b/tests/lib/screenshot-testing/support/test-environment.js @@ -82,4 +82,75 @@ TestingEnvironment.prototype._call = function (params, done) { }); }; +TestingEnvironment.prototype.setupFixture = function (fixtureClass, done) { + this.backup = JSON.stringify(this); + + console.log(" Setting up fixture " + fixtureClass + "..."); + + var setupFile = path.join("./support", "setupDatabase.php"), + processArgs = [setupFile, "--server=" + JSON.stringify(config.phpServer), "--fixture=" + fixtureClass]; + + if (options['persist-fixture-data']) { + processArgs.push('--persist-fixture-data'); + } + + if (options['drop']) { + processArgs.push('--drop'); + } + + var child = require('child_process').spawn(config.php, processArgs); + + child.stdout.on("data", function (data) { + fs.write("/dev/stdout", data, "w"); + }); + + child.stderr.on("data", function (data) { + fs.write("/dev/stderr", data, "w"); + }); + + child.on("exit", function (code) { + if (code) { + done(new Error("Failed to setup fixture " + fixtureClass + " (error code = " + code + ")")); + } else { + done(); + } + }); +}; + +TestingEnvironment.prototype.teardownFixture = function (fixtureClass, done) { + if (options['persist-fixture-data']) { + done(); + return; + } + + console.log(" Tearing down fixture " + fixtureClass + "..."); + + var teardownFile = path.join("./support", "teardownDatabase.php"), + child = require('child_process').spawn( + config.php, [teardownFile, "--server=" + JSON.stringify(config.phpServer), "--fixture=" + fixtureClass]); + + child.stdout.on("data", function (data) { + fs.write("/dev/stdout", data, "w"); + }); + + child.stderr.on("data", function (data) { + fs.write("/dev/stderr", data, "w"); + }); + + var self = this; + child.on("exit", function (code) { + for (var key in self.backup) { + self[key] = self.backup[key]; + } + + self.backup = {}; + + if (code) { + done(new Error("Failed to teardown fixture " + fixtureClass + " (error code = " + code + ")")); + } else { + done(); + } + }); +}; + exports.TestingEnvironment = new TestingEnvironment();
\ No newline at end of file |