diff options
author | Benaka <diosmosis@users.noreply.github.com> | 2017-09-11 16:08:42 +0300 |
---|---|---|
committer | Matthieu Aubry <mattab@users.noreply.github.com> | 2017-09-11 16:08:42 +0300 |
commit | f0ddb70c85a6211540797665f17d36b6d79128a4 (patch) | |
tree | 605ef83a15a39b1d30d6641234b83aee58ec8b16 /tests/PHPUnit | |
parent | 1092e4d7290333591f0f9beead6580a1424bc99f (diff) |
Changes to support custom periods (#11837)
* Separate Archive query creation responsibility from Archive class.
* Add ability for plugins to define custom period types.
* Make period responsible for determining start/end time of periods, not LogAggregator.
* Allow specifying custom archive writer in PluginsArchiver.
Diffstat (limited to 'tests/PHPUnit')
-rw-r--r-- | tests/PHPUnit/Integration/ArchiveTest.php | 20 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Period/FactoryTest.php | 128 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/PeriodTest.php | 34 |
3 files changed, 147 insertions, 35 deletions
diff --git a/tests/PHPUnit/Integration/ArchiveTest.php b/tests/PHPUnit/Integration/ArchiveTest.php index da5879c843..20c12cf1be 100644 --- a/tests/PHPUnit/Integration/ArchiveTest.php +++ b/tests/PHPUnit/Integration/ArchiveTest.php @@ -36,7 +36,14 @@ class Archive extends PiwikArchive { return parent::get($archiveNames, $archiveDataType, $idSubtable); } +} +class CustomArchiveQueryFactory extends PiwikArchive\ArchiveQueryFactory +{ + public function newInstance(\Piwik\Archive\Parameters $params, $forceIndexedBySite, $forceIndexedByDate) + { + return new Archive($params, $forceIndexedBySite, $forceIndexedByDate); + } } /** @@ -251,7 +258,7 @@ class ArchiveTest extends IntegrationTestCase $this->assertEquals('UserLanguage_LanguageCode fr', $userLanguageReport->getFirstRow()->getColumn('label')); $this->assertEquals('UserLanguage_LanguageCode fr', $userLanguageReport->getLastRow()->getColumn('label')); - $parameters = new Parameters(new Site(1), $period, new Segment('', '')); + $parameters = new Parameters(new Site(1), $period, new Segment('', [])); $parameters->setRequestedPlugin('UserLanguage'); $result = ArchiveSelector::getArchiveIdAndVisits($parameters, $period->getDateStart()->getDateStartUTC()); @@ -387,10 +394,21 @@ class ArchiveTest extends IntegrationTestCase return $archive->getNumeric('nb_visits'); } + /** + * @return Archive + */ private function getArchive($period, $day = '2010-03-04,2010-03-07') { + /** @noinspection PhpIncompatibleReturnTypeInspection */ return Archive::build(self::$fixture->idSite, $period, $day); } + + public function provideContainerConfig() + { + return [ + PiwikArchive\ArchiveQueryFactory::class => \DI\object(CustomArchiveQueryFactory::class), + ]; + } } ArchiveTest::$fixture = new OneVisitorTwoVisits();
\ No newline at end of file diff --git a/tests/PHPUnit/Integration/Period/FactoryTest.php b/tests/PHPUnit/Integration/Period/FactoryTest.php new file mode 100644 index 0000000000..3c36416c9d --- /dev/null +++ b/tests/PHPUnit/Integration/Period/FactoryTest.php @@ -0,0 +1,128 @@ +<?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\Tests\Integration\Period; + +use Piwik\Config; +use Piwik\Period; +use Piwik\Period\Day; +use Piwik\Period\Month; +use Piwik\Period\Range; +use Piwik\Period\Week; +use Piwik\Period\Year; +use Piwik\Tests\Framework\TestCase\IntegrationTestCase; + +class TestPeriod +{ + // empty +} + +class TestPeriodFactory extends Period\Factory +{ + /** + * @var Config + */ + private $config; + + // use constructor to make sure period factories are injected + public function __construct(Config $config) + { + $this->config = $config; + } + + public function shouldHandle($strPeriod, $strDate) + { + return $strPeriod == 'customperiod'; + } + + public function make($strPeriod, $date, $timezone) + { + return new TestPeriod(); + } +} + +class MockPluginManager extends \Piwik\Plugin\Manager +{ + public function findComponents($componentName, $expectedSubclass) + { + if ($componentName == 'PeriodFactory') { + return [ + TestPeriodFactory::class, + ]; + } + + return parent::findComponents($componentName, $expectedSubclass); + } +} + +class FactoryTest extends IntegrationTestCase +{ + /** + * @dataProvider getBuildTestData + */ + public function test_build_CreatesCorrectPeriodInstances($strPeriod, $date, $timezone, $expectedPeriodClass, + $expectedRangeString) + { + $period = Period\Factory::build($strPeriod, $date, $timezone); + $this->assertInstanceOf($expectedPeriodClass, $period); + $this->assertEquals($expectedRangeString, $period->getRangeString()); + } + + public function getBuildTestData() + { + return [ + ['day', '2015-01-01', 'UTC', Day::class, '2015-01-01,2015-01-01'], + ['week', '2015-01-01', 'UTC', Week::class, '2014-12-29,2015-01-04'], + ['month', '2015-01-01', 'UTC', Month::class, '2015-01-01,2015-01-31'], + ['year', '2015-01-01', 'UTC', Year::class, '2015-01-01,2015-12-31'], + + ['range', '2015-01-01,2015-01-10', 'UTC', Range::class, '2015-01-01,2015-01-10'], + ['range', '2015-01-01,2015-01-10', 'Antarctica/Casey', Range::class, '2015-01-01,2015-01-10'], + + // multiple periods + ['day', '2015-01-01,2015-01-10', 'UTC', Range::class, '2015-01-01,2015-01-10'], + ['week', '2015-01-01,2015-01-10', 'UTC', Range::class, '2014-12-29,2015-01-11'], + ['month', '2015-01-01,2015-02-10', 'UTC', Range::class, '2015-01-01,2015-02-28'], + ['year', '2015-01-01,2016-01-10', 'UTC', Range::class, '2015-01-01,2016-12-31'], + ]; + } + + public function test_build_CreatesCustomPeriodInstances() + { + Config::getInstance()->General['enabled_periods_API'] .= ',customperiod'; + + $period = Period\Factory::build('customperiod', '2015-01-01'); + $this->assertInstanceOf(TestPeriod::class, $period); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage General_ExceptionInvalidPeriod + */ + public function test_build_ThrowsIfPeriodIsUnrecognized() + { + Period\Factory::build('garbageperiod', '2015-01-01'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage General_ExceptionInvalidPeriod + */ + public function test_build_ThrowsIfPeriodIsNotEnabledForApi() + { + Config::getInstance()->General['enabled_periods_API'] = 'day'; + Period\Factory::build('week', '2015-01-01'); + } + + public function provideContainerConfig() + { + return [ + \Piwik\Plugin\Manager::class => \DI\object(MockPluginManager::class), + ]; + } +} diff --git a/tests/PHPUnit/Unit/PeriodTest.php b/tests/PHPUnit/Unit/PeriodTest.php index f4c7d4ee1d..32b4c71a56 100644 --- a/tests/PHPUnit/Unit/PeriodTest.php +++ b/tests/PHPUnit/Unit/PeriodTest.php @@ -52,40 +52,6 @@ class PeriodTest extends \PHPUnit_Framework_TestCase $this->assertNotEmpty($label); } - public function testFactoryDay() - { - $period = Period\Factory::build('day', Date::today()); - $this->assertInstanceOf('\Piwik\Period\Day', $period); - } - - public function testFactoryMonth() - { - $period = Period\Factory::build('month', Date::today()); - $this->assertInstanceOf('\Piwik\Period\Month', $period); - } - - public function testFactoryWeek() - { - $period = Period\Factory::build('week', Date::today()); - $this->assertInstanceOf('\Piwik\Period\Week', $period); - } - - public function testFactoryYear() - { - $period = Period\Factory::build('year', Date::today()); - $this->assertInstanceOf('\Piwik\Period\Year', $period); - } - - public function testFactoryInvalid() - { - try { - Period\Factory::build('inValid', Date::today()); - } catch (\Exception $e) { - return; - } - $this->fail('Expected Exception not raised'); - } - public function testValidate_ValidDates() { Period::checkDateFormat('today'); |