Debian Codegraph @

tests/phpunit/unit/includes/libs/WRStats/BagOStuffStatsStoreTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\WRStats;
4
5 use PHPUnit\Framework\TestCase;
6 use Wikimedia\ObjectCache\HashBagOStuff;
7 use Wikimedia\WRStats\BagOStuffStatsStore;
8 use Wikimedia\WRStats\EntityKey;
9 use Wikimedia\WRStats\GlobalEntityKey;
10 use Wikimedia\WRStats\LocalEntityKey;
11
12 /**
13 * @covers \Wikimedia\WRStats\BagOStuffStatsStore
14 */
15 class BagOStuffStatsStoreTest extends TestCase {
16
17 /**
18 * @var HashBagOStuff
19 */
20 private $cache;
21
22 /**
23 * @var float
24 */
25 private $mockTime = 1000000.0;
26
27 public function setUp(): void {
28 parent::setUp();
29 $this->cache = new HashBagOStuff();
30 $this->cache->setMockTime( $this->mockTime );
31 }
32
33 private function tickMockTime( $time ) {
34 $this->mockTime += $time;
35 $this->cache->setMockTime( $this->mockTime );
36 }
37
38 private function getStatsStore() {
39 return new BagOStuffStatsStore( $this->cache );
40 }
41
42 public static function provideMakeKey() {
43 yield [ [ 'prefix' ], [ 'internals' ], new LocalEntityKey( [ 'key' ] ), 'local:prefix:internals:key' ];
44 yield [ [ 'prefix' ], [ 'internals' ], new GlobalEntityKey( [ 'key' ] ), 'global:prefix:internals:key' ];
45 yield [ [ 'p', 'q' ], [ 'i', 'j' ], new GlobalEntityKey( [ 'k', 'h' ] ), 'global:p:q:i:j:k:h' ];
46 }
47
48 /**
49 * @param array $prefix
50 * @param array $internals
51 * @param EntityKey $entity
52 * @param string $expected
53 *
54 * @dataProvider provideMakeKey
55 */
56 public function testMakeKey( $prefix, $internals, $entity, $expected ) {
57 $store = $this->getStatsStore();
58 $this->assertSame(
59 $expected,
60 $store->makeKey(
61 $prefix,
62 $internals,
63 $entity
64 )
65 );
66 }
67
68 public function testIncrAndExpiry() {
69 $store = $this->getStatsStore();
70
71 $store->incr( [ 'a' => 1, 'b' => 2 ], 10 );
72
73 $this->tickMockTime( 2 );
74
75 $store->incr( [ 'b' => 1, 'c' => 1 ], 10 );
76
77 $values = $store->query( [ 'a', 'b', 'c' ] );
78 $this->assertSame( 1, $values['a'] );
79 $this->assertSame( 3, $values['b'] );
80 $this->assertSame( 1, $values['c'] );
81
82 $this->tickMockTime( 9 );
83
84 // The TTL is counted from the time the value was first set,
85 // not the time it was last updated. So the entries
86 // for a and b should have expired now.
87 $values = $store->query( [ 'a', 'b', 'c' ] );
88 $this->assertArrayNotHasKey( 'a', $values );
89 $this->assertArrayNotHasKey( 'b', $values );
90 $this->assertSame( 1, $values['c'] );
91 }
92
93 public function testDelete() {
94 $store = $this->getStatsStore();
95
96 $store->incr( [ 'a' => 1, 'b' => 2 ], 10 );
97
98 $store->delete( [ 'b' ] );
99
100 $values = $store->query( [ 'a', 'b' ] );
101 $this->assertSame( 1, $values['a'] );
102 $this->assertArrayNotHasKey( 'b', $values );
103 }
104
105 }