Skip to content

Commit

Permalink
Enhancement: Assert that normalization of JSON is skipped when not de…
Browse files Browse the repository at this point in the history
…coding to object
  • Loading branch information
localheinz committed Oct 9, 2018
1 parent c28f2e9 commit a2cfbf6
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions test/Unit/Normalizer/AbstractNormalizerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ final public function testNormalizeRejectsInvalidJson(): void
{
$json = $this->faker()->realText();

$reflection = new \ReflectionClass($this->className());

$normalizer = $reflection->newInstanceWithoutConstructor();
$normalizer = $this->createNormalizer();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage(\sprintf(
Expand All @@ -46,6 +44,41 @@ final public function testNormalizeRejectsInvalidJson(): void
$normalizer->normalize($json);
}

/**
* @dataProvider providerJsonNotDecodingToObject
*
* @param string $json
*/
final public function testNormalizeDoesNotModifyWhenJsonDecodedIsNotAnObject(string $json): void
{
$normalizer = $this->createNormalizer();

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

$this->assertSame($json, $normalized);
}

public function providerJsonNotDecodingToObject(): \Generator
{
$faker = $this->faker();

$values = [
'array' => $faker->words,
'bool-false' => false,
'bool-true' => true,
'float' => $faker->randomFloat(),
'int' => $faker->randomNumber(),
'null' => null,
'string' => $faker->sentence,
];

foreach ($values as $key => $value) {
yield $key => [
\json_encode($value),
];
}
}

final protected function className(): string
{
return \preg_replace(
Expand All @@ -58,4 +91,11 @@ final protected function className(): string
)
);
}

private function createNormalizer(): Normalizer\NormalizerInterface
{
$reflection = new \ReflectionClass($this->className());

return $reflection->newInstanceWithoutConstructor();
}
}

0 comments on commit a2cfbf6

Please sign in to comment.