1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\Cache; |
4
|
|
|
|
5
|
|
|
use OutOfBoundsException; |
6
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
7
|
|
|
use Wikibase\DataModel\Term\Term; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers Wikibase\EntityStore\Cache\CachedItemIdForTermLookup |
11
|
|
|
* |
12
|
|
|
* @licence GPLv2+ |
13
|
|
|
* @author Thomas Pellissier Tanon |
14
|
|
|
*/ |
15
|
|
|
class CachedItemIdForTermLookupTest extends \PHPUnit_Framework_TestCase { |
16
|
|
|
|
17
|
|
|
public function testGetItemsForTermWithCacheHit() { |
18
|
|
|
$itemIds = [ new ItemId( 'Q1' ) ]; |
19
|
|
|
|
20
|
|
|
$itemIdForTermLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\ItemIdForTermLookup' ) |
21
|
|
|
->disableOriginalConstructor() |
22
|
|
|
->getMock(); |
23
|
|
|
|
24
|
|
|
$entityIdForTermCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityIdForTermCache' ) |
25
|
|
|
->disableOriginalConstructor() |
26
|
|
|
->getMock(); |
27
|
|
|
$entityIdForTermCacheMock->expects( $this->once() ) |
28
|
|
|
->method( 'fetch' ) |
29
|
|
|
->with( $this->equalTo( new Term( 'en', 'foo' ) ), $this->equalTo( 'item' ) ) |
30
|
|
|
->willReturn( $itemIds ); |
31
|
|
|
|
32
|
|
|
$itemIdForTermLookup = new CachedItemIdForTermLookup( $itemIdForTermLookupMock, $entityIdForTermCacheMock ); |
33
|
|
|
$this->assertEquals( |
34
|
|
|
$itemIds, |
35
|
|
|
$itemIdForTermLookup->getItemIdsForTerm( new Term( 'en', 'foo' ) ) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testGetItemsForTermWithCacheMiss() { |
40
|
|
|
$itemIds = [ new ItemId( 'Q1' ) ]; |
41
|
|
|
|
42
|
|
|
$itemIdForTermLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\ItemIdForTermLookup' ) |
43
|
|
|
->disableOriginalConstructor() |
44
|
|
|
->getMock(); |
45
|
|
|
$itemIdForTermLookupMock->expects( $this->once() ) |
46
|
|
|
->method( 'getItemIdsForTerm' ) |
47
|
|
|
->with( $this->equalTo( new Term( 'en', 'foo' ) ) ) |
48
|
|
|
->willReturn( $itemIds ); |
49
|
|
|
|
50
|
|
|
$entityIdForTermCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityIdForTermCache' ) |
51
|
|
|
->disableOriginalConstructor() |
52
|
|
|
->getMock(); |
53
|
|
|
$entityIdForTermCacheMock->expects( $this->once() ) |
54
|
|
|
->method( 'fetch' ) |
55
|
|
|
->with( $this->equalTo( new Term( 'en', 'foo' ) ), $this->equalTo( 'item' ) ) |
56
|
|
|
->willThrowException( new OutOfBoundsException() ); |
57
|
|
|
|
58
|
|
|
$itemIdForTermLookup = new CachedItemIdForTermLookup( $itemIdForTermLookupMock, $entityIdForTermCacheMock ); |
59
|
|
|
$this->assertEquals( |
60
|
|
|
$itemIds, |
61
|
|
|
$itemIdForTermLookup->getItemIdsForTerm( new Term( 'en', 'foo' ) ) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|