1 | <?php |
||
17 | class QuantityHtmlFormatterTest extends ValueFormatterTestBase { |
||
18 | |||
19 | /** |
||
20 | * @deprecated since DataValues Interfaces 0.2, just use getInstance. |
||
21 | */ |
||
22 | protected function getFormatterClass() { |
||
25 | |||
26 | /** |
||
27 | * @see ValueFormatterTestBase::getInstance |
||
28 | * |
||
29 | * @param FormatterOptions|null $options |
||
30 | * |
||
31 | * @return QuantityHtmlFormatter |
||
32 | */ |
||
33 | protected function getInstance( FormatterOptions $options = null ) { |
||
36 | |||
37 | /** |
||
38 | * @param FormatterOptions|null $options |
||
39 | * @param DecimalFormatter|null $decimalFormatter |
||
40 | * @param string|null $quantityWithUnitFormat |
||
41 | * |
||
42 | * @return QuantityHtmlFormatter |
||
43 | */ |
||
44 | private function getQuantityHtmlFormatter( |
||
63 | |||
64 | /** |
||
65 | * @see ValueFormatterTestBase::validProvider |
||
66 | */ |
||
67 | public function validProvider() { |
||
68 | return array( |
||
|
|||
69 | 'Unbounded, Unit 1' => array( |
||
70 | UnboundedQuantityValue::newFromNumber( '+2', '1' ), |
||
71 | '2' |
||
72 | ), |
||
73 | 'Unbounded, String unit' => array( |
||
74 | UnboundedQuantityValue::newFromNumber( '+2', 'Ultrameter' ), |
||
75 | '2 <span class="wb-unit">Ultrameter</span>' |
||
76 | ), |
||
77 | 'Bounded, Unit 1' => array( |
||
78 | QuantityValue::newFromNumber( '+2', '1', '+3', '+1' ), |
||
79 | '2±1' |
||
80 | ), |
||
81 | 'Bounded, String unit' => array( |
||
82 | QuantityValue::newFromNumber( '+2', 'Ultrameter', '+3', '+1' ), |
||
83 | '2±1 <span class="wb-unit">Ultrameter</span>' |
||
84 | ), |
||
85 | 'HTML injection' => array( |
||
86 | QuantityValue::newFromNumber( '+2', '<b>injection</b>', '+2', '+2' ), |
||
87 | '2±0 <span class="wb-unit"><b>injection</b></span>' |
||
88 | ), |
||
89 | ); |
||
90 | } |
||
91 | |||
92 | public function testFormatWithFormatString() { |
||
98 | |||
99 | /** |
||
100 | * @dataProvider applyUnitOptionProvider |
||
101 | */ |
||
102 | public function testGivenHtmlCharacters_formatEscapesHtmlCharacters( |
||
116 | |||
117 | public function applyUnitOptionProvider() { |
||
144 | |||
145 | } |
||
146 |
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.