Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / MapCacheLRUTest.php
1 <?php
2 /**
3 * @group Cache
4 */
5 class MapCacheLRUTest extends PHPUnit\Framework\TestCase {
6
7 use MediaWikiCoversValidator;
8
9 /**
10 * @covers MapCacheLRU::newFromArray()
11 * @covers MapCacheLRU::toArray()
12 * @covers MapCacheLRU::getAllKeys()
13 * @covers MapCacheLRU::clear()
14 */
15 function testArrayConversion() {
16 $raw = [ 'd' => 4, 'c' => 3, 'b' => 2, 'a' => 1 ];
17 $cache = MapCacheLRU::newFromArray( $raw, 3 );
18
19 $this->assertSame( true, $cache->has( 'a' ) );
20 $this->assertSame( true, $cache->has( 'b' ) );
21 $this->assertSame( true, $cache->has( 'c' ) );
22 $this->assertSame( 1, $cache->get( 'a' ) );
23 $this->assertSame( 2, $cache->get( 'b' ) );
24 $this->assertSame( 3, $cache->get( 'c' ) );
25
26 $this->assertSame(
27 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
28 $cache->toArray()
29 );
30 $this->assertSame(
31 [ 'a', 'b', 'c' ],
32 $cache->getAllKeys()
33 );
34
35 $cache->clear( 'a' );
36 $this->assertSame(
37 [ 'b' => 2, 'c' => 3 ],
38 $cache->toArray()
39 );
40
41 $cache->clear();
42 $this->assertSame(
43 [],
44 $cache->toArray()
45 );
46 }
47
48 /**
49 * @covers MapCacheLRU::has()
50 * @covers MapCacheLRU::get()
51 * @covers MapCacheLRU::set()
52 */
53 function testLRU() {
54 $raw = [ 'a' => 1, 'b' => 2, 'c' => 3 ];
55 $cache = MapCacheLRU::newFromArray( $raw, 3 );
56
57 $this->assertSame( true, $cache->has( 'c' ) );
58 $this->assertSame(
59 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
60 $cache->toArray()
61 );
62
63 $this->assertSame( 3, $cache->get( 'c' ) );
64 $this->assertSame(
65 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
66 $cache->toArray()
67 );
68
69 $this->assertSame( 1, $cache->get( 'a' ) );
70 $this->assertSame(
71 [ 'b' => 2, 'c' => 3, 'a' => 1 ],
72 $cache->toArray()
73 );
74
75 $cache->set( 'a', 1 );
76 $this->assertSame(
77 [ 'b' => 2, 'c' => 3, 'a' => 1 ],
78 $cache->toArray()
79 );
80
81 $cache->set( 'b', 22 );
82 $this->assertSame(
83 [ 'c' => 3, 'a' => 1, 'b' => 22 ],
84 $cache->toArray()
85 );
86
87 $cache->set( 'd', 4 );
88 $this->assertSame(
89 [ 'a' => 1, 'b' => 22, 'd' => 4 ],
90 $cache->toArray()
91 );
92
93 $cache->set( 'e', 5, 0.33 );
94 $this->assertSame(
95 [ 'e' => 5, 'b' => 22, 'd' => 4 ],
96 $cache->toArray()
97 );
98
99 $cache->set( 'f', 6, 0.66 );
100 $this->assertSame(
101 [ 'b' => 22, 'f' => 6, 'd' => 4 ],
102 $cache->toArray()
103 );
104
105 $cache->set( 'g', 7, 0.90 );
106 $this->assertSame(
107 [ 'f' => 6, 'g' => 7, 'd' => 4 ],
108 $cache->toArray()
109 );
110
111 $cache->set( 'g', 7, 1.0 );
112 $this->assertSame(
113 [ 'f' => 6, 'd' => 4, 'g' => 7 ],
114 $cache->toArray()
115 );
116 }
117 }