testGetItemsForQueryWithCacheHit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 21
rs 9.3142
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\EntityStore\Cache;
4
5
use Ask\Language\Description\AnyValue;
6
use OutOfBoundsException;
7
use Wikibase\DataModel\Entity\ItemId;
8
9
/**
10
 * @covers Wikibase\EntityStore\Cache\CachedItemIdForQueryLookup
11
 *
12
 * @licence GPLv2+
13
 * @author Thomas Pellissier Tanon
14
 */
15
class CachedItemIdForQueryLookupTest extends \PHPUnit_Framework_TestCase {
16
17
	public function testGetItemsForQueryWithCacheHit() {
18
		$itemIds = [ new ItemId( 'Q1' ) ];
19
20
		$itemIdForQueryLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\ItemIdForQueryLookup' )
21
			->disableOriginalConstructor()
22
			->getMock();
23
24
		$entityIdForQueryCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityIdForQueryCache' )
25
			->disableOriginalConstructor()
26
			->getMock();
27
		$entityIdForQueryCacheMock->expects( $this->once() )
28
			->method( 'fetch' )
29
			->with( $this->equalTo( new AnyValue() ), $this->isNull(), $this->equalTo( 'item' ) )
30
			->willReturn( $itemIds );
31
32
		$itemIdForQueryLookup = new CachedItemIdForQueryLookup( $itemIdForQueryLookupMock, $entityIdForQueryCacheMock );
33
		$this->assertEquals(
34
			$itemIds,
35
			$itemIdForQueryLookup->getItemIdsForQuery( new AnyValue() )
36
		);
37
	}
38
39
	public function testGetItemsForQueryWithCacheMiss() {
40
		$itemIds = [ new ItemId( 'Q1' ) ];
41
42
		$itemIdForQueryLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\ItemIdForQueryLookup' )
43
			->disableOriginalConstructor()
44
			->getMock();
45
		$itemIdForQueryLookupMock->expects( $this->once() )
46
			->method( 'getItemIdsForQuery' )
47
			->with( $this->equalTo( new AnyValue() ) )
48
			->willReturn( $itemIds );
49
50
		$entityIdForQueryCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityIdForQueryCache' )
51
			->disableOriginalConstructor()
52
			->getMock();
53
		$entityIdForQueryCacheMock->expects( $this->once() )
54
			->method( 'fetch' )
55
			->with( $this->equalTo( new AnyValue() ), $this->isNull(), $this->equalTo( 'item' ) )
56
			->willThrowException( new OutOfBoundsException() );
57
58
		$itemIdForQueryLookup = new CachedItemIdForQueryLookup( $itemIdForQueryLookupMock, $entityIdForQueryCacheMock );
59
		$this->assertEquals(
60
			$itemIds,
61
			$itemIdForQueryLookup->getItemIdsForQuery( new AnyValue() )
62
		);
63
	}
64
}
65