Debian Codegraph @

tests/phpunit/unit/includes/Storage/EditResultCacheTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use MediaWiki\Config\ServiceOptions;
6 use MediaWiki\Json\FormatJson;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\Storage\EditResult;
9 use MediaWiki\Storage\EditResultCache;
10 use MediaWikiUnitTestCase;
11 use Wikimedia\ObjectCache\BagOStuff;
12 use Wikimedia\Rdbms\IConnectionProvider;
13 use Wikimedia\Rdbms\IDatabase;
14 use Wikimedia\Rdbms\SelectQueryBuilder;
15
16 /**
17 * @covers \MediaWiki\Storage\EditResultCache
18 */
19 class EditResultCacheTest extends MediaWikiUnitTestCase {
20
21 private const DUMMY_KEY = 'wiki:dummy:key';
22
23 /**
24 * Returns an EditResult for testing.
25 *
26 * @return EditResult
27 */
28 private function getEditResult(): EditResult {
29 return new EditResult(
30 false,
31 100,
32 EditResult::REVERT_ROLLBACK,
33 123,
34 125,
35 true,
36 false,
37 [ 'mw-rollback' ]
38 );
39 }
40
41 /**
42 * @covers \MediaWiki\Storage\EditResultCache::set
43 */
44 public function testSet() {
45 $editResult = $this->getEditResult();
46
47 $mainStash = $this->createMock( BagOStuff::class );
48 $mainStash->expects( $this->once() )
49 ->method( 'set' )
50 ->with(
51 self::DUMMY_KEY,
52 FormatJson::encode( $editResult )
53 )
54 ->willReturn( true );
55 $mainStash->expects( $this->once() )
56 ->method( 'makeKey' )
57 ->willReturn( self::DUMMY_KEY );
58
59 $erCache = new EditResultCache(
60 $mainStash,
61 $this->createNoOpMock( IConnectionProvider::class ),
62 new ServiceOptions(
63 EditResultCache::CONSTRUCTOR_OPTIONS,
64 [ MainConfigNames::RCMaxAge => BagOStuff::TTL_MONTH ]
65 )
66 );
67
68 $result = $erCache->set( 123, $editResult );
69 $this->assertTrue( $result, 'EditResultCache::set()' );
70 }
71
72 /**
73 * @covers \MediaWiki\Storage\EditResultCache::get
74 */
75 public function testGetFromStash() {
76 $editResult = $this->getEditResult();
77
78 $mainStash = $this->createMock( BagOStuff::class );
79 $mainStash->expects( $this->once() )
80 ->method( 'get' )
81 ->with( self::DUMMY_KEY )
82 ->willReturn( FormatJson::encode( $editResult ) );
83 $mainStash->expects( $this->once() )
84 ->method( 'makeKey' )
85 ->willReturn( self::DUMMY_KEY );
86
87 $erCache = new EditResultCache(
88 $mainStash,
89 $this->createNoOpMock( IConnectionProvider::class ),
90 new ServiceOptions(
91 EditResultCache::CONSTRUCTOR_OPTIONS,
92 [ MainConfigNames::RCMaxAge => BagOStuff::TTL_MONTH ]
93 )
94 );
95 $res = $erCache->get( 126 );
96
97 $this->assertEquals( $editResult, $res, 'Correct EditResult is returned' );
98 }
99
100 /**
101 * @covers \MediaWiki\Storage\EditResultCache::get
102 */
103 public function testGetFromRevertTag() {
104 $editResult = $this->getEditResult();
105
106 $mainStash = $this->createMock( BagOStuff::class );
107 $mainStash->expects( $this->once() )
108 ->method( 'get' )
109 ->with( self::DUMMY_KEY )
110 ->willReturn( false );
111 $mainStash->expects( $this->once() )
112 ->method( 'makeKey' )
113 ->willReturn( self::DUMMY_KEY );
114
115 $dbr = $this->createMock( IDatabase::class );
116 $dbr->expects( $this->once() )
117 ->method( 'selectField' )
118 ->willReturn( FormatJson::encode( $editResult ) );
119 $dbr->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $dbr ) );
120 $dbProvider = $this->createMock( IConnectionProvider::class );
121 $dbProvider->expects( $this->once() )
122 ->method( 'getReplicaDatabase' )
123 ->willReturn( $dbr );
124
125 $erCache = new EditResultCache(
126 $mainStash,
127 $dbProvider,
128 new ServiceOptions(
129 EditResultCache::CONSTRUCTOR_OPTIONS,
130 [ MainConfigNames::RCMaxAge => BagOStuff::TTL_MONTH ]
131 )
132 );
133 $res = $erCache->get( 126 );
134
135 $this->assertEquals( $editResult, $res, 'Correct EditResult is returned' );
136 }
137
138 /**
139 * @covers \MediaWiki\Storage\EditResultCache::get
140 */
141 public function testGetMissingEntry() {
142 $mainStash = $this->createMock( BagOStuff::class );
143 $mainStash->expects( $this->once() )
144 ->method( 'get' )
145 ->with( self::DUMMY_KEY )
146 ->willReturn( false );
147 $mainStash->expects( $this->once() )
148 ->method( 'makeKey' )
149 ->willReturn( self::DUMMY_KEY );
150
151 $dbr = $this->createMock( IDatabase::class );
152 $dbr->expects( $this->once() )
153 ->method( 'selectField' )
154 ->willReturn( false );
155 $dbr->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $dbr ) );
156 $dbProvider = $this->createMock( IConnectionProvider::class );
157 $dbProvider->expects( $this->once() )
158 ->method( 'getReplicaDatabase' )
159 ->willReturn( $dbr );
160
161 $erCache = new EditResultCache(
162 $mainStash,
163 $dbProvider,
164 new ServiceOptions(
165 EditResultCache::CONSTRUCTOR_OPTIONS,
166 [ MainConfigNames::RCMaxAge => BagOStuff::TTL_MONTH ]
167 )
168 );
169 $res = $erCache->get( 126 );
170
171 $this->assertNull( $res, 'Null is returned' );
172 }
173 }