1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\TestRunner\Commands;
use Piwik\Application\Environment;
use Piwik\Config;
use Piwik\Db;
use Piwik\Plugin\ConsoleCommand;
use Piwik\Tests\Framework\TestingEnvironmentManipulator;
use Piwik\Tests\Framework\TestingEnvironmentVariables;
use Piwik\Url;
use Piwik\Tests\Framework\Fixture;
/**
* 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->addRequiredArgument(
'fixture',
"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->addRequiredValueOption(
'db-name',
null,
"The name of the database that will contain the fixture data. This option is required to be set."
);
$this->addRequiredValueOption(
'file',
null,
"The file location of the fixture. If this option is included the file will be required explicitly."
);
$this->addRequiredValueOption(
'db-host',
null,
"The hostname of the MySQL database to use. Uses the default config value if not specified."
);
$this->addRequiredValueOption(
'db-user',
null,
"The name of the MySQL user to use. Uses the default config value if not specified."
);
$this->addRequiredValueOption(
'db-pass',
null,
"The MySQL user password to use. Uses the default config value if not specified."
);
$this->addNoValueOption(
'teardown',
null,
"If specified, the fixture will be torn down and the database deleted. Won't work if the --db-name " .
"option isn't supplied."
);
$this->addNoValueOption(
'persist-fixture-data',
null,
"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->addNoValueOption(
'drop',
null,
"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->addRequiredValueOption(
'sqldump',
null,
"Creates an SQL dump after setting up the fixture and outputs the dump to the file specified by this option."
);
$this->addNoValueOption(
'save-config',
null,
"Saves the current configuration file as a config for a new Piwik domain. For example save-config --matomo-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->addNoValueOption(
'set-symlinks',
null,
"Used by UI tests. Creates symlinks to root directory in tests/PHPUnit/proxy."
);
$this->addRequiredValueOption(
'server-global',
null,
"Used by UI tests. Sets the \$_SERVER global variable from a JSON string."
);
$this->addRequiredValueOption(
'plugins',
null,
"Used by UI tests. Comma separated list of plugin names to activate and install when setting up a fixture."
);
$this->addNoValueOption('enable-logging', null, 'If enabled, tests will log to the configured log file.');
}
protected function doExecute(): int
{
$input = $this->getInput();
$output = $this->getOutput();
if (!defined('PIWIK_TEST_MODE')) {
define('PIWIK_TEST_MODE', true);
}
if ($input->getOption('enable-logging')) {
putenv("MATOMO_TESTS_ENABLE_LOGGING=1");
}
Environment::setGlobalEnvironmentManipulator(new TestingEnvironmentManipulator(new TestingEnvironmentVariables()));
$serverGlobal = $input->getOption('server-global');
if ($serverGlobal) {
$_SERVER = json_decode($serverGlobal, true);
}
// Tear down any DB that already exists
Db::destroyDatabaseObject();
if (Config::getInstance()->database_tests['tables_prefix'] !== '') {
throw new \Exception("To generate OmniFixture for the UI tests, you must set an empty tables_prefix in [database_tests]");
}
$this->requireFixtureFiles();
$this->setIncludePathAsInTestBootstrap();
$host = Config::getHostname();
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);
}
}
if ($input->getOption('set-symlinks')) {
$this->createSymbolicLinksForUITests();
}
$fixture = $this->createFixture($allowSave = !empty($configDomainToSave));
$this->setupDatabaseOverrides($fixture);
// perform setup and/or teardown
if ($input->getOption('teardown')) {
$fixture->getTestEnvironment()->save();
$fixture->performTearDown();
} else {
$fixture->performSetUp();
}
$this->writeSuccessMessage("Fixture successfully set up!");
$sqlDumpPath = $input->getOption('sqldump');
if ($sqlDumpPath) {
$this->createSqlDump($sqlDumpPath);
}
if (!empty($configDomainToSave)) {
Config::getInstance()->forceSave();
}
return self::SUCCESS;
}
private function createSymbolicLinksForUITests()
{
// make sure symbolic links exist (phantomjs doesn't support symlink-ing yet)
foreach (array('libs', 'plugins', 'tests', 'misc', 'node_modules', 'piwik.js', 'matomo.js') as $linkName) {
$linkPath = PIWIK_INCLUDE_PATH . '/tests/PHPUnit/proxy/' . $linkName;
if (!file_exists($linkPath)) {
$target = PIWIK_INCLUDE_PATH . '/' . $linkName;
$success = @symlink($target, $linkPath);
// setting symlink might fail when the symlink already exists but pointing to a no longer existing path/file
// eg when sometimes running it on a VM and sometimes on the VM's host itself.
if (!$success) {
unlink($linkPath);
symlink($target, $linkPath);
}
}
}
}
private function createSqlDump($sqlDumpPath)
{
$output = $this->getOutput();
$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("SQL dump created!");
}
private function setupDatabaseOverrides(Fixture $fixture)
{
$input = $this->getInput();
$testingEnvironment = $fixture->getTestEnvironment();
$optionsToOverride = array(
'dbname' => $fixture->getDbName(),
'host' => $input->getOption('db-host'),
'username' => $input->getOption('db-user'),
'password' => $input->getOption('db-pass'),
'tables_prefix' => '',
);
foreach ($optionsToOverride as $configOption => $value) {
if ($value) {
$testingEnvironment->overrideConfig('database_tests', $configOption, $value);
Config::getInstance()->database[$configOption] = $value;
}
}
}
private function createFixture($allowSave)
{
$input = $this->getInput();
$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'.");
}
/** @var Fixture $fixture */
$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 = array_merge($fixture->extraPluginsToLoad, explode(',', $extraPluginsToLoad));
$fixture->extraPluginsToLoad = array_unique($fixture->extraPluginsToLoad);
}
$fixture->extraDiEnvironments = array('ui-test');
return $fixture;
}
private function requireFixtureFiles()
{
$file = $this->getInput()->getOption('file');
if ($file) {
if (is_file($file)) {
require_once $file;
} elseif (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 . MATOMO_PLUGINS_PATH . '/plugins');
}
@ini_set('include_path', PIWIK_INCLUDE_SEARCH_PATH);
@set_include_path(PIWIK_INCLUDE_SEARCH_PATH);
}
}
|