diff options
author | diosmosis <diosmosis@users.noreply.github.com> | 2021-01-05 04:04:24 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-05 04:04:24 +0300 |
commit | 27241f171961cfc8198d6356e5e579c3bf547aa5 (patch) | |
tree | 8b78a64a2dd85817f4ac86be4be14e39caddf1b5 /tests | |
parent | da9c6159702a228c1d50b65b87dd416730c092b0 (diff) |
Add unit test for QueueConsumer::shouldSkipArchiveBecauseLowerPeriodOrSegmentIsInProgress(). (#16996)
* Add unit test for QueueConsumer::shouldSkipArchiveBecauseLowerPeriodOrSegmentIsInProgress().
* move tests to integration
Diffstat (limited to 'tests')
-rw-r--r-- | tests/PHPUnit/Integration/CronArchive/QueueConsumerTest.php | 208 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/CronArchive/QueueConsumerTest.php | 95 |
2 files changed, 208 insertions, 95 deletions
diff --git a/tests/PHPUnit/Integration/CronArchive/QueueConsumerTest.php b/tests/PHPUnit/Integration/CronArchive/QueueConsumerTest.php index c3a66f3f1e..9cf881d5d0 100644 --- a/tests/PHPUnit/Integration/CronArchive/QueueConsumerTest.php +++ b/tests/PHPUnit/Integration/CronArchive/QueueConsumerTest.php @@ -12,6 +12,7 @@ namespace Piwik\Tests\Integration\CronArchive; use Piwik\ArchiveProcessor\Rules; use Piwik\CliMulti\RequestParser; use Piwik\Common; +use Piwik\Period\Factory; use Piwik\Plugins\CustomDimensions; use Piwik\Container\StaticContainer; use Piwik\CronArchive; @@ -30,6 +31,7 @@ use Piwik\Segment; use Piwik\Tests\Framework\Fixture; use Piwik\Tests\Framework\TestCase\IntegrationTestCase; use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; class QueueConsumerTest extends IntegrationTestCase { @@ -704,6 +706,212 @@ class QueueConsumerTest extends IntegrationTestCase $this->assertFalse($result); } + /** + * @dataProvider getTestDataForHasIntersectingPeriod + */ + public function test_hasIntersectingPeriod($archivesToProcess, $invalidatedArchive, $expected) + { + $periods = array_flip(Piwik::$idPeriods); + foreach ($archivesToProcess as &$archive) { + $periodLabel = $periods[$archive['period']]; + $archive['periodObj'] = Factory::build($periodLabel, $archive['date1']); + } + + $periodLabel = $periods[$invalidatedArchive['period']]; + $invalidatedArchive['periodObj'] = Factory::build($periodLabel, $invalidatedArchive['date1']); + + $actual = QueueConsumer::hasIntersectingPeriod($archivesToProcess, $invalidatedArchive); + $this->assertEquals($expected, $actual); + } + + public function getTestDataForHasIntersectingPeriod() + { + return [ + // no intersecting periods + [ + [ + ['period' => 1, 'date1' => '2020-03-04', 'date2' => '2020-03-04'], + ['period' => 3, 'date1' => '2020-04-01', 'date2' => '2020-04-30'], + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==abc'], + ], + ['period' => 1, 'date1' => '2020-03-05', 'date2' => '2020-03-05'], + false, + ], + + // intersecting periods + [ + [ + ['period' => 1, 'date1' => '2020-03-04', 'date2' => '2020-03-04'], + ['period' => 3, 'date1' => '2020-04-01', 'date2' => '2020-04-30'], + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==abc'], + ], + ['period' => 2, 'date1' => '2020-03-02', 'date2' => '2020-03-08'], + true, + ], + + // all same period, different segments + [ + [ + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==def'], + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==ghi'], + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==abc'], + ], + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==lmn'], + false, + ], + + // all same period, all visits in one + [ + [ + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => ''], + ], + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==lmn'], + true, + ], + + // all same period, different segments, all visits in next + [ + [ + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==def'], + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==ghi'], + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==abc'], + ], + ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15'], + true, + ], + ]; + } + + /** + * @dataProvider getTestDataForShouldSkipArchiveBecauseLowerPeriodOrSegmentIsInProgress + */ + public function test_shouldSkipArchiveBecauseLowerPeriodOrSegmentIsInProgress($cliMultiProcesses, $archiveToProcess, $expected) + { + $cliRequestProcessor = $this->getMockRequestParser($cliMultiProcesses); + + /** @var QueueConsumer $queueConsumer */ + $queueConsumer = $this->getQueueConsumerWithMocks($cliRequestProcessor); + + $periods = array_flip(Piwik::$idPeriods); + + $archiveToProcess['periodObj'] = Factory::build($periods[$archiveToProcess['period']], $archiveToProcess['date']); + $actual = $queueConsumer->shouldSkipArchiveBecauseLowerPeriodOrSegmentIsInProgress($archiveToProcess); + $this->assertEquals($expected, $actual); + } + + public function getTestDataForShouldSkipArchiveBecauseLowerPeriodOrSegmentIsInProgress() + { + return [ + // test idSite different + [ + [ + ['idSite' => 5, 'date' => '2020-03-04', 'period' => 'day'], + ], + ['idsite' => 3, 'date' => '2020-03-04', 'period' => 1], + false, + ], + + // test no period/date + [ + [ + ['idSite' => 3], + ], + ['idsite' => 3, 'date' => '2020-03-04', 'period' => 1], + false, + ], + + // test same segment + [ + [ + ['idSite' => 3, 'date' => '2020-03-04', 'period' => 'day', 'segment' => 'pageUrl=@%2C'], + ], + ['idsite' => 3, 'date' => '2020-03-04', 'period' => 1, 'segment' => 'pageUrl=@%2C'], + 'lower or same period in progress in another local climulti process (period = day, date = 2020-03-04)', + ], + [ + [ + ['idSite' => 3, 'date' => '2020-03-04', 'period' => 'day', 'segment' => 'pageUrl=@%252C'], + ], + ['idsite' => 3, 'date' => '2020-03-04', 'period' => 1, 'segment' => 'pageUrl=@%2C'], + 'lower or same period in progress in another local climulti process (period = day, date = 2020-03-04)', + ], + + // test different segment + [ + [ + ['idSite' => 3, 'date' => '2020-03-04', 'period' => 'day', 'segment' => 'pageUrl=@%2C'], + ], + ['idsite' => 3, 'date' => '2020-03-04', 'period' => 1, 'segment' => 'pageUrl=@%2Ca'], + false, + ], + [ + [ + ['idSite' => 3, 'date' => '2020-03-04', 'period' => 'day', 'segment' => 'pageUrl=@%252C'], + ], + ['idsite' => 3, 'date' => '2020-03-04', 'period' => 1, 'segment' => 'pageUrl%3D%40%252C'], + false, + ], + + // test lower periods together + [ + [ + ['idSite' => 3, 'date' => '2020-03-04', 'period' => 'day'], + ], + ['idsite' => 3, 'date' => '2020-03-04', 'period' => 1], + 'lower or same period in progress in another local climulti process (period = day, date = 2020-03-04)', + ], + [ + [ + ['idSite' => 3, 'date' => '2020-03-04', 'period' => 'day'], + ], + ['idsite' => 3, 'date' => '2020-03-01', 'period' => 3], + 'lower or same period in progress in another local climulti process (period = day, date = 2020-03-04)', + ], + [ + [ + ['idSite' => 3, 'date' => '2020-03-01', 'period' => 'month'], + ], + ['idsite' => 3, 'date' => '2020-03-01', 'period' => 1], + false, + ], + + // test segment w/ non-segment + [ + [ + ['idSite' => 3, 'date' => '2020-03-04', 'period' => 'day', 'segment' => ''], + ], + ['idsite' => 3, 'date' => '2020-03-04', 'period' => 2, 'segment' => 'pageUrl=@%2C'], + 'lower or same period in progress in another local climulti process (period = day, date = 2020-03-04)', + ], + [ + [ + ['idSite' => 3, 'date' => '2020-03-04', 'period' => 'day', 'segment' => 'pageUrl=@%2C'], + ], + ['idsite' => 3, 'date' => '2020-03-04', 'period' => 1, 'segment' => ''], + 'lower or same period in progress in another local climulti process (period = day, date = 2020-03-04)', + ], + ]; + } + + private function getMockRequestParser($cliMultiProcesses) + { + $mock = $this->getMockBuilder(RequestParser::class) + ->disableOriginalConstructor() + ->onlyMethods(['getInProgressArchivingCommands']) + ->getMock(); + $mock->method('getInProgressArchivingCommands')->willReturn($cliMultiProcesses); + return $mock; + } + + private function getQueueConsumerWithMocks($cliRequestProcessor) + { + $mockCronArchive = $this->getMockBuilder(CronArchive::class) + ->disableOriginalConstructor() + ->getMock(); + + return new QueueConsumer(new NullLogger(), null, null, null, new Model(), new SegmentArchiving(null), $mockCronArchive, $cliRequestProcessor); + } + protected static function configureFixture($fixture) { parent::configureFixture($fixture); diff --git a/tests/PHPUnit/Unit/CronArchive/QueueConsumerTest.php b/tests/PHPUnit/Unit/CronArchive/QueueConsumerTest.php deleted file mode 100644 index f8353ae554..0000000000 --- a/tests/PHPUnit/Unit/CronArchive/QueueConsumerTest.php +++ /dev/null @@ -1,95 +0,0 @@ -<?php -/** - * Matomo - free/libre analytics platform - * - * @link https://matomo.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * - */ - -namespace PHPUnit\Unit\CronArchive; - - -use PHPUnit\Framework\TestCase; -use Piwik\CronArchive\QueueConsumer; -use Piwik\Period\Factory; -use Piwik\Piwik; - -class QueueConsumerTest extends TestCase -{ - /** - * @dataProvider getTestDataForHasIntersectingPeriod - */ - public function test_hasIntersectingPeriod($archivesToProcess, $invalidatedArchive, $expected) - { - $periods = array_flip(Piwik::$idPeriods); - foreach ($archivesToProcess as &$archive) { - $periodLabel = $periods[$archive['period']]; - $archive['periodObj'] = Factory::build($periodLabel, $archive['date1']); - } - - $periodLabel = $periods[$invalidatedArchive['period']]; - $invalidatedArchive['periodObj'] = Factory::build($periodLabel, $invalidatedArchive['date1']); - - $actual = QueueConsumer::hasIntersectingPeriod($archivesToProcess, $invalidatedArchive); - $this->assertEquals($expected, $actual); - } - - public function getTestDataForHasIntersectingPeriod() - { - return [ - // no intersecting periods - [ - [ - ['period' => 1, 'date1' => '2020-03-04', 'date2' => '2020-03-04'], - ['period' => 3, 'date1' => '2020-04-01', 'date2' => '2020-04-30'], - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==abc'], - ], - ['period' => 1, 'date1' => '2020-03-05', 'date2' => '2020-03-05'], - false, - ], - - // intersecting periods - [ - [ - ['period' => 1, 'date1' => '2020-03-04', 'date2' => '2020-03-04'], - ['period' => 3, 'date1' => '2020-04-01', 'date2' => '2020-04-30'], - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==abc'], - ], - ['period' => 2, 'date1' => '2020-03-02', 'date2' => '2020-03-08'], - true, - ], - - // all same period, different segments - [ - [ - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==def'], - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==ghi'], - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==abc'], - ], - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==lmn'], - false, - ], - - // all same period, all visits in one - [ - [ - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => ''], - ], - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==lmn'], - true, - ], - - // all same period, different segments, all visits in next - [ - [ - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==def'], - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==ghi'], - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15', 'segment' => 'pageUrl==abc'], - ], - ['period' => 1, 'date1' => '2020-03-15', 'date2' => '2020-03-15'], - true, - ], - ]; - } -}
\ No newline at end of file |