diff options
author | dizzy <diosmosis@users.noreply.github.com> | 2021-03-12 05:45:08 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-12 05:45:08 +0300 |
commit | b669dfd33517935d98e646edb21dfc8d5f637f0f (patch) | |
tree | b758c90936dd32214e89f1ead237175757fa2bf5 /tests/PHPUnit/Unit | |
parent | ef21f8e995a8605c84ff67636e0091efa274586a (diff) |
Make sure not to clear the tracker cache so often when invalidating in core:archive (#17321)
* Make sure not to clear the tracker cache so often when invalidating in core:archive
* apply review feedback
* remove comment
* missed another withDelegatedCacheClears call
* fix test
* fix test
Diffstat (limited to 'tests/PHPUnit/Unit')
-rw-r--r-- | tests/PHPUnit/Unit/Tracker/CacheTest.php | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/PHPUnit/Unit/Tracker/CacheTest.php b/tests/PHPUnit/Unit/Tracker/CacheTest.php new file mode 100644 index 0000000000..145cd56004 --- /dev/null +++ b/tests/PHPUnit/Unit/Tracker/CacheTest.php @@ -0,0 +1,72 @@ +<?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\Tracker; + +use Matomo\Cache\Lazy; +use Piwik\Tests\Framework\TestCase\UnitTestCase; +use Piwik\Tracker\Cache; + +class CacheTest extends UnitTestCase +{ + private $methodsCalled; + + public function setUp(): void + { + parent::setUp(); + $this->methodsCalled = []; + } + + public function test_withDelegatedCacheClears_onlyCallsCacheClearsAtTheEndOnce() + { + $this->assertEmpty($this->methodsCalled); + Cache::withDelegatedCacheClears(function () { + Cache::clearCacheGeneral(); + Cache::deleteTrackerCache(); + Cache::clearCacheGeneral(); + Cache::deleteCacheWebsiteAttributes(1); + Cache::clearCacheGeneral(); + Cache::deleteCacheWebsiteAttributes(1); + Cache::deleteCacheWebsiteAttributes(1); + Cache::deleteCacheWebsiteAttributes(2); + Cache::deleteTrackerCache(); + Cache::deleteCacheWebsiteAttributes(2); + Cache::deleteTrackerCache(); + + $this->assertEmpty($this->methodsCalled); + }); + + $expectedCalls = [ + 'delete.general', + 'flushAll', + 'delete.1', + 'delete.2', + ]; + $this->assertEquals($expectedCalls, $this->methodsCalled); + } + + public function provideContainerConfig() + { + $mockLazyCache = $this->getMockBuilder(Lazy::class) + ->onlyMethods(['flushAll', 'delete']) + ->disableOriginalConstructor() + ->getMock(); + $mockLazyCache + ->method('flushAll')->willReturnCallback(function () { + $this->methodsCalled[] = 'flushAll'; + }); + $mockLazyCache->method('delete')->willReturnCallback(function ($key) { + $this->methodsCalled[] = 'delete.' . $key; + }); + + return [ + Lazy::class => $mockLazyCache, + ]; + } +}
\ No newline at end of file |