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