From cee507e62f8c1d5c4104cf033a3dd21a8da3886c Mon Sep 17 00:00:00 2001 From: Alexander Miertsch Date: Sun, 26 Jul 2015 00:26:25 +0200 Subject: [PATCH 1/2] Add MessageFactory interface + FQCNMessageFactory - Closes #10 --- src/Messaging/FQCNMessageFactory.php | 48 ++++++++++++++++++ src/Messaging/MessageFactory.php | 28 +++++++++++ tests/Messaging/FQCNMessageFactoryTest.php | 58 ++++++++++++++++++++++ tests/Mock/InvalidMessage.php | 17 +++++++ 4 files changed, 151 insertions(+) create mode 100644 src/Messaging/FQCNMessageFactory.php create mode 100644 src/Messaging/MessageFactory.php create mode 100644 tests/Messaging/FQCNMessageFactoryTest.php create mode 100644 tests/Mock/InvalidMessage.php diff --git a/src/Messaging/FQCNMessageFactory.php b/src/Messaging/FQCNMessageFactory.php new file mode 100644 index 0000000..54a2fe5 --- /dev/null +++ b/src/Messaging/FQCNMessageFactory.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 7/26/15 - 12:11 AM + */ +namespace Prooph\Common\Messaging; +use Assert\Assertion; + +/** + * Class FQCNMessageFactory + * + * @package Prooph\Common\Messaging + * @author Alexander Miertsch + */ +class FQCNMessageFactory implements MessageFactory +{ + /** + * @param string $messageName + * @param array $messageData + * @throws \UnexpectedValueException + * @return DomainMessage + */ + public function createMessageFromArray($messageName, array $messageData) + { + if (! class_exists($messageName)) { + throw new \UnexpectedValueException('Given message name is not a valid class: ' . (string)$messageName); + } + + $ref = new \ReflectionClass($messageName); + + if (!$ref->isSubclassOf(DomainMessage::class)) { + throw new \UnexpectedValueException(sprintf( + 'Message class %s is not a sub class of %s', + $messageName, + DomainMessage::class + )); + } + + $messageData['name'] = $messageName; + + return $messageName::fromArray($messageData); + } +} \ No newline at end of file diff --git a/src/Messaging/MessageFactory.php b/src/Messaging/MessageFactory.php new file mode 100644 index 0000000..3a34822 --- /dev/null +++ b/src/Messaging/MessageFactory.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 7/26/15 - 12:08 AM + */ + +namespace Prooph\Common\Messaging; + +/** + * Interface MessageFactory + * + * @package Prooph\Common\Messaging + * @author Alexander Miertsch + */ +interface MessageFactory +{ + /** + * @param string $messageName + * @param array $messageData + * @return DomainMessage + */ + public function createMessageFromArray($messageName, array $messageData); +} \ No newline at end of file diff --git a/tests/Messaging/FQCNMessageFactoryTest.php b/tests/Messaging/FQCNMessageFactoryTest.php new file mode 100644 index 0000000..829fd4b --- /dev/null +++ b/tests/Messaging/FQCNMessageFactoryTest.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 7/26/15 - 12:14 AM + */ +namespace ProophTest\Common\Messaging; +use Prooph\Common\Messaging\FQCNMessageFactory; +use ProophTest\Common\Mock\DoSomething; +use Rhumsaa\Uuid\Uuid; + +/** + * Class FQCNMessageFactoryTest + * + * @package ProophTest\Common\Messaging + * @author Alexander Miertsch + */ +final class FQCNMessageFactoryTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var FQCNMessageFactory + */ + private $messageFactory; + + protected function setUp() + { + $this->messageFactory = new FQCNMessageFactory(); + } + + /** + * @test + */ + function it_creates_a_new_message_from_array_and_fqcn() + { + $uuid = Uuid::uuid4(); + $createdAt = new \DateTimeImmutable(); + + + $command = $this->messageFactory->createMessageFromArray(DoSomething::class, [ + 'uuid' => $uuid->toString(), + 'version' => 1, + 'payload' => ['command' => 'payload'], + 'metadata' => ['command' => 'metadata'], + 'created_at' => $createdAt->format(\DateTime::ISO8601), + ]); + + $this->assertEquals(DoSomething::class, $command->messageName()); + $this->assertEquals($uuid->toString(), $command->uuid()->toString()); + $this->assertEquals($createdAt->format(\DateTime::ISO8601), $command->createdAt()->format(\DateTime::ISO8601)); + $this->assertEquals(1, $command->version()); + $this->assertEquals(['command' => 'payload'], $command->payload()); + $this->assertEquals(['command' => 'metadata'], $command->metadata()); + } +} \ No newline at end of file diff --git a/tests/Mock/InvalidMessage.php b/tests/Mock/InvalidMessage.php new file mode 100644 index 0000000..eefccde --- /dev/null +++ b/tests/Mock/InvalidMessage.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 7/26/15 - 12:15 AM + */ +namespace ProophTest\Common\Mock; + + +final class InvalidMessage +{ + +} \ No newline at end of file From 54ead1aaa5a3ef6ea97d8c01799e43a38a9383c3 Mon Sep 17 00:00:00 2001 From: Alexander Miertsch Date: Sun, 26 Jul 2015 00:30:31 +0200 Subject: [PATCH 2/2] Delete ServiceLocator - Closes #11 --- src/ServiceLocator/ServiceInitializer.php | 27 ---- src/ServiceLocator/ServiceLocator.php | 51 ------ .../ZF2/Zf2InitializerProxy.php | 69 --------- .../ZF2/Zf2ServiceManagerProxy.php | 125 --------------- tests/Zf2ServiceManagerProxyTest.php | 145 ------------------ 5 files changed, 417 deletions(-) delete mode 100644 src/ServiceLocator/ServiceInitializer.php delete mode 100644 src/ServiceLocator/ServiceLocator.php delete mode 100644 src/ServiceLocator/ZF2/Zf2InitializerProxy.php delete mode 100644 src/ServiceLocator/ZF2/Zf2ServiceManagerProxy.php delete mode 100644 tests/Zf2ServiceManagerProxyTest.php diff --git a/src/ServiceLocator/ServiceInitializer.php b/src/ServiceLocator/ServiceInitializer.php deleted file mode 100644 index 4fdcf52..0000000 --- a/src/ServiceLocator/ServiceInitializer.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Date: 5/9/15 - 9:57 PM - */ - -namespace Prooph\Common\ServiceLocator; - -/** - * Interface ServiceInitializer - * - * @package Prooph\Common\ServiceLocator - * @author Alexander Miertsch - */ -interface ServiceInitializer -{ - /** - * @param mixed $service - * @param ServiceLocator $serviceLocator - */ - public function initialize($service, ServiceLocator $serviceLocator); -} \ No newline at end of file diff --git a/src/ServiceLocator/ServiceLocator.php b/src/ServiceLocator/ServiceLocator.php deleted file mode 100644 index e307711..0000000 --- a/src/ServiceLocator/ServiceLocator.php +++ /dev/null @@ -1,51 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Date: 3/5/15 - 8:46 PM - */ - -namespace Prooph\Common\ServiceLocator; - -/** - * Interface ServiceLocator - * - * @package Prooph\Common\ServiceLocator - * @author Alexander Miertsch - */ -interface ServiceLocator -{ - /** - * @param string $serviceName - * @return mixed - */ - public function get($serviceName); - - /** - * @param string $serviceName - * @return bool - */ - public function has($serviceName); - - /** - * @param string $serviceName - * @param mixed $service - * @param bool $allowOverride - */ - public function set($serviceName, $service, $allowOverride = false); - - /** - * @param string $alias - * @param string $orgServiceName - */ - public function setAlias($alias, $orgServiceName); - - /** - * @param ServiceInitializer $initializer - */ - public function addInitializer(ServiceInitializer $initializer); -} \ No newline at end of file diff --git a/src/ServiceLocator/ZF2/Zf2InitializerProxy.php b/src/ServiceLocator/ZF2/Zf2InitializerProxy.php deleted file mode 100644 index 5621c1a..0000000 --- a/src/ServiceLocator/ZF2/Zf2InitializerProxy.php +++ /dev/null @@ -1,69 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Date: 5/9/15 - 9:59 PM - */ -namespace Prooph\Common\ServiceLocator\ZF2; - -use Prooph\Common\ServiceLocator\ServiceInitializer; -use Zend\ServiceManager\InitializerInterface; -use Zend\ServiceManager\ServiceLocatorInterface; - -/** - * Class Zf2InitializerProxy - * - * This proxy uses the double dispatch pattern to act as ZF2 Initializer against the ZF2 ServiceManager but proxies - * the call to the ServiceInitializer and passing it the Zf2ServiceManagerProxy as ServiceLocator - * - * @package Prooph\Common\ServiceLocator\ZF2 - * @author Alexander Miertsch - */ -final class Zf2InitializerProxy implements InitializerInterface -{ - /** - * @var ServiceInitializer - */ - private $initializer; - - /** - * @var Zf2ServiceManagerProxy - */ - private $serviceManagerProxy; - - /** - * @param ServiceInitializer $initializer - * @param Zf2ServiceManagerProxy $proxy - * @return Zf2InitializerProxy - */ - public static function proxy(ServiceInitializer $initializer, Zf2ServiceManagerProxy $proxy) - { - return new self($initializer, $proxy); - } - - /** - * @param ServiceInitializer $initializer - * @param Zf2ServiceManagerProxy $proxy - */ - private function __construct(ServiceInitializer $initializer, Zf2ServiceManagerProxy $proxy) - { - $this->initializer = $initializer; - $this->serviceManagerProxy = $proxy; - } - - /** - * Initialize - * - * @param $instance - * @param ServiceLocatorInterface $serviceLocator - * @return mixed - */ - public function initialize($instance, ServiceLocatorInterface $serviceLocator) - { - return $this->initializer->initialize($instance, $this->serviceManagerProxy); - } -} \ No newline at end of file diff --git a/src/ServiceLocator/ZF2/Zf2ServiceManagerProxy.php b/src/ServiceLocator/ZF2/Zf2ServiceManagerProxy.php deleted file mode 100644 index 8381d0d..0000000 --- a/src/ServiceLocator/ZF2/Zf2ServiceManagerProxy.php +++ /dev/null @@ -1,125 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Date: 3/5/15 - 8:48 PM - */ -namespace Prooph\Common\ServiceLocator\ZF2; - -use Prooph\Common\ServiceLocator\ServiceInitializer; -use Prooph\Common\ServiceLocator\ServiceLocator; -use Zend\ServiceManager\Config; -use Zend\ServiceManager\ServiceManager; - -/** - * Class Zf2ServiceManagerProxy - * - * @package Prooph\Common\ServiceLocator\ZF2 - * @author Alexander Miertsch - */ -final class Zf2ServiceManagerProxy implements ServiceLocator -{ - /** - * @var ServiceManager - */ - private $zf2ServiceManager; - - /** - * @param array $config - * @return Zf2ServiceManagerProxy - */ - public static function createFromConfigurationArray(array $config) - { - $config = new Config($config); - - return new self(new ServiceManager($config)); - } - - /** - * @param ServiceManager $serviceManager - * @return Zf2ServiceManagerProxy - */ - public static function proxy(ServiceManager $serviceManager) - { - return new self($serviceManager); - } - - /** - * @param ServiceManager $serviceManager - */ - private function __construct(ServiceManager $serviceManager) - { - $this->zf2ServiceManager = $serviceManager; - } - - /** - * @param string $serviceName - * @return mixed - */ - public function get($serviceName) - { - return $this->getServiceManager()->get($serviceName); - } - - /** - * @param string $serviceName - * @return bool - */ - public function has($serviceName) - { - return $this->getServiceManager()->has($serviceName); - } - - /** - * @param string $serviceName - * @param mixed $service - * @param bool $allowOverride - */ - public function set($serviceName, $service, $allowOverride = false) - { - - if ($allowOverride) { - $orgAllowOverride = $this->getServiceManager()->getAllowOverride(); - $this->getServiceManager()->setAllowOverride($allowOverride); - } - - $this->getServiceManager()->setService($serviceName, $service); - - if ($allowOverride) { - $this->getServiceManager()->setAllowOverride($orgAllowOverride); - } - } - - /** - * @return ServiceManager - */ - private function getServiceManager() - { - if (is_null($this->zf2ServiceManager)) { - $this->zf2ServiceManager = new ServiceManager(); - } - - return $this->zf2ServiceManager; - } - - /** - * @param string $alias - * @param string $orgServiceName - */ - public function setAlias($alias, $orgServiceName) - { - $this->getServiceManager()->setAlias($alias, $orgServiceName); - } - - /** - * @param ServiceInitializer $initializer - */ - public function addInitializer(ServiceInitializer $initializer) - { - $this->getServiceManager()->addInitializer(Zf2InitializerProxy::proxy($initializer, $this)); - } -} \ No newline at end of file diff --git a/tests/Zf2ServiceManagerProxyTest.php b/tests/Zf2ServiceManagerProxyTest.php deleted file mode 100644 index 4058bad..0000000 --- a/tests/Zf2ServiceManagerProxyTest.php +++ /dev/null @@ -1,145 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - * - * Date: 3/5/15 - 8:58 PM - */ -namespace ProophTest\Common; - -use Prooph\Common\ServiceLocator\ZF2\Zf2ServiceManagerProxy; -use ProophTest\Common\Mock\ServiceInitializerMock; -use Zend\ServiceManager\Config; -use Zend\ServiceManager\ServiceManager; - -/** - * Class Zf2ServiceManagerProxyTest - * - * @package ProophTest\Common - * @author Alexander Miertsch - */ -class Zf2ServiceManagerProxyTest extends \PHPUnit_Framework_TestCase -{ - /** - * @test - */ - function it_can_be_created_from_zf2_services_confirm_config_array() - { - $service = new \stdClass(); - - $proxy = Zf2ServiceManagerProxy::createFromConfigurationArray([ - 'factories' => [ - 'std_service' => function ($services) use ($service) { - return $service; - } - ], - ]); - - $this->assertTrue($proxy->has('std_service')); - $this->assertSame($service, $proxy->get('std_service')); - } - - /** - * @test - */ - function it_can_be_created_with_an_instance_of_a_service_manager() - { - $service = new \stdClass(); - - $smConfig = new Config([ - 'factories' => [ - 'std_service' => function ($services) use ($service) { - return $service; - } - ], - ]); - - $sm = new ServiceManager($smConfig); - - $proxy = Zf2ServiceManagerProxy::proxy($sm); - - $this->assertTrue($proxy->has('std_service')); - $this->assertSame($service, $proxy->get('std_service')); - } - - /** - * @test - */ - function it_allows_overriding_a_service_if_specified() - { - $service = new \stdClass(); - - $smConfig = new Config([ - 'factories' => [ - 'std_service' => function ($services) use ($service) { - return $service; - } - ], - ]); - - $sm = new ServiceManager($smConfig); - - $proxy = Zf2ServiceManagerProxy::proxy($sm); - - $anotherService = new \stdClass(); - - $proxy->set('std_service', $anotherService, true); - - $this->assertNotSame($service, $proxy->get('std_service')); - $this->assertSame($anotherService, $proxy->get('std_service')); - } - - /** - * @test - */ - function it_sets_an_alias_for_a_service() - { - $service = new \stdClass(); - - $smConfig = new Config([ - 'factories' => [ - 'std_service' => function ($services) use ($service) { - return $service; - } - ], - ]); - - $sm = new ServiceManager($smConfig); - - $proxy = Zf2ServiceManagerProxy::proxy($sm); - - $proxy->setAlias('aliased_service', 'std_service'); - - $this->assertTrue($proxy->has('std_service')); - $this->assertTrue($proxy->has('aliased_service')); - $this->assertSame($service, $proxy->get('aliased_service')); - } - - /** - * @test - */ - function it_adds_an_initializer_by_using_a_proxy() - { - $service = new \stdClass(); - - $smConfig = new Config([ - 'factories' => [ - 'std_service' => function ($services) use ($service) { - return $service; - } - ], - ]); - - $sm = new ServiceManager($smConfig); - - $proxy = Zf2ServiceManagerProxy::proxy($sm); - - $proxy->addInitializer(new ServiceInitializerMock()); - - $this->assertSame($service, $proxy->get('std_service')); - $this->assertSame($proxy, $service->locator); - } -} \ No newline at end of file