LazyHandlerRegistryTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace JMS\Serializer\Tests\Handler;
  3. use JMS\Serializer\GraphNavigator;
  4. use JMS\Serializer\Handler\LazyHandlerRegistry;
  5. abstract class LazyHandlerRegistryTest extends HandlerRegistryTest
  6. {
  7. protected $container;
  8. protected function setUp()
  9. {
  10. $this->container = $this->createContainer();
  11. parent::setUp();
  12. }
  13. protected function createHandlerRegistry()
  14. {
  15. return new LazyHandlerRegistry($this->container);
  16. }
  17. public function testRegisteredHandlersCanBeRetrievedWhenBeingDefinedAsServices()
  18. {
  19. $jsonSerializationHandler = new HandlerService();
  20. $this->registerHandlerService('handler.serialization.json', $jsonSerializationHandler);
  21. $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, '\stdClass', 'json', array('handler.serialization.json', 'handle'));
  22. $jsonDeserializationHandler = new HandlerService();
  23. $this->registerHandlerService('handler.deserialization.json', $jsonDeserializationHandler);
  24. $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_DESERIALIZATION, '\stdClass', 'json', array('handler.deserialization.json', 'handle'));
  25. $xmlSerializationHandler = new HandlerService();
  26. $this->registerHandlerService('handler.serialization.xml', $xmlSerializationHandler);
  27. $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, '\stdClass', 'xml', array('handler.serialization.xml', 'handle'));
  28. $xmlDeserializationHandler = new HandlerService();
  29. $this->registerHandlerService('handler.deserialization.xml', $xmlDeserializationHandler);
  30. $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_DESERIALIZATION, '\stdClass', 'xml', array('handler.deserialization.xml', 'handle'));
  31. $this->assertSame(array($jsonSerializationHandler, 'handle'), $this->handlerRegistry->getHandler(GraphNavigator::DIRECTION_SERIALIZATION, '\stdClass', 'json'));
  32. $this->assertSame(array($jsonDeserializationHandler, 'handle'), $this->handlerRegistry->getHandler(GraphNavigator::DIRECTION_DESERIALIZATION, '\stdClass', 'json'));
  33. $this->assertSame(array($xmlSerializationHandler, 'handle'), $this->handlerRegistry->getHandler(GraphNavigator::DIRECTION_SERIALIZATION, '\stdClass', 'xml'));
  34. $this->assertSame(array($xmlDeserializationHandler, 'handle'), $this->handlerRegistry->getHandler(GraphNavigator::DIRECTION_DESERIALIZATION, '\stdClass', 'xml'));
  35. }
  36. abstract protected function createContainer();
  37. abstract protected function registerHandlerService($serviceId, $listener);
  38. }
  39. class HandlerService
  40. {
  41. public function handle()
  42. {
  43. }
  44. }