| 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 |
|
| 12 |
|
| 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 |
|
| 23 |
$cache->expects( $this->once() )->method( 'resolve' ) |
| 24 |
->with( [ 1, 9 ] )->willReturn( [] ); |
| 25 |
|
| 26 |
|
| 27 |
$cache->get( 9 ); |
| 28 |
} |
| 29 |
|
| 30 |
public function testGet() { |
| 31 |
$cache = $this->mockTitleLocalCache(); |
| 32 |
$cachePriv = TestingAccessWrapper::newFromObject( $cache ); |
| 33 |
|
| 34 |
|
| 35 |
$res1 = $this->insertPage( 'TitleLocalCacheTest_testGet1' ); |
| 36 |
$cachePriv->targets->set( $res1['id'], $res1['title'] ); |
| 37 |
|
| 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 |
|
| 44 |
$cache->add( $res1['id'] ); |
| 45 |
$cache->add( $res2['id'] ); |
| 46 |
|
| 47 |
|
| 48 |
$this->assertSame( $res1['title'], $cache->get( $res1['id'] ), 'First title' ); |
| 49 |
|
| 50 |
|
| 51 |
$this->assertSame( $res2['title'], $cache->get( $res2['id'] ), 'Second title' ); |
| 52 |
} |
| 53 |
|
| 54 |
public function testClearAll() { |
| 55 |
$cache = $this->mockTitleLocalCache(); |
| 56 |
|
| 57 |
|
| 58 |
$cachePriv = TestingAccessWrapper::newFromObject( $cache ); |
| 59 |
$cachePriv->targets->set( 1, $this->mockTitle() ); |
| 60 |
|
| 61 |
$cache->add( 2 ); |
| 62 |
$cache->add( 3 ); |
| 63 |
$cache->clearAll(); |
| 64 |
|
| 65 |
$this->assertNull( $cache->get( 1 ), 'Cache was cleared' ); |
| 66 |
|
| 67 |
|
| 68 |
$cache->expects( $this->once() )->method( 'resolve' ) |
| 69 |
->with( [ 4 ] ) |
| 70 |
->willReturn( [] ); |
| 71 |
$cache->add( 4 ); |
| 72 |
$cache->get( 4 ); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 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 |
} |