1<?php
2
3declare(strict_types=1);
4
5namespace JMS\Serializer\Tests\Handler;
6
7use Doctrine\Common\Collections\ArrayCollection;
8use JMS\Serializer\Handler\ArrayCollectionHandler;
9use JMS\Serializer\Metadata\ClassMetadata;
10use JMS\Serializer\SerializationContext;
11use JMS\Serializer\Tests\Fixtures\ExclusionStrategy\AlwaysExcludeExclusionStrategy;
12use JMS\Serializer\Visitor\SerializationVisitorInterface;
13use Metadata\MetadataFactoryInterface;
14use PHPUnit\Framework\TestCase;
15
16class ArrayCollectionHandlerTest extends TestCase
17{
18    /**
19     * @doesNotPerformAssertions
20     */
21    public function testSerializeArray()
22    {
23        $handler = new ArrayCollectionHandler();
24
25        $visitor = $this->getMockBuilder(SerializationVisitorInterface::class)->getMock();
26        $visitor->method('visitArray')->with(['foo'])->willReturn(['foo']);
27
28        $context = $this->getMockBuilder(SerializationContext::class)->getMock();
29        $type = ['name' => 'ArrayCollection', 'params' => []];
30
31        $collection = new ArrayCollection(['foo']);
32
33        $handler->serializeCollection($visitor, $collection, $type, $context);
34    }
35
36    /**
37     * @doesNotPerformAssertions
38     */
39    public function testSerializeArraySkipByExclusionStrategy()
40    {
41        $handler = new ArrayCollectionHandler(false);
42
43        $visitor = $this->getMockBuilder(SerializationVisitorInterface::class)->getMock();
44        $visitor->method('visitArray')->with([])->willReturn([]);
45
46        $context = $this->getMockBuilder(SerializationContext::class)->getMock();
47
48        $factoryMock = $this->getMockBuilder(MetadataFactoryInterface::class)->getMock();
49        $factoryMock->method('getMetadataForClass')->willReturn(new ClassMetadata(ArrayCollection::class));
50
51        $context->method('getExclusionStrategy')->willReturn(new AlwaysExcludeExclusionStrategy());
52        $context->method('getMetadataFactory')->willReturn($factoryMock);
53
54        $type = ['name' => 'ArrayCollection', 'params' => []];
55
56        $collection = new ArrayCollection(['foo']);
57
58        $handler->serializeCollection($visitor, $collection, $type, $context);
59    }
60}
61