Skip to content

Commit

Permalink
Add JMS Serializer formatter
Browse files Browse the repository at this point in the history
To server as example of more flexible formatting.
  • Loading branch information
lcobucci committed Mar 26, 2018
1 parent e1ef09c commit 5591447
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"require-dev": {
"doctrine/coding-standard": "^4.0",
"infection/infection": "^0.8",
"jms/serializer": "^1.11",
"middlewares/negotiation": "^1.0",
"phpstan/phpstan": "^0.10@dev",
"phpstan/phpstan-phpunit": "^0.10@dev",
Expand All @@ -35,6 +36,7 @@
"zendframework/zend-diactoros": "^1.7"
},
"suggest": {
"jms/serializer": "For content formatting using a more flexible serializer",
"middlewares/negotiation": "For acceptable format identification",
"zendframework/zend-diactoros": "For concrete implementation of PSR-7"
},
Expand Down
44 changes: 44 additions & 0 deletions src/Formatter/JmsSerializer.php
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
);
}
}
}
67 changes: 67 additions & 0 deletions tests/Formatter/JmsSerializerTest.php
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']);
}
}

0 comments on commit 5591447

Please sign in to comment.