1<?php 2 3declare(strict_types=1); 4 5namespace JMS\Serializer\Tests\Twig; 6 7use JMS\Serializer\Twig\SerializerExtension; 8use JMS\Serializer\Twig\SerializerRuntimeExtension; 9use JMS\Serializer\Twig\SerializerRuntimeHelper; 10use PHPUnit\Framework\TestCase; 11 12class SerializerExtensionTest extends TestCase 13{ 14 public function testSerialize() 15 { 16 $mockSerializer = $this->getMockBuilder('JMS\Serializer\SerializerInterface')->getMock(); 17 $obj = new \stdClass(); 18 $mockSerializer 19 ->expects($this->once()) 20 ->method('serialize') 21 ->with($this->equalTo($obj), $this->equalTo('json')); 22 $serializerExtension = new SerializerExtension($mockSerializer); 23 $serializerExtension->serialize($obj); 24 25 self::assertEquals('jms_serializer', $serializerExtension->getName()); 26 27 $filters = $serializerExtension->getFilters(); 28 self::assertInstanceOf('Twig_SimpleFilter', $filters[0]); 29 self::assertSame([$serializerExtension, 'serialize'], $filters[0]->getCallable()); 30 31 self::assertEquals( 32 [new \Twig_SimpleFunction('serialization_context', '\JMS\Serializer\SerializationContext::create')], 33 $serializerExtension->getFunctions() 34 ); 35 } 36 37 public function testRuntimeSerializerHelper() 38 { 39 $obj = new \stdClass(); 40 41 $mockSerializer = $this->getMockBuilder('JMS\Serializer\SerializerInterface')->getMock(); 42 $mockSerializer 43 ->expects($this->once()) 44 ->method('serialize') 45 ->with($this->equalTo($obj), $this->equalTo('json')); 46 47 $serializerExtension = new SerializerRuntimeHelper($mockSerializer); 48 $serializerExtension->serialize($obj); 49 } 50 51 public function testRuntimeSerializerExtension() 52 { 53 $serializerExtension = new SerializerRuntimeExtension(); 54 55 self::assertEquals('jms_serializer', $serializerExtension->getName()); 56 self::assertEquals( 57 [new \Twig_SimpleFilter('serialize', [SerializerRuntimeHelper::class, 'serialize'])], 58 $serializerExtension->getFilters() 59 ); 60 self::assertEquals( 61 [new \Twig_SimpleFunction('serialization_context', '\JMS\Serializer\SerializationContext::create')], 62 $serializerExtension->getFunctions() 63 ); 64 } 65} 66