-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 lcobucci/implement-basic-formatters
Implement basic formatters
- Loading branch information
Showing
5 changed files
with
271 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter; | ||
use Throwable; | ||
use const JSON_HEX_AMP; | ||
use const JSON_HEX_APOS; | ||
use const JSON_HEX_QUOT; | ||
use const JSON_HEX_TAG; | ||
use const JSON_UNESCAPED_SLASHES; | ||
use function json_encode; | ||
use function json_last_error; | ||
use function json_last_error_msg; | ||
use function sprintf; | ||
|
||
final class Json implements Formatter | ||
{ | ||
private const DEFAULT_FLAGS = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $flags; | ||
|
||
public function __construct(int $flags = self::DEFAULT_FLAGS) | ||
{ | ||
$this->flags = $flags; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format($content): string | ||
{ | ||
try { | ||
$encoded = json_encode($content, $this->flags); | ||
} catch (Throwable $exception) { | ||
throw new ContentCouldNotBeFormatted( | ||
'An exception was thrown during JSON formatting', | ||
$exception->getCode(), | ||
$exception | ||
); | ||
} | ||
|
||
if ($encoded === false) { | ||
throw new ContentCouldNotBeFormatted( | ||
sprintf('Given data cannot be formatted as JSON: %s', json_last_error_msg()), | ||
json_last_error() | ||
); | ||
} | ||
|
||
return $encoded; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter; | ||
use function is_object; | ||
use function method_exists; | ||
|
||
final class StringCast implements Formatter | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format($content): string | ||
{ | ||
if (is_object($content) && ! method_exists($content, '__toString')) { | ||
throw new ContentCouldNotBeFormatted('Given data could not be cast to string'); | ||
} | ||
|
||
return (string) $content; | ||
} | ||
} |
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,111 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Tests\Formatter; | ||
|
||
use JsonSerializable; | ||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter\Json; | ||
use Lcobucci\ContentNegotiation\Tests\PersonDto; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
use const JSON_HEX_AMP; | ||
use const JSON_HEX_APOS; | ||
use const JSON_HEX_QUOT; | ||
use const JSON_HEX_TAG; | ||
use const JSON_UNESCAPED_SLASHES; | ||
use function acos; | ||
|
||
/** | ||
* @coversDefaultClass \Lcobucci\ContentNegotiation\Formatter\Json | ||
*/ | ||
final class JsonTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
*/ | ||
public function constructorShouldAllowTheConfigurationOfEncodingFlags(): void | ||
{ | ||
self::assertAttributeSame(JSON_UNESCAPED_SLASHES, 'flags', new Json(JSON_UNESCAPED_SLASHES)); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
*/ | ||
public function constructorShouldUseDefaultFlagsWhenNothingWasSet(): void | ||
{ | ||
self::assertAttributeSame( | ||
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_SLASHES, | ||
'flags', | ||
new Json() | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::format() | ||
* | ||
* @uses \Lcobucci\ContentNegotiation\Formatter\Json::__construct() | ||
* | ||
*/ | ||
public function formatShouldReturnAJsonEncodedValue(): void | ||
{ | ||
self::assertJsonStringEqualsJsonString( | ||
'{"id":1,"name":"Test"}', | ||
$this->format(new PersonDto(1, 'Test')) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::format() | ||
* | ||
* @uses \Lcobucci\ContentNegotiation\Formatter\Json::__construct() | ||
*/ | ||
public function formatShouldRaiseExceptionWhenContentCouldNotBeEncoded(): void | ||
{ | ||
$this->expectException(ContentCouldNotBeFormatted::class); | ||
$this->expectExceptionMessage('Inf and NaN cannot be JSON encoded'); | ||
|
||
$this->format(acos(8)); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::format() | ||
* | ||
* @uses \Lcobucci\ContentNegotiation\Formatter\Json::__construct() | ||
*/ | ||
public function formatShouldConvertAnyExceptionDuringJsonSerialization(): void | ||
{ | ||
$this->expectException(ContentCouldNotBeFormatted::class); | ||
$this->expectExceptionMessage('An exception was thrown during JSON formatting'); | ||
|
||
$this->format( | ||
new class implements JsonSerializable | ||
{ | ||
public function jsonSerialize(): void | ||
{ | ||
throw new RuntimeException('This should be converted'); | ||
} | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @param mixed $content | ||
*/ | ||
private function format($content): string | ||
{ | ||
$formatter = new Json(); | ||
|
||
return $formatter->format($content); | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Tests\Formatter; | ||
|
||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter\StringCast; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Lcobucci\ContentNegotiation\Formatter\StringCast | ||
*/ | ||
final class StringCastTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
* @dataProvider validData | ||
* | ||
* @covers ::format() | ||
* | ||
* @param mixed $content | ||
*/ | ||
public function formatShouldSimplyReturnTheStringRepresentationOfTheContent( | ||
string $expected, | ||
$content | ||
): void { | ||
$formatter = new StringCast(); | ||
|
||
self::assertSame($expected, $formatter->format($content)); | ||
} | ||
|
||
/** | ||
* @return mixed[][] | ||
*/ | ||
public function validData(): array | ||
{ | ||
$test = new class | ||
{ | ||
public function __toString(): string | ||
{ | ||
return 'test'; | ||
} | ||
}; | ||
|
||
return [ | ||
['test', 'test'], | ||
['test', $test], | ||
['1', 1], | ||
['1', true], | ||
['', false], | ||
['', null], | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::format() | ||
*/ | ||
public function formatShouldRaiseExceptionWhenContentCouldNotBeCastToString(): void | ||
{ | ||
$this->expectException(ContentCouldNotBeFormatted::class); | ||
|
||
$content = new class | ||
{ | ||
}; | ||
|
||
$formatter = new StringCast(); | ||
$formatter->format($content); | ||
} | ||
} |