-
-
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.
To server as example of more flexible formatting.
- Loading branch information
Showing
3 changed files
with
113 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,44 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Formatter; | ||
|
||
use JMS\Serializer\SerializerInterface; | ||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter; | ||
use Throwable; | ||
|
||
final class JmsSerializer implements Formatter | ||
{ | ||
/** | ||
* @var SerializerInterface | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $format; | ||
|
||
public function __construct(SerializerInterface $serializer, string $format) | ||
{ | ||
$this->serializer = $serializer; | ||
$this->format = $format; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format($content): string | ||
{ | ||
try { | ||
return $this->serializer->serialize($content, $this->format); | ||
} catch (Throwable $exception) { | ||
throw new ContentCouldNotBeFormatted( | ||
sprintf('Given content could not be formatted in %s using JMS Serializer', $this->format), | ||
$exception->getCode(), | ||
$exception | ||
); | ||
} | ||
} | ||
} |
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,67 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Lcobucci\ContentNegotiation\Tests\Formatter; | ||
|
||
use JMS\Serializer\SerializerInterface; | ||
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted; | ||
use Lcobucci\ContentNegotiation\Formatter\JmsSerializer; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
|
||
/** | ||
* @coversDefaultClass \Lcobucci\ContentNegotiation\Formatter\JmsSerializer | ||
*/ | ||
final class JmsSerializerTest extends TestCase | ||
{ | ||
/** | ||
* @var SerializerInterface|MockObject | ||
*/ | ||
private $serializer; | ||
|
||
/** | ||
* @before | ||
*/ | ||
public function createSerializer(): void | ||
{ | ||
$this->serializer = $this->createMock(SerializerInterface::class); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
*/ | ||
public function formatShouldSimplyForwardCallToSerializer(): void | ||
{ | ||
$content = ['a' => 'test']; | ||
|
||
$this->serializer->expects(self::once()) | ||
->method('serialize') | ||
->with($content, 'json') | ||
->willReturn('{"a":"test"}'); | ||
|
||
$formatter = new JmsSerializer($this->serializer, 'json'); | ||
|
||
self::assertSame('{"a":"test"}', $formatter->format($content)); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @covers ::__construct() | ||
* @covers ::format() | ||
*/ | ||
public function formatShouldConvertAnyRaisedException(): void | ||
{ | ||
$this->expectException(ContentCouldNotBeFormatted::class); | ||
|
||
$this->serializer->method('serialize') | ||
->willThrowException(new RuntimeException()); | ||
|
||
$formatter = new JmsSerializer($this->serializer, 'json'); | ||
$formatter->format(['a' => 'test']); | ||
} | ||
} |