Debian Codegraph @

extensions/Echo/tests/phpunit/Cache/TitleLocalCacheTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Test\Cache;
4
5 use MediaWiki\Extension\Notifications\Cache\TitleLocalCache;
6 use MediaWiki\Title\Title;
7 use MediaWikiIntegrationTestCase;
8 use Wikimedia\TestingAccessWrapper;
9
10 /**
11 * @covers \MediaWiki\Extension\Notifications\Cache\TitleLocalCache
12 * @group Database
13 */
14 class TitleLocalCacheTest extends MediaWikiIntegrationTestCase {
15
16 public function testAdd() {
17 $cache = $this->mockTitleLocalCache();
18
19 $cache->add( 1 );
20 $cache->add( 9 );
21
22 // Resolutions should be batched
23 $cache->expects( $this->once() )->method( 'resolve' )
24 ->with( [ 1, 9 ] )->willReturn( [] );
25
26 // Trigger
27 $cache->get( 9 );
28 }
29
30 public function testGet() {
31 $cache = $this->mockTitleLocalCache();
32 $cachePriv = TestingAccessWrapper::newFromObject( $cache );
33
34 // First title included in cache
35 $res1 = $this->insertPage( 'TitleLocalCacheTest_testGet1' );
36 $cachePriv->targets->set( $res1['id'], $res1['title'] );
37 // Second title not in internal cache, resolves from db.
38 $res2 = $this->insertPage( 'TitleLocalCacheTest_testGet2' );
39 $cache->expects( $this->once() )->method( 'resolve' )
40 ->with( [ $res2['id'] ] )
41 ->willReturn( [ $res2['id'] => $res2['title'] ] );
42
43 // Register demand for both
44 $cache->add( $res1['id'] );
45 $cache->add( $res2['id'] );
46
47 // Should not call resolve() for first title
48 $this->assertSame( $res1['title'], $cache->get( $res1['id'] ), 'First title' );
49
50 // Should resolve() for second title
51 $this->assertSame( $res2['title'], $cache->get( $res2['id'] ), 'Second title' );
52 }
53
54 public function testClearAll() {
55 $cache = $this->mockTitleLocalCache();
56
57 // Add 1 to cache
58 $cachePriv = TestingAccessWrapper::newFromObject( $cache );
59 $cachePriv->targets->set( 1, $this->mockTitle() );
60 // Add 2 and 3 to demand
61 $cache->add( 2 );
62 $cache->add( 3 );
63 $cache->clearAll();
64
65 $this->assertNull( $cache->get( 1 ), 'Cache was cleared' );
66
67 // Lookups batch was cleared
68 $cache->expects( $this->once() )->method( 'resolve' )
69 ->with( [ 4 ] )
70 ->willReturn( [] );
71 $cache->add( 4 );
72 $cache->get( 4 );
73 }
74
75 /**
76 * @return Title
77 */
78 protected function mockTitle() {
79 $title = $this->createMock( Title::class );
80
81 return $title;
82 }
83
84 private function mockTitleLocalCache(): TitleLocalCache {
85 $services = $this->getServiceContainer();
86 return $this->getMockBuilder( TitleLocalCache::class )
87 ->setConstructorArgs( [
88 $services->getPageStore(),
89 $services->getTitleFactory()
90 ] )
91 ->onlyMethods( [ 'resolve' ] )->getMock();
92 }
93 }