-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from localheinz/feature/config-hash-normalizer
Enhancement: Implement ConfigHashNormalizer
- Loading branch information
Showing
4 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller. | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/composer-normalize | ||
*/ | ||
|
||
namespace Localheinz\Composer\Normalize\Normalizer; | ||
|
||
use Localheinz\Json\Normalizer\NormalizerInterface; | ||
|
||
final class ConfigHashNormalizer implements NormalizerInterface | ||
{ | ||
public function normalize(string $json): string | ||
{ | ||
$decoded = \json_decode($json); | ||
|
||
if (null === $decoded && JSON_ERROR_NONE !== \json_last_error()) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
'"%s" is not valid JSON.', | ||
$json | ||
)); | ||
} | ||
|
||
if (!\property_exists($decoded, 'config')) { | ||
return $json; | ||
} | ||
|
||
$config = (array) $decoded->config; | ||
|
||
\ksort($config); | ||
|
||
$decoded->config = $config; | ||
|
||
return \json_encode($decoded); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller. | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/composer-normalize | ||
*/ | ||
|
||
namespace Localheinz\Composer\Normalize\Test\Unit\Normalizer; | ||
|
||
use Localheinz\Json\Normalizer\NormalizerInterface; | ||
use Localheinz\Test\Util\Helper; | ||
use PHPUnit\Framework; | ||
|
||
abstract class AbstractNormalizerTestCase extends Framework\TestCase | ||
{ | ||
use Helper; | ||
|
||
final public function testImplementsNormalizerInterface() | ||
{ | ||
$this->assertClassImplementsInterface(NormalizerInterface::class, $this->className()); | ||
} | ||
|
||
final public function testNormalizeRejectsInvalidJson() | ||
{ | ||
$json = $this->faker()->realText(); | ||
|
||
$reflection = new \ReflectionClass($this->className()); | ||
|
||
$normalizer = $reflection->newInstanceWithoutConstructor(); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage(\sprintf( | ||
'"%s" is not valid JSON.', | ||
$json | ||
)); | ||
|
||
$normalizer->normalize($json); | ||
} | ||
|
||
final protected function className(): string | ||
{ | ||
return \preg_replace( | ||
'/Test$/', | ||
'', | ||
\str_replace( | ||
'Localheinz\\Composer\\Normalize\\Test\\Unit\\', | ||
'Localheinz\\Composer\\Normalize\\', | ||
static::class | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller. | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/composer-normalize | ||
*/ | ||
|
||
namespace Localheinz\Composer\Normalize\Test\Unit\Normalizer; | ||
|
||
use Localheinz\Composer\Normalize\Normalizer\ConfigHashNormalizer; | ||
|
||
final class ConfigHashNormalizerTest extends AbstractNormalizerTestCase | ||
{ | ||
public function testNormalizeDoesNotModifyOtherProperty() | ||
{ | ||
$json = <<<'JSON' | ||
{ | ||
"foo": { | ||
"qux": "quux", | ||
"bar": "baz" | ||
} | ||
} | ||
JSON; | ||
|
||
$normalizer = new ConfigHashNormalizer(); | ||
|
||
$this->assertSame($json, $normalizer->normalize($json)); | ||
} | ||
|
||
public function testNormalizeSortsConfigHashIfPropertyExists() | ||
{ | ||
$json = <<<'JSON' | ||
{ | ||
"config": { | ||
"sort-packages": true, | ||
"preferred-install": "dist" | ||
}, | ||
"foo": { | ||
"qux": "quux", | ||
"bar": "baz" | ||
} | ||
} | ||
JSON; | ||
|
||
$normalized = <<<'JSON' | ||
{ | ||
"config": { | ||
"preferred-install": "dist", | ||
"sort-packages": true | ||
}, | ||
"foo": { | ||
"qux": "quux", | ||
"bar": "baz" | ||
} | ||
} | ||
JSON; | ||
|
||
$normalizer = new ConfigHashNormalizer(); | ||
|
||
$this->assertSame(\json_encode(\json_decode($normalized)), $normalizer->normalize($json)); | ||
} | ||
} |