-
-
Notifications
You must be signed in to change notification settings - Fork 6
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 #3 from localheinz/feature/no-final-new-line-norma…
…lizer Enhancement: Implement NoFinalNewLineNormalizer
- Loading branch information
Showing
3 changed files
with
135 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,29 @@ | ||
<?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/json-normalizer | ||
*/ | ||
|
||
namespace Localheinz\Json\Normalizer; | ||
|
||
final class NoFinalNewLineNormalizer implements NormalizerInterface | ||
{ | ||
public function normalize(string $json): string | ||
{ | ||
if (null === \json_decode($json) && JSON_ERROR_NONE !== \json_last_error()) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
'"%s" is not valid JSON.', | ||
$json | ||
)); | ||
} | ||
|
||
return \rtrim($json); | ||
} | ||
} |
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,82 @@ | ||
<?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/json-normalizer | ||
*/ | ||
|
||
namespace Localheinz\Json\Normalizer\Test\Unit; | ||
|
||
use Localheinz\Json\Normalizer\NoFinalNewLineNormalizer; | ||
use Localheinz\Json\Normalizer\NormalizerInterface; | ||
use Localheinz\Test\Util\Helper; | ||
use PHPUnit\Framework; | ||
|
||
final class NoFinalNewLineNormalizerTest extends Framework\TestCase | ||
{ | ||
use Helper; | ||
|
||
public function testImplementsNormalizerInterface() | ||
{ | ||
$this->assertClassImplementsInterface(NormalizerInterface::class, NoFinalNewLineNormalizer::class); | ||
} | ||
|
||
public function testNormalizeRejectsInvalidJson() | ||
{ | ||
$json = $this->faker()->realText(); | ||
|
||
$normalizer = new NoFinalNewLineNormalizer(); | ||
|
||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage(\sprintf( | ||
'"%s" is not valid JSON.', | ||
$json | ||
)); | ||
|
||
$normalizer->normalize($json); | ||
} | ||
|
||
/** | ||
* @dataProvider providerWhitespace | ||
* | ||
* @param string $whitespace | ||
*/ | ||
public function testNormalizeRemovesAllWhitespaceFromEndOfJson(string $whitespace) | ||
{ | ||
$json = <<<'JSON' | ||
{ | ||
"name": "Andreas Möller", | ||
"url": "https://localheinz.com" | ||
} | ||
JSON; | ||
$json .= $whitespace; | ||
|
||
$normalized = \rtrim($json); | ||
|
||
$normalizer = new NoFinalNewLineNormalizer(); | ||
|
||
$this->assertSame($normalized, $normalizer->normalize($json)); | ||
} | ||
|
||
public function providerWhitespace() | ||
{ | ||
$values = [ | ||
'', | ||
' ', | ||
"\t", | ||
PHP_EOL, | ||
]; | ||
|
||
foreach ($values as $value) { | ||
yield [ | ||
$value, | ||
]; | ||
} | ||
} | ||
} |