Completed
Push — master ( 388143...889690 )
by Thomas
05:32 queued 02:57
created

testGetEntityDocumentForIdWithException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 18

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.3143
cc 1
eloc 18
nc 1
nop 0
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\CachedEntityDocumentLookup
10
 *
11
 * @licence GPLv2+
12
 * @author Thomas Pellissier Tanon
13
 */
14
class CachedEntityDocumentLookupTest extends \PHPUnit_Framework_TestCase {
15
16
	public function testGetEntityDocumentForIdWithCacheHit() {
17
		$item = new Item( new ItemId( 'Q1' ) );
18
19
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
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 CachedEntityDocumentLookup( $entityDocumentLookupMock, $entityDocumentCacheMock );
32
		$this->assertEquals(
33
			$item,
34
			$entityLookup->getEntityDocumentForId( new ItemId( 'Q1' ) )
35
		);
36
	}
37
38
	public function testGetEntityDocumentForIdWithCacheMiss() {
39
		$item = new Item( new ItemId( 'Q1' ) );
40
41
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
42
			->disableOriginalConstructor()
43
			->getMock();
44
		$entityDocumentLookupMock->expects( $this->once() )
45
			->method( 'getEntityDocumentForId' )
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 CachedEntityDocumentLookup( $entityDocumentLookupMock, $entityDocumentCacheMock );
58
		$this->assertEquals(
59
			$item,
60
			$entityLookup->getEntityDocumentForId( new ItemId( 'Q1' ) )
61
		);
62
	}
63
64
	public function testGetEntityDocumentForIdWithoutDocument() {
65
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
66
			->disableOriginalConstructor()
67
			->getMock();
68
		$entityDocumentLookupMock->expects( $this->once() )
69
			->method( 'getEntityDocumentForId' )
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 CachedEntityDocumentLookup( $entityDocumentLookupMock, $entityDocumentCacheMock );
82
83
		$this->assertNull( $entityLookup->getEntityDocumentForId( new ItemId( 'Q1' ) ) );
84
	}
85
86
	public function testGetEntityDocumentsForIdsWithCacheHit() {
87
		$item = new Item( new ItemId( 'Q1' ) );
88
89
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
90
			->disableOriginalConstructor()
91
			->getMock();
92
93
		$entityDocumentCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityDocumentCache' )
94
			->disableOriginalConstructor()
95
			->getMock();
96
		$entityDocumentCacheMock->expects( $this->once() )
97
			->method( 'fetch' )
98
			->with( $this->equalTo( new ItemId( 'Q1' ) ) )
99
			->willReturn( $item );
100
101
		$entityLookup = new CachedEntityDocumentLookup( $entityDocumentLookupMock, $entityDocumentCacheMock );
102
		$this->assertEquals(
103
			[ $item ],
104
			$entityLookup->getEntityDocumentsForIds( [ new ItemId( 'Q1' ) ] )
105
		);
106
	}
107
108
	public function testGetEntityDocumentsForIdsWithCacheMiss() {
109
		$item = new Item( new ItemId( 'Q1' ) );
110
111
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
112
			->disableOriginalConstructor()
113
			->getMock();
114
		$entityDocumentLookupMock->expects( $this->once() )
115
			->method( 'getEntityDocumentsForIds' )
116
			->with( $this->equalTo( [ new ItemId( 'Q1' ) ] ) )
117
			->willReturn( [ $item ] );
118
119
		$entityDocumentCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityDocumentCache' )
120
			->disableOriginalConstructor()
121
			->getMock();
122
		$entityDocumentCacheMock->expects( $this->once() )
123
			->method( 'fetch' )
124
			->with( $this->equalTo( new ItemId( 'Q1' ) ) )
125
			->willReturn( null );
126
127
		$entityLookup = new CachedEntityDocumentLookup( $entityDocumentLookupMock, $entityDocumentCacheMock );
128
		$this->assertEquals(
129
			[ $item ],
130
			$entityLookup->getEntityDocumentsForIds( [ new ItemId( 'Q1' ) ] )
131
		);
132
	}
133
134
	public function testGetEntityDocumentsForIdsWithoutReturns() {
135
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
136
			->disableOriginalConstructor()
137
			->getMock();
138
		$entityDocumentLookupMock->expects( $this->once() )
139
			->method( 'getEntityDocumentsForIds' )
140
			->with( $this->equalTo( [ new ItemId( 'Q1' ) ] ) )
141
			->willReturn( [] );
142
143
		$entityDocumentCacheMock = $this->getMockBuilder( 'Wikibase\EntityStore\Cache\EntityDocumentCache' )
144
			->disableOriginalConstructor()
145
			->getMock();
146
		$entityDocumentCacheMock->expects( $this->once() )
147
			->method( 'fetch' )
148
			->with( $this->equalTo( new ItemId( 'Q1' ) ) )
149
			->willReturn( null );
150
151
		$entityLookup = new CachedEntityDocumentLookup( $entityDocumentLookupMock, $entityDocumentCacheMock );
152
		$this->assertEquals(
153
			[],
154
			$entityLookup->getEntityDocumentsForIds( [ new ItemId( 'Q1' ) ] )
155
		);
156
	}
157
}
158