Skip to content

Commit d2d4b47

Browse files
benno5020jrfnl
authored andcommitted
Util/Common: refactor IsCamelCapsTest to use data providers
This commit moves all existing test cases to data providers and adds descriptive names to better convey what they intend to test. See #846
1 parent 957cab3 commit d2d4b47

File tree

1 file changed

+270
-42
lines changed

1 file changed

+270
-42
lines changed

tests/Core/Util/Common/IsCamelCapsTest.php

Lines changed: 270 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,117 +24,345 @@ final class IsCamelCapsTest extends TestCase
2424
/**
2525
* Test valid public function/method names.
2626
*
27+
* @param string $name The tested name.
28+
* @param bool $strict Value of the $strict flag.
29+
*
30+
* @dataProvider dataValidNotClassFormatPublic
31+
*
2732
* @return void
2833
*/
29-
public function testValidNotClassFormatPublic()
34+
public function testValidNotClassFormatPublic($name, $strict)
3035
{
31-
$this->assertTrue(Common::isCamelCaps('thisIsCamelCaps', false, true, true));
32-
$this->assertTrue(Common::isCamelCaps('thisISCamelCaps', false, true, false));
36+
$this->assertTrue(Common::isCamelCaps($name, false, true, $strict));
3337

3438
}//end testValidNotClassFormatPublic()
3539

3640

41+
/**
42+
* Data provider.
43+
*
44+
* @see testValidNotClassFormatPublic()
45+
*
46+
* @return array<string, array<string, string|bool>>
47+
*/
48+
public static function dataValidNotClassFormatPublic()
49+
{
50+
return [
51+
'lower camelCase string in strict mode' => [
52+
'name' => 'thisIsCamelCaps',
53+
'strict' => true,
54+
],
55+
'lower camelCase string with acronym in relaxed mode' => [
56+
'name' => 'thisISCamelCaps',
57+
'strict' => false,
58+
],
59+
];
60+
61+
}//end dataValidNotClassFormatPublic()
62+
63+
3764
/**
3865
* Test invalid public function/method names.
3966
*
67+
* @param string $name The tested name.
68+
*
69+
* @dataProvider dataInvalidNotClassFormatPublic
70+
*
4071
* @return void
4172
*/
42-
public function testInvalidNotClassFormatPublic()
73+
public function testInvalidNotClassFormatPublic($name)
4374
{
44-
$this->assertFalse(Common::isCamelCaps('_thisIsCamelCaps', false, true, true));
45-
$this->assertFalse(Common::isCamelCaps('thisISCamelCaps', false, true, true));
46-
$this->assertFalse(Common::isCamelCaps('ThisIsCamelCaps', false, true, true));
75+
$this->assertFalse(Common::isCamelCaps($name, false, true, true));
4776

48-
$this->assertFalse(Common::isCamelCaps('3thisIsCamelCaps', false, true, true));
49-
$this->assertFalse(Common::isCamelCaps('*thisIsCamelCaps', false, true, true));
50-
$this->assertFalse(Common::isCamelCaps('-thisIsCamelCaps', false, true, true));
77+
}//end testInvalidNotClassFormatPublic()
5178

52-
$this->assertFalse(Common::isCamelCaps('this*IsCamelCaps', false, true, true));
53-
$this->assertFalse(Common::isCamelCaps('this-IsCamelCaps', false, true, true));
54-
$this->assertFalse(Common::isCamelCaps('this_IsCamelCaps', false, true, true));
55-
$this->assertFalse(Common::isCamelCaps('this_is_camel_caps', false, true, true));
5679

57-
}//end testInvalidNotClassFormatPublic()
80+
/**
81+
* Data provider.
82+
*
83+
* @see testInvalidNotClassFormatPublic()
84+
*
85+
* @return array<string, array<string, string>>
86+
*/
87+
public static function dataInvalidNotClassFormatPublic()
88+
{
89+
return [
90+
'string with initial underscore (invalid when $public is true)' => [
91+
'name' => '_thisIsCamelCaps',
92+
],
93+
'lower camelCase string with acronym (invalid when $strict is true)' => [
94+
'name' => 'thisISCamelCaps',
95+
],
96+
'PascalCase string' => [
97+
'name' => 'ThisIsCamelCaps',
98+
],
99+
'lower camelCase string with initial digit' => [
100+
'name' => '3thisIsCamelCaps',
101+
],
102+
'lower camelCase string with initial [^a-zA-z_] character: *' => [
103+
'name' => '*thisIsCamelCaps',
104+
],
105+
'lower camelCase string with initial [^a-zA-z_] character: -' => [
106+
'name' => '-thisIsCamelCaps',
107+
],
108+
'lower camelCase string with medial [^a-zA-z_] character: *' => [
109+
'name' => 'this*IsCamelCaps',
110+
],
111+
'lower camelCase string with medial [^a-zA-z_] character: -' => [
112+
'name' => 'this-IsCamelCaps',
113+
],
114+
'lower camelCase string with single medial underscore' => [
115+
'name' => 'this_IsCamelCaps',
116+
],
117+
'snake_case string' => [
118+
'name' => 'this_is_camel_caps',
119+
],
120+
];
121+
122+
}//end dataInvalidNotClassFormatPublic()
58123

59124

60125
/**
61126
* Test valid private method names.
62127
*
128+
* @param string $name The tested name.
129+
* @param bool $strict Value of the $strict flag.
130+
*
131+
* @dataProvider dataValidNotClassFormatPrivate
132+
*
63133
* @return void
64134
*/
65-
public function testValidNotClassFormatPrivate()
135+
public function testValidNotClassFormatPrivate($name, $strict)
66136
{
67-
$this->assertTrue(Common::isCamelCaps('_thisIsCamelCaps', false, false, true));
68-
$this->assertTrue(Common::isCamelCaps('_thisISCamelCaps', false, false, false));
69-
$this->assertTrue(Common::isCamelCaps('_i18N', false, false, true));
70-
$this->assertTrue(Common::isCamelCaps('_i18n', false, false, true));
137+
$this->assertTrue(Common::isCamelCaps($name, false, false, $strict));
71138

72139
}//end testValidNotClassFormatPrivate()
73140

74141

142+
/**
143+
* Data provider.
144+
*
145+
* @see testValidNotClassFormatPrivate()
146+
*
147+
* @return array<string, array<string, string|bool>>
148+
*/
149+
public static function dataValidNotClassFormatPrivate()
150+
{
151+
return [
152+
'lower camelCase string with initial underscore' => [
153+
'name' => '_thisIsCamelCaps',
154+
'strict' => true,
155+
],
156+
'lower camelCase string with acronym and initial underscore' => [
157+
'name' => '_thisISCamelCaps',
158+
'strict' => false,
159+
],
160+
'_i18N' => [
161+
'name' => '_i18N',
162+
'strict' => true,
163+
],
164+
'_i18n' => [
165+
'name' => '_i18n',
166+
'strict' => true,
167+
],
168+
];
169+
170+
}//end dataValidNotClassFormatPrivate()
171+
172+
75173
/**
76174
* Test invalid private method names.
77175
*
176+
* @param string $name The tested name.
177+
* @param bool $strict Value of the $strict flag.
178+
*
179+
* @dataProvider dataInvalidNotClassFormatPrivate
180+
*
78181
* @return void
79182
*/
80-
public function testInvalidNotClassFormatPrivate()
183+
public function testInvalidNotClassFormatPrivate($name, $strict)
81184
{
82-
$this->assertFalse(Common::isCamelCaps('thisIsCamelCaps', false, false, true));
83-
$this->assertFalse(Common::isCamelCaps('_thisISCamelCaps', false, false, true));
84-
$this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', false, false, true));
85-
$this->assertFalse(Common::isCamelCaps('__thisIsCamelCaps', false, false, true));
86-
$this->assertFalse(Common::isCamelCaps('__thisISCamelCaps', false, false, false));
87-
88-
$this->assertFalse(Common::isCamelCaps('3thisIsCamelCaps', false, false, true));
89-
$this->assertFalse(Common::isCamelCaps('*thisIsCamelCaps', false, false, true));
90-
$this->assertFalse(Common::isCamelCaps('-thisIsCamelCaps', false, false, true));
91-
$this->assertFalse(Common::isCamelCaps('_this_is_camel_caps', false, false, true));
185+
$this->assertFalse(Common::isCamelCaps($name, false, false, $strict));
92186

93187
}//end testInvalidNotClassFormatPrivate()
94188

95189

190+
/**
191+
* Data provider.
192+
*
193+
* @see testInvalidNotClassFormatPrivate()
194+
*
195+
* @return array<string, array<string, string|bool>>
196+
*/
197+
public static function dataInvalidNotClassFormatPrivate()
198+
{
199+
return [
200+
'lower camelCase string without initial underscore' => [
201+
'name' => 'thisIsCamelCaps',
202+
'strict' => true,
203+
],
204+
'lower camelCase string with initial underscore, but with an acronym, in strict mode' => [
205+
'name' => '_thisISCamelCaps',
206+
'strict' => true,
207+
],
208+
'PascalCase string with initial underscore' => [
209+
'name' => '_ThisIsCamelCaps',
210+
'strict' => true,
211+
],
212+
'lower camelCase string with two initial underscores' => [
213+
'name' => '__thisIsCamelCaps',
214+
'strict' => true,
215+
],
216+
'lower camelCase string with two initial underscores and acronym in relaxed mode' => [
217+
'name' => '__thisISCamelCaps',
218+
'strict' => false,
219+
],
220+
'lower camelCase string with initial digit' => [
221+
'name' => '3thisIsCamelCaps',
222+
'strict' => true,
223+
],
224+
'lower camelCase string with initial [^a-zA-Z_] character: *' => [
225+
'name' => '*thisIsCamelCaps',
226+
'strict' => true,
227+
],
228+
'lower camelCase string with initial [^a-zA-Z_] character: -' => [
229+
'name' => '-thisIsCamelCaps',
230+
'strict' => true,
231+
],
232+
'snake_case string with initial underscore' => [
233+
'name' => '_this_is_camel_caps',
234+
'strict' => true,
235+
],
236+
];
237+
238+
}//end dataInvalidNotClassFormatPrivate()
239+
240+
96241
/**
97242
* Test valid class names.
98243
*
244+
* @param string $name The tested name.
245+
* @param bool $strict Value of the $strict flag.
246+
*
247+
* @dataProvider dataValidClassFormatPublic
248+
*
99249
* @return void
100250
*/
101-
public function testValidClassFormatPublic()
251+
public function testValidClassFormatPublic($name, $strict)
102252
{
103-
$this->assertTrue(Common::isCamelCaps('ThisIsCamelCaps', true, true, true));
104-
$this->assertTrue(Common::isCamelCaps('ThisISCamelCaps', true, true, false));
105-
$this->assertTrue(Common::isCamelCaps('This3IsCamelCaps', true, true, false));
253+
$this->assertTrue(Common::isCamelCaps($name, true, true, $strict));
106254

107255
}//end testValidClassFormatPublic()
108256

109257

258+
/**
259+
* Data provider.
260+
*
261+
* @see testValidClassFormatPublic()
262+
*
263+
* @return array<string, array<string, string|bool>>
264+
*/
265+
public static function dataValidClassFormatPublic()
266+
{
267+
return [
268+
'PascalCase string' => [
269+
'name' => 'ThisIsCamelCaps',
270+
'strict' => true,
271+
],
272+
'PascalCase string with acronym' => [
273+
'name' => 'ThisISCamelCaps',
274+
'strict' => false,
275+
],
276+
'PascalCase string with digit between words' => [
277+
'name' => 'This3IsCamelCaps',
278+
'strict' => false,
279+
],
280+
];
281+
282+
}//end dataValidClassFormatPublic()
283+
284+
110285
/**
111286
* Test invalid class names.
112287
*
288+
* @param string $name The tested name.
289+
*
290+
* @dataProvider dataInvalidClassFormat
291+
*
113292
* @return void
114293
*/
115-
public function testInvalidClassFormat()
294+
public function testInvalidClassFormat($name)
116295
{
117-
$this->assertFalse(Common::isCamelCaps('thisIsCamelCaps', true));
118-
$this->assertFalse(Common::isCamelCaps('This-IsCamelCaps', true));
119-
$this->assertFalse(Common::isCamelCaps('This_Is_Camel_Caps', true));
296+
$this->assertFalse(Common::isCamelCaps($name, true));
120297

121298
}//end testInvalidClassFormat()
122299

123300

301+
/**
302+
* Data provider.
303+
*
304+
* @see testInvalidClassFormat()
305+
*
306+
* @return array<string, array<string, string>>
307+
*/
308+
public static function dataInvalidClassFormat()
309+
{
310+
return [
311+
'lower camelCase string' => [
312+
'name' => 'thisIsCamelCaps',
313+
],
314+
'PascalCase string with medial illegal character: -' => [
315+
'name' => 'This-IsCamelCaps',
316+
],
317+
'capitalised snake case' => [
318+
'name' => 'This_Is_Camel_Caps',
319+
],
320+
];
321+
322+
}//end dataInvalidClassFormat()
323+
324+
124325
/**
125326
* Test invalid class names with the private flag set.
126327
*
127328
* Note that the private flag is ignored if the class format
128329
* flag is set, so these names are all invalid.
129330
*
331+
* @param string $name The tested name.
332+
* @param bool $public Value of the $public flag.
333+
*
334+
* @dataProvider dataInvalidClassFormatPrivate
335+
*
130336
* @return void
131337
*/
132-
public function testInvalidClassFormatPrivate()
338+
public function testInvalidClassFormatPrivate($name, $public)
133339
{
134-
$this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', true, true));
135-
$this->assertFalse(Common::isCamelCaps('_ThisIsCamelCaps', true, false));
340+
$this->assertFalse(Common::isCamelCaps($name, true, $public));
136341

137342
}//end testInvalidClassFormatPrivate()
138343

139344

345+
/**
346+
* Data provider.
347+
*
348+
* @see testInvalidClassFormatPrivate()
349+
*
350+
* @return array<string, array<string, string|bool>>
351+
*/
352+
public static function dataInvalidClassFormatPrivate()
353+
{
354+
return [
355+
'PascalCase string with initial underscore (public)' => [
356+
'name' => '_ThisIsCamelCaps',
357+
'public' => true,
358+
],
359+
'PascalCase string with initial underscore (private)' => [
360+
'name' => '_ThisIsCamelCaps',
361+
'public' => false,
362+
],
363+
];
364+
365+
}//end dataInvalidClassFormatPrivate()
366+
367+
140368
}//end class

0 commit comments

Comments
 (0)