Skip to content

Commit

Permalink
Enhancement: Use fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Dec 26, 2022
1 parent cf7f81e commit 941cdf8
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 72 deletions.
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Expand Up @@ -81,6 +81,11 @@
<code>$schemaDecoded</code>
</MixedAssignment>
</file>
<file src="test/Unit/Vendor/Composer/BinNormalizerTest.php">
<MixedAssignment occurrences="1">
<code>$fileInfo</code>
</MixedAssignment>
</file>
<file src="test/Unit/Vendor/Composer/ComposerJsonNormalizerTest.php">
<MixedAssignment occurrences="1">
<code>$fileInfo</code>
Expand Down
@@ -0,0 +1 @@
{"bin":["another-script.php","script.php"],"keywords":["foo","bar"]}
@@ -0,0 +1,10 @@
{
"bin": [
"script.php",
"another-script.php"
],
"keywords": [
"foo",
"bar"
]
}
@@ -0,0 +1,6 @@
{
"keywords": [
"foo",
"bar"
]
}
@@ -0,0 +1,7 @@
{
"bin": "foo.php",
"keywords": [
"foo",
"bar"
]
}
129 changes: 57 additions & 72 deletions test/Unit/Vendor/Composer/BinNormalizerTest.php
Expand Up @@ -14,6 +14,7 @@
namespace Ergebnis\Json\Normalizer\Test\Unit\Vendor\Composer;

use Ergebnis\Json\Json;
use Ergebnis\Json\Normalizer\Test;
use Ergebnis\Json\Normalizer\Vendor;

/**
Expand All @@ -25,86 +26,70 @@
*/
final class BinNormalizerTest extends AbstractComposerTestCase
{
public function testNormalizeDoesNotModifyOtherProperty(): void
/**
* @dataProvider provideScenario
*/
public function testNormalizeNormalizes(Test\Fixture\Vendor\Composer\ComposerJsonNormalizer\Scenario $scenario): void
{
$json = Json::fromString(
<<<'JSON'
{
"foo": {
"qux": "quux",
"bar": "baz"
}
}
JSON
);

$normalizer = new Vendor\Composer\BinNormalizer();

$normalized = $normalizer->normalize($json);

self::assertJsonStringIdenticalToJsonString($json->encoded(), $normalized->encoded());
}

public function testNormalizeDoesNotModifyBinIfPropertyExistsAsString(): void
{
$json = Json::fromString(
<<<'JSON'
{
"bin": "foo.php",
"foo": {
"qux": "quux",
"bar": "baz"
}
}
JSON
);
$json = $scenario->original();

$normalizer = new Vendor\Composer\BinNormalizer();

$normalized = $normalizer->normalize($json);

self::assertJsonStringIdenticalToJsonString($json->encoded(), $normalized->encoded());
self::assertJsonStringIdenticalToJsonString($scenario->normalized()->encoded(), $normalized->encoded());
}

public function testNormalizeSortsBinIfPropertyExistsAsArray(): void
/**
* @return \Generator<string, array{0: Test\Fixture\Vendor\Composer\ComposerJsonNormalizer\Scenario}>
*/
public static function provideScenario(): \Generator
{
$json = Json::fromString(
<<<'JSON'
{
"bin": [
"script.php",
"another-script.php"
],
"foo": {
"qux": "quux",
"bar": "baz"
}
}
JSON
);

$expected = \json_encode(
\json_decode(
<<<'JSON'
{
"bin": [
"another-script.php",
"script.php"
],
"foo": {
"qux": "quux",
"bar": "baz"
}
}
JSON
),
0,
);

$normalizer = new Vendor\Composer\BinNormalizer();

$normalized = $normalizer->normalize($json);

self::assertJsonStringIdenticalToJsonString($expected, $normalized->encoded());
$basePath = __DIR__ . '/../../../';

$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . '/../../../Fixture/Vendor/Composer/BinNormalizer/NormalizeNormalizes'));

foreach ($iterator as $fileInfo) {
/** @var \SplFileInfo $fileInfo */
if (!$fileInfo->isFile()) {
continue;
}

if ('original.json' !== $fileInfo->getBasename()) {
continue;
}

$originalFile = $fileInfo->getRealPath();

$normalizedFile = \preg_replace(
'/original\.json$/',
'normalized.json',
$originalFile,
);

if (!\is_string($normalizedFile)) {
throw new \RuntimeException(\sprintf(
'Unable to deduce normalized JSON file name from original JSON file name "%s".',
$originalFile,
));
}

if (!\file_exists($normalizedFile)) {
$normalizedFile = $originalFile;
}

$key = \substr(
$fileInfo->getPath(),
\strlen($basePath),
);

yield $key => [
Test\Fixture\Vendor\Composer\ComposerJsonNormalizer\Scenario::create(
$key,
Json::fromFile($originalFile),
Json::fromFile($normalizedFile),
),
];
}
}
}

0 comments on commit 941cdf8

Please sign in to comment.