Completed
Push — BoundedQuantityValue ( 1533af )
by Daniel
02:40
created

testGivenHtmlCharacters_formatEscapesHtmlCharacters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 1
eloc 11
nc 1
nop 3
1
<?php
2
3
namespace ValueFormatters\Test;
4
5
use DataValues\QuantityValue;
6
use ValueFormatters\DecimalFormatter;
7
use ValueFormatters\FormatterOptions;
8
use ValueFormatters\QuantityHtmlFormatter;
9
10
/**
11
 * @covers ValueFormatters\QuantityHtmlFormatter
12
 *
13
 * @license GPL-2.0+
14
 * @author Thiemo Mättig
15
 */
16
class QuantityHtmlFormatterTest extends ValueFormatterTestBase {
17
18
	/**
19
	 * @deprecated since DataValues Interfaces 0.2, just use getInstance.
20
	 */
21
	protected function getFormatterClass() {
22
		throw new \LogicException( 'Should not be called, use getInstance' );
23
	}
24
25
	/**
26
	 * @see ValueFormatterTestBase::getInstance
27
	 *
28
	 * @param FormatterOptions|null $options
29
	 *
30
	 * @return QuantityHtmlFormatter
31
	 */
32
	protected function getInstance( FormatterOptions $options = null ) {
33
		return $this->getQuantityHtmlFormatter( $options );
34
	}
35
36
	/**
37
	 * @param FormatterOptions|null $options
38
	 * @param DecimalFormatter|null $decimalFormatter
39
	 * @param string|null $quantityWithUnitFormat
40
	 *
41
	 * @return QuantityHtmlFormatter
42
	 */
43
	private function getQuantityHtmlFormatter(
44
		FormatterOptions $options = null,
45
		DecimalFormatter $decimalFormatter = null,
46
		$quantityWithUnitFormat = null
47
	) {
48
		$vocabularyUriFormatter = $this->getMock( 'ValueFormatters\ValueFormatter' );
49
		$vocabularyUriFormatter->expects( $this->any() )
50
			->method( 'format' )
51
			->will( $this->returnCallback( function( $unit ) {
52
				return $unit === '1' ? null : $unit;
53
			} ) );
54
55
		return new QuantityHtmlFormatter(
56
			$options,
57
			$decimalFormatter,
58
			$vocabularyUriFormatter,
59
			$quantityWithUnitFormat
60
		);
61
	}
62
63
	/**
64
	 * @see ValueFormatterTestBase::validProvider
65
	 */
66
	public function validProvider() {
67
		return array(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('Unit 1' =>...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...
68
			'Unit 1' => array(
69
				QuantityValue::newFromNumber( '+2', '1', '+3', '+1' ),
0 ignored issues
show
Unused Code introduced by
The call to QuantityValue::newFromNumber() has too many arguments starting with '+3'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
70
				'2±1'
71
			),
72
			'String unit' => array(
73
				QuantityValue::newFromNumber( '+2', 'Ultrameter', '+3', '+1' ),
0 ignored issues
show
Unused Code introduced by
The call to QuantityValue::newFromNumber() has too many arguments starting with '+3'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
74
				'2±1 <span class="wb-unit">Ultrameter</span>'
75
			),
76
			'HTML injection' => array(
77
				QuantityValue::newFromNumber( '+2', '<b>injection</b>', '+2', '+2' ),
0 ignored issues
show
Unused Code introduced by
The call to QuantityValue::newFromNumber() has too many arguments starting with '+2'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
78
				'2 <span class="wb-unit">&lt;b&gt;injection&lt;/b&gt;</span>'
79
			),
80
		);
81
	}
82
83
	public function testFormatWithFormatString() {
84
		$formatter = $this->getQuantityHtmlFormatter( null, null, '$2&thinsp;$1' );
85
		$value = QuantityValue::newFromNumber( '+5', 'USD' );
86
		$formatted = $formatter->format( $value );
87
		$this->assertSame( '<span class="wb-unit">USD</span>&thinsp;5', $formatted );
88
	}
89
90
	/**
91
	 * @dataProvider applyUnitOptionProvider
92
	 */
93
	public function testGivenHtmlCharacters_formatEscapesHtmlCharacters(
94
		FormatterOptions $options = null,
95
		$unit,
96
		$expected
97
	) {
98
		$decimalFormatter = $this->getMock( 'ValueFormatters\DecimalFormatter' );
99
		$decimalFormatter->expects( $this->any() )
100
			->method( 'format' )
101
			->will( $this->returnValue( '<b>+2</b>' ) );
102
103
		$formatter = $this->getQuantityHtmlFormatter( $options, $decimalFormatter );
104
		$formatted = $formatter->format( QuantityValue::newFromNumber( '+2', $unit ) );
105
		$this->assertSame( $expected, $formatted );
106
	}
107
108
	public function applyUnitOptionProvider() {
109
		$noUnit = new FormatterOptions();
110
		$noUnit->setOption( QuantityHtmlFormatter::OPT_APPLY_UNIT, false );
111
112
		return array(
113
			'Disabled without unit' => array(
114
				$noUnit,
115
				'1',
116
				'&lt;b&gt;+2&lt;/b&gt;'
117
			),
118
			'Disabled with unit' => array(
119
				$noUnit,
120
				'<b>m</b>',
121
				'&lt;b&gt;+2&lt;/b&gt;'
122
			),
123
			'Default without unit' => array(
124
				null,
125
				'1',
126
				'&lt;b&gt;+2&lt;/b&gt;'
127
			),
128
			'Default with unit' => array(
129
				null,
130
				'<b>m</b>',
131
				'&lt;b&gt;+2&lt;/b&gt; <span class="wb-unit">&lt;b&gt;m&lt;/b&gt;</span>'
132
			),
133
		);
134
	}
135
136
}
137