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

testGetPropertyForIdWithException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4286
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\EntityStore\Internal;
4
5
use Wikibase\DataModel\Entity\Item;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Entity\Property;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Services\Lookup\EntityLookupException;
10
11
/**
12
 * @covers Wikibase\EntityStore\Internal\DispatchingEntityLookup
13
 *
14
 * @licence GPLv2+
15
 * @author Thomas Pellissier Tanon
16
 */
17
class DispatchingEntityLookupTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testGetEntity() {
20
		$item = new Item( new ItemId( 'Q1' ) );
21
22
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
23
			->disableOriginalConstructor()
24
			->getMock();
25
		$entityDocumentLookupMock->expects( $this->once() )
26
			->method( 'getEntityDocumentForId' )
27
			->with( $this->equalTo( new ItemId( 'Q1' ) ) )
28
			->willReturn( $item );
29
30
		$entityLookup = new DispatchingEntityLookup( $entityDocumentLookupMock );
31
		$this->assertEquals(
32
			$item,
33
			$entityLookup->getEntityDocumentForId( new ItemId( 'Q1' ) )
34
		);
35
	}
36
37
	public function testHasEntity() {
38
		$item = new Item( new ItemId( 'Q1' ) );
39
40
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
41
			->disableOriginalConstructor()
42
			->getMock();
43
		$entityDocumentLookupMock->expects( $this->once() )
44
			->method( 'getEntityDocumentForId' )
45
			->with( $this->equalTo( new ItemId( 'Q1' ) ) )
46
			->willReturn( $item );
47
48
		$entityLookup = new DispatchingEntityLookup( $entityDocumentLookupMock );
49
		$this->assertEquals(
50
			$item,
51
			$entityLookup->getEntityDocumentForId( new ItemId( 'Q1' ) )
52
		);
53
	}
54
55
	public function testGetEntityDocumentForId() {
56
		$item = new Item( new ItemId( 'Q1' ) );
57
58
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
59
			->disableOriginalConstructor()
60
			->getMock();
61
		$entityDocumentLookupMock->expects( $this->once() )
62
			->method( 'getEntityDocumentForId' )
63
			->with( $this->equalTo( new ItemId( 'Q1' ) ) )
64
			->willReturn( $item );
65
66
		$entityLookup = new DispatchingEntityLookup( $entityDocumentLookupMock );
67
		$this->assertEquals(
68
			$item,
69
			$entityLookup->getEntityDocumentForId( new ItemId( 'Q1' ) )
70
		);
71
	}
72
73
	public function testGetEntityDocumentForIdWithException() {
74
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
75
			->disableOriginalConstructor()
76
			->getMock();
77
		$entityDocumentLookupMock->expects( $this->once() )
78
			->method( 'getEntityDocumentForId' )
79
			->with( $this->equalTo( new ItemId( 'Q1' ) ) )
80
			->willThrowException( new EntityLookupException( new ItemId( 'Q1' ) ) );
81
82
		$entityLookup = new DispatchingEntityLookup( $entityDocumentLookupMock );
83
		$this->setExpectedException( 'Wikibase\DataModel\Services\Lookup\EntityLookupException' );
84
		$entityLookup->getEntityDocumentForId( new ItemId( 'Q1' ) );
85
	}
86
87
	public function testGetEntityDocumentsForIds() {
88
		$item = new Item( new ItemId( 'Q1' ) );
89
90
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
91
			->disableOriginalConstructor()
92
			->getMock();
93
		$entityDocumentLookupMock->expects( $this->once() )
94
			->method( 'getEntityDocumentsForIds' )
95
			->with( $this->equalTo( [ new ItemId( 'Q1' ) ] ) )
96
			->willReturn( [ $item ] );
97
98
		$entityLookup = new DispatchingEntityLookup( $entityDocumentLookupMock );
99
		$this->assertEquals(
100
			[ $item ],
101
			$entityLookup->getEntityDocumentsForIds( [ new ItemId( 'Q1' ) ] )
102
		);
103
	}
104
105
	public function testGetItemForId() {
106
		$item = new Item( new ItemId( 'Q1' ) );
107
108
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
109
			->disableOriginalConstructor()
110
			->getMock();
111
		$entityDocumentLookupMock->expects( $this->once() )
112
			->method( 'getEntityDocumentForId' )
113
			->with( $this->equalTo( new ItemId( 'Q1' ) ) )
114
			->willReturn( $item );
115
116
		$entityLookup = new DispatchingEntityLookup( $entityDocumentLookupMock );
117
		$this->assertEquals(
118
			$item,
119
			$entityLookup->getItemForId( new ItemId( 'Q1' ) )
120
		);
121
	}
122
123
	public function testGetItemForIdWithException() {
124
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
125
			->disableOriginalConstructor()
126
			->getMock();
127
		$entityDocumentLookupMock->expects( $this->once() )
128
			->method( 'getEntityDocumentForId' )
129
			->with( $this->equalTo( new ItemId( 'Q1' ) ) )
130
			->willThrowException( new EntityLookupException( new ItemId( 'Q1' ) ) );
131
132
		$entityLookup = new DispatchingEntityLookup( $entityDocumentLookupMock );
133
		$this->setExpectedException( 'Wikibase\DataModel\Services\Lookup\ItemLookupException' );
134
		$entityLookup->getItemForId( new ItemId( 'Q1' ) );
135
	}
136
137
	public function testGetPropertyForId() {
138
		$property = new Property( new PropertyId( 'P1' ), null, 'string' );
139
140
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
141
			->disableOriginalConstructor()
142
			->getMock();
143
		$entityDocumentLookupMock->expects( $this->once() )
144
			->method( 'getEntityDocumentForId' )
145
			->with( $this->equalTo( new PropertyId( 'P1' ) ) )
146
			->willReturn( $property );
147
148
		$entityLookup = new DispatchingEntityLookup( $entityDocumentLookupMock );
149
		$this->assertEquals(
150
			$property,
151
			$entityLookup->getPropertyForId( new PropertyId( 'P1' ) )
152
		);
153
	}
154
155
	public function testGetPropertyForIdWithException() {
156
		$entityDocumentLookupMock = $this->getMockBuilder( 'Wikibase\EntityStore\EntityDocumentLookup' )
157
			->disableOriginalConstructor()
158
			->getMock();
159
		$entityDocumentLookupMock->expects( $this->once() )
160
			->method( 'getEntityDocumentForId' )
161
			->with( $this->equalTo( new PropertyId( 'P1' ) ) )
162
			->willThrowException( new EntityLookupException( new PropertyId( 'P1' ) ) );
163
164
		$entityLookup = new DispatchingEntityLookup( $entityDocumentLookupMock );
165
		$this->setExpectedException( 'Wikibase\DataModel\Services\Lookup\PropertyLookupException' );
166
		$entityLookup->getPropertyForId( new PropertyId( 'P1' ) );
167
	}
168
}
169