diff --git a/plugins/CoreConsole/Commands/SetupFixture.php b/plugins/CoreConsole/Commands/SetupFixture.php index 513e828fa96ae9603e40c34f3db92060ded1ee9d..41f0da6595c9bcf47523d21191d2c07d884c9da5 100644 --- a/plugins/CoreConsole/Commands/SetupFixture.php +++ b/plugins/CoreConsole/Commands/SetupFixture.php @@ -19,8 +19,6 @@ use Symfony\Component\Console\Output\OutputInterface; /** * Console commands that sets up a fixture either in a local MySQL database or a remote one. - * - * TODO: use this console command in UI tests instead of setUpDatabase.php/tearDownDatabase.php scripts */ class SetupFixture extends ConsoleCommand { @@ -46,13 +44,23 @@ class SetupFixture extends ConsoleCommand $this->addOption('teardown', null, InputOption::VALUE_NONE, "If specified, the fixture will be torn down and the database deleted. Won't work if the --db-name " . "option isn't supplied."); + $this->addOption('persist-fixture-data', null, InputOption::VALUE_NONE, + "If specified, the database will not be dropped after the fixture is setup. If the database already " . + "and the fixture was successfully setup before, nothing will happen."); + $this->addOption('drop', null, InputOption::VALUE_NONE, + "Forces the database to be dropped before setting up the fixture. Should be used in conjunction with" . + " --persist-fixture-data when updating a pre-existing test database."); + $this->addOption('set-phantomjs-symlinks', null, InputOption::VALUE_NONE, + "Used by UI tests. Creates symlinks to root directory in tests/PHPUnit/proxy."); + $this->addOption('server-global', null, InputOption::VALUE_REQUIRED, + "Used by UI tests. Sets the \$_SERVER global variable from a JSON string."); } protected function execute(InputInterface $input, OutputInterface $output) { - $dbName = $input->getOption('db-name'); - if (!$dbName) { - throw new \Exception("Required argument --db-name is not set."); + $serverGlobal = $input->getOption('server-global'); + if ($serverGlobal) { + $_SERVER = json_decode($serverGlobal, true); } $this->requireFixtureFiles(); @@ -86,9 +94,21 @@ class SetupFixture extends ConsoleCommand // create the fixture $fixture = new $fixtureClass(); - $fixture->dbName = $dbName; $fixture->printToScreen = true; + $dbName = $input->getOption('db-name'); + if ($dbName) { + $fixture->dbName = $dbName; + } + + if ($input->getOption('persist-fixture-data')) { + $fixture->persistFixtureData = true; + } + + if ($input->getOption('drop')) { + $fixture->resetPersistedFixture = true; + } + Config::getInstance()->setTestEnvironment(); $fixture->createConfig = false; @@ -96,7 +116,7 @@ class SetupFixture extends ConsoleCommand $testingEnvironment = $fixture->getTestEnvironment(); $optionsToOverride = array( - 'dbname' => $dbName, + 'dbname' => $fixture->getDbName(), 'host' => $input->getOption('db-host'), 'user' => $input->getOption('db-user'), 'password' => $input->getOption('db-pass') @@ -119,6 +139,16 @@ class SetupFixture extends ConsoleCommand $fixture->performSetUp(); } + if ($input->getOption('set-phantomjs-symlinks')) { + // make sure symbolic links exist (phantomjs doesn't support symlink-ing yet) + foreach (array('libs', 'plugins', 'tests', 'piwik.js') as $linkName) { + $linkPath = PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/' . $linkName; + if (!file_exists($linkPath)) { + symlink(PIWIK_INCLUDE_PATH . '/' . $linkName, $linkPath); + } + } + } + $this->writeSuccessMessage($output, array("Fixture successfully setup!")); } @@ -153,4 +183,4 @@ class SetupFixture extends ConsoleCommand @ini_set('include_path', PIWIK_INCLUDE_SEARCH_PATH); @set_include_path(PIWIK_INCLUDE_SEARCH_PATH); } -} \ No newline at end of file +} diff --git a/tests/PHPUnit/Fixture.php b/tests/PHPUnit/Fixture.php index 2a5de7dd66d4c174d64ec757cc551f1d033ca32e..e15cc1f1b36859d7a0c62905b1a8ced4790c647b 100644 --- a/tests/PHPUnit/Fixture.php +++ b/tests/PHPUnit/Fixture.php @@ -81,6 +81,19 @@ class Fixture extends PHPUnit_Framework_Assert // empty } + public function getDbName() + { + if ($this->dbName !== false) { + return $this->dbName; + } + + if ($this->persistFixtureData) { + return str_replace("\\", "_", get_class($this)); + } + + return Config::getInstance()->database_tests['dbname']; + } + public function performSetUp($setupEnvironmentOnly = false) { try { @@ -88,8 +101,9 @@ class Fixture extends PHPUnit_Framework_Assert Config::getInstance()->setTestEnvironment(); } + $this->dbName = $this->getDbName(); + if ($this->persistFixtureData) { - $this->dbName = str_replace("\\", "_", get_class($this)); $this->dropDatabaseInSetUp = false; $this->dropDatabaseInTearDown = false; $this->overwriteExisting = false; @@ -109,8 +123,6 @@ class Fixture extends PHPUnit_Framework_Assert if ($this->dropDatabaseInSetUp || $this->resetPersistedFixture ) { - $this->log("Dropping database..."); - $this->dropDatabase(); } @@ -135,7 +147,7 @@ class Fixture extends PHPUnit_Framework_Assert include "DataFiles/Currencies.php"; include "DataFiles/LanguageToCountry.php"; include "DataFiles/Providers.php"; - + if (!$this->isFixtureSetUp()) { DbHelper::truncateAllTables(); } @@ -157,9 +169,9 @@ class Fixture extends PHPUnit_Framework_Assert Translate::reloadLanguage('en'); APILanguageManager::getInstance()->setLanguageForUser('superUserLogin', 'en'); } - + FakeAccess::$superUserLogin = 'superUserLogin'; - + \Piwik\SettingsPiwik::$cachedKnownSegmentsToArchive = null; \Piwik\CacheFile::$invalidateOpCacheBeforeRead = true; @@ -669,7 +681,7 @@ class Fixture extends PHPUnit_Framework_Assert return Db::fetchOne("SELECT COUNT(*) FROM " . Common::prefixTable('goal') . " WHERE idgoal = ? AND idsite = ?", array($idGoal, $idSite)) != 0; } - + /** * Connects to MySQL w/o specifying a database. */ @@ -698,6 +710,8 @@ class Fixture extends PHPUnit_Framework_Assert { $dbName = $dbName ?: $this->dbName; + $this->log("Dropping database '$dbName'..."); + $config = _parse_ini_file(PIWIK_INCLUDE_PATH . '/config/config.ini.php', true); $originalDbName = $config['database']['dbname']; if ($dbName == $originalDbName @@ -733,7 +747,6 @@ class Test_Piwik_BaseFixture extends Fixture { } - // needed by tests that use stored segments w/ the proxy index.php class Test_Access_OverrideLogin extends Access { diff --git a/tests/lib/screenshot-testing/support/setupDatabase.php b/tests/lib/screenshot-testing/support/setupDatabase.php deleted file mode 100644 index 65567073540d1b69bcb39320b6aef888ab56142c..0000000000000000000000000000000000000000 --- a/tests/lib/screenshot-testing/support/setupDatabase.php +++ /dev/null @@ -1,47 +0,0 @@ -<?php -/** - * Piwik - Open source web analytics - * - * @link http://piwik.org - * @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"; - -if (!empty($fixtureClass)) { - if (!class_exists($fixtureClass)) { - $fixtureClass = "Piwik\\Tests\\Fixtures\\$fixtureClass"; - } - - $fixture = new $fixtureClass(); - if (in_array("--persist-fixture-data", $argv) - && get_class($fixture) != "Fixture" - ) { - $fixture->persistFixtureData = true; - } - if (in_array("--drop", $argv)) { - $fixture->resetPersistedFixture = true; - } - $fixture->printToScreen = true; - $fixture->performSetUp(); -} - -// make sure symbolic links exist (phantomjs doesn't support symlink-ing yet) -foreach (array('libs', 'plugins', 'tests', 'piwik.js') as $linkName) { - $linkPath = PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/' . $linkName; - if (!file_exists($linkPath)) { - symlink(PIWIK_INCLUDE_PATH . '/' . $linkName, $linkPath); - } -} \ No newline at end of file diff --git a/tests/lib/screenshot-testing/support/teardownDatabase.php b/tests/lib/screenshot-testing/support/teardownDatabase.php deleted file mode 100644 index 572e3d9608d1d7e8520d74d21bcb9d152141c6b7..0000000000000000000000000000000000000000 --- a/tests/lib/screenshot-testing/support/teardownDatabase.php +++ /dev/null @@ -1,31 +0,0 @@ -<?php -/** - * Piwik - Open source web analytics - * - * @link http://piwik.org - * @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"; - -if (!class_exists($fixtureClass)) { - $fixtureClass = "Piwik\\Tests\\Fixtures\\$fixtureClass"; -} - -$fixture = new $fixtureClass(); -$fixture->dropDatabaseInSetUp = false; -$fixture->printToScreen = true; -$fixture->performSetUp($environmentOnly = true); -$fixture->performTearDown(""); \ No newline at end of file diff --git a/tests/lib/screenshot-testing/support/test-environment.js b/tests/lib/screenshot-testing/support/test-environment.js index 4d3e041f773766c8aab13191daf6964f2594acfe..e7a0f1f44ebecbf6ed327702e376fca2ba61df29 100644 --- a/tests/lib/screenshot-testing/support/test-environment.js +++ b/tests/lib/screenshot-testing/support/test-environment.js @@ -91,27 +91,10 @@ TestingEnvironment.prototype._call = function (params, done) { }); }; -var droppedOnce = false; -TestingEnvironment.prototype.setupFixture = function (fixtureClass, done) { - console.log(" Setting up fixture " + fixtureClass + "..."); - - testEnvironment.deleteAndSave(); - - 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'] - && !droppedOnce - ) { - processArgs.push('--drop'); - droppedOnce = true; - } - - var child = require('child_process').spawn(config.php, processArgs); +TestingEnvironment.prototype.executeConsoleCommand = function (command, args, callback) { + var consoleFile = path.join(PIWIK_INCLUDE_PATH, 'console'), + commandArgs = [consoleFile, command].concat(args), + child = require('child_process').spawn(config.php, commandArgs); var firstLine = true; child.stdout.on("data", function (data) { @@ -132,11 +115,34 @@ TestingEnvironment.prototype.setupFixture = function (fixtureClass, done) { fs.write("/dev/stderr", data, "w"); }); - child.on("exit", function (code) { - testEnvironment.reload(); + child.on("exit", callback); +}; + +var droppedOnce = false; +TestingEnvironment.prototype.setupFixture = function (fixtureClass, done) { + console.log(" Setting up fixture " + fixtureClass + "..."); + + this.deleteAndSave(); + + var args = [fixtureClass || "UITestFixture", '--set-phantomjs-symlinks', '--server-global=' + JSON.stringify(config.phpServer)]; + + if (options['persist-fixture-data']) { + args.push('--persist-fixture-data'); + } + + if (options['drop'] + && !droppedOnce + ) { + args.push('--drop'); + droppedOnce = true; + } + + var self = this; + this.executeConsoleCommand('tests:setup-fixture', args, function (code) { + self.reload(); console.log(); - + if (code) { done(new Error("Failed to setup fixture " + fixtureClass + " (error code = " + code + ")")); } else { @@ -156,37 +162,14 @@ TestingEnvironment.prototype.teardownFixture = function (fixtureClass, done) { console.log(); 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]); - - var firstLine = true; - child.stdout.on("data", function (data) { - if (firstLine) { - data = " " + data; - firstLine = false; - } - - fs.write("/dev/stdout", data.replace(/\n/g, "\n "), "w"); - }); - - child.stderr.on("data", function (data) { - if (firstLine) { - data = " " + data; - firstLine = false; - } - - fs.write("/dev/stderr", data, "w"); - }); - - var self = this; - child.on("exit", function (code) { + var args = [fixtureClass || "UITestFixture", "--teardown", '--server-global=' + JSON.stringify(config.phpServer)]; + this.executeConsoleCommand('tests:setup-fixture', args, function (code) { if (code) { done(new Error("Failed to teardown fixture " + fixtureClass + " (error code = " + code + ")")); } else { done(); } - }); + }) }; TestingEnvironment.prototype.deleteAndSave = function () { @@ -194,4 +177,4 @@ TestingEnvironment.prototype.deleteAndSave = function () { this.reload(); }; -exports.TestingEnvironment = new TestingEnvironment(); \ No newline at end of file +exports.TestingEnvironment = new TestingEnvironment();