Completed
Push — master ( 8ea799...e22a3b )
by
unknown
23s
created

QuantityHtmlFormatterTest::newQuantityValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 11
nc 1
nop 2
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(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('Unbounded,...on&lt;/b&gt;</span>')); (array<string,array<Value...lFormatterTest|string>>) is incompatible with the return type declared by the abstract method ValueFormatters\Test\Val...TestBase::validProvider of type array[].

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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">&lt;b&gt;injection&lt;/b&gt;</span>'
110
			),
111
		);
112
	}
113
114
	public function testFormatWithFormatString() {
115
		$formatter = $this->getQuantityHtmlFormatter( null, null, '$2&thinsp;$1' );
116
		$value = QuantityValue::newFromNumber( '+5', 'USD' );
117
		$formatted = $formatter->format( $value );
118
		$this->assertSame( '<span class="wb-unit">USD</span>&thinsp;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
				'&lt;b&gt;+2&lt;/b&gt;'
148
			),
149
			'Disabled with unit' => array(
150
				$noUnit,
151
				QuantityValue::newFromNumber( 2, '<b>m</b>' ),
152
				'&lt;b&gt;+2&lt;/b&gt;'
153
			),
154
			'Default without unit' => array(
155
				null,
156
				QuantityValue::newFromNumber( 2, '1' ),
157
				'&lt;b&gt;+2&lt;/b&gt;'
158
			),
159
			'Default with unit' => array(
160
				null,
161
				QuantityValue::newFromNumber( 2, '<b>m</b>' ),
162
				'&lt;b&gt;+2&lt;/b&gt; <span class="wb-unit">&lt;b&gt;m&lt;/b&gt;</span>'
163
			),
164
			'HTML escaping bounded' => array(
165
				null,
166
				$this->newQuantityValue( 'DataValues\QuantityValue' ),
167
				'&lt;b&gt;+2&lt;/b&gt;'
168
			),
169
			'HTML escaping bounded with uncertainty' => array(
170
				null,
171
				$this->newQuantityValue( 'DataValues\QuantityValue', 1 ),
172
				'&lt;b&gt;+2&lt;/b&gt;±&lt;b&gt;+2&lt;/b&gt;'
173
			),
174
			'HTML escaping unbounded' => array(
175
				null,
176
				$this->newQuantityValue( 'DataValues\UnboundedQuantityValue' ),
177
				'&lt;b&gt;+2&lt;/b&gt;'
178
			),
179
		);
180
	}
181
182
}
183