|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Component\JsonStreamer\Tests; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Config\ConfigCacheFactory; |
| 16 | +use Symfony\Component\JsonStreamer\Mapping\PropertyMetadataLoader; |
| 17 | +use Symfony\Component\JsonStreamer\Mapping\PropertyMetadataLoaderInterface; |
| 18 | +use Symfony\Component\JsonStreamer\StreamerDumper; |
| 19 | +use Symfony\Component\JsonStreamer\Tests\Fixtures\Enum\DummyBackedEnum; |
| 20 | +use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\ClassicDummy; |
| 21 | +use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithArray; |
| 22 | +use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithNameAttributes; |
| 23 | +use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithOtherDummies; |
| 24 | +use Symfony\Component\JsonStreamer\Tests\Fixtures\Model\SelfReferencingDummy; |
| 25 | +use Symfony\Component\TypeInfo\Type; |
| 26 | +use Symfony\Component\TypeInfo\TypeResolver\TypeResolver; |
| 27 | + |
| 28 | +class StreamerDumperTest extends TestCase |
| 29 | +{ |
| 30 | + private string $cacheDir; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + parent::setUp(); |
| 35 | + |
| 36 | + $this->cacheDir = \sprintf('%s/symfony_json_streamer_test/any', sys_get_temp_dir()); |
| 37 | + |
| 38 | + if (is_dir($this->cacheDir)) { |
| 39 | + array_map('unlink', glob($this->cacheDir.'/*')); |
| 40 | + rmdir($this->cacheDir); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public function testDumpWithConfigCache() |
| 45 | + { |
| 46 | + $path = $this->cacheDir.'/streamer.php'; |
| 47 | + |
| 48 | + $dumper = new StreamerDumper($this->createMock(PropertyMetadataLoaderInterface::class), $this->cacheDir, new ConfigCacheFactory(true)); |
| 49 | + $dumper->dump(Type::int(), $path, fn () => 'CONTENT'); |
| 50 | + |
| 51 | + $this->assertFileExists($path); |
| 52 | + $this->assertFileExists($path.'.meta'); |
| 53 | + $this->assertFileExists($path.'.meta.json'); |
| 54 | + |
| 55 | + $this->assertStringEqualsFile($path, 'CONTENT'); |
| 56 | + } |
| 57 | + |
| 58 | + public function testDumpWithoutConfigCache() |
| 59 | + { |
| 60 | + $path = $this->cacheDir.'/streamer.php'; |
| 61 | + |
| 62 | + $dumper = new StreamerDumper($this->createMock(PropertyMetadataLoaderInterface::class), $this->cacheDir); |
| 63 | + $dumper->dump(Type::int(), $path, fn () => 'CONTENT'); |
| 64 | + |
| 65 | + $this->assertFileExists($path); |
| 66 | + $this->assertStringEqualsFile($path, 'CONTENT'); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @dataProvider getCacheResourcesDataProvider |
| 71 | + * |
| 72 | + * @param list<class-string> $expectedClassNames |
| 73 | + */ |
| 74 | + public function testGetCacheResources(Type $type, array $expectedClassNames) |
| 75 | + { |
| 76 | + $path = $this->cacheDir.'/streamer.php'; |
| 77 | + |
| 78 | + $dumper = new StreamerDumper(new PropertyMetadataLoader(TypeResolver::create()), $this->cacheDir, new ConfigCacheFactory(true)); |
| 79 | + $dumper->dump($type, $path, fn () => 'CONTENT'); |
| 80 | + |
| 81 | + $resources = json_decode(file_get_contents($path.'.meta.json'), true)['resources']; |
| 82 | + $classNames = array_column($resources, 'className'); |
| 83 | + |
| 84 | + $this->assertSame($expectedClassNames, $classNames); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return iterable<array{0: Type, 1: list<class-string>}> |
| 89 | + */ |
| 90 | + public static function getCacheResourcesDataProvider(): iterable |
| 91 | + { |
| 92 | + yield 'scalar' => [Type::int(), []]; |
| 93 | + yield 'enum' => [Type::enum(DummyBackedEnum::class), [DummyBackedEnum::class]]; |
| 94 | + yield 'object' => [Type::object(ClassicDummy::class), [ClassicDummy::class]]; |
| 95 | + yield 'collection of objects' => [ |
| 96 | + Type::list(Type::object(ClassicDummy::class)), |
| 97 | + [ClassicDummy::class], |
| 98 | + ]; |
| 99 | + yield 'generic with objects' => [ |
| 100 | + Type::generic(Type::object(ClassicDummy::class), Type::object(DummyWithArray::class)), |
| 101 | + [DummyWithArray::class, ClassicDummy::class], |
| 102 | + ]; |
| 103 | + yield 'union with objects' => [ |
| 104 | + Type::union(Type::int(), Type::object(ClassicDummy::class), Type::object(DummyWithArray::class)), |
| 105 | + [ClassicDummy::class, DummyWithArray::class], |
| 106 | + ]; |
| 107 | + yield 'intersection with objects' => [ |
| 108 | + Type::intersection(Type::object(ClassicDummy::class), Type::object(DummyWithArray::class)), |
| 109 | + [ClassicDummy::class, DummyWithArray::class], |
| 110 | + ]; |
| 111 | + yield 'object with object properties' => [ |
| 112 | + Type::object(DummyWithOtherDummies::class), |
| 113 | + [DummyWithOtherDummies::class, DummyWithNameAttributes::class, ClassicDummy::class], |
| 114 | + ]; |
| 115 | + yield 'object with self reference' => [Type::object(SelfReferencingDummy::class), [SelfReferencingDummy::class]]; |
| 116 | + } |
| 117 | +} |
0 commit comments