Skip to content
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

Increase test coverage #411

Merged
merged 1 commit into from
Sep 18, 2023
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/ComposerRequireChecker/JsonLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private static function getFileContentFromPath(string $path): string
Assert::file($path);
Assert::readable($path);
} catch (InvalidArgumentException $exception) {
throw new NotReadable('unable to read ' . $path, 0, $exception);
throw new NotReadable('unable to read file: ' . $exception->getMessage(), 0, $exception);
}

$content = file_get_contents($path);
Expand Down
46 changes: 46 additions & 0 deletions test/ComposerRequireCheckerTest/JsonLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,76 @@
use ComposerRequireChecker\JsonLoader;
use PHPUnit\Framework\TestCase;

use function file_exists;

/** @covers \ComposerRequireChecker\JsonLoader */
final class JsonLoaderTest extends TestCase
{
public function testHasErrorWithWrongPath(): void
{
$path = __DIR__ . '/wrong/path/non-existing-file.json';
$this->expectException(NotReadable::class);
$this->expectExceptionMessage('unable to read file: The file "' . $path . '" does not exist.');
$this->expectExceptionCode(0);
JsonLoader::getData($path);
}

public function testHasErrorWithInvalidFile(): void
{
$path = __DIR__ . '/../fixtures/invalidJson';
$this->expectException(InvalidJson::class);
$this->expectExceptionMessage('error parsing ' . $path . ': Syntax error');
$this->expectExceptionCode(0);
JsonLoader::getData($path);
}

public function testHasErrorWithUnreadableFile(): void
{
$path = '/etc/shadow';
if (! file_exists($path)) {
$this->markTestSkipped('This system does not have ' . $path);
}

$this->expectException(NotReadable::class);
$this->expectExceptionMessage('unable to read file: The path "' . $path . '" is not readable.');
$this->expectExceptionCode(0);
JsonLoader::getData($path);
}

public function testHasErrorWithDirectory(): void
{
$path = __DIR__;
$this->expectException(NotReadable::class);
$this->expectExceptionMessage('unable to read file: The path "' . $path . '" is not a file.');
$this->expectExceptionCode(0);
JsonLoader::getData($path);
}

public function testHasDataWithValidFileButNoArrayContent(): void
{
$path = __DIR__ . '/../fixtures/validJsonNotAnArray.json';
$this->expectException(InvalidJson::class);
$this->expectExceptionMessage('error parsing ' . $path . ': Expected an array.');
$this->expectExceptionCode(0);
JsonLoader::getData($path);
}

public function testHasDataWithValidFileButExcessiveDepth(): void
{
$path = __DIR__ . '/../fixtures/validJsonExcessiveDepth.json';
$this->expectException(InvalidJson::class);
$this->expectExceptionMessage('error parsing ' . $path . ': Maximum stack depth exceeded');
$this->expectExceptionCode(0);
JsonLoader::getData($path);
}

public function testHasDataWithValidFileWithVeryLargeDepth(): void
{
$path = __DIR__ . '/../fixtures/validJsonVeryLargeDepth.json';
$data = JsonLoader::getData($path);
$this->assertEquals('bar', $data['foo'] ?? null);
}

public function testHasDataWithValidFile(): void
{
$path = __DIR__ . '/../fixtures/validJson.json';
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/validJsonExcessiveDepth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"foo": "bar",
"baz": [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
"This string is at a depth of 513, which is larger than the allowed maximum of 512. 512 is the built-in default."
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
}
6 changes: 6 additions & 0 deletions test/fixtures/validJsonVeryLargeDepth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"foo": "bar",
"baz": [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[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