Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--plugins/CoreConsole/Commands/SetupFixture.php171
-rw-r--r--tests/PHPUnit/Fixture.php21
-rw-r--r--tests/PHPUnit/Fixtures/OmniFixture.php17
-rw-r--r--tests/PHPUnit/Fixtures/UITestFixture.php23
-rw-r--r--tests/PHPUnit/Integration/BackwardsCompatibility1XTest.php24
-rw-r--r--tests/resources/OmniFixture-dump.sql.gzbin0 -> 624382 bytes
6 files changed, 171 insertions, 85 deletions
diff --git a/plugins/CoreConsole/Commands/SetupFixture.php b/plugins/CoreConsole/Commands/SetupFixture.php
index 9ff1f89676..f448f05f33 100644
--- a/plugins/CoreConsole/Commands/SetupFixture.php
+++ b/plugins/CoreConsole/Commands/SetupFixture.php
@@ -8,6 +8,7 @@
namespace Piwik\Plugins\CoreConsole\Commands;
+use \Fixture;
use Piwik\Config;
use Piwik\Plugin\ConsoleCommand;
use Piwik\Url;
@@ -18,6 +19,28 @@ 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 SetupFixture extends ConsoleCommand
{
@@ -49,6 +72,8 @@ class SetupFixture extends ConsoleCommand
$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('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,
@@ -64,26 +89,88 @@ class SetupFixture extends ConsoleCommand
$_SERVER = json_decode($serverGlobal, true);
}
- $this->requireFixtureFiles();
+ $this->requireFixtureFiles($input);
$this->setIncludePathAsInTestBootstrap();
- $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'.");
- }
- }
-
$host = Url::getHost();
if (empty($host)) {
Url::setHost('localhost');
}
- // get the fixture class
+ $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);
+ }
+ }
+
+ 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->write("<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'";
+ 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;
@@ -93,7 +180,6 @@ class SetupFixture extends ConsoleCommand
throw new \Exception("Cannot find fixture class '$fixtureClass'.");
}
- // create the fixture
$fixture = new $fixtureClass();
$fixture->printToScreen = true;
@@ -115,52 +201,16 @@ class SetupFixture extends ConsoleCommand
$fixture->extraPluginsToLoad = explode(',', $extraPluginsToLoad);
}
- if($fixture->createConfig) {
+ if ($fixture->createConfig) {
Config::getInstance()->setTestEnvironment();
}
- $fixture->createConfig = false;
-
- // setup database overrides
- $testingEnvironment = $fixture->getTestEnvironment();
-
- $optionsToOverride = array(
- 'dbname' => $fixture->getDbName(),
- 'host' => $input->getOption('db-host'),
- 'user' => $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;
- }
- }
-
- // perform setup and/or teardown
- if ($input->getOption('teardown')) {
- $testingEnvironment->save();
- $fixture->performTearDown();
- } else {
- $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);
- }
- }
- }
+ $fixture->createConfig = false;
- $this->writeSuccessMessage($output, array("Fixture successfully setup!"));
+ return $fixture;
}
- private function requireFixtureFiles()
+ private function requireFixtureFiles(InputInterface $input)
{
require_once PIWIK_INCLUDE_PATH . '/libs/PiwikTracker/PiwikTracker.php';
require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/FakeAccess.php';
@@ -180,6 +230,17 @@ class SetupFixture extends ConsoleCommand
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()
@@ -193,4 +254,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 281c452333..f8ff1c6a67 100644
--- a/tests/PHPUnit/Fixture.php
+++ b/tests/PHPUnit/Fixture.php
@@ -29,6 +29,8 @@ use Piwik\Site;
use Piwik\Tracker\Cache;
use Piwik\Translate;
use Piwik\Url;
+use Piwik\Plugins\CoreUpdater\CoreUpdater;
+use Piwik\Updater;
/**
* Base type for all integration test fixtures. Integration test fixtures
@@ -781,6 +783,25 @@ class Fixture extends PHPUnit_Framework_Assert
}
return $result;
}
+
+ public static function updateDatabase()
+ {
+ $updater = new Updater();
+ $componentsWithUpdateFile = CoreUpdater::getComponentUpdates($updater);
+ if (empty($componentsWithUpdateFile)) {
+ return false;
+ }
+
+ $result = CoreUpdater::updateComponents($updater, $componentsWithUpdateFile);
+ if (!empty($result['coreError'])
+ && !empty($result['warnings'])
+ && !empty($result['errors'])
+ ) {
+ throw new \Exception("Failed to update database (errors or warnings found): " . print_r($result, true));
+ }
+
+ return $result;
+ }
}
// TODO: remove when other plugins don't use BaseFixture
diff --git a/tests/PHPUnit/Fixtures/OmniFixture.php b/tests/PHPUnit/Fixtures/OmniFixture.php
index a688a91f3f..3eb5cb9ac2 100644
--- a/tests/PHPUnit/Fixtures/OmniFixture.php
+++ b/tests/PHPUnit/Fixtures/OmniFixture.php
@@ -8,7 +8,10 @@
namespace Piwik\Tests\Fixtures;
use Piwik\Date;
+use Piwik\Access;
+use Piwik\Option;
use ReflectionClass;
+use Piwik\Plugins\VisitsSummary\API as VisitsSummaryAPI;
/**
* This fixture is the combination of every other fixture defined by Piwik. Should be used
@@ -42,6 +45,7 @@ class OmniFixture extends \Fixture
&& $className != __CLASS__
&& $className != "Piwik_Test_Fixture_SqlDump"
&& $className != "Piwik\\Tests\\Fixtures\\UpdaterTestFixture"
+ && $className != "Piwik\\Tests\\Fixtures\\UITestFixture"
) {
$klassReflect = new ReflectionClass($className);
if (!strpos($klassReflect->getFilename(), "tests/PHPUnit/Fixtures")
@@ -87,6 +91,13 @@ class OmniFixture extends \Fixture
foreach ($this->fixtures as $name => $fixture) {
$fixture->setUp();
}
+
+ Option::set("Tests.forcedNowTimestamp", $this->now->getTimestamp());
+
+ // launch archiving so tests don't run out of time
+ $date = Date::factory($this->dateTime)->toString();
+ VisitsSummaryAPI::getInstance()->get($this->idSite, 'year', $date);
+ VisitsSummaryAPI::getInstance()->get($this->idSite, 'year', $date, urlencode($this->segment));
}
public function tearDown()
@@ -95,4 +106,10 @@ class OmniFixture extends \Fixture
$fixture->tearDown();
}
}
+
+ public static function createAccessInstance()
+ {
+ Access::setSingletonInstance($access = new \Test_Access_OverrideLogin());
+ \Piwik\Piwik::postEvent('Request.initAuthenticationObject');
+ }
} \ No newline at end of file
diff --git a/tests/PHPUnit/Fixtures/UITestFixture.php b/tests/PHPUnit/Fixtures/UITestFixture.php
index 23fc35aac9..ed19987143 100644
--- a/tests/PHPUnit/Fixtures/UITestFixture.php
+++ b/tests/PHPUnit/Fixtures/UITestFixture.php
@@ -8,8 +8,8 @@
namespace Piwik\Tests\Fixtures;
use Exception;
-use Piwik\Access;
use Piwik\AssetManager;
+use Piwik\Access;
use Piwik\Common;
use Piwik\Date;
use Piwik\Db;
@@ -18,18 +18,28 @@ use Piwik\FrontController;
use Piwik\Option;
use Piwik\Plugins\SegmentEditor\API as APISegmentEditor;
use Piwik\Plugins\UsersManager\API as UsersManagerAPI;
-use Piwik\Plugins\VisitsSummary\API as VisitsSummaryAPI;
use Piwik\WidgetsList;
+use Fixture;
/**
* Fixture for UI tests.
*/
-class UITestFixture extends OmniFixture
+class UITestFixture extends \Piwik_Test_Fixture_SqlDump
{
+ const FIXTURE_LOCATION = '/tests/resources/OmniFixture-dump.sql.gz';
+
+ public function __construct()
+ {
+ $this->dumpUrl = PIWIK_INCLUDE_PATH . self::FIXTURE_LOCATION;
+ $this->tablesPrefix = '';
+ }
+
public function setUp()
{
parent::setUp();
+ self::updateDatabase();
+
// make sure site has an early enough creation date (for period selector tests)
Db::get()->update(Common::prefixTable("site"),
array('ts_created' => '2011-01-01'),
@@ -41,13 +51,6 @@ class UITestFixture extends OmniFixture
DbHelper::createAnonymousUser();
UsersManagerAPI::getInstance()->setSuperUserAccess('superUserLogin', true);
-
- Option::set("Tests.forcedNowTimestamp", $this->now->getTimestamp());
-
- // launch archiving so tests don't run out of time
- $date = Date::factory($this->dateTime)->toString();
- VisitsSummaryAPI::getInstance()->get($this->idSite, 'year', $date);
- VisitsSummaryAPI::getInstance()->get($this->idSite, 'year', $date, urlencode($this->segment));
}
public function performSetUp($setupEnvironmentOnly = false)
diff --git a/tests/PHPUnit/Integration/BackwardsCompatibility1XTest.php b/tests/PHPUnit/Integration/BackwardsCompatibility1XTest.php
index da23211edf..f69e4d0c56 100644
--- a/tests/PHPUnit/Integration/BackwardsCompatibility1XTest.php
+++ b/tests/PHPUnit/Integration/BackwardsCompatibility1XTest.php
@@ -8,9 +8,7 @@
use Piwik\Common;
use Piwik\Db;
-use Piwik\Plugins\CoreUpdater\CoreUpdater;
use Piwik\Plugins\VisitFrequency\API as VisitFrequencyApi;
-use Piwik\Updater;
/**
* Tests that Piwik 2.0 works w/ data from Piwik 1.12.
@@ -27,7 +25,10 @@ class Test_Piwik_Integration_BackwardsCompatibility1XTest extends IntegrationTes
{
parent::setUpBeforeClass();
- self::updateDatabase();
+ $result = Fixture::updateDatabase();
+ if ($result === false) {
+ throw new \Exception("Failed to update pre-2.0 database (nothing to update).");
+ }
// truncate log tables so old data won't be re-archived
foreach (array('log_visit', 'log_link_visit_action', 'log_conversion', 'log_conversion_item') as $table) {
@@ -49,23 +50,6 @@ class Test_Piwik_Integration_BackwardsCompatibility1XTest extends IntegrationTes
VisitFrequencyApi::getInstance()->get(1, 'year', '2012-12-29');
}
- private static function updateDatabase()
- {
- $updater = new Updater();
- $componentsWithUpdateFile = CoreUpdater::getComponentUpdates($updater);
- if (empty($componentsWithUpdateFile)) {
- throw new \Exception("Failed to update pre-2.0 database (nothing to update).");
- }
-
- $result = CoreUpdater::updateComponents($updater, $componentsWithUpdateFile);
- if (!empty($result['coreError'])
- && !empty($result['warnings'])
- && !empty($result['errors'])
- ) {
- throw new \Exception("Failed to update pre-2.0 database (errors or warnings found): " . print_r($result, true));
- }
- }
-
public function setUp()
{
parent::setUp();
diff --git a/tests/resources/OmniFixture-dump.sql.gz b/tests/resources/OmniFixture-dump.sql.gz
new file mode 100644
index 0000000000..9c8dc1b75a
--- /dev/null
+++ b/tests/resources/OmniFixture-dump.sql.gz
Binary files differ