Skip to content

Commit db62123

Browse files
jrfnlgrogy
authored andcommitted
Tests: add dedicated test class for the ParallelLintError class
This safeguards and documents the current behaviour of the methods in the class. Includes ignoring one line in the class for code coverage as the return of `getcwd()` cannot be mocked without adding additional test frameworks into the mix.
1 parent ff3f688 commit db62123

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

src/Errors/ParallelLintError.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function getShortFilePath()
4848

4949
if ($cwd === '/') {
5050
// For root directory in unix, do not modify path
51-
return $this->filePath;
51+
return $this->filePath; // @codeCoverageIgnore
5252
}
5353

5454
return preg_replace('/' . preg_quote($cwd, '/') . '/', '', $this->filePath, 1);
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace PHP_Parallel_Lint\PhpParallelLint\Tests\Unit\Errors;
4+
5+
use PHP_Parallel_Lint\PhpParallelLint\Errors\ParallelLintError;
6+
use PHP_Parallel_Lint\PhpParallelLint\Tests\UnitTestCase;
7+
8+
/**
9+
* @covers \PHP_Parallel_Lint\PhpParallelLint\Errors\ParallelLintError
10+
*/
11+
class ParallelLintErrorTest extends UnitTestCase
12+
{
13+
/**
14+
* Test retrieving the error message.
15+
*
16+
* @dataProvider dataGetMessage
17+
*
18+
* @param mixed $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 testGetMessage($message, $expected)
24+
{
25+
$error = new ParallelLintError('test.php', $message);
26+
$this->assertSame($expected, $error->getMessage());
27+
}
28+
29+
/**
30+
* Data provider.
31+
*
32+
* @return array
33+
*/
34+
public function dataGetMessage()
35+
{
36+
return array(
37+
'Message: empty string' => array(
38+
'message' => '',
39+
'expected' => '',
40+
),
41+
'Message: plain text' => array(
42+
'message' => 'plain text',
43+
'expected' => 'plain text',
44+
),
45+
'Message: plain text with leading and trailing whitespace' => array(
46+
'message' => '
47+
plain text
48+
',
49+
'expected' => '
50+
plain text',
51+
),
52+
);
53+
}
54+
55+
/**
56+
* Test retrieving the file path.
57+
*
58+
* @dataProvider dataGetFilePath
59+
*
60+
* @param string $filePath The file path input to run the test with.
61+
*
62+
* @return void
63+
*/
64+
public function testGetFilePath($filePath)
65+
{
66+
$error = new ParallelLintError($filePath, '');
67+
$this->assertSame($filePath, $error->getFilePath());
68+
}
69+
70+
/**
71+
* Test retrieving the short file path.
72+
*
73+
* @dataProvider dataGetFilePath
74+
*
75+
* @param string $filePath The file path input to run the test with.
76+
* @param string $expectedShort The expected method return value.
77+
*
78+
* @return void
79+
*/
80+
public function testGetShortFilePath($filePath, $expectedShort)
81+
{
82+
$error = new ParallelLintError($filePath, '');
83+
$this->assertSame($expectedShort, $error->getShortFilePath());
84+
}
85+
86+
/**
87+
* Data provider.
88+
*
89+
* @return array
90+
*/
91+
public function dataGetFilePath()
92+
{
93+
$cwd = getcwd();
94+
95+
return array(
96+
'No path (empty string)' => array(
97+
'filePath' => '',
98+
'expectedShort' => '',
99+
),
100+
'Filename only' => array(
101+
'filePath' => 'file.php',
102+
'expectedShort' => 'file.php',
103+
),
104+
'Plain path, linux slashes' => array(
105+
'filePath' => 'path/to/file.php',
106+
'expectedShort' => 'path/to/file.php',
107+
),
108+
'Plain path, windows slashes' => array(
109+
'filePath' => 'path\to\file.php',
110+
'expectedShort' => 'path\to\file.php',
111+
),
112+
'Path relative to current working directory' => array(
113+
'filePath' => $cwd . '/subdir/file.php',
114+
'expectedShort' => '/subdir/file.php',
115+
),
116+
'Path relative to current working directory with double working dir' => array(
117+
'filePath' => $cwd . $cwd . '/subdir/file.php',
118+
'expectedShort' => $cwd . '/subdir/file.php',
119+
),
120+
);
121+
}
122+
123+
/**
124+
* Test retrieving the error in Json serialized format.
125+
*
126+
* @requires PHP 5.4
127+
*
128+
* @return void
129+
*/
130+
public function testJsonSerialize()
131+
{
132+
$expected = '{"type":"error","file":"path\/to\/file.php","message":"error message"}';
133+
134+
$error = new ParallelLintError('path/to/file.php', 'error message');
135+
$this->assertJsonStringEqualsJsonString($expected, json_encode($error));
136+
}
137+
}

0 commit comments

Comments
 (0)