1<?php
2
3declare(strict_types=1);
4
5namespace JMS\Serializer\Tests\Serializer;
6
7use Doctrine\Common\Annotations\AnnotationReader;
8use JMS\Serializer\Construction\UnserializeObjectConstructor;
9use JMS\Serializer\DeserializationContext;
10use JMS\Serializer\Handler\HandlerRegistry;
11use JMS\Serializer\Metadata\Driver\AnnotationDriver;
12use JMS\Serializer\Naming\CamelCaseNamingStrategy;
13use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
14use JMS\Serializer\SerializationContext;
15use JMS\Serializer\SerializerBuilder;
16use JMS\Serializer\Visitor\Factory\JsonDeserializationVisitorFactory;
17use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory;
18use Metadata\MetadataFactory;
19use PHPUnit\Framework\TestCase;
20
21class SerializationContextFactoryTest extends TestCase
22{
23    protected $serializer;
24    protected $metadataFactory;
25    protected $handlerRegistry;
26    protected $unserializeObjectConstructor;
27    protected $serializationVisitors;
28    protected $deserializationVisitors;
29
30    public function setUp()
31    {
32        parent::setUp();
33
34        $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
35        $this->metadataFactory = new MetadataFactory(new AnnotationDriver(new AnnotationReader(), $namingStrategy));
36        $this->handlerRegistry = new HandlerRegistry();
37        $this->unserializeObjectConstructor = new UnserializeObjectConstructor();
38
39        $this->serializationVisitors = ['json' => new JsonSerializationVisitorFactory()];
40        $this->deserializationVisitors = ['json' => new JsonDeserializationVisitorFactory()];
41    }
42
43    public function testSerializeUseProvidedSerializationContext()
44    {
45        $contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\SerializationContextFactoryInterface');
46        $context = new SerializationContext();
47        $context->setSerializeNull(true);
48
49        $contextFactoryMock
50            ->expects($this->once())
51            ->method('createSerializationContext')
52            ->will($this->returnValue($context));
53
54        $builder = SerializerBuilder::create();
55        $builder->setSerializationContextFactory($contextFactoryMock);
56        $serializer = $builder->build();
57
58        $result = $serializer->serialize(['value' => null], 'json');
59
60        self::assertEquals('{"value":null}', $result);
61    }
62
63    public function testDeserializeUseProvidedDeserializationContext()
64    {
65        $contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\DeserializationContextFactoryInterface');
66        $context = new DeserializationContext();
67
68        $contextFactoryMock
69            ->expects($this->once())
70            ->method('createDeserializationContext')
71            ->will($this->returnValue($context));
72
73        $builder = SerializerBuilder::create();
74        $builder->setDeserializationContextFactory($contextFactoryMock);
75        $serializer = $builder->build();
76
77        $result = $serializer->deserialize('{"value":null}', 'array', 'json');
78
79        self::assertEquals(['value' => null], $result);
80    }
81
82    public function testToArrayUseProvidedSerializationContext()
83    {
84        $contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\SerializationContextFactoryInterface');
85        $context = new SerializationContext();
86        $context->setSerializeNull(true);
87
88        $contextFactoryMock
89            ->expects($this->once())
90            ->method('createSerializationContext')
91            ->will($this->returnValue($context));
92
93        $builder = SerializerBuilder::create();
94        $builder->setSerializationContextFactory($contextFactoryMock);
95        $serializer = $builder->build();
96
97        $result = $serializer->toArray(['value' => null]);
98
99        self::assertEquals(['value' => null], $result);
100    }
101
102    public function testFromArrayUseProvidedDeserializationContext()
103    {
104        $contextFactoryMock = $this->getMockForAbstractClass('JMS\\Serializer\\ContextFactory\\DeserializationContextFactoryInterface');
105        $context = new DeserializationContext();
106
107        $contextFactoryMock
108            ->expects($this->once())
109            ->method('createDeserializationContext')
110            ->will($this->returnValue($context));
111
112        $builder = SerializerBuilder::create();
113        $builder->setDeserializationContextFactory($contextFactoryMock);
114        $serializer = $builder->build();
115
116        $result = $serializer->fromArray(['value' => null], 'array');
117
118        self::assertEquals(['value' => null], $result);
119    }
120}
121