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 SMW\Tests\DataValues\ValueFormatters; |
||
4 | |||
5 | use SMW\DataValues\ValueFormatters\MonolingualTextValueFormatter; |
||
6 | use SMW\DataValues\MonolingualTextValue; |
||
7 | |||
8 | /** |
||
9 | * @covers \SMW\DataValues\ValueFormatters\MonolingualTextValueFormatter |
||
10 | * @group semantic-mediawiki |
||
11 | * |
||
12 | * @license GNU GPL v2+ |
||
13 | * @since 2.4 |
||
14 | * |
||
15 | * @author mwjames |
||
16 | */ |
||
17 | class MonolingualTextValueFormatterTest extends \PHPUnit_Framework_TestCase { |
||
18 | |||
19 | public function testCanConstruct() { |
||
20 | |||
21 | $this->assertInstanceOf( |
||
22 | '\SMW\DataValues\ValueFormatters\MonolingualTextValueFormatter', |
||
23 | new MonolingualTextValueFormatter() |
||
24 | ); |
||
25 | } |
||
26 | |||
27 | public function testIsFormatterForValidation() { |
||
28 | |||
29 | $monolingualTextValue = $this->getMockBuilder( '\SMW\DataValues\MonolingualTextValue' ) |
||
30 | ->disableOriginalConstructor() |
||
31 | ->getMock(); |
||
32 | |||
33 | $instance = new MonolingualTextValueFormatter(); |
||
34 | |||
35 | $this->assertTrue( |
||
36 | $instance->isFormatterFor( $monolingualTextValue ) |
||
37 | ); |
||
38 | } |
||
39 | |||
40 | public function testToUseCaptionOutput() { |
||
41 | |||
42 | $monolingualTextValue = new MonolingualTextValue(); |
||
43 | $monolingualTextValue->setCaption( 'ABC' ); |
||
44 | |||
45 | $instance = new MonolingualTextValueFormatter( $monolingualTextValue ); |
||
46 | |||
47 | $this->assertEquals( |
||
48 | 'ABC', |
||
49 | $instance->format( MonolingualTextValueFormatter::WIKI_SHORT ) |
||
50 | ); |
||
51 | |||
52 | $this->assertEquals( |
||
53 | 'ABC', |
||
54 | $instance->format( MonolingualTextValueFormatter::HTML_SHORT ) |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @dataProvider stringValueProvider |
||
60 | */ |
||
61 | public function testFormat( $stringValue, $type, $linker, $expected ) { |
||
62 | |||
63 | $monolingualTextValue = new MonolingualTextValue(); |
||
64 | $monolingualTextValue->setUserValue( $stringValue ); |
||
65 | |||
66 | $instance = new MonolingualTextValueFormatter( $monolingualTextValue ); |
||
67 | |||
68 | $this->assertEquals( |
||
69 | $expected, |
||
70 | $instance->format( $type, $linker ) |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | public function testTryToFormatOnMissingDataValueThrowsException() { |
||
75 | |||
76 | $instance = new MonolingualTextValueFormatter(); |
||
77 | |||
78 | $this->setExpectedException( 'RuntimeException' ); |
||
79 | $instance->format( MonolingualTextValueFormatter::VALUE ); |
||
80 | } |
||
81 | |||
82 | public function stringValueProvider() { |
||
83 | |||
84 | $provider[] = array( |
||
0 ignored issues
–
show
|
|||
85 | 'foo@en', |
||
86 | MonolingualTextValueFormatter::VALUE, |
||
87 | null, |
||
88 | 'foo (en)' |
||
89 | ); |
||
90 | |||
91 | $provider[] = array( |
||
92 | 'foo@en', |
||
93 | MonolingualTextValueFormatter::WIKI_SHORT, |
||
94 | null, |
||
95 | 'foo (en)' |
||
96 | ); |
||
97 | |||
98 | $provider[] = array( |
||
99 | 'foo@en', |
||
100 | MonolingualTextValueFormatter::HTML_SHORT, |
||
101 | null, |
||
102 | 'foo (en)' |
||
103 | ); |
||
104 | |||
105 | $provider[] = array( |
||
106 | 'foo@en', |
||
107 | MonolingualTextValueFormatter::WIKI_LONG, |
||
108 | null, |
||
109 | 'foo (en)' |
||
110 | ); |
||
111 | |||
112 | $provider[] = array( |
||
113 | 'foo@en', |
||
114 | MonolingualTextValueFormatter::HTML_LONG, |
||
115 | null, |
||
116 | 'foo (en)' |
||
117 | ); |
||
118 | |||
119 | return $provider; |
||
120 | } |
||
121 | |||
122 | } |
||
123 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.