Skip to content

Commit

Permalink
Merge branch '4.7.x' into php8.3
Browse files Browse the repository at this point in the history
  • Loading branch information
fezfez committed Sep 18, 2023
2 parents 07aacdc + a4847a4 commit ba557f2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
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);

Check warning on line 54 in src/ComposerRequireChecker/JsonLoader.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.1, ubuntu-latest)

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { try { Assert::file($path); - Assert::readable($path); + } catch (InvalidArgumentException $exception) { throw new NotReadable('unable to read file: ' . $exception->getMessage(), 0, $exception); }
} 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."
]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
}
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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