WikibaseResourceNodeFormatterFactoryTest   C
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 25

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 25
dl 0
loc 219
rs 5
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A newFactory() 0 9 1
B testFormatterFormatGlobeCoordinate() 0 42 1
A testFormatterFormatMonolingualText() 0 18 1
A testFormatterFormatQuantity() 0 18 1
A testFormatterFormatString() 0 17 1
A testFormatterFormatTime() 0 18 1
A testFormatterFormatUnknown() 0 17 1
B testFormatterFormatWikibaseItem() 0 27 1
A testFormatterFormatWikibaseProperty() 0 16 1
A getQ42() 0 6 1
A getP214() 0 7 1
A getP625() 0 10 1
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters;
4
5
use DataValues\DecimalValue;
6
use DataValues\Geo\Values\GlobeCoordinateValue;
7
use DataValues\Geo\Values\LatLongValue;
8
use DataValues\MonolingualTextValue;
9
use DataValues\QuantityValue;
10
use DataValues\StringValue;
11
use DataValues\TimeValue;
12
use DataValues\UnknownValue;
13
use Doctrine\Common\Cache\ArrayCache;
14
use PPP\DataModel\JsonLdResourceNode;
15
use PPP\Wikidata\WikibaseResourceNode;
16
use Wikibase\DataModel\Entity\EntityIdValue;
17
use Wikibase\DataModel\Entity\Item;
18
use Wikibase\DataModel\Entity\ItemId;
19
use Wikibase\DataModel\Entity\Property;
20
use Wikibase\DataModel\Entity\PropertyId;
21
use Wikibase\DataModel\Snak\PropertyValueSnak;
22
use Wikibase\DataModel\Statement\Statement;
23
use Wikibase\DataModel\Statement\StatementList;
24
use Wikibase\DataModel\Term\Fingerprint;
25
use Wikibase\DataModel\Term\Term;
26
use Wikibase\DataModel\Term\TermList;
27
use Wikibase\EntityStore\InMemory\InMemoryEntityStore;
28
29
/**
30
 * @covers PPP\Wikidata\ValueFormatters\WikibaseResourceNodeFormatterFactory
31
 *
32
 * @licence AGPLv3+
33
 * @author Thomas Pellissier Tanon
34
 */
35
class WikibaseResourceNodeFormatterFactoryTest extends \PHPUnit_Framework_TestCase {
36
37
	private function newFactory() {
38
		$entityStore = new InMemoryEntityStore(array(
39
			$this->getQ42(),
40
			$this->getP214(),
41
			$this->getP625()
42
		));
43
44
		return new WikibaseResourceNodeFormatterFactory('en', $entityStore, array(), new ArrayCache());
45
	}
46
47
	public function testFormatterFormatGlobeCoordinate() {
48
		$this->assertEquals(
49
			new JsonLdResourceNode(
50
				'42, 42',
51
				(object) array(
52
					'@context' => 'http://schema.org',
53
					'@type' => 'GeoCoordinates',
54
					'name' => '42, 42',
55
					'latitude' => 42.0,
56
					'longitude' => 42.0,
57
					'@reverse' => (object) array(
58
						'geo' => array(
59
							(object) array(
60
								'@type' => 'Thing',
61
								'@id' => 'http://www.wikidata.org/entity/Q42',
62
								'name' => (object) array('@value' => 'Douglas Adams', '@language' => 'en'),
63
								'potentialAction' => array(
64
									(object) array(
65
										'@type' => 'ViewAction',
66
										'name' => array(
67
											(object) array('@value' => 'View on Wikidata', '@language' => 'en'),
68
											(object) array('@value' => 'Voir sur Wikidata', '@language' => 'fr')
69
										),
70
										'image' => '//upload.wikimedia.org/wikipedia/commons/f/ff/Wikidata-logo.svg',
71
										'target' => '//www.wikidata.org/entity/Q42'
72
									)
73
								)
74
							)
75
						)
76
					)
77
				)
78
			),
79
			$this->newFactory()->newWikibaseResourceNodeFormatter()->format(
80
				new WikibaseResourceNode(
81
					'',
82
					new GlobeCoordinateValue(new LatLongValue(42, 42), 1),
83
					new ItemId('Q42'),
84
					new PropertyId('P625')
85
				)
86
			)
87
		);
88
	}
89
90
	public function testFormatterFormatMonolingualText() {
91
		$this->assertEquals(
92
			new JsonLdResourceNode(
93
				'foo',
94
				(object) array(
95
					'@context' => 'http://schema.org',
96
					'@type' => 'Text',
97
					'http://www.w3.org/1999/02/22-rdf-syntax-ns#value' => (object) array(
98
						'@language' => 'en',
99
						'@value' => 'foo'
100
					)
101
				)
102
			),
103
			$this->newFactory()->newWikibaseResourceNodeFormatter()->format(
104
				new WikibaseResourceNode('', new MonolingualTextValue('en', 'foo'))
105
			)
106
		);
107
	}
108
109
	public function testFormatterFormatQuantity() {
110
		$this->assertEquals(
111
			new JsonLdResourceNode(
112
				'1234.0±1.0',
113
				(object) array(
114
					'@context' => 'http://schema.org',
115
					'@type' => 'QuantitativeValue',
116
					'name' => '1234.0±1.0',
117
					'value' => (object) array('@type' => 'Integer', '@value' => 1234),
118
					'minValue' => (object) array('@type' => 'Float', '@value' => 1233.3333),
119
					'maxValue' => (object) array('@type' => 'Integer', '@value' => 1235),
120
				)
121
			),
122
			$this->newFactory()->newWikibaseResourceNodeFormatter()->format(
123
				new WikibaseResourceNode('', new QuantityValue(new DecimalValue(1234), '1', new DecimalValue(1235), new DecimalValue(1233.3333)))
124
			)
125
		);
126
	}
127
128
	public function testFormatterFormatString() {
129
		$this->assertEquals(
130
			new JsonLdResourceNode(
131
				'foo',
132
				(object) array(
133
					'@context' => 'http://schema.org',
134
					'@type' => 'Text',
135
					'http://www.w3.org/1999/02/22-rdf-syntax-ns#value' => (object) array(
136
						'@value' => 'foo'
137
					)
138
				)
139
			),
140
			$this->newFactory()->newWikibaseResourceNodeFormatter()->format(
141
				new WikibaseResourceNode('', new StringValue('foo'))
142
			)
143
		);
144
	}
145
146
	public function testFormatterFormatTime() {
147
		$this->assertEquals(
148
			new JsonLdResourceNode(
149
				'1952-03-11',
150
				(object) array(
151
					'@context' => 'http://schema.org',
152
					'@type' => 'Date',
153
					'http://www.w3.org/1999/02/22-rdf-syntax-ns#value' => (object) array(
154
						'@type' => 'Date',
155
						'@value' => '1952-03-11'
156
					)
157
				)
158
			),
159
			$this->newFactory()->newWikibaseResourceNodeFormatter()->format(
160
				new WikibaseResourceNode('', new TimeValue('+1952-03-11T00:00:00Z', 0, 0, 0, TimeValue::PRECISION_DAY, 'http://www.wikidata.org/entity/Q1985786'))
161
			)
162
		);
163
	}
164
165
	public function testFormatterFormatUnknown() {
166
		$this->assertEquals(
167
			new JsonLdResourceNode(
168
				'foo',
169
				(object) array(
170
					'@context' => 'http://schema.org',
171
					'@type' => 'Text',
172
					'http://www.w3.org/1999/02/22-rdf-syntax-ns#value' => (object) array(
173
						'@value' => 'foo'
174
					)
175
				)
176
			),
177
			$this->newFactory()->newWikibaseResourceNodeFormatter()->format(
178
				new WikibaseResourceNode('', new UnknownValue('foo'))
179
			)
180
		);
181
	}
182
183
	public function testFormatterFormatWikibaseItem() {
184
		$this->assertEquals(
185
			new JsonLdResourceNode(
186
				'Douglas Adams',
187
				(object) array(
188
					'@context' => 'http://schema.org',
189
					'@type' => 'Thing',
190
					'@id' => 'http://www.wikidata.org/entity/Q42',
191
					'name' => (object) array('@value' => 'Douglas Adams', '@language' => 'en'),
192
					'potentialAction' => array(
193
						(object) array(
194
							'@type' => 'ViewAction',
195
							'name' => array(
196
								(object) array('@value' => 'View on Wikidata', '@language' => 'en'),
197
								(object) array('@value' => 'Voir sur Wikidata', '@language' => 'fr')
198
							),
199
							'image' => '//upload.wikimedia.org/wikipedia/commons/f/ff/Wikidata-logo.svg',
200
							'target' => '//www.wikidata.org/entity/Q42'
201
						)
202
					)
203
				)
204
			),
205
			$this->newFactory()->newWikibaseResourceNodeFormatter()->format(
206
				new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q42')))
207
			)
208
		);
209
	}
210
211
	public function testFormatterFormatWikibaseProperty() {
212
		$this->assertEquals(
213
			new JsonLdResourceNode(
214
				'VIAF identifier',
215
				(object) array(
216
					'@context' => 'http://schema.org',
217
					'@type' => 'Property',
218
					'@id' => 'http://www.wikidata.org/entity/P214',
219
					'name' => (object) array('@value' => 'VIAF identifier', '@language' => 'en')
220
				)
221
			),
222
			$this->newFactory()->newWikibaseResourceNodeFormatter()->format(
223
				new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P214')))
224
			)
225
		);
226
	}
227
228
	private function getQ42() {
229
		return new Item(
230
			new ItemId('Q42'),
231
			new Fingerprint(new TermList(array(new Term('en', 'Douglas Adams'))))
232
		);
233
	}
234
235
	private function getP214() {
236
		return new Property(
237
			new PropertyId('P214'),
238
			new Fingerprint(new TermList(array(new Term('en', 'VIAF identifier')))),
239
			'string'
240
		);
241
	}
242
243
	private function getP625() {
244
		return new Property(
245
			new PropertyId('P625'),
246
			new Fingerprint(new TermList(array(new Term('en', 'geo coordinates')))),
247
			'globe-coordinate',
248
			new StatementList(array(
249
				new Statement(new PropertyValueSnak(new PropertyId('P1628'), new StringValue('http://schema.org/geo')))
250
			))
251
		);
252
	}
253
}
254