Repository: symfony/symfony
File: src/Symfony/Bundle/FrameworkBundle/Tests/Functional/JsonStreamerTest.php (PHP)

Code before:
        $writer = static::getContainer()->get('json_streamer.stream_writer.alias');

        $this->assertSame('{"@name":"DUMMY","range":"10..20"}', (string) $writer->write(new Dummy(), Type::object(Dummy::class)));
    }

    public function testRead()
    {
        /** @var StreamReaderInterface $reader */
        $reader = static::getContainer()->get('json_streamer.stream_reader.alias');

        $expected = new Dummy();
        $expected->name = 'dummy';
        $expected->range = [0, 1];

        $this->assertEquals($expected, $reader->read('{"@name": "DUMMY", "range": "0..1"}', Type::object(Dummy::class)));
    }

    public function testDefaultOptionsAreApplied()
    {
        /** @var StreamWriterInterface $writer */
        $writer = static::getContainer()->get('json_streamer.stream_writer.alias');

        $dummy = new DummyWithNullableProperties();

        $this->assertSame('{"name":null,"age":null}', (string) $writer->write($dummy, Type::object(DummyWithNullableProperties::class)));
        $this->assertSame('{}', (string) $writer->write($dummy, Type::object(DummyWithNullableProperties::class), ['include_null_properties' => false]));
    }

    public function testWarmupStreamableClasses()
    {
        /** @var Filesystem $fs */
        $fs = static::getContainer()->get('filesystem');

        $streamWritersDir = \sprintf('%s/json_streamer/stream_writer/', static::getContainer()->getParameter('kernel.cache_dir'));

        // clear already created stream writers
        if ($fs->exists($streamWritersDir)) {
            $fs->remove($streamWritersDir);
        }

        static::getContainer()->get('json_streamer.cache_warmer.streamer.alias')->warmUp(static::getContainer()->getParameter('kernel.cache_dir'));

        $this->assertFileExists($streamWritersDir);

        if (!class_exists(StreamerDumper::class)) {
            $this->assertCount(4, glob($streamWritersDir.'/*'));
        } else {
            $this->assertCount(4, glob($streamWritersDir.'/*.php'));
            $this->assertCount(4, glob($streamWritersDir.'/*.php.meta'));
            $this->assertCount(4, glob($streamWritersDir.'/*.php.meta.json'));
        }

Review comment (refactor):
I suppose we need a similar test on the component's side.
Maybe we can remove this one from FWB to not have to bump the dep?

Code after:

        $this->assertSame('{"@name":"DUMMY","range":"10..20"}', (string) $writer->write(new Dummy(), Type::object(Dummy::class)));
    }

    public function testRead()
    {
        /** @var StreamReaderInterface $reader */
        $reader = static::getContainer()->get('json_streamer.stream_reader.alias');

        $expected = new Dummy();
        $expected->name = 'dummy';
        $expected->range = [0, 1];

        $this->assertEquals($expected, $reader->read('{"@name": "DUMMY", "range": "0..1"}', Type::object(Dummy::class)));
    }

    public function testWarmupStreamableClasses()
    {
        /** @var Filesystem $fs */
        $fs = static::getContainer()->get('filesystem');

        $streamWritersDir = \sprintf('%s/json_streamer/stream_writer/', static::getContainer()->getParameter('kernel.cache_dir'));

        // clear already created stream writers
        if ($fs->exists($streamWritersDir)) {
            $fs->remove($streamWritersDir);
        }

        static::getContainer()->get('json_streamer.cache_warmer.streamer.alias')->warmUp(static::getContainer()->getParameter('kernel.cache_dir'));

        $this->assertFileExists($streamWritersDir);

        if (!class_exists(StreamerDumper::class)) {
            $this->assertCount(2, glob($streamWritersDir.'/*'));
        } else {
            $this->assertCount(2, glob($streamWritersDir.'/*.php'));
            $this->assertCount(2, glob($streamWritersDir.'/*.php.meta'));
            $this->assertCount(2, glob($streamWritersDir.'/*.php.meta.json'));
        }
    }
}