forked from SimpleBus/jms-serializer-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e039419
Showing
14 changed files
with
257 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
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 |
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,6 @@ | ||
vendor/ | ||
phpunit.xml | ||
.couscous/ | ||
composer.lock | ||
/tests/Functional/cache/ | ||
/tests/Functional/temp/ |
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,14 @@ | ||
language: php | ||
|
||
php: | ||
- 5.4 | ||
|
||
before_script: | ||
- composer selfupdate | ||
- composer install --prefer-dist | ||
|
||
script: | ||
- vendor/bin/phpunit | ||
|
||
notifications: | ||
email: [email protected] |
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,4 @@ | ||
# Change log | ||
|
||
## [Unreleased][unreleased] | ||
|
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,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. |
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,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/) |
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,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" } | ||
} | ||
} |
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,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> |
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,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); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace SimpleBus\JMSSerializerBridge; | ||
|
||
final class SerializerMetadata | ||
{ | ||
public static function dir() | ||
{ | ||
return __DIR__ . '/metadata'; | ||
} | ||
} |
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,6 @@ | ||
SimpleBus\Asynchronous\Message\Envelope\DefaultMessageEnvelope: | ||
properties: | ||
type: | ||
type: string | ||
serializedMessage: | ||
type: string |
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,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); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,7 @@ | ||
<?php | ||
|
||
use Doctrine\Common\Annotations\AnnotationRegistry; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
AnnotationRegistry::registerLoader('class_exists'); |