1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PPP\Wikidata\ValueFormatters\JsonLd\Entity; |
4
|
|
|
|
5
|
|
|
use OutOfBoundsException; |
6
|
|
|
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdFormatterTestBase; |
7
|
|
|
use ValueFormatters\FormatterOptions; |
8
|
|
|
use Wikibase\DataModel\Entity\EntityIdParsingException; |
9
|
|
|
use Wikibase\DataModel\Entity\Item; |
10
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
11
|
|
|
use Wikibase\DataModel\Term\Fingerprint; |
12
|
|
|
use Wikibase\DataModel\Term\Term; |
13
|
|
|
use Wikibase\DataModel\Term\TermList; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers PPP\Wikidata\ValueFormatters\JsonLd\Entity\UnitSymbolFormatter |
17
|
|
|
* |
18
|
|
|
* @licence GPLv2+ |
19
|
|
|
* @author Thomas Pellissier Tanon |
20
|
|
|
*/ |
21
|
|
|
class UnitSymbolFormatterTest extends JsonLdFormatterTestBase { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @see JsonLdFormatterTestBase::validProvider |
25
|
|
|
*/ |
26
|
|
|
public function validProvider() { |
27
|
|
|
$item = new Item(new ItemId('Q11573'), new Fingerprint(new TermList(array(new Term('en', 'meter'))))); |
28
|
|
|
|
29
|
|
|
$entityIriParserMock = $this->getMock('Wikibase\DataModel\Entity\EntityIdParser'); |
30
|
|
|
$entityIriParserMock->expects($this->any()) |
31
|
|
|
->method('parse') |
32
|
|
|
->with($this->equalTo('http://www.wikidata.org/entity/Q11573')) |
33
|
|
|
->willReturn(new ItemId('Q11573')); |
34
|
|
|
|
35
|
|
|
$itemLookupMock = $this->getMock('Wikibase\DataModel\Services\Lookup\ItemLookup'); |
36
|
|
|
$itemLookupMock->expects($this->any()) |
37
|
|
|
->method('getItemForId') |
38
|
|
|
->with($this->equalTo(new ItemId('Q11573'))) |
39
|
|
|
->willReturn($item); |
40
|
|
|
|
41
|
|
|
$entityOntologyMockSuccess = $this->getMockBuilder('PPP\Wikidata\ValueFormatters\JsonLd\Entity\EntityOntology') |
42
|
|
|
->disableOriginalConstructor() |
43
|
|
|
->getMock(); |
44
|
|
|
$entityOntologyMockSuccess->expects($this->once()) |
45
|
|
|
->method('getUnitSymbol') |
46
|
|
|
->with($this->equalTo($item)) |
47
|
|
|
->willReturn('m'); |
48
|
|
|
|
49
|
|
|
$entityOntologyMockFailure = $this->getMockBuilder('PPP\Wikidata\ValueFormatters\JsonLd\Entity\EntityOntology') |
50
|
|
|
->disableOriginalConstructor() |
51
|
|
|
->getMock(); |
52
|
|
|
$entityOntologyMockFailure->expects($this->once()) |
53
|
|
|
->method('getUnitSymbol') |
54
|
|
|
->with($this->equalTo($item)) |
55
|
|
|
->willThrowException(new OutOfBoundsException()); |
56
|
|
|
|
57
|
|
|
return array( |
58
|
|
|
array( |
59
|
|
|
'http://www.wikidata.org/entity/Q11573', |
60
|
|
|
'm', |
61
|
|
|
null, |
62
|
|
|
new UnitSymbolFormatter($entityIriParserMock, $entityOntologyMockSuccess, $itemLookupMock) |
63
|
|
|
), |
64
|
|
|
array( |
65
|
|
|
'http://www.wikidata.org/entity/Q11573', |
66
|
|
|
'meter', |
67
|
|
|
null, |
68
|
|
|
new UnitSymbolFormatter($entityIriParserMock, $entityOntologyMockFailure, $itemLookupMock) |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testWithEntityIdParsingException() { |
74
|
|
|
$entityIriParserMock = $this->getMock('Wikibase\DataModel\Entity\EntityIdParser'); |
75
|
|
|
$entityIriParserMock->expects($this->once()) |
76
|
|
|
->method('parse') |
77
|
|
|
->with($this->equalTo('http://www.wikidata.org/entity/Q11573')) |
78
|
|
|
->willThrowException(new EntityIdParsingException()); |
79
|
|
|
|
80
|
|
|
$itemLookupMock = $this->getMock('Wikibase\DataModel\Services\Lookup\ItemLookup'); |
81
|
|
|
$entityOntologyMock = $this->getMockBuilder('PPP\Wikidata\ValueFormatters\JsonLd\Entity\EntityOntology') |
82
|
|
|
->disableOriginalConstructor() |
83
|
|
|
->getMock(); |
84
|
|
|
$formatter = new UnitSymbolFormatter($entityIriParserMock, $entityOntologyMock, $itemLookupMock); |
85
|
|
|
|
86
|
|
|
$this->setExpectedException('ValueFormatters\FormattingException'); |
87
|
|
|
$formatter->format('http://www.wikidata.org/entity/Q11573'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testWithItemNotFoundException() { |
91
|
|
|
$entityIriParserMock = $this->getMock('Wikibase\DataModel\Entity\EntityIdParser'); |
92
|
|
|
$entityIriParserMock->expects($this->once()) |
93
|
|
|
->method('parse') |
94
|
|
|
->with($this->equalTo('http://www.wikidata.org/entity/Q11573')) |
95
|
|
|
->willReturn(new ItemId('Q11573')); |
96
|
|
|
|
97
|
|
|
$itemLookupMock = $this->getMock('Wikibase\DataModel\Services\Lookup\ItemLookup'); |
98
|
|
|
$itemLookupMock->expects($this->once()) |
99
|
|
|
->method('getItemForId') |
100
|
|
|
->with($this->equalTo(new ItemId('Q11573'))) |
101
|
|
|
->willReturn(null); |
102
|
|
|
|
103
|
|
|
$entityOntologyMock = $this->getMockBuilder('PPP\Wikidata\ValueFormatters\JsonLd\Entity\EntityOntology') |
104
|
|
|
->disableOriginalConstructor() |
105
|
|
|
->getMock(); |
106
|
|
|
$formatter = new UnitSymbolFormatter($entityIriParserMock, $entityOntologyMock, $itemLookupMock); |
107
|
|
|
|
108
|
|
|
$this->setExpectedException('ValueFormatters\FormattingException'); |
109
|
|
|
$formatter->format('http://www.wikidata.org/entity/Q11573'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testWithoutSymbolAndLabel() { |
113
|
|
|
$entityIriParserMock = $this->getMock('Wikibase\DataModel\Entity\EntityIdParser'); |
114
|
|
|
$entityIriParserMock->expects($this->once()) |
115
|
|
|
->method('parse') |
116
|
|
|
->with($this->equalTo('http://www.wikidata.org/entity/Q11573')) |
117
|
|
|
->willReturn(new ItemId('Q11573')); |
118
|
|
|
|
119
|
|
|
$itemLookupMock = $this->getMock('Wikibase\DataModel\Services\Lookup\ItemLookup'); |
120
|
|
|
$itemLookupMock->expects($this->once()) |
121
|
|
|
->method('getItemForId') |
122
|
|
|
->with($this->equalTo(new ItemId('Q11573'))) |
123
|
|
|
->willReturn(new Item(new ItemId('Q11573'))); |
124
|
|
|
|
125
|
|
|
$entityOntologyMock = $this->getMockBuilder('PPP\Wikidata\ValueFormatters\JsonLd\Entity\EntityOntology') |
126
|
|
|
->disableOriginalConstructor() |
127
|
|
|
->getMock(); |
128
|
|
|
$entityOntologyMock->expects($this->once()) |
129
|
|
|
->method('getUnitSymbol') |
130
|
|
|
->with($this->equalTo(new Item(new ItemId('Q11573')))) |
131
|
|
|
->willThrowException(new OutOfBoundsException()); |
132
|
|
|
|
133
|
|
|
$formatter = new UnitSymbolFormatter($entityIriParserMock, $entityOntologyMock, $itemLookupMock); |
134
|
|
|
|
135
|
|
|
$this->setExpectedException('ValueFormatters\FormattingException'); |
136
|
|
|
$formatter->format('http://www.wikidata.org/entity/Q11573'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @see JsonLdFormatterTestBase::getInstance |
141
|
|
|
*/ |
142
|
|
|
protected function getInstance(FormatterOptions $options = null) { |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|