|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\Item; |
|
6
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @covers Wikibase\EntityStore\Cache\CachedItemLookup |
|
10
|
|
|
* |
|
11
|
|
|
* @licence GPLv2+ |
|
12
|
|
|
* @author Thomas Pellissier Tanon |
|
13
|
|
|
*/ |
|
14
|
|
|
class CachedItemLookupTest extends \PHPUnit_Framework_TestCase { |
|
15
|
|
|
|
|
16
|
|
|
public function testGetItemForIdWithCacheHit() { |
|
17
|
|
|
$item = new Item( new ItemId( 'Q1' ) ); |
|
18
|
|
|
|
|
19
|
|
|
$itemLookupMock = $this->getMockBuilder( 'Wikibase\DataModel\Services\Lookup\ItemLookup' ) |
|
20
|
|
|
->disableOriginalConstructor() |
|
21
|
|
|
->getMock(); |
|
22
|
|
|
|
|
23
|
|
|
$entityDocumentCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityDocumentCache' ) |
|
24
|
|
|
->disableOriginalConstructor() |
|
25
|
|
|
->getMock(); |
|
26
|
|
|
$entityDocumentCacheMock->expects( $this->once() ) |
|
27
|
|
|
->method( 'fetch' ) |
|
28
|
|
|
->with( $this->equalTo( new ItemId( 'Q1' ) ) ) |
|
29
|
|
|
->willReturn( $item ); |
|
30
|
|
|
|
|
31
|
|
|
$entityLookup = new CachedItemLookup( $itemLookupMock, $entityDocumentCacheMock ); |
|
32
|
|
|
$this->assertEquals( |
|
33
|
|
|
$item, |
|
34
|
|
|
$entityLookup->getItemForId( new ItemId( 'Q1' ) ) |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testGetItemForIdWithCacheMiss() { |
|
39
|
|
|
$item = new Item( new ItemId( 'Q1' ) ); |
|
40
|
|
|
|
|
41
|
|
|
$itemLookupMock = $this->getMockBuilder( 'Wikibase\DataModel\Services\Lookup\ItemLookup' ) |
|
42
|
|
|
->disableOriginalConstructor() |
|
43
|
|
|
->getMock(); |
|
44
|
|
|
$itemLookupMock->expects( $this->once() ) |
|
45
|
|
|
->method( 'getItemForId' ) |
|
46
|
|
|
->with( $this->equalTo( new ItemId( 'Q1' ) ) ) |
|
47
|
|
|
->willReturn( $item ); |
|
48
|
|
|
|
|
49
|
|
|
$entityDocumentCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityDocumentCache' ) |
|
50
|
|
|
->disableOriginalConstructor() |
|
51
|
|
|
->getMock(); |
|
52
|
|
|
$entityDocumentCacheMock->expects( $this->once() ) |
|
53
|
|
|
->method( 'fetch' ) |
|
54
|
|
|
->with( $this->equalTo( new ItemId( 'Q1' ) ) ) |
|
55
|
|
|
->willReturn( null ); |
|
56
|
|
|
|
|
57
|
|
|
$entityLookup = new CachedItemLookup( $itemLookupMock, $entityDocumentCacheMock ); |
|
58
|
|
|
$this->assertEquals( |
|
59
|
|
|
$item, |
|
60
|
|
|
$entityLookup->getItemForId( new ItemId( 'Q1' ) ) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testGetItemForIdWithoutDocument() { |
|
65
|
|
|
$itemLookupMock = $this->getMockBuilder( 'Wikibase\DataModel\Services\Lookup\ItemLookup' ) |
|
66
|
|
|
->disableOriginalConstructor() |
|
67
|
|
|
->getMock(); |
|
68
|
|
|
$itemLookupMock->expects( $this->once() ) |
|
69
|
|
|
->method( 'getItemForId' ) |
|
70
|
|
|
->with( $this->equalTo( new ItemId( 'Q1' ) ) ) |
|
71
|
|
|
->willReturn( null ); |
|
72
|
|
|
|
|
73
|
|
|
$entityDocumentCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityDocumentCache' ) |
|
74
|
|
|
->disableOriginalConstructor() |
|
75
|
|
|
->getMock(); |
|
76
|
|
|
$entityDocumentCacheMock->expects( $this->once() ) |
|
77
|
|
|
->method( 'fetch' ) |
|
78
|
|
|
->with( $this->equalTo( new ItemId( 'Q1' ) ) ) |
|
79
|
|
|
->willReturn( null ); |
|
80
|
|
|
|
|
81
|
|
|
$entityLookup = new CachedItemLookup( $itemLookupMock, $entityDocumentCacheMock ); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertNull( $entityLookup->getItemForId( new ItemId( 'Q1' ) ) ); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|