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

testGetEntityDocumentWithException()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 29
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\EntityStore\Api;
4
5
use Mediawiki\Api\SimpleRequest;
6
use Wikibase\DataModel\Entity\Item;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\Property;
9
use Wikibase\DataModel\Entity\PropertyId;
10
use Wikibase\EntityStore\EntityStore;
11
use Wikibase\EntityStore\EntityStoreOptions;
12
use Wikibase\EntityStore\Internal\EntitySerializationFactory;
13
14
/**
15
 * @covers Wikibase\EntityStore\Api\ApiEntityLookup
16
 *
17
 * @licence GPLv2+
18
 * @author Thomas Pellissier Tanon
19
 */
20
class ApiEntityLookupTest extends \PHPUnit_Framework_TestCase {
21
22
	public function testGetEntityDocumentsForIds() {
23
		$mediawikiApiMock = $this->getMockBuilder( 'Mediawiki\Api\MediawikiApi' )
24
			->disableOriginalConstructor()
25
			->getMock();
26
		$mediawikiApiMock->expects( $this->once() )
27
			->method( 'getRequest' )
28
			->with( $this->equalTo(
29
				new SimpleRequest(
30
					'wbgetentities',
31
					[
32
						'ids' => 'Q42|P42'
33
					]
34
				)
35
			) )
36
			->will( $this->returnValue( [
37
				'entities' => [
38
					[
39
						'id' => 'Q42',
40
						'type' => 'item'
41
					],
42
					[
43
						'id' => 'P42',
44
						'type' => 'property',
45
						'datatype' => 'string'
46
					]
47
				]
48
			] ) );
49
50
		$serializationFactory = new EntitySerializationFactory();
51
		$lookup = new ApiEntityLookup(
52
			$mediawikiApiMock,
53
			$serializationFactory->newEntityDeserializer(),
54
			new EntityStoreOptions( [
55
				EntityStore::OPTION_LANGUAGES => null,
56
				EntityStore::OPTION_LANGUAGE_FALLBACK => false
57
			] )
58
		);
59
60
		$this->assertEquals(
61
			[
62
				new Item( new ItemId( 'Q42' ) ),
63
				new Property( new PropertyId( 'P42' ), null, 'string' )
64
			],
65
			$lookup->getEntityDocumentsForIds( [ new ItemId( 'Q42' ), new PropertyId( 'P42' ) ] )
66
		);
67
	}
68
69
	public function testGetEntityDocumentsForIdsWithEmptyInput() {
70
		$mediawikiApiMock = $this->getMockBuilder( 'Mediawiki\Api\MediawikiApi' )
71
			->disableOriginalConstructor()
72
			->getMock();
73
74
		$serializationFactory = new EntitySerializationFactory();
75
		$lookup = new ApiEntityLookup(
76
			$mediawikiApiMock,
77
			$serializationFactory->newEntityDeserializer(),
78
			new EntityStoreOptions( [
79
				EntityStore::OPTION_LANGUAGES => null,
80
				EntityStore::OPTION_LANGUAGE_FALLBACK => false
81
			] )
82
		);
83
84
		$this->assertEquals( [], $lookup->getEntityDocumentsForIds( [] ) );
85
	}
86
87
	public function testGetEntityDocumentForId() {
88
		$mediawikiApiMock = $this->getMockBuilder( 'Mediawiki\Api\MediawikiApi' )
89
			->disableOriginalConstructor()
90
			->getMock();
91
		$mediawikiApiMock->expects( $this->once() )
92
			->method( 'getRequest' )
93
			->with( $this->equalTo(
94
				new SimpleRequest(
95
					'wbgetentities',
96
					[
97
						'ids' => 'Q42',
98
						'languages' => 'en|fr',
99
						'languagefallback' => true
100
					]
101
				)
102
			) )
103
			->will( $this->returnValue( [
104
				'entities' => [
105
					[
106
						'id' => 'Q42',
107
						'type' => 'item'
108
					]
109
				]
110
			] ) );
111
112
		$serializationFactory = new EntitySerializationFactory();
113
		$lookup = new ApiEntityLookup(
114
			$mediawikiApiMock,
115
			$serializationFactory->newEntityDeserializer(),
116
			new EntityStoreOptions( [
117
				EntityStore::OPTION_LANGUAGES => [ 'en', 'fr' ],
118
				EntityStore::OPTION_LANGUAGE_FALLBACK => true
119
			] )
120
		);
121
122
		$this->assertEquals(
123
			new Item( new ItemId( 'Q42' ) ),
124
			$lookup->getEntityDocumentForId( new ItemId( 'Q42' ) )
125
		);
126
	}
127
128
	public function testGetEntityDocumentWithoutDocument() {
129
		$mediawikiApiMock = $this->getMockBuilder( 'Mediawiki\Api\MediawikiApi' )
130
			->disableOriginalConstructor()
131
			->getMock();
132
		$mediawikiApiMock->expects( $this->once() )
133
			->method( 'getRequest' )
134
			->with( $this->equalTo(
135
				new SimpleRequest(
136
					'wbgetentities',
137
					[
138
						'ids' => 'Q42',
139
						'languages' => 'en|fr'
140
					]
141
				)
142
			) )
143
			->will( $this->returnValue( [ 'entities' => [] ] ) );
144
145
		$serializationFactory = new EntitySerializationFactory();
146
		$lookup = new ApiEntityLookup(
147
			$mediawikiApiMock,
148
			$serializationFactory->newEntityDeserializer(),
149
			new EntityStoreOptions( [
150
				EntityStore::OPTION_LANGUAGES => [ 'en', 'fr' ],
151
				EntityStore::OPTION_LANGUAGE_FALLBACK => false
152
			] ) );
153
154
		$this->assertNull( $lookup->getEntityDocumentForId( new ItemId( 'Q42' ) ) );
155
	}
156
}
157