ArrayCollectionHandlerTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace JMS\Serializer\Tests\Handler;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use JMS\Serializer\Handler\ArrayCollectionHandler;
  5. use JMS\Serializer\Metadata\ClassMetadata;
  6. use JMS\Serializer\SerializationContext;
  7. use JMS\Serializer\Tests\Fixtures\ExclusionStrategy\AlwaysExcludeExclusionStrategy;
  8. use JMS\Serializer\VisitorInterface;
  9. use Metadata\MetadataFactoryInterface;
  10. class ArrayCollectionHandlerTest extends \PHPUnit_Framework_TestCase
  11. {
  12. public function testSerializeArray()
  13. {
  14. $handler = new ArrayCollectionHandler();
  15. $visitor = $this->getMockBuilder(VisitorInterface::class)->getMock();
  16. $visitor->method('visitArray')->with(['foo'])->willReturn(['foo']);
  17. $context = $this->getMockBuilder(SerializationContext::class)->getMock();
  18. $type = ['name' => 'ArrayCollection', 'params' => []];
  19. $collection = new ArrayCollection(['foo']);
  20. $handler->serializeCollection($visitor, $collection, $type, $context);
  21. }
  22. public function testSerializeArraySkipByExclusionStrategy()
  23. {
  24. $handler = new ArrayCollectionHandler(false);
  25. $visitor = $this->getMockBuilder(VisitorInterface::class)->getMock();
  26. $visitor->method('visitArray')->with([])->willReturn([]);
  27. $context = $this->getMockBuilder(SerializationContext::class)->getMock();
  28. $factoryMock = $this->getMockBuilder(MetadataFactoryInterface::class)->getMock();
  29. $factoryMock->method('getMetadataForClass')->willReturn(new ClassMetadata(ArrayCollection::class));
  30. $context->method('getExclusionStrategy')->willReturn(new AlwaysExcludeExclusionStrategy());
  31. $context->method('getMetadataFactory')->willReturn($factoryMock);
  32. $type = ['name' => 'ArrayCollection', 'params' => []];
  33. $collection = new ArrayCollection(['foo']);
  34. $handler->serializeCollection($visitor, $collection, $type, $context);
  35. }
  36. }