PageRenderTime 84ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/PHPUnit/BenchmarkTestCase.php

https://github.com/CodeYellowBV/piwik
PHP | 113 lines | 74 code | 15 blank | 24 comment | 6 complexity | 6639792abcbdb9996396a6669d93ebc8 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. */
  8. use Piwik\Config;
  9. use Piwik\Db;
  10. use Piwik\Plugins\Goals\API;
  11. require_once PIWIK_INCLUDE_PATH . '/tests/PHPUnit/IntegrationTestCase.php';
  12. require_once PIWIK_INCLUDE_PATH . '/tests/LocalTracker.php';
  13. // require fixtures
  14. foreach (glob(PIWIK_INCLUDE_PATH . '/tests/PHPUnit/Benchmarks/Fixtures/*.php') as $file) {
  15. require_once $file;
  16. }
  17. /**
  18. * Base class for benchmarks.
  19. */
  20. abstract class BenchmarkTestCase extends IntegrationTestCase
  21. {
  22. protected static $fixture;
  23. public static function setUpBeforeClass()
  24. {
  25. $dbName = false;
  26. if (!empty($GLOBALS['PIWIK_BENCHMARK_DATABASE'])) {
  27. $dbName = $GLOBALS['PIWIK_BENCHMARK_DATABASE'];
  28. }
  29. // connect to database
  30. self::createTestConfig();
  31. self::connectWithoutDatabase();
  32. // create specified fixture (global var not set, use default no-data fixture (see end of this file))
  33. if (empty($GLOBALS['PIWIK_BENCHMARK_FIXTURE'])) {
  34. $fixtureName = 'Piwik_Test_Fixture_EmptyOneSite';
  35. } else {
  36. $fixtureName = 'Piwik_Test_Fixture_' . $GLOBALS['PIWIK_BENCHMARK_FIXTURE'];
  37. }
  38. self::$fixture = new $fixtureName;
  39. // figure out if the desired fixture has already been setup, and if not empty the database
  40. $installedFixture = false;
  41. try {
  42. if (isset(self::$fixture->tablesPrefix)) {
  43. Config::getInstance()->database['tables_prefix'] = self::$fixture->tablesPrefix;
  44. }
  45. Db::query("USE " . $dbName);
  46. $installedFixture = \Piwik\Option::get('benchmark_fixture_name');
  47. } catch (Exception $ex) {
  48. // ignore
  49. }
  50. $createEmptyDatabase = $fixtureName != $installedFixture;
  51. parent::_setUpBeforeClass($dbName, $createEmptyDatabase);
  52. // if we created an empty database, setup the fixture
  53. if ($createEmptyDatabase) {
  54. self::$fixture->setUp();
  55. \Piwik\Option::set('benchmark_fixture_name', $fixtureName);
  56. }
  57. }
  58. public static function tearDownAfterClass()
  59. {
  60. // only drop the database if PIWIK_BENCHMARK_DATABASE isn't set
  61. $dropDatabase = empty($GLOBALS['PIWIK_BENCHMARK_DATABASE']);
  62. parent::_tearDownAfterClass($dropDatabase);
  63. }
  64. /**
  65. * Creates a tracking object that invokes the tracker directly (w/o going through HTTP).
  66. */
  67. public static function getLocalTracker($idSite)
  68. {
  69. $t = new Piwik_LocalTracker($idSite, Fixture::getTrackerUrl());
  70. $t->setUserAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)");
  71. $t->setBrowserLanguage('fr');
  72. $t->setLocalTime('12:34:06');
  73. $t->setResolution(1024, 768);
  74. $t->setBrowserHasCookies(true);
  75. $t->setPlugins($flash = true, $java = true, $director = false);
  76. $t->setTokenAuth(Fixture::getTokenAuth());
  77. return $t;
  78. }
  79. }
  80. /**
  81. * Reusable fixture. Adds one site w/ goals and no visit data.
  82. */
  83. class Piwik_Test_Fixture_EmptyOneSite
  84. {
  85. public $date = '2010-01-01';
  86. public $period = 'day';
  87. public $idSite = 1;
  88. public function setUp()
  89. {
  90. // add one site
  91. Fixture::createWebsite(
  92. $this->date, $ecommerce = 1, $siteName = "Site #0", $siteUrl = "http://whatever.com/");
  93. // add two goals
  94. $goals = API::getInstance();
  95. $goals->addGoal($this->idSite, 'all', 'url', 'http', 'contains', false, 5);
  96. $goals->addGoal($this->idSite, 'all', 'url', 'http', 'contains');
  97. }
  98. }