1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests\DataValues\ValueFormatters; |
4
|
|
|
|
5
|
|
|
use SMW\DataValues\ValueFormatters\StringValueFormatter; |
6
|
|
|
use SMWStringValue as StringValue; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @covers \SMW\DataValues\ValueFormatters\StringValueFormatter |
10
|
|
|
* @group semantic-mediawiki |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 2.4 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class StringValueFormatterTest extends \PHPUnit_Framework_TestCase { |
18
|
|
|
|
19
|
|
|
public function testCanConstruct() { |
20
|
|
|
|
21
|
|
|
$this->assertInstanceOf( |
22
|
|
|
'\SMW\DataValues\ValueFormatters\StringValueFormatter', |
23
|
|
|
new StringValueFormatter() |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testIsFormatterForValidation() { |
28
|
|
|
|
29
|
|
|
$stringValue = $this->getMockBuilder( '\SMWStringValue' ) |
30
|
|
|
->disableOriginalConstructor() |
31
|
|
|
->getMock(); |
32
|
|
|
|
33
|
|
|
$instance = new StringValueFormatter(); |
34
|
|
|
|
35
|
|
|
$this->assertTrue( |
36
|
|
|
$instance->isFormatterFor( $stringValue ) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testToUseCaptionOutput() { |
41
|
|
|
|
42
|
|
|
$stringValue = new StringValue( '_txt' ); |
43
|
|
|
$stringValue->setCaption( 'ABC[<>]' ); |
44
|
|
|
|
45
|
|
|
$instance = new StringValueFormatter( $stringValue ); |
46
|
|
|
|
47
|
|
|
$this->assertEquals( |
48
|
|
|
'ABC[<>]', |
49
|
|
|
$instance->format( StringValueFormatter::WIKI_SHORT ) |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$this->assertEquals( |
53
|
|
|
'ABC[<>]', |
54
|
|
|
$instance->format( StringValueFormatter::HTML_SHORT ) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @dataProvider stringValueProvider |
60
|
|
|
*/ |
61
|
|
|
public function testFormat( $stringUserValue, $type, $linker, $expected ) { |
62
|
|
|
|
63
|
|
|
$stringValue = new StringValue( '_txt' ); |
64
|
|
|
$stringValue->setUserValue( $stringUserValue ); |
65
|
|
|
|
66
|
|
|
$instance = new StringValueFormatter( $stringValue ); |
67
|
|
|
|
68
|
|
|
$this->assertEquals( |
69
|
|
|
$expected, |
70
|
|
|
$instance->format( $type, $linker ) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testTryToFormatOnMissingDataValueThrowsException() { |
75
|
|
|
|
76
|
|
|
$instance = new StringValueFormatter(); |
77
|
|
|
|
78
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
79
|
|
|
$instance->format( StringValueFormatter::VALUE ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function stringValueProvider() { |
83
|
|
|
|
84
|
|
|
$provider[] = array( |
|
|
|
|
85
|
|
|
'foo', |
86
|
|
|
StringValueFormatter::VALUE, |
87
|
|
|
null, |
88
|
|
|
'foo' |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$provider[] = array( |
92
|
|
|
'foo', |
93
|
|
|
StringValueFormatter::WIKI_SHORT, |
94
|
|
|
null, |
95
|
|
|
'foo' |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$provider[] = array( |
99
|
|
|
'foo', |
100
|
|
|
StringValueFormatter::HTML_SHORT, |
101
|
|
|
null, |
102
|
|
|
'foo' |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$provider[] = array( |
106
|
|
|
'foo', |
107
|
|
|
StringValueFormatter::WIKI_LONG, |
108
|
|
|
null, |
109
|
|
|
'foo' |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$provider[] = array( |
113
|
|
|
'foo', |
114
|
|
|
StringValueFormatter::HTML_LONG, |
115
|
|
|
null, |
116
|
|
|
'foo' |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
// > 255 |
120
|
|
|
$text = 'Lorem ipsum dolor sit amet consectetuer justo Nam quis lobortis vel. Sapien nulla enim Lorem enim pede ' . |
121
|
|
|
'lorem nulla justo diam wisi. Libero Nam turpis neque leo scelerisque nec habitasse a lacus mattis. Accumsan ' . |
122
|
|
|
'tincidunt Sed adipiscing nec facilisis tortor Nunc Sed ipsum tellus'; |
123
|
|
|
|
124
|
|
|
$provider[] = array( |
125
|
|
|
$text, |
126
|
|
|
StringValueFormatter::HTML_LONG, |
127
|
|
|
null, |
128
|
|
|
'Lorem ipsum dolor sit amet consectetuer ju <span class="smwwarning">…</span> nec facilisis tortor Nunc Sed ipsum tellus' |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$provider[] = array( |
132
|
|
|
$text, |
133
|
|
|
StringValueFormatter::WIKI_LONG, |
134
|
|
|
null, |
135
|
|
|
'Lorem ipsum dolor sit amet consectetuer ju <span class="smwwarning">…</span> nec facilisis tortor Nunc Sed ipsum tellus' |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
// XMLContentEncode |
139
|
|
|
$provider[] = array( |
140
|
|
|
'<foo>', |
141
|
|
|
StringValueFormatter::HTML_LONG, |
142
|
|
|
null, |
143
|
|
|
'<foo>' |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$provider[] = array( |
147
|
|
|
'<foo>', |
148
|
|
|
StringValueFormatter::HTML_SHORT, |
149
|
|
|
null, |
150
|
|
|
'<foo>' |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
return $provider; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
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.