Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasnoback committed Mar 1, 2015
0 parents commit e039419
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml export-ignore
/phpunit.xml.dist export-ignore
/composer.lock export-ignore
/tests export-ignore
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vendor/
phpunit.xml
.couscous/
composer.lock
/tests/Functional/cache/
/tests/Functional/temp/
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: php

php:
- 5.4

before_script:
- composer selfupdate
- composer install --prefer-dist

script:
- vendor/bin/phpunit

notifications:
email: [email protected]
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Change log

## [Unreleased][unreleased]

19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2015 Matthias Noback

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# JMSSerializerBridge

[![Build Status](https://travis-ci.org/SimpleBus/JMSSerializerBridge.svg?branch=master)](https://travis-ci.org/SimpleBus/JMSSerializerBridge)

By [Matthias Noback](http://php-and-symfony.matthiasnoback.nl/)
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "simple-bus/jms-serializer-bridge",
"type": "bundle",
"description": "Bridge for using JMSSerializer as message serializer for SimpleBus/Asynchronous",
"keywords": ["asynchronous", "message", "serialization"],
"homepage": "http://github.com/SimpleBus/JMSSerializerBridge",
"license": "MIT",
"authors": [
{
"name": "Matthias Noback",
"email": "[email protected]",
"homepage": "http://php-and-symfony.matthiasnoback.nl"
}
],
"require": {
"php": ">=5.4",
"beberlei/assert": "~2.0",
"simple-bus/message-bus": "~1.0",
"simple-bus/asynchronous": "dev-master",
"jms/serializer": "~0.12"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4" : { "SimpleBus\\JMSSerializerBridge\\" : "src" }
},
"autoload-dev": {
"psr-4" : { "SimpleBus\\JMSSerializerBridge\\Tests\\" : "tests" }
}
}
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
<testsuites>
<testsuite>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
76 changes: 76 additions & 0 deletions src/JMSSerializerMessageSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace SimpleBus\JMSSerializerBridge;

use Assert\Assertion;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;
use SimpleBus\Asynchronous\Message\Envelope\MessageEnvelope;
use SimpleBus\Asynchronous\Message\Envelope\MessageEnvelopeFactory;
use SimpleBus\Asynchronous\Message\Serializer\MessageSerializer;
use SimpleBus\Message\Message;

class JMSSerializerMessageSerializer implements MessageSerializer
{
/**
* @var MessageEnvelopeFactory
*/
private $messageEnvelopeFactory;

/**
* @var SerializerInterface
*/
private $serializer;

/**
* @var string
*/
private $format;

public function __construct(
MessageEnvelopeFactory $messageEnvelopeFactory,
SerializerInterface $serializer,
$format
) {
$this->messageEnvelopeFactory = $messageEnvelopeFactory;
$this->serializer = $serializer;
$this->format = $format;
}

public function serialize(Message $message)
{
$type = get_class($message);
$messageBody = $this->serializeObject($message);
$envelope = $this->messageEnvelopeFactory->createEnvelopeForSerializedMessage($type, $messageBody);

return $this->serializeObject($envelope);
}

public function deserialize($serializedEnvelope)
{
$envelope = $this->deserializeObject($serializedEnvelope, $this->messageEnvelopeFactory->envelopeClass());
/** @var $envelope MessageEnvelope */

$message = $this->deserializeObject($envelope->serializedMessage(), $envelope->type());
Assertion::isInstanceOf($message, 'SimpleBus\Message\Message');

return $message;
}

private function serializeObject($object)
{
$serializationContext = SerializationContext::create()
->setSerializeNull(true);

return $this->serializer->serialize($object, $this->format, $serializationContext);
}

private function deserializeObject($serializedObject, $type)
{
$deserializationContext = DeserializationContext::create()
->setSerializeNull(true);

return $this->serializer->deserialize($serializedObject, $type, $this->format, $deserializationContext);
}
}
11 changes: 11 additions & 0 deletions src/SerializerMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace SimpleBus\JMSSerializerBridge;

final class SerializerMetadata
{
public static function dir()
{
return __DIR__ . '/metadata';
}
}
6 changes: 6 additions & 0 deletions src/metadata/Message.Envelope.DefaultMessageEnvelope.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SimpleBus\Asynchronous\Message\Envelope\DefaultMessageEnvelope:
properties:
type:
type: string
serializedMessage:
type: string
29 changes: 29 additions & 0 deletions tests/Integration/JMSSerializerMessageSerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace SimpleBus\JMSSerializerBridge\Tests\Integration;

use JMS\Serializer\SerializerBuilder;
use SimpleBus\Asynchronous\Message\Envelope\DefaultMessageEnvelopeFactory;
use SimpleBus\JMSSerializerBridge\JMSSerializerMessageSerializer;
use SimpleBus\JMSSerializerBridge\SerializerMetadata;

class JMSSerializerMessageSerializerTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function it_serializes_and_deserializes_messages()
{
$messageEnvelopeFactory = new DefaultMessageEnvelopeFactory();
$format = 'json';
$jmsSerializer = SerializerBuilder::create()
->addMetadataDir(SerializerMetadata::dir(), 'SimpleBus\Asynchronous')
->build();
$originalMessage = new SampleMessage('test', 123);

$messageSerializer = new JMSSerializerMessageSerializer($messageEnvelopeFactory, $jmsSerializer, $format);
$serializedMessageEnvelope = $messageSerializer->serialize($originalMessage);
$deserializedMessage = $messageSerializer->deserialize($serializedMessageEnvelope);
$this->assertEquals($deserializedMessage, $originalMessage);
}
}
25 changes: 25 additions & 0 deletions tests/Integration/SampleMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace SimpleBus\JMSSerializerBridge\Tests\Integration;

use SimpleBus\Message\Message;
use JMS\Serializer\Annotation as Serializer;

class SampleMessage implements Message
{
/**
* @Serializer\Type("string")
*/
private $foo;

/**
* @Serializer\Type("integer")
*/
private $bar;

public function __construct($foo, $bar)
{
$this->foo = $foo;
$this->bar = $bar;
}
}
7 changes: 7 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Doctrine\Common\Annotations\AnnotationRegistry;

require __DIR__ . '/../vendor/autoload.php';

AnnotationRegistry::registerLoader('class_exists');

0 comments on commit e039419

Please sign in to comment.