diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2014-10-28 22:39:14 +0300 |
---|---|---|
committer | Thomas Steur <tsteur@users.noreply.github.com> | 2014-10-28 22:39:14 +0300 |
commit | 29810a54a3a454c5aecb517bfce96aafa7d23ffb (patch) | |
tree | 35cf86ac1c25904879718be36b4817aa78422d0a /plugins/CoreConsole | |
parent | 2061e35f44d1e203fb2f4d8694c9940491a30cb6 (diff) | |
parent | 4e9eb794899bb2d096385e6df01714194c36e29c (diff) |
Merge pull request #6540 from piwik/testsOnAws
Added a test runner that launches tests on AWS
Diffstat (limited to 'plugins/CoreConsole')
-rw-r--r-- | plugins/CoreConsole/Commands/TestsRun.php | 206 | ||||
-rw-r--r-- | plugins/CoreConsole/Commands/TestsRunUI.php | 83 | ||||
-rw-r--r-- | plugins/CoreConsole/Commands/TestsSetupFixture.php | 273 |
3 files changed, 0 insertions, 562 deletions
diff --git a/plugins/CoreConsole/Commands/TestsRun.php b/plugins/CoreConsole/Commands/TestsRun.php deleted file mode 100644 index e8b5e3ffec..0000000000 --- a/plugins/CoreConsole/Commands/TestsRun.php +++ /dev/null @@ -1,206 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * - */ - -namespace Piwik\Plugins\CoreConsole\Commands; - -use Piwik\Common; -use Piwik\Profiler; -use Piwik\Plugin\ConsoleCommand; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * Executes PHP tests. - */ -class TestsRun extends ConsoleCommand -{ - private $returnVar = 0; - - protected function configure() - { - $this->setName('tests:run'); - $this->setDescription('Run Piwik PHPUnit tests one testsuite after the other'); - $this->addArgument('group', InputArgument::OPTIONAL, 'Run only a specific test group. Separate multiple groups by comma, for instance core,plugins', ''); - $this->addOption('options', 'o', InputOption::VALUE_OPTIONAL, 'All options will be forwarded to phpunit', ''); - $this->addOption('xhprof', null, InputOption::VALUE_NONE, 'Profile using xhprof.'); - $this->addOption('file', null, InputOption::VALUE_REQUIRED, 'Execute tests within this file. Should be a path relative to the tests/PHPUnit directory.'); - $this->addOption('testsuite', null, InputOption::VALUE_REQUIRED, 'Execute tests of a specific test suite, for instance UnitTests, IntegrationTests or SystemTests.'); - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - $options = $input->getOption('options'); - $groups = $input->getArgument('group'); - - $groups = explode(",", $groups); - $groups = array_filter($groups, 'strlen'); - - $command = '../../vendor/phpunit/phpunit/phpunit'; - - if (!$this->isCoverageEnabled($options) && $this->isXdebugLoaded()) { - $output->writeln('<comment>Did you know? You can run tests faster by disabling xdebug</comment>'); - } - - // force xdebug usage for coverage options - if ($this->isCoverageEnabled($options) && !$this->isXdebugLoaded()) { - - $output->writeln('<info>xdebug extension required for code coverage.</info>'); - - $output->writeln('<info>searching for xdebug extension...</info>'); - - $extensionDir = shell_exec('php-config --extension-dir'); - $xdebugFile = trim($extensionDir) . DIRECTORY_SEPARATOR . 'xdebug.so'; - - if (!file_exists($xdebugFile)) { - - $dialog = $this->getHelperSet()->get('dialog'); - - $xdebugFile = $dialog->askAndValidate($output, 'xdebug not found. Please provide path to xdebug.so', function($xdebugFile) { - return file_exists($xdebugFile); - }); - } else { - - $output->writeln('<info>xdebug extension found in extension path.</info>'); - } - - $output->writeln("<info>using $xdebugFile as xdebug extension.</info>"); - - $phpunitPath = trim(shell_exec('which phpunit')); - - $command = sprintf('php -d zend_extension=%s %s', $xdebugFile, $phpunitPath); - } - - if ($input->getOption('xhprof')) { - Profiler::setupProfilerXHProf($isMainRun = true); - - putenv('PIWIK_USE_XHPROF=1'); - } - - $testFile = $input->getOption('file'); - if (!empty($testFile)) { - $this->executeTestFile($testFile, $options, $command, $output); - } else { - $suite = $this->getTestsuite($input); - $this->executeTestGroups($suite, $groups, $options, $command, $output); - } - - return $this->returnVar; - } - - private function executeTestFile($testFile, $options, $command, OutputInterface $output) - { - if ('/' !== substr($testFile, 0, 1)) { - $testFile = '../../' . $testFile; - } - - $params = $options . " " . $testFile; - $this->executeTestRun($command, $params, $output); - } - - private function executeTestGroups($suite, $groups, $options, $command, OutputInterface $output) - { - if (empty($suite) && empty($groups)) { - foreach ($this->getTestsSuites() as $suite) { - $suite = $this->buildTestSuiteName($suite); - $this->executeTestGroups($suite, $groups, $options, $command, $output); - } - - return; - } - - $params = $this->buildPhpUnitCliParams($suite, $groups, $options); - - $this->executeTestRun($command, $params, $output); - } - - private function executeTestRun($command, $params, OutputInterface $output) - { - $cmd = $this->getCommand($command, $params); - $output->writeln('Executing command: <info>' . $cmd . '</info>'); - passthru($cmd, $returnVar); - $output->writeln(""); - - $this->returnVar += $returnVar; - } - - private function getTestsSuites() - { - return array('unit', 'integration', 'system'); - } - - /** - * @param $command - * @param $params - * @return string - */ - private function getCommand($command, $params) - { - return sprintf('cd %s/tests/PHPUnit && %s %s', PIWIK_DOCUMENT_ROOT, $command, $params); - } - - private function buildPhpUnitCliParams($suite, $groups, $options) - { - $params = $options . " "; - - if (!empty($groups)) { - $groups = implode(',', $groups); - $params .= '--group ' . $groups . ' '; - } else { - $groups = ''; - } - - if (!empty($suite)) { - $params .= ' --testsuite ' . $suite; - } else { - $suite = ''; - } - - $params = str_replace('%suite%', $suite, $params); - $params = str_replace('%group%', $groups, $params); - - return $params; - } - - private function getTestsuite(InputInterface $input) - { - $suite = $input->getOption('testsuite'); - - if (empty($suite)) { - return; - } - - $availableSuites = $this->getTestsSuites(); - - if (!in_array($suite, $availableSuites)) { - throw new \InvalidArgumentException('Invalid testsuite specified. Use one of: ' . implode(', ', $availableSuites)); - } - - $suite = $this->buildTestSuiteName($suite); - - return $suite; - } - - private function buildTestSuiteName($suite) - { - return ucfirst($suite) . 'Tests'; - } - - private function isCoverageEnabled($options) - { - return false !== strpos($options, '--coverage'); - } - - private function isXdebugLoaded() - { - return extension_loaded('xdebug'); - } - -}
\ No newline at end of file diff --git a/plugins/CoreConsole/Commands/TestsRunUI.php b/plugins/CoreConsole/Commands/TestsRunUI.php deleted file mode 100644 index c3859579ed..0000000000 --- a/plugins/CoreConsole/Commands/TestsRunUI.php +++ /dev/null @@ -1,83 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */ -namespace Piwik\Plugins\CoreConsole\Commands; - -use Piwik\AssetManager; -use Piwik\Plugin\ConsoleCommand; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; - -class TestsRunUI extends ConsoleCommand -{ - protected function configure() - { - $this->setName('tests:run-ui'); - $this->setDescription('Run screenshot tests'); - $this->addArgument('specs', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Run only a specific test spec. Separate multiple specs by comma, for instance core,integration', array()); - $this->addOption("persist-fixture-data", null, InputOption::VALUE_NONE, "Persist test data in a database and do not execute tear down."); - $this->addOption('keep-symlinks', null, InputOption::VALUE_NONE, "Keep recursive directory symlinks so test pages can be viewed in a browser."); - $this->addOption('print-logs', null, InputOption::VALUE_NONE, "Print webpage logs even if tests succeed."); - $this->addOption('drop', null, InputOption::VALUE_NONE, "Drop the existing database and re-setup a persisted fixture."); - $this->addOption('assume-artifacts', null, InputOption::VALUE_NONE, "Assume the diffviewer and processed screenshots will be stored on the builds artifacts server. For use with travis build."); - $this->addOption('plugin', null, InputOption::VALUE_REQUIRED, "Execute all tests for a plugin."); - $this->addOption('skip-delete-assets', null, InputOption::VALUE_NONE, "Skip deleting of merged assets (will speed up a test run, but not by a lot)."); - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - $specs = $input->getArgument('specs'); - $persistFixtureData = $input->getOption("persist-fixture-data"); - $keepSymlinks = $input->getOption('keep-symlinks'); - $printLogs = $input->getOption('print-logs'); - $drop = $input->getOption('drop'); - $assumeArtifacts = $input->getOption('assume-artifacts'); - $plugin = $input->getOption('plugin'); - $skipDeleteAssets = $input->getOption('skip-delete-assets'); - - if (!$skipDeleteAssets) { - AssetManager::getInstance()->removeMergedAssets(); - } - - $options = array(); - if ($persistFixtureData) { - $options[] = "--persist-fixture-data"; - } - - if ($keepSymlinks) { - $options[] = "--keep-symlinks"; - } - - if ($printLogs) { - $options[] = "--print-logs"; - } - - if ($drop) { - $options[] = "--drop"; - } - - if ($assumeArtifacts) { - $options[] = "--assume-artifacts"; - } - - if ($plugin) { - $options[] = "--plugin=" . $plugin; - } - $options = implode(" ", $options); - - $specs = implode(" ", $specs); - - $cmd = "phantomjs '" . PIWIK_INCLUDE_PATH . "/tests/lib/screenshot-testing/run-tests.js' $options $specs"; - - $output->writeln('Executing command: <info>' . $cmd . '</info>'); - $output->writeln(''); - - passthru($cmd); - } -} diff --git a/plugins/CoreConsole/Commands/TestsSetupFixture.php b/plugins/CoreConsole/Commands/TestsSetupFixture.php deleted file mode 100644 index 8002eaaa7e..0000000000 --- a/plugins/CoreConsole/Commands/TestsSetupFixture.php +++ /dev/null @@ -1,273 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */ - -namespace Piwik\Plugins\CoreConsole\Commands; - -use Piwik\Config; -use Piwik\Plugin\ConsoleCommand; -use Piwik\Url; -use Piwik\Tests\Framework\Fixture; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; - -/** - * Console commands that sets up a fixture either in a local MySQL database or a remote one. - * - * Examples: - * - * To setup a fixture provided by Piwik: - * - * ./console tests:setup-fixture UITestFixture - * - * To setup your own fixture created solely for test purposes and stored outside of Piwik: - * - * ./console tests:setup-fixture MyFixtureType --file=../devfixtures/MyFixtureType.php - * - * To setup a fixture or use existing data if present: - * - * ./console tests:setup-fixture UITestFixture --persist-fixture-data - * - * To re-setup a fixture that is already present: - * - * ./console tests:setup-fixture UITestFixture --persist-fixture-data --drop - * - * To create an SQL dump for a fixture: - * - * ./console tests:setup-fixture OmniFixture --sqldump=OmniFixtureDump.sql - */ -class TestsSetupFixture extends ConsoleCommand -{ - protected function configure() - { - $this->setName('tests:setup-fixture'); - $this->setDescription('Create a database and fill it with data using a Piwik test fixture.'); - - $this->addArgument('fixture', InputArgument::REQUIRED, - "The class name of the fixture to apply. Doesn't need to have a namespace if it exists in the " . - "Piwik\\Tests\\Fixtures namespace."); - - $this->addOption('db-name', null, InputOption::VALUE_REQUIRED, - "The name of the database that will contain the fixture data. This option is required to be set."); - $this->addOption('file', null, InputOption::VALUE_REQUIRED, - "The file location of the fixture. If this option is included the file will be required explicitly."); - $this->addOption('db-host', null, InputOption::VALUE_REQUIRED, - "The hostname of the MySQL database to use. Uses the default config value if not specified."); - $this->addOption('db-user', null, InputOption::VALUE_REQUIRED, - "The name of the MySQL user to use. Uses the default config value if not specified."); - $this->addOption('db-pass', null, InputOption::VALUE_REQUIRED, - "The MySQL user password to use. Uses the default config value if not specified."); - $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('sqldump', null, InputOption::VALUE_REQUIRED, - "Creates an SQL dump after setting up the fixture and outputs the dump to the file specified by this option."); - $this->addOption('save-config', null, InputOption::VALUE_NONE, - "Saves the current configuration file as a config for a new Piwik domain. For example save-config --piwik-domain=mytest.localhost.com will create " - . "a mytest.config.ini.php file in the config/ directory. Using /etc/hosts you can redirect to 127.0.0.1 and use the saved " - . "config."); - $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."); - $this->addOption('plugins', null, InputOption::VALUE_REQUIRED, - "Used by UI tests. Comma separated list of plugin names to activate and install when setting up a fixture."); - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - $serverGlobal = $input->getOption('server-global'); - if ($serverGlobal) { - $_SERVER = json_decode($serverGlobal, true); - } - - $this->requireFixtureFiles($input); - $this->setIncludePathAsInTestBootstrap(); - - $host = Url::getHost(); - if (empty($host)) { - $host = 'localhost'; - Url::setHost('localhost'); - } - - $configDomainToSave = $input->getOption('save-config'); - if (!empty($configDomainToSave)) { - $pathToDomainConfig = PIWIK_INCLUDE_PATH . '/config/' . $host . '.config.ini.php'; - - if (!file_exists($pathToDomainConfig)) { - link(PIWIK_INCLUDE_PATH . '/config/config.ini.php', $pathToDomainConfig); - } - } - - $fixture = $this->createFixture($input); - - $this->setupDatabaseOverrides($input, $fixture); - - // perform setup and/or teardown - if ($input->getOption('teardown')) { - $fixture->getTestEnvironment()->save(); - $fixture->performTearDown(); - } else { - $fixture->performSetUp(); - } - - if ($input->getOption('set-phantomjs-symlinks')) { - $this->createSymbolicLinksForUITests(); - } - - $this->writeSuccessMessage($output, array("Fixture successfully setup!")); - - $sqlDumpPath = $input->getOption('sqldump'); - if ($sqlDumpPath) { - $this->createSqlDump($sqlDumpPath, $output); - } - - if (!empty($configDomainToSave)) { - Config::getInstance()->forceSave(); - } - } - - private function createSymbolicLinksForUITests() - { - // 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); - } - } - } - - private function createSqlDump($sqlDumpPath, OutputInterface $output) - { - $output->writeln("<info>Creating SQL dump...</info>"); - - $databaseConfig = Config::getInstance()->database; - $dbUser = $databaseConfig['username']; - $dbPass = $databaseConfig['password']; - $dbHost = $databaseConfig['host']; - $dbName = $databaseConfig['dbname']; - - $command = "mysqldump --user='$dbUser' --password='$dbPass' --host='$dbHost' '$dbName' > '$sqlDumpPath'"; - $output->writeln("<info>Executing $command...</info>"); - passthru($command); - - $this->writeSuccessMessage($output, array("SQL dump created!")); - } - - private function setupDatabaseOverrides(InputInterface $input, Fixture $fixture) - { - $testingEnvironment = $fixture->getTestEnvironment(); - - $optionsToOverride = array( - 'dbname' => $fixture->getDbName(), - 'host' => $input->getOption('db-host'), - 'username' => $input->getOption('db-user'), - 'password' => $input->getOption('db-pass') - ); - foreach ($optionsToOverride as $configOption => $value) { - if ($value) { - $configOverride = $testingEnvironment->configOverride; - $configOverride['database_tests'][$configOption] = $configOverride['database'][$configOption] = $value; - $testingEnvironment->configOverride = $configOverride; - - Config::getInstance()->database[$configOption] = $value; - } - } - } - - private function createFixture(InputInterface $input) - { - $fixtureClass = $input->getArgument('fixture'); - if (class_exists("Piwik\\Tests\\Fixtures\\" . $fixtureClass)) { - $fixtureClass = "Piwik\\Tests\\Fixtures\\" . $fixtureClass; - } - - if (!class_exists($fixtureClass)) { - throw new \Exception("Cannot find fixture class '$fixtureClass'."); - } - - $fixture = new $fixtureClass(); - $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; - } - - $extraPluginsToLoad = $input->getOption('plugins'); - if ($extraPluginsToLoad) { - $fixture->extraPluginsToLoad = explode(',', $extraPluginsToLoad); - } - - if ($fixture->createConfig) { - Config::getInstance()->setTestEnvironment($pathLocal = null, $pathGlobal = null, $pathCommon = null, $allowSaving = true); - } - - $fixture->createConfig = false; - - return $fixture; - } - - private function requireFixtureFiles(InputInterface $input) - { - require_once PIWIK_INCLUDE_PATH . '/libs/PiwikTracker/PiwikTracker.php'; - require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/FakeAccess.php'; - require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/TestingEnvironment.php'; - require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/IntegrationTestCase.php'; - - $fixturesToLoad = array( - '/tests/PHPUnit/UI/Fixtures/*.php', - '/plugins/*/tests/Fixtures/*.php', - '/plugins/*/Test/Fixtures/*.php', - ); - foreach($fixturesToLoad as $fixturePath) { - foreach (glob(PIWIK_INCLUDE_PATH . $fixturePath) as $file) { - require_once $file; - } - } - - $file = $input->getOption('file'); - if ($file) { - if (is_file($file)) { - require_once $file; - } else if (is_file(PIWIK_INCLUDE_PATH . '/' . $file)) { - require_once PIWIK_INCLUDE_PATH . '/' . $file; - } else { - throw new \Exception("Cannot find --file option file '$file'."); - } - } - } - - private function setIncludePathAsInTestBootstrap() - { - if (!defined('PIWIK_INCLUDE_SEARCH_PATH')) { - define('PIWIK_INCLUDE_SEARCH_PATH', get_include_path() - . PATH_SEPARATOR . PIWIK_INCLUDE_PATH . '/core' - . PATH_SEPARATOR . PIWIK_INCLUDE_PATH . '/libs' - . PATH_SEPARATOR . PIWIK_INCLUDE_PATH . '/plugins'); - } - @ini_set('include_path', PIWIK_INCLUDE_SEARCH_PATH); - @set_include_path(PIWIK_INCLUDE_SEARCH_PATH); - } -} |