DataValues /
Number
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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( |
||
| 68 | 'Unit 1' => array( |
||
| 69 | QuantityValue::newFromNumber( '+2', '1', '+3', '+1' ), |
||
|
0 ignored issues
–
show
|
|||
| 70 | '2±1' |
||
| 71 | ), |
||
| 72 | 'String unit' => array( |
||
| 73 | QuantityValue::newFromNumber( '+2', 'Ultrameter', '+3', '+1' ), |
||
|
0 ignored issues
–
show
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 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
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 Loading history...
|
|||
| 78 | '2 <span class="wb-unit"><b>injection</b></span>' |
||
| 79 | ), |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function testFormatWithFormatString() { |
||
| 84 | $formatter = $this->getQuantityHtmlFormatter( null, null, '$2 $1' ); |
||
| 85 | $value = QuantityValue::newFromNumber( '+5', 'USD' ); |
||
| 86 | $formatted = $formatter->format( $value ); |
||
| 87 | $this->assertSame( '<span class="wb-unit">USD</span> 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 | '<b>+2</b>' |
||
| 117 | ), |
||
| 118 | 'Disabled with unit' => array( |
||
| 119 | $noUnit, |
||
| 120 | '<b>m</b>', |
||
| 121 | '<b>+2</b>' |
||
| 122 | ), |
||
| 123 | 'Default without unit' => array( |
||
| 124 | null, |
||
| 125 | '1', |
||
| 126 | '<b>+2</b>' |
||
| 127 | ), |
||
| 128 | 'Default with unit' => array( |
||
| 129 | null, |
||
| 130 | '<b>m</b>', |
||
| 131 | '<b>+2</b> <span class="wb-unit"><b>m</b></span>' |
||
| 132 | ), |
||
| 133 | ); |
||
| 134 | } |
||
| 135 | |||
| 136 | } |
||
| 137 |
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
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.