Skip to content

Commit e222caf

Browse files
committed
SyntaxErrorGetNormalizeMessageTest: refactor to dataprovider
Refactor the tests in the `SyntaxErrorGetNormalizeMessageTest` class to use data providers, which allow for adding more tests in a straight forward manner. Note: this is a 1-on-1 refactor of the test with all existing test cases still being tested but no other changes.
1 parent 8f1056e commit e222caf

File tree

1 file changed

+63
-7
lines changed

1 file changed

+63
-7
lines changed

tests/Unit/Errors/SyntaxErrorGetNormalizeMessageTest.php

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,73 @@
77

88
class SyntaxErrorGetNormalizeMessageTest extends UnitTestCase
99
{
10-
public function testInWordInErrorMessage()
10+
const FILEPATH_MSG_TEMPLATE = "Parse error: unexpected 'Foo' (T_STRING) in %s on line 2";
11+
const FILEPATH_MSG_EXPECTED = "Unexpected 'Foo' (T_STRING)";
12+
13+
/**
14+
* Test retrieving a normalized error message.
15+
*
16+
* @dataProvider dataMessageNormalization
17+
*
18+
* @param string $message The message input to run the test with.
19+
* @param string $expected The expected method return value.
20+
*
21+
* @return void
22+
*/
23+
public function testMessageNormalization($message, $expected)
1124
{
12-
$message = 'Fatal error: \'break\' not in the \'loop\' or \'switch\' context in test.php on line 2';
1325
$error = new SyntaxError('test.php', $message);
14-
$this->assertSame('\'break\' not in the \'loop\' or \'switch\' context', $error->getNormalizedMessage());
26+
$this->assertSame($expected, $error->getNormalizedMessage());
27+
}
28+
29+
/**
30+
* Data provider.
31+
*
32+
* @return array
33+
*/
34+
public function dataMessageNormalization()
35+
{
36+
return array(
37+
'Strip leading and trailing information' => array(
38+
'message' => "Fatal error: 'break' not in the 'loop' or 'switch' context in test.php on line 2",
39+
'expected' => "'break' not in the 'loop' or 'switch' context",
40+
),
41+
);
42+
}
43+
44+
/**
45+
* Test retrieving a normalized error message with variations for the file path.
46+
*
47+
* @dataProvider dataFilePathHandling
48+
*
49+
* @param string $filePath The file path input to run the test with.
50+
* @param string $fileName The file name which is expected to be in the error message.
51+
*
52+
* @return void
53+
*/
54+
public function testFilePathHandling($filePath, $fileName)
55+
{
56+
$message = sprintf(self::FILEPATH_MSG_TEMPLATE, $fileName);
57+
$error = new SyntaxError($filePath, $message);
58+
$this->assertSame(self::FILEPATH_MSG_EXPECTED, $error->getNormalizedMessage());
1559
}
1660

17-
public function testInWordInErrorMessageAndInFileName()
61+
/**
62+
* Data provider.
63+
*
64+
* @return array
65+
*/
66+
public function dataFilePathHandling()
1867
{
19-
$message = 'Fatal error: \'break\' not in the \'loop\' or \'switch\' context in test in file.php on line 2';
20-
$error = new SyntaxError('test in file.php', $message);
21-
$this->assertSame('\'break\' not in the \'loop\' or \'switch\' context', $error->getNormalizedMessage());
68+
return array(
69+
'Plain file name' => array(
70+
'filePath' => 'test.php',
71+
'fileName' => 'test.php',
72+
),
73+
'File name containing spaces' => array(
74+
'filePath' => 'test in file.php',
75+
'fileName' => 'test in file.php',
76+
),
77+
);
2278
}
2379
}

0 commit comments

Comments
 (0)