diff --git a/composer.json b/composer.json index 82f2d64..c1df3fb 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,10 @@ "psr/log": "~1.0" }, "require-dev": { - "phpunit/phpunit": "~4.0", + "phpunit/phpunit": "~4.1", + "doctrine/common": "~2.3", "doctrine/orm": "~2.3", + "jms/serializer": "~0.14", "empi89/php-amqp-stubs": "dev-master" }, "suggest": { diff --git a/composer.lock b/composer.lock index 4287933..184dc88 100644 --- a/composer.lock +++ b/composer.lock @@ -1,9 +1,10 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" + "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" ], - "hash": "c0855dfa38777dae9686ccfef8eb451d", + "hash": "d7f40b6da4ae6c92a663ca034e3066d3", "packages": [ { "name": "psr/log", @@ -47,16 +48,16 @@ "packages-dev": [ { "name": "doctrine/annotations", - "version": "v1.1.2", + "version": "v1.2.0", "source": { "type": "git", "url": "https://github.com/doctrine/annotations.git", - "reference": "40db0c96985aab2822edbc4848b3bd2429e02670" + "reference": "d9b1a37e9351ddde1f19f09a02e3d6ee92e82efd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/annotations/zipball/40db0c96985aab2822edbc4848b3bd2429e02670", - "reference": "40db0c96985aab2822edbc4848b3bd2429e02670", + "url": "https://api.github.com/repos/doctrine/annotations/zipball/d9b1a37e9351ddde1f19f09a02e3d6ee92e82efd", + "reference": "d9b1a37e9351ddde1f19f09a02e3d6ee92e82efd", "shasum": "" }, "require": { @@ -64,12 +65,13 @@ "php": ">=5.3.2" }, "require-dev": { - "doctrine/cache": "1.*" + "doctrine/cache": "1.*", + "phpunit/phpunit": "4.*" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.3.x-dev" } }, "autoload": { @@ -104,7 +106,7 @@ { "name": "Johannes Schmitt", "email": "schmittjoh@gmail.com", - "homepage": "http://jmsyst.com", + "homepage": "https://github.com/schmittjoh", "role": "Developer of wrapped JMSSerializerBundle" } ], @@ -115,7 +117,7 @@ "docblock", "parser" ], - "time": "2013-06-16 21:33:03" + "time": "2014-07-06 15:52:21" }, { "name": "doctrine/cache", @@ -261,16 +263,16 @@ }, { "name": "doctrine/common", - "version": "v2.4.1", + "version": "v2.4.2", "source": { "type": "git", "url": "https://github.com/doctrine/common.git", - "reference": "ceb18cf9b0230f3ea208b6238130fd415abda0a7" + "reference": "5db6ab40e4c531f14dad4ca96a394dfce5d4255b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/common/zipball/ceb18cf9b0230f3ea208b6238130fd415abda0a7", - "reference": "ceb18cf9b0230f3ea208b6238130fd415abda0a7", + "url": "https://api.github.com/repos/doctrine/common/zipball/5db6ab40e4c531f14dad4ca96a394dfce5d4255b", + "reference": "5db6ab40e4c531f14dad4ca96a394dfce5d4255b", "shasum": "" }, "require": { @@ -281,6 +283,9 @@ "doctrine/lexer": "1.*", "php": ">=5.3.2" }, + "require-dev": { + "phpunit/phpunit": "~3.7" + }, "type": "library", "extra": { "branch-alias": { @@ -319,7 +324,7 @@ { "name": "Johannes Schmitt", "email": "schmittjoh@gmail.com", - "homepage": "http://jmsyst.com", + "homepage": "https://github.com/schmittjoh", "role": "Developer of wrapped JMSSerializerBundle" } ], @@ -332,7 +337,7 @@ "persistence", "spl" ], - "time": "2013-09-07 10:20:34" + "time": "2014-05-21 19:28:51" }, { "name": "doctrine/dbal", @@ -518,16 +523,16 @@ }, { "name": "doctrine/orm", - "version": "v2.4.2", + "version": "v2.4.4", "source": { "type": "git", "url": "https://github.com/doctrine/doctrine2.git", - "reference": "0363a5548d9263f979f9ca149decb9cfc66419ab" + "reference": "fc19c3b53dcd00e6584db40669fdd699c4671f97" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/doctrine/doctrine2/zipball/0363a5548d9263f979f9ca149decb9cfc66419ab", - "reference": "0363a5548d9263f979f9ca149decb9cfc66419ab", + "url": "https://api.github.com/repos/doctrine/doctrine2/zipball/fc19c3b53dcd00e6584db40669fdd699c4671f97", + "reference": "fc19c3b53dcd00e6584db40669fdd699c4671f97", "shasum": "" }, "require": { @@ -590,7 +595,7 @@ "database", "orm" ], - "time": "2014-02-08 16:35:09" + "time": "2014-07-11 03:05:53" }, { "name": "empi89/php-amqp-stubs", @@ -598,12 +603,12 @@ "source": { "type": "git", "url": "https://github.com/empi89/php-amqp-stubs.git", - "reference": "22abea6d85b8ebfa06104c82c9a2359f26eaec52" + "reference": "f8f402e81edf56871fa7192df01b8e47f29e2e13" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/empi89/php-amqp-stubs/zipball/22abea6d85b8ebfa06104c82c9a2359f26eaec52", - "reference": "22abea6d85b8ebfa06104c82c9a2359f26eaec52", + "url": "https://api.github.com/repos/empi89/php-amqp-stubs/zipball/f8f402e81edf56871fa7192df01b8e47f29e2e13", + "reference": "f8f402e81edf56871fa7192df01b8e47f29e2e13", "shasum": "" }, "require": { @@ -628,20 +633,276 @@ "AMQP", "testing" ], - "time": "2014-04-21 08:32:58" + "time": "2014-07-17 15:28:25" + }, + { + "name": "jms/metadata", + "version": "1.5.1", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/metadata.git", + "reference": "22b72455559a25777cfd28c4ffda81ff7639f353" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/metadata/zipball/22b72455559a25777cfd28c4ffda81ff7639f353", + "reference": "22b72455559a25777cfd28c4ffda81ff7639f353", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "doctrine/cache": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.5.x-dev" + } + }, + "autoload": { + "psr-0": { + "Metadata\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache" + ], + "authors": [ + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Class/method/property metadata management in PHP", + "keywords": [ + "annotations", + "metadata", + "xml", + "yaml" + ], + "time": "2014-07-12 07:13:19" + }, + { + "name": "jms/parser-lib", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/parser-lib.git", + "reference": "c509473bc1b4866415627af0e1c6cc8ac97fa51d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/parser-lib/zipball/c509473bc1b4866415627af0e1c6cc8ac97fa51d", + "reference": "c509473bc1b4866415627af0e1c6cc8ac97fa51d", + "shasum": "" + }, + "require": { + "phpoption/phpoption": ">=0.9,<2.0-dev" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-0": { + "JMS\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache2" + ], + "description": "A library for easily creating recursive-descent parsers.", + "time": "2012-11-18 18:08:43" + }, + { + "name": "jms/serializer", + "version": "0.16.0", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/serializer.git", + "reference": "c8a171357ca92b6706e395c757f334902d430ea9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/serializer/zipball/c8a171357ca92b6706e395c757f334902d430ea9", + "reference": "c8a171357ca92b6706e395c757f334902d430ea9", + "shasum": "" + }, + "require": { + "doctrine/annotations": "1.*", + "jms/metadata": "~1.1", + "jms/parser-lib": "1.*", + "php": ">=5.3.2", + "phpcollection/phpcollection": "~0.1" + }, + "require-dev": { + "doctrine/orm": "~2.1", + "doctrine/phpcr-odm": "~1.0.1", + "jackalope/jackalope-doctrine-dbal": "1.0.*", + "propel/propel1": "~1.7", + "symfony/filesystem": "2.*", + "symfony/form": "~2.1", + "symfony/translation": "~2.0", + "symfony/validator": "~2.0", + "symfony/yaml": "2.*", + "twig/twig": ">=1.8,<2.0-dev" + }, + "suggest": { + "symfony/yaml": "Required if you'd like to serialize data to YAML format." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.15-dev" + } + }, + "autoload": { + "psr-0": { + "JMS\\Serializer": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache2" + ], + "authors": [ + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Library for (de-)serializing data of any complexity; supports XML, JSON, and YAML.", + "homepage": "http://jmsyst.com/libs/serializer", + "keywords": [ + "deserialization", + "jaxb", + "json", + "serialization", + "xml" + ], + "time": "2014-03-18 08:39:00" + }, + { + "name": "phpcollection/phpcollection", + "version": "0.4.0", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/php-collection.git", + "reference": "b8bf55a0a929ca43b01232b36719f176f86c7e83" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/php-collection/zipball/b8bf55a0a929ca43b01232b36719f176f86c7e83", + "reference": "b8bf55a0a929ca43b01232b36719f176f86c7e83", + "shasum": "" + }, + "require": { + "phpoption/phpoption": "1.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.3-dev" + } + }, + "autoload": { + "psr-0": { + "PhpCollection": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache2" + ], + "authors": [ + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "General-Purpose Collection Library for PHP", + "keywords": [ + "collection", + "list", + "map", + "sequence", + "set" + ], + "time": "2014-03-11 13:46:42" + }, + { + "name": "phpoption/phpoption", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/php-option.git", + "reference": "5d099bcf0393908bf4ad69cc47dafb785d51f7f5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/5d099bcf0393908bf4ad69cc47dafb785d51f7f5", + "reference": "5d099bcf0393908bf4ad69cc47dafb785d51f7f5", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "psr-0": { + "PhpOption\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache2" + ], + "authors": [ + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Option Type for PHP", + "keywords": [ + "language", + "option", + "php", + "type" + ], + "time": "2014-01-09 22:37:17" }, { "name": "phpunit/php-code-coverage", - "version": "2.0.6", + "version": "2.0.9", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "bccecf50645068b44f49a84009e2a0499a500b99" + "reference": "ed8ac99ce38c3fd134128c898f7ca74665abef7f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/bccecf50645068b44f49a84009e2a0499a500b99", - "reference": "bccecf50645068b44f49a84009e2a0499a500b99", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/ed8ac99ce38c3fd134128c898f7ca74665abef7f", + "reference": "ed8ac99ce38c3fd134128c898f7ca74665abef7f", "shasum": "" }, "require": { @@ -693,7 +954,7 @@ "testing", "xunit" ], - "time": "2014-04-30 09:01:21" + "time": "2014-06-29 08:14:40" }, { "name": "phpunit/php-file-iterator", @@ -880,16 +1141,16 @@ }, { "name": "phpunit/phpunit", - "version": "4.1.0", + "version": "4.1.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "efb1b1334605594417a3bd466477772d06d460a8" + "reference": "a71c4842c5fb836d8b200624583b859ec34e8a26" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/efb1b1334605594417a3bd466477772d06d460a8", - "reference": "efb1b1334605594417a3bd466477772d06d460a8", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a71c4842c5fb836d8b200624583b859ec34e8a26", + "reference": "a71c4842c5fb836d8b200624583b859ec34e8a26", "shasum": "" }, "require": { @@ -950,20 +1211,20 @@ "testing", "xunit" ], - "time": "2014-05-02 07:13:40" + "time": "2014-07-18 07:15:58" }, { "name": "phpunit/phpunit-mock-objects", - "version": "2.1.0", + "version": "2.1.5", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "da0eb04d8ee95ec2898187e407e519c118d3d27c" + "reference": "7878b9c41edb3afab92b85edf5f0981014a2713a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/da0eb04d8ee95ec2898187e407e519c118d3d27c", - "reference": "da0eb04d8ee95ec2898187e407e519c118d3d27c", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/7878b9c41edb3afab92b85edf5f0981014a2713a", + "reference": "7878b9c41edb3afab92b85edf5f0981014a2713a", "shasum": "" }, "require": { @@ -1007,7 +1268,7 @@ "mock", "xunit" ], - "time": "2014-05-02 07:04:11" + "time": "2014-06-12 07:22:15" }, { "name": "sebastian/comparator", @@ -1278,32 +1539,34 @@ }, { "name": "symfony/console", - "version": "v2.4.4", + "version": "v2.5.2", "target-dir": "Symfony/Component/Console", "source": { "type": "git", "url": "https://github.com/symfony/Console.git", - "reference": "2e452005b1e1d003d23702d227e23614679eb5ca" + "reference": "386fa63407805959bd2c5fe540294721ad4224c8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Console/zipball/2e452005b1e1d003d23702d227e23614679eb5ca", - "reference": "2e452005b1e1d003d23702d227e23614679eb5ca", + "url": "https://api.github.com/repos/symfony/Console/zipball/386fa63407805959bd2c5fe540294721ad4224c8", + "reference": "386fa63407805959bd2c5fe540294721ad4224c8", "shasum": "" }, "require": { "php": ">=5.3.3" }, "require-dev": { + "psr/log": "~1.0", "symfony/event-dispatcher": "~2.1" }, "suggest": { + "psr/log": "For using the console logger", "symfony/event-dispatcher": "" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "2.4-dev" + "dev-master": "2.5-dev" } }, "autoload": { @@ -1329,21 +1592,21 @@ ], "description": "Symfony Console Component", "homepage": "http://symfony.com", - "time": "2014-04-27 13:34:57" + "time": "2014-07-15 14:15:12" }, { "name": "symfony/yaml", - "version": "v2.4.4", + "version": "v2.5.2", "target-dir": "Symfony/Component/Yaml", "source": { "type": "git", "url": "https://github.com/symfony/Yaml.git", - "reference": "65539ecde838f9c0d18b006b2101e3deb4b5c9ff" + "reference": "f868ecdbcc0276b6158dfbf08b9e98ce07f014e1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/65539ecde838f9c0d18b006b2101e3deb4b5c9ff", - "reference": "65539ecde838f9c0d18b006b2101e3deb4b5c9ff", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/f868ecdbcc0276b6158dfbf08b9e98ce07f014e1", + "reference": "f868ecdbcc0276b6158dfbf08b9e98ce07f014e1", "shasum": "" }, "require": { @@ -1352,7 +1615,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.4-dev" + "dev-master": "2.5-dev" } }, "autoload": { @@ -1378,7 +1641,7 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2014-04-18 20:37:09" + "time": "2014-07-09 09:05:48" } ], "aliases": [], diff --git a/src/TreeHouse/Queue/Message/Composer/DoctrineMessageComposer.php b/src/TreeHouse/Queue/Message/Composer/DoctrineMessageComposer.php new file mode 100644 index 0000000..1e1ad7b --- /dev/null +++ b/src/TreeHouse/Queue/Message/Composer/DoctrineMessageComposer.php @@ -0,0 +1,61 @@ +doctrine = $doctrine; + $this->className = $className; + + parent::__construct($serializer); + } + + /** + * @param mixed $payload + * + * @throws \RuntimeException + * + * @return string + */ + protected function serialize($payload) + { + // convert identifier to Doctrine object + if (is_array($payload) || is_scalar($payload)) { + // just use the id when no keys are given (Doctrine expects an array like [id: 1234] + if (is_array($payload) && is_numeric(key($payload))) { + $payload = current($payload); + } + + $payload = $this->doctrine->getRepository($this->className)->find($payload); + } + + // anything else is just wrong at this point + if (!is_object($payload) || !($payload instanceof $this->className)) { + throw new \RuntimeException( + sprintf('Expecting object of type %s, but got %s', $this->className, var_export($payload, true)) + ); + } + + return parent::serialize($payload); + } +} diff --git a/src/TreeHouse/Queue/Message/Serializer/DoctrineSerializer.php b/src/TreeHouse/Queue/Message/Serializer/DoctrineSerializer.php index 058f226..df5ad24 100644 --- a/src/TreeHouse/Queue/Message/Serializer/DoctrineSerializer.php +++ b/src/TreeHouse/Queue/Message/Serializer/DoctrineSerializer.php @@ -6,6 +6,14 @@ class DoctrineSerializer implements SerializerInterface { + /** + * @var ManagerRegistry + */ + protected $doctrine; + + /** + * @param ManagerRegistry $doctrine + */ public function __construct(ManagerRegistry $doctrine) { $this->doctrine = $doctrine; @@ -20,7 +28,7 @@ public function serialize($value) } /** - * @param $value + * @param integer|object $value * * @return array */ @@ -29,7 +37,7 @@ protected function getIdentifierValues($value) // if a raw identifier is passed, return it in an array. // this would be the same if we passed in an object with that id if (is_numeric($value)) { - return [$value]; + return [intval($value)]; } $class = get_class($value); diff --git a/src/TreeHouse/Queue/Message/Serializer/JmsSerializer.php b/src/TreeHouse/Queue/Message/Serializer/JmsSerializer.php new file mode 100644 index 0000000..07daac3 --- /dev/null +++ b/src/TreeHouse/Queue/Message/Serializer/JmsSerializer.php @@ -0,0 +1,49 @@ +serializer = $serializer; + $this->groups = $groups; + $this->format = $format; + } + + /** + * @inheritdoc + */ + public function serialize($value) + { + $context = SerializationContext::create(); + if (!empty($this->groups)) { + $context->setGroups($this->groups); + } + + return $this->serializer->serialize($value, $this->format, $context); + } +} diff --git a/tests/TreeHouse/Queue/Tests/Message/Composer/DoctrineMessageComposerTest.php b/tests/TreeHouse/Queue/Tests/Message/Composer/DoctrineMessageComposerTest.php new file mode 100644 index 0000000..670c123 --- /dev/null +++ b/tests/TreeHouse/Queue/Tests/Message/Composer/DoctrineMessageComposerTest.php @@ -0,0 +1,83 @@ +getDoctrineMock(); + $composer = new DoctrineMessageComposer($doctrine, new JsonSerializer(), ObjectMock::class); + $this->assertInstanceOf(DoctrineMessageComposer::class, $composer); + + return $composer; + } + + public function testCompose() + { + $doctrine = $this->getDoctrineMock(); + $composer = new DoctrineMessageComposer($doctrine, new JsonSerializer(), ObjectMock::class); + + $message = $composer->compose(1234); + $this->assertEquals('[1234]', $message->getBody(), 'identifier is looked up'); + + $message = $composer->compose(['5678']); + $this->assertEquals('[5678]', $message->getBody(), 'arrays are supported, numeric values cast to integers'); + + $message = $composer->compose(['id' => 3456]); + $this->assertEquals('[3456]', $message->getBody(), 'associative arrays are passed directly to Doctrine'); + + $message = $composer->compose(new ObjectMock(6789)); + $this->assertEquals('[6789]', $message->getBody(), 'existing object is not converted'); + } + + /** + * @dataProvider getInvalidArguments + * @expectedException \RuntimeException + */ + public function testInvalidArguments($arg) + { + $doctrine = $this->getDoctrineMock(); + $composer = new DoctrineMessageComposer($doctrine, new JsonSerializer(), ObjectMock::class); + + $composer->compose($arg); + } + + public function getInvalidArguments() + { + return [ + [new \stdClass], + [false], + [null], + ]; + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|ManagerRegistry + */ + private function getDoctrineMock() + { + $repo = $this->getMockBuilder(ObjectRepository::class)->getMockForAbstractClass(); + $repo->expects($this->any()) + ->method('find') + ->will($this->returnCallback(function ($value) { + if (is_array($value) && is_numeric(current($value))) { + $value = current($value); + } + + return is_numeric($value) ? new ObjectMock(intval($value)) : null; + })) + ; + + $doctrine = $this->getMockBuilder(ManagerRegistry::class)->getMockForAbstractClass(); + $doctrine->expects($this->any())->method('getRepository')->will($this->returnValue($repo)); + + return $doctrine; + } +} diff --git a/tests/TreeHouse/Queue/Tests/Message/MessagePropertiesTest.php b/tests/TreeHouse/Queue/Tests/Message/MessagePropertiesTest.php index e44116a..00de6aa 100644 --- a/tests/TreeHouse/Queue/Tests/Message/MessagePropertiesTest.php +++ b/tests/TreeHouse/Queue/Tests/Message/MessagePropertiesTest.php @@ -1,6 +1,6 @@ getMockBuilder(ClassMetadata::class)->getMock(); - $meta->expects($this->any())->method('getIdentifierValues')->will($this->returnCallback(function ($value) { - /** @var EntityMock $value */ - return ['id' => $value->getId()]; - })); + $meta->expects($this->any()) + ->method('getIdentifierValues') + ->will($this->returnCallback( + function (ObjectMock $value) { + return ['id' => $value->getId()]; + } + ) + ); $manager = $this->getMockBuilder(ObjectManager::class)->getMock(); $manager->expects($this->any())->method('getClassMetadata')->will($this->returnValue($meta)); @@ -58,18 +66,3 @@ protected function setUp() ; } } - -class EntityMock -{ - private $id; - - public function __construct($id) - { - $this->id = $id; - } - - public function getId() - { - return $this->id; - } -} diff --git a/tests/TreeHouse/Queue/Tests/Message/Serializer/JmsSerializerTest.php b/tests/TreeHouse/Queue/Tests/Message/Serializer/JmsSerializerTest.php new file mode 100644 index 0000000..d561454 --- /dev/null +++ b/tests/TreeHouse/Queue/Tests/Message/Serializer/JmsSerializerTest.php @@ -0,0 +1,57 @@ +jmsSerializer); + + $this->assertInstanceOf(JmsSerializer::class, $serializer); + } + + public function testSerialize() + { + $this->jmsSerializer->expects($this->once())->method('serialize'); + + $serializer = new JmsSerializer($this->jmsSerializer); + $serializer->serialize([1234]); + } + + public function testFormatAndGroups() + { + $format = 'yml'; + $groups = ['foo']; + + $this->jmsSerializer + ->expects($this->once()) + ->method('serialize') + ->with( + $this->equalTo([1234]), + $this->equalTo($format), + $this->callback(function (SerializationContext $context) use ($groups) { + return $context->getExclusionStrategy() instanceof GroupsExclusionStrategy; + }) + ) + ; + + $serializer = new JmsSerializer($this->jmsSerializer, $groups, $format); + $serializer->serialize([1234]); + } + + protected function setUp() + { + $this->jmsSerializer = $this->getMockBuilder(SerializerInterface::class)->getMockForAbstractClass(); + } +} diff --git a/tests/TreeHouse/Queue/Tests/Mock/ObjectMock.php b/tests/TreeHouse/Queue/Tests/Mock/ObjectMock.php new file mode 100644 index 0000000..86514fb --- /dev/null +++ b/tests/TreeHouse/Queue/Tests/Mock/ObjectMock.php @@ -0,0 +1,35 @@ +id = $id; + } + + /** + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * @inheritdoc + */ + public function jsonSerialize() + { + return [$this->id]; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 65a6eed..ec7c5e0 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -12,6 +12,6 @@ * @var ClassLoader $loader */ $loader = require $file; -$loader->add('TreeHouse\Queue\Tests', __DIR__ . '/TreeHouse/Queue/Tests'); +$loader->addPsr4('TreeHouse\\Queue\\Tests\\', __DIR__ . '/TreeHouse/Queue/Tests/'); AnnotationRegistry::registerLoader([$loader, 'loadClass']);