ConfigStaticManifestTest.php
6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
class ConfigStaticManifestTest extends SapphireTest {
/* Example statics */
// Different access levels
static $nolevel;
public static $public;
protected static $protected;
private static $private;
static public $public2;
static protected $protected2;
static private $private2;
static $nolevel_after_private;
// Assigning values
static $snone;
static $snull = null;
static $sint = 1;
static $sfloat = 2.5;
static $sstring = 'string';
static $sarray = array(1, 2, array(3, 4), 5);
static $sheredoc = <<<DOC
heredoc
DOC;
static $snowdoc = <<<'DOC'
nowdoc
DOC;
// @codingStandardsIgnoreStart
// Assigning multiple values
static $onone, $onull = null, $oint = 1, $ofloat = 2.5, $ostring = 'string', $oarray = array(1, 2, array(3, 4), 5), $oheredoc = <<<DOC
heredoc
DOC
, $onowdoc = <<<'DOC'
nowdoc
DOC;
// @codingStandardsIgnoreEnd
static
$mnone,
$mnull = null,
$mint = 1,
$mfloat = 2.5,
$mstring = 'string',
$marray = array(
1, 2,
array(3, 4),
5
),
$mheredoc = <<<DOC
heredoc
DOC
,
$mnowdoc = <<<'DOC'
nowdoc
DOC;
static /* Has comment inline */ $commented_int = 1, /* And here */ $commented_string = 'string';
static
/**
* Has docblock inline
*/
$docblocked_int = 1,
/** And here */
$docblocked_string = 'string';
// Should ignore static methpds
static function static_method() {}
// Should ignore method statics
function instanceMethod() {
static $method_static;
}
/* The tests */
protected function parseSelf() {
static $statics = null;
if ($statics === null) {
$parser = new SS_ConfigStaticManifest_Parser(__FILE__);
$parser->parse();
}
return $parser;
}
public function testParsingAccessLevels() {
$statics = $this->parseSelf()->getStatics();
$levels = array(
'nolevel' => null,
'public' => T_PUBLIC,
'public2' => T_PUBLIC,
'protected' => T_PROTECTED,
'protected2' => T_PROTECTED,
'private' => T_PRIVATE,
'private2' => T_PRIVATE,
'nolevel_after_private' => null
);
foreach($levels as $var => $level) {
$this->assertEquals(
$level,
$statics[__CLASS__][$var]['access'],
'Variable '.$var.' has '.($level ? token_name($level) : 'no').' access level'
);
}
}
public function testParsingValues() {
$statics = $this->parseSelf()->getStatics();
// Check assigning values
$values = array(
'none',
'null',
'int',
'float',
'string',
'array',
'heredoc',
'nowdoc'
);
$prepends = array(
's', // Each on it's own
'o', // All on one line
'm' // All in on static statement, but each on own line
);
foreach ($values as $value) {
foreach ($prepends as $prepend) {
$var = "$prepend$value";
$this->assertEquals(
self::$$var,
$statics[__CLASS__][$var]['value'],
'Variable '.$var.' value is extracted properly'
);
}
}
}
public function testIgnoreComments() {
$statics = $this->parseSelf()->getStatics();
$this->assertEquals(self::$commented_int, $statics[__CLASS__]['commented_int']['value']);
$this->assertEquals(self::$commented_string, $statics[__CLASS__]['commented_string']['value']);
$this->assertEquals(self::$docblocked_int, $statics[__CLASS__]['docblocked_int']['value']);
$this->assertEquals(self::$docblocked_string, $statics[__CLASS__]['docblocked_string']['value']);
}
public function testIgnoresMethodStatics() {
$statics = $this->parseSelf()->getStatics();
$this->assertNull(@$statics[__CLASS__]['method_static']);
}
public function testIgnoresStaticMethods() {
$statics = $this->parseSelf()->getStatics();
$this->assertNull(@$statics[__CLASS__]['static_method']);
}
public function testParsingShortArray() {
if(version_compare(PHP_VERSION, '5.4', '<')) {
$this->markTestSkipped('This test requires PHP 5.4 or higher');
return;
}
$parser = new SS_ConfigStaticManifest_Parser(__DIR__ .
'/ConfigStaticManifestTest/ConfigStaticManifestTestMyObject.php');
$parser->parse();
$statics = $parser->getStatics();
$expectedValue = array(
'Name' => 'Varchar',
'Description' => 'Text',
);
$this->assertEquals($expectedValue, $statics['ConfigStaticManifestTestMyObject']['db']['value']);
}
public function testParsingNamespacesclass() {
$parser = new SS_ConfigStaticManifest_Parser(__DIR__ .
'/ConfigStaticManifestTest/ConfigStaticManifestTestNamespace.php');
$parser->parse();
$statics = $parser->getStatics();
$expectedValue = array(
'Name' => 'Varchar',
'Description' => 'Text',
);
$this->assertEquals($expectedValue, $statics['config\staticmanifest\NamespaceTest']['db']['value']);
}
public function testParsingMultyStringClass() {
static $tokens = array(
array(T_OPEN_TAG, "<?php\n", 1),
array(T_WHITESPACE, "\n", 2),
array(T_CLASS, 'class', 3),
array(T_WHITESPACE, ' ', 3),
':',
array(T_STRING, 'ss', 3),
':',
array(T_STRING, 'test2', 3),
array(T_WHITESPACE, ' ', 3),
array(T_EXTENDS, 'extends', 3),
array(T_WHITESPACE, ' ', 3),
':',
array(T_STRING, 'ss', 3),
':',
array(T_STRING, 'test', 3),
array(T_WHITESPACE, ' ', 3),
array(T_IMPLEMENTS, 'implements', 3),
array(T_WHITESPACE, ' ', 3),
array(T_STRING, 'TestOnly', 3),
array(T_WHITESPACE, ' ', 3),
'{',
array(T_WHITESPACE, "\n\t", 3),
array(T_PRIVATE, 'private', 4),
array(T_WHITESPACE, ' ', 4),
array(T_STATIC, 'static', 4),
array(T_WHITESPACE, ' ', 4),
array(T_VARIABLE, '$test', 4),
array(T_WHITESPACE, ' ', 4),
'=',
array(T_WHITESPACE, ' ', 4),
array(T_ARRAY, 'array', 4),
'(',
array(T_LNUMBER, '3', 4),
')',
';',
array(T_WHITESPACE, "\n", 4),
'}',
array(T_WHITESPACE, "\n", 5),
);
$parser = new ConfigStaticManifestTest_Parser($tokens);
$parser->parse();
$statics = $parser->getStatics();
$expected = array(
'test' => array(
'access' => T_PRIVATE,
'value' => array(3)
)
);
$this->assertEquals($expected, $statics[':ss:test2']);
}
}
class ConfigStaticManifestTest_Parser extends SS_ConfigStaticManifest_Parser implements TestOnly {
public function __construct($tokens) {
$this->path = __FILE__;
$this->tokens = $tokens;
$this->length = count($this->tokens);
$this->pos = 0;
}
}