1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueFormatters\Test; |
4
|
|
|
|
5
|
|
|
use DataValues\DecimalValue; |
6
|
|
|
use DataValues\QuantityValue; |
7
|
|
|
use DataValues\UnboundedQuantityValue; |
8
|
|
|
use ValueFormatters\DecimalFormatter; |
9
|
|
|
use ValueFormatters\FormatterOptions; |
10
|
|
|
use ValueFormatters\QuantityHtmlFormatter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @covers ValueFormatters\QuantityHtmlFormatter |
14
|
|
|
* |
15
|
|
|
* @license GPL-2.0+ |
16
|
|
|
* @author Thiemo Mättig |
17
|
|
|
*/ |
18
|
|
|
class QuantityHtmlFormatterTest extends ValueFormatterTestBase { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @deprecated since DataValues Interfaces 0.2, just use getInstance. |
22
|
|
|
*/ |
23
|
|
|
protected function getFormatterClass() { |
24
|
|
|
throw new \LogicException( 'Should not be called, use getInstance' ); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @see ValueFormatterTestBase::getInstance |
29
|
|
|
* |
30
|
|
|
* @param FormatterOptions|null $options |
31
|
|
|
* |
32
|
|
|
* @return QuantityHtmlFormatter |
33
|
|
|
*/ |
34
|
|
|
protected function getInstance( FormatterOptions $options = null ) { |
35
|
|
|
return $this->getQuantityHtmlFormatter( $options ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param FormatterOptions|null $options |
40
|
|
|
* @param DecimalFormatter|null $decimalFormatter |
41
|
|
|
* @param string|null $quantityWithUnitFormat |
42
|
|
|
* |
43
|
|
|
* @return QuantityHtmlFormatter |
44
|
|
|
*/ |
45
|
|
|
private function getQuantityHtmlFormatter( |
46
|
|
|
FormatterOptions $options = null, |
47
|
|
|
DecimalFormatter $decimalFormatter = null, |
48
|
|
|
$quantityWithUnitFormat = null |
49
|
|
|
) { |
50
|
|
|
$vocabularyUriFormatter = $this->getMock( 'ValueFormatters\ValueFormatter' ); |
51
|
|
|
$vocabularyUriFormatter->expects( $this->any() ) |
52
|
|
|
->method( 'format' ) |
53
|
|
|
->will( $this->returnCallback( function( $unit ) { |
54
|
|
|
return $unit === '1' ? null : $unit; |
55
|
|
|
} ) ); |
56
|
|
|
|
57
|
|
|
return new QuantityHtmlFormatter( |
58
|
|
|
$options, |
59
|
|
|
$decimalFormatter, |
60
|
|
|
$vocabularyUriFormatter, |
61
|
|
|
$quantityWithUnitFormat |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $className |
67
|
|
|
* @param int $uncertaintyMargin |
68
|
|
|
* |
69
|
|
|
* @return QuantityValue |
70
|
|
|
*/ |
71
|
|
|
private function newQuantityValue( $className, $uncertaintyMargin = 0 ) { |
72
|
|
|
$quantity = $this->getMockBuilder( $className ) |
73
|
|
|
->disableOriginalConstructor() |
74
|
|
|
->getMock(); |
75
|
|
|
|
76
|
|
|
$quantity->expects( $this->any() ) |
77
|
|
|
->method( 'getAmount' ) |
78
|
|
|
->will( $this->returnValue( new DecimalValue( 2 ) ) ); |
79
|
|
|
$quantity->expects( $this->any() ) |
80
|
|
|
->method( 'getUncertaintyMargin' ) |
81
|
|
|
->will( $this->returnValue( new DecimalValue( $uncertaintyMargin ) ) ); |
82
|
|
|
|
83
|
|
|
return $quantity; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @see ValueFormatterTestBase::validProvider |
88
|
|
|
*/ |
89
|
|
|
public function validProvider() { |
90
|
|
|
return array( |
|
|
|
|
91
|
|
|
'Unbounded, Unit 1' => array( |
92
|
|
|
UnboundedQuantityValue::newFromNumber( '+2', '1' ), |
93
|
|
|
'2' |
94
|
|
|
), |
95
|
|
|
'Unbounded, String unit' => array( |
96
|
|
|
UnboundedQuantityValue::newFromNumber( '+2', 'Ultrameter' ), |
97
|
|
|
'2 <span class="wb-unit">Ultrameter</span>' |
98
|
|
|
), |
99
|
|
|
'Unit 1' => array( |
100
|
|
|
QuantityValue::newFromNumber( '+2', '1', '+3', '+1' ), |
101
|
|
|
'2±1' |
102
|
|
|
), |
103
|
|
|
'String unit' => array( |
104
|
|
|
QuantityValue::newFromNumber( '+2', 'Ultrameter', '+3', '+1' ), |
105
|
|
|
'2±1 <span class="wb-unit">Ultrameter</span>' |
106
|
|
|
), |
107
|
|
|
'HTML injection' => array( |
108
|
|
|
QuantityValue::newFromNumber( '+2', '<b>injection</b>', '+2', '+2' ), |
109
|
|
|
'2 <span class="wb-unit"><b>injection</b></span>' |
110
|
|
|
), |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testFormatWithFormatString() { |
115
|
|
|
$formatter = $this->getQuantityHtmlFormatter( null, null, '$2 $1' ); |
116
|
|
|
$value = QuantityValue::newFromNumber( '+5', 'USD' ); |
117
|
|
|
$formatted = $formatter->format( $value ); |
118
|
|
|
$this->assertSame( '<span class="wb-unit">USD</span> 5', $formatted ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @dataProvider applyUnitOptionProvider |
123
|
|
|
*/ |
124
|
|
|
public function testGivenHtmlCharacters_formatEscapesHtmlCharacters( |
125
|
|
|
FormatterOptions $options = null, |
126
|
|
|
UnboundedQuantityValue $value, |
127
|
|
|
$expected |
128
|
|
|
) { |
129
|
|
|
$decimalFormatter = $this->getMock( 'ValueFormatters\DecimalFormatter' ); |
130
|
|
|
$decimalFormatter->expects( $this->any() ) |
131
|
|
|
->method( 'format' ) |
132
|
|
|
->will( $this->returnValue( '<b>+2</b>' ) ); |
133
|
|
|
|
134
|
|
|
$formatter = $this->getQuantityHtmlFormatter( $options, $decimalFormatter ); |
135
|
|
|
$formatted = $formatter->format( $value ); |
136
|
|
|
$this->assertSame( $expected, $formatted ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function applyUnitOptionProvider() { |
140
|
|
|
$noUnit = new FormatterOptions(); |
141
|
|
|
$noUnit->setOption( QuantityHtmlFormatter::OPT_APPLY_UNIT, false ); |
142
|
|
|
|
143
|
|
|
return array( |
144
|
|
|
'Disabled without unit' => array( |
145
|
|
|
$noUnit, |
146
|
|
|
QuantityValue::newFromNumber( 2, '1' ), |
147
|
|
|
'<b>+2</b>' |
148
|
|
|
), |
149
|
|
|
'Disabled with unit' => array( |
150
|
|
|
$noUnit, |
151
|
|
|
QuantityValue::newFromNumber( 2, '<b>m</b>' ), |
152
|
|
|
'<b>+2</b>' |
153
|
|
|
), |
154
|
|
|
'Default without unit' => array( |
155
|
|
|
null, |
156
|
|
|
QuantityValue::newFromNumber( 2, '1' ), |
157
|
|
|
'<b>+2</b>' |
158
|
|
|
), |
159
|
|
|
'Default with unit' => array( |
160
|
|
|
null, |
161
|
|
|
QuantityValue::newFromNumber( 2, '<b>m</b>' ), |
162
|
|
|
'<b>+2</b> <span class="wb-unit"><b>m</b></span>' |
163
|
|
|
), |
164
|
|
|
'HTML escaping bounded' => array( |
165
|
|
|
null, |
166
|
|
|
$this->newQuantityValue( 'DataValues\QuantityValue' ), |
167
|
|
|
'<b>+2</b>' |
168
|
|
|
), |
169
|
|
|
'HTML escaping bounded with uncertainty' => array( |
170
|
|
|
null, |
171
|
|
|
$this->newQuantityValue( 'DataValues\QuantityValue', 1 ), |
172
|
|
|
'<b>+2</b>±<b>+2</b>' |
173
|
|
|
), |
174
|
|
|
'HTML escaping unbounded' => array( |
175
|
|
|
null, |
176
|
|
|
$this->newQuantityValue( 'DataValues\UnboundedQuantityValue' ), |
177
|
|
|
'<b>+2</b>' |
178
|
|
|
), |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.