summaryrefslogtreecommitdiff
blob: fdd722d3d0e11ddb9d085719c7fe25ddcefe7e84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

use Wikimedia\TestingAccessWrapper;

/**
 * @covers EchoTitleLocalCache
 * @group Database
 */
class EchoTitleLocalCacheTest extends MediaWikiTestCase {

	public function testCreate() {
		$cache = EchoTitleLocalCache::create();
		$this->assertInstanceOf( EchoTitleLocalCache::class, $cache );
	}

	public function testAdd() {
		$cache = $this->getMockBuilder( EchoTitleLocalCache::class )
			->setMethods( [ 'resolve' ] )->getMock();

		$cache->add( 1 );
		$cache->add( 9 );

		// Resolutions should be batched
		$cache->expects( $this->exactly( 1 ) )->method( 'resolve' )
			->with( [ 1, 9 ] )->willReturn( [] );

		// Trigger
		$cache->get( 9 );
	}

	public function testGet() {
		$cache = $this->getMockBuilder( EchoTitleLocalCache::class )
			->setMethods( [ 'resolve' ] )->getMock();
		$cachePriv = TestingAccessWrapper::newFromObject( $cache );

		// First title included in cache
		$res1 = $this->insertPage( 'EchoTitleLocalCacheTest_testGet1' );
		$cachePriv->targets->set( $res1['id'], $res1['title'] );
		// Second title not in internal cache, resolves from db.
		$res2 = $this->insertPage( 'EchoTitleLocalCacheTest_testGet2' );
		$cache->expects( $this->exactly( 1 ) )->method( 'resolve' )
			->with( [ $res2['id'] ] )
			->willReturn( [ $res2['id'] => $res2['title'] ] );

		// Register demand for both
		$cache->add( $res1['id'] );
		$cache->add( $res2['id'] );

		// Should not call resolve() for first title
		$this->assertSame( $res1['title'], $cache->get( $res1['id'] ), 'First title' );

		// Should resolve() for second title
		$this->assertSame( $res2['title'], $cache->get( $res2['id'] ), 'Second title' );
	}

	public function testClearAll() {
		$cache = $this->getMockBuilder( EchoTitleLocalCache::class )
			->setMethods( [ 'resolve' ] )->getMock();

		// Add 1 to cache
		$cachePriv = TestingAccessWrapper::newFromObject( $cache );
		$cachePriv->targets->set( 1, $this->mockTitle() );
		// Add 2 and 3 to demand
		$cache->add( 2 );
		$cache->add( 3 );
		$cache->clearAll();

		$this->assertSame( null, $cache->get( 1 ), 'Cache was cleared' );

		// Lookups batch was cleared
		$cache->expects( $this->exactly( 1 ) )->method( 'resolve' )
			->with( [ 4 ] )
			->willReturn( [] );
		$cache->add( 4 );
		$cache->get( 4 );
	}

	/**
	 * @return Title
	 */
	protected function mockTitle() {
		$title = $this->getMockBuilder( Title::class )
			->disableOriginalConstructor()
			->getMock();

		return $title;
	}
}