Skip to content

Tests: add dedicated test class for the ParallelLintError class #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Errors/ParallelLintError.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getShortFilePath()

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

return preg_replace('/' . preg_quote($cwd, '/') . '/', '', $this->filePath, 1);
Expand Down
137 changes: 137 additions & 0 deletions tests/Unit/Errors/ParallelLintErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace PHP_Parallel_Lint\PhpParallelLint\Tests\Unit\Errors;

use PHP_Parallel_Lint\PhpParallelLint\Errors\ParallelLintError;
use PHP_Parallel_Lint\PhpParallelLint\Tests\UnitTestCase;

/**
* @covers \PHP_Parallel_Lint\PhpParallelLint\Errors\ParallelLintError
*/
class ParallelLintErrorTest extends UnitTestCase
{
/**
* Test retrieving the error message.
*
* @dataProvider dataGetMessage
*
* @param mixed $message The message input to run the test with.
* @param string $expected The expected method return value.
*
* @return void
*/
public function testGetMessage($message, $expected)
{
$error = new ParallelLintError('test.php', $message);
$this->assertSame($expected, $error->getMessage());
}

/**
* Data provider.
*
* @return array
*/
public function dataGetMessage()
{
return array(
'Message: empty string' => array(
'message' => '',
'expected' => '',
),
'Message: plain text' => array(
'message' => 'plain text',
'expected' => 'plain text',
),
'Message: plain text with leading and trailing whitespace' => array(
'message' => '
plain text
',
'expected' => '
plain text',
),
);
}

/**
* Test retrieving the file path.
*
* @dataProvider dataGetFilePath
*
* @param string $filePath The file path input to run the test with.
*
* @return void
*/
public function testGetFilePath($filePath)
{
$error = new ParallelLintError($filePath, '');
$this->assertSame($filePath, $error->getFilePath());
}

/**
* Test retrieving the short file path.
*
* @dataProvider dataGetFilePath
*
* @param string $filePath The file path input to run the test with.
* @param string $expectedShort The expected method return value.
*
* @return void
*/
public function testGetShortFilePath($filePath, $expectedShort)
{
$error = new ParallelLintError($filePath, '');
$this->assertSame($expectedShort, $error->getShortFilePath());
}

/**
* Data provider.
*
* @return array
*/
public function dataGetFilePath()
{
$cwd = getcwd();

return array(
'No path (empty string)' => array(
'filePath' => '',
'expectedShort' => '',
),
'Filename only' => array(
'filePath' => 'file.php',
'expectedShort' => 'file.php',
),
'Plain path, linux slashes' => array(
'filePath' => 'path/to/file.php',
'expectedShort' => 'path/to/file.php',
),
'Plain path, windows slashes' => array(
'filePath' => 'path\to\file.php',
'expectedShort' => 'path\to\file.php',
),
'Path relative to current working directory' => array(
'filePath' => $cwd . '/subdir/file.php',
'expectedShort' => '/subdir/file.php',
),
'Path relative to current working directory with double working dir' => array(
'filePath' => $cwd . $cwd . '/subdir/file.php',
'expectedShort' => $cwd . '/subdir/file.php',
),
);
}

/**
* Test retrieving the error in Json serialized format.
*
* @requires PHP 5.4
*
* @return void
*/
public function testJsonSerialize()
{
$expected = '{"type":"error","file":"path\/to\/file.php","message":"error message"}';

$error = new ParallelLintError('path/to/file.php', 'error message');
$this->assertJsonStringEqualsJsonString($expected, json_encode($error));
}
}