|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bridge\Twig\Tests\Extension; |
| 13 | + |
| 14 | +use Doctrine\Common\Annotations\AnnotationReader; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | +use Symfony\Bridge\Twig\Extension\SerializerExtension; |
| 17 | +use Symfony\Bridge\Twig\Extension\SerializerRuntime; |
| 18 | +use Symfony\Bridge\Twig\Tests\Extension\Fixtures\SerializerModelFixture; |
| 19 | +use Symfony\Component\Serializer\Encoder\JsonEncoder; |
| 20 | +use Symfony\Component\Serializer\Encoder\YamlEncoder; |
| 21 | +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; |
| 22 | +use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader; |
| 23 | +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
| 24 | +use Symfony\Component\Serializer\Serializer; |
| 25 | +use Twig\Environment; |
| 26 | +use Twig\Loader\ArrayLoader; |
| 27 | +use Twig\RuntimeLoader\RuntimeLoaderInterface; |
| 28 | + |
| 29 | +/** |
| 30 | + * @author Jesse Rushlow <[email protected]> |
| 31 | + */ |
| 32 | +class SerializerExtensionTest extends TestCase |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @dataProvider serializerDataProvider |
| 36 | + */ |
| 37 | + public function testSerializeFilter(string $template, string $expectedResult) |
| 38 | + { |
| 39 | + $twig = $this->getTwig($template); |
| 40 | + |
| 41 | + self::assertSame($expectedResult, $twig->render('template', ['object' => new SerializerModelFixture()])); |
| 42 | + } |
| 43 | + |
| 44 | + public function serializerDataProvider(): \Generator |
| 45 | + { |
| 46 | + yield ['{{ object|serialize }}', '{"name":"howdy","title":"fixture"}']; |
| 47 | + yield ['{{ object|serialize(\'yaml\') }}', '{ name: howdy, title: fixture }']; |
| 48 | + yield ['{{ object|serialize(\'yaml\', {groups: \'read\'}) }}', '{ name: howdy }']; |
| 49 | + } |
| 50 | + |
| 51 | + private function getTwig(string $template): Environment |
| 52 | + { |
| 53 | + $meta = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); |
| 54 | + $runtime = new SerializerRuntime(new Serializer([new ObjectNormalizer($meta)], [new JsonEncoder(), new YamlEncoder()])); |
| 55 | + |
| 56 | + $mockRuntimeLoader = $this->createMock(RuntimeLoaderInterface::class); |
| 57 | + $mockRuntimeLoader |
| 58 | + ->method('load') |
| 59 | + ->willReturnMap([ |
| 60 | + ['Symfony\Bridge\Twig\Extension\SerializerRuntime', $runtime], |
| 61 | + ]) |
| 62 | + ; |
| 63 | + |
| 64 | + $twig = new Environment(new ArrayLoader(['template' => $template])); |
| 65 | + $twig->addExtension(new SerializerExtension()); |
| 66 | + $twig->addRuntimeLoader($mockRuntimeLoader); |
| 67 | + |
| 68 | + return $twig; |
| 69 | + } |
| 70 | +} |
0 commit comments