Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JMS Serializer formatter #4

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
45 changes: 45 additions & 0 deletions src/Formatter/JmsSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace Lcobucci\ContentNegotiation\Formatter;

use JMS\Serializer\SerializerInterface;
use Lcobucci\ContentNegotiation\ContentCouldNotBeFormatted;
use Lcobucci\ContentNegotiation\Formatter;
use Throwable;
use function sprintf;

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']);
}
}