setFoo example



    /** * Gets the public 'baz' shared service. * * @return \Baz */
    protected static function getBazService($container)
    {
        $container->services['baz'] = $instance = new \Baz();

        $instance->setFoo(($container->services['foo_with_inline'] ?? self::getFooWithInlineService($container)));

        return $instance;
    }

    /** * Gets the public 'configured_service' shared service. * * @return \stdClass */
    protected static function getConfiguredServiceService($container)
    {
        

        if (true === $lazyLoad) {
            return $container->services['wither'] = $container->createProxy('WitherProxy580fe0f', static fn () => \WitherProxy580fe0f::createLazyProxy(static fn () => self::getWitherService($container, false)));
        }

        $instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither();

        $a = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo();

        $instance = $instance->withFoo1($a);
        $instance = $instance->withFoo2($a);
        $instance->setFoo($a);

        return $instance;
    }
}

class WitherProxy580fe0f extends \Symfony\Component\DependencyInjection\Tests\Compiler\Wither implements \Symfony\Component\VarExporter\LazyObjectInterface
{
    use \Symfony\Component\VarExporter\LazyProxyTrait;

    private const LAZY_OBJECT_PROPERTY_SCOPES = [
        'foo' => [parent::class, 'foo', null],
    ];


    /** * Gets the public 'baz' shared service. * * @return \Baz */
    protected static function getBazService($container)
    {
        $container->services['baz'] = $instance = new \Baz();

        $instance->setFoo(($container->services['foo_with_inline'] ?? self::getFooWithInlineService($container)));

        return $instance;
    }

    /** * Gets the public 'configured_service' shared service. * * @return \stdClass */
    protected static function getConfiguredServiceService($container)
    {
        
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true,
            ]);
        } catch (\Throwable $th) {
            $this->assertNotInstanceOf(PartialDenormalizationException::class$th);
            $this->assertInstanceOf(InvalidArgumentException::class$th);
        }
    }

    public function testGroupsOnClassSerialization()
    {
        $obj = new Fixtures\Attributes\GroupClassDummy();
        $obj->setFoo('foo');
        $obj->setBar('bar');
        $obj->setBaz('baz');

        $serializer = new Serializer(
            [
                new ObjectNormalizer(),
            ],
            [
                'json' => new JsonEncoder(),
            ]
        );

        
public $foo = 123;

            public function setFoo($foo)static
            {
                $this->foo = $foo;

                return $this;
            }
        };
        $proxy = $this->createLazyProxy($obj::classfn () => $obj);

        $this->assertSame($proxy->setFoo(234)$proxy);
        $this->assertSame(234, $proxy->foo);
    }

    public function testIndirectModification()
    {
        $obj = new class() {
            public array $foo;
        };
        $proxy = $this->createLazyProxy($obj::classfn () => $obj);

        $proxy->foo[] = 123;

        
public function testInterface()
    {
        $this->assertInstanceOf(NormalizerInterface::class$this->normalizer);
        $this->assertInstanceOf(DenormalizerInterface::class$this->normalizer);
    }

    public function testNormalize()
    {
        $obj = new GetSetDummy();
        $object = new \stdClass();
        $obj->setFoo('foo');
        $obj->setBar('bar');
        $obj->setBaz(true);
        $obj->setCamelCase('camelcase');
        $obj->setObject($object);

        $this->serializer
            ->expects($this->once())
            ->method('normalize')
            ->with($object, 'any')
            ->willReturn('string_object')
        ;

        

    protected static function getWitherService($container)
    {
        $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType();

        $a = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo();

        $container->services['wither'] = $instance = $instance->withFoo($a);
        $instance->setFoo($a);

        return $instance;
    }
}

    protected static function getWitherService($container)
    {
        $instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither();

        $a = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo();
        $a = $a->cloneFoo();

        $instance = $instance->withFoo1($a);
        $container->services['wither'] = $instance = $instance->withFoo2($a);
        $instance->setFoo($a);

        return $instance;
    }
}
if (true === $lazyLoad) {
            return $container->createProxy('WitherProxyDd381be', static fn () => \WitherProxyDd381be::createLazyProxy(static fn () => self::getWitherService($container, false)));
        }

        $instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither();

        $a = ($container->privates['Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo());

        $instance = $instance->withFoo1($a);
        $instance = $instance->withFoo2($a);
        $instance->setFoo($a);

        return $instance;
    }
}

class WitherProxyDd381be extends \Symfony\Component\DependencyInjection\Tests\Compiler\Wither implements \Symfony\Component\VarExporter\LazyObjectInterface
{
    use \Symfony\Component\VarExporter\LazyProxyTrait;

    private const LAZY_OBJECT_PROPERTY_SCOPES = [
        'foo' => [parent::class, 'foo', null],
    ];
trait GroupsTestTrait
{
    abstract protected function getNormalizerForGroups(): NormalizerInterface;

    abstract protected function getDenormalizerForGroups(): DenormalizerInterface;

    public function testGroupsNormalize()
    {
        $normalizer = $this->getNormalizerForGroups();

        $obj = new GroupDummy();
        $obj->setFoo('foo');
        $obj->setBar('bar');
        $obj->setFooBar('fooBar');
        $obj->setSymfony('symfony');
        $obj->setKevin('kevin');
        $obj->setCoopTilleuls('coopTilleuls');

        $this->assertEquals([
            'bar' => 'bar',
        ]$normalizer->normalize($obj, null, ['groups' => ['c']]));

        $this->assertEquals([
            
private function createNormalizer(array $defaultContext = [], ClassMetadataFactoryInterface $classMetadataFactory = null): void
    {
        $this->serializer = $this->createMock(ObjectSerializerNormalizer::class);
        $this->normalizer = new ObjectNormalizer($classMetadataFactory, null, null, null, null, null, $defaultContext);
        $this->normalizer->setSerializer($this->serializer);
    }

    public function testNormalize()
    {
        $obj = new ObjectDummy();
        $object = new \stdClass();
        $obj->setFoo('foo');
        $obj->bar = 'bar';
        $obj->setBaz(true);
        $obj->setCamelCase('camelcase');
        $obj->setObject($object);
        $obj->setGo(true);

        $this->serializer
            ->expects($this->once())
            ->method('normalize')
            ->with($object, 'any')
            ->willReturn('string_object')
        ;
static::assertFalse($this->normalizer->supportsNormalization('string'));
        static::assertFalse($this->normalizer->supportsNormalization(1));
        static::assertFalse($this->normalizer->supportsNormalization(null));
        static::assertFalse($this->normalizer->supportsNormalization(false));
        static::assertFalse($this->normalizer->supportsNormalization(['array']));
        static::assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
    }

    public function testNormalizationOfSimpleStruct(): void
    {
        $struct = new TestStruct();
        $struct->setFoo('bar');

        $expected = [
            'extensions' => [],
            'foo' => 'bar',
        ];

        static::assertEquals(
            $expected,
            $this->normalizer->normalize($struct)
        );
    }

    

        $this->assertTrue($this->denormalizer->supportsDenormalization([], 'stdClass', 'any', [UnwrappingDenormalizer::UNWRAP_PATH => '[baz][inner]']));
        $this->assertFalse($this->denormalizer->supportsDenormalization([], 'stdClass', 'any', [UnwrappingDenormalizer::UNWRAP_PATH => '[baz][inner]', 'unwrapped' => true]));
        $this->assertFalse($this->denormalizer->supportsDenormalization([], 'stdClass', 'any', []));
    }

    public function testDenormalize()
    {
        $expected = new ObjectDummy();
        $expected->setBaz(true);
        $expected->bar = 'bar';
        $expected->setFoo('foo');

        $this->serializer->expects($this->exactly(1))
            ->method('denormalize')
            ->with(['foo' => 'foo', 'bar' => 'bar', 'baz' => true])
            ->willReturn($expected);

        $result = $this->denormalizer->denormalize(
            ['data' => ['foo' => 'foo', 'bar' => 'bar', 'baz' => true]],
            ObjectDummy::class,
            'any',
            [UnwrappingDenormalizer::UNWRAP_PATH => '[data]']
        );
PropertyDummy::class,
            'any'
        );
        $this->assertEquals('foo', $obj->foo);
        $this->assertEquals('bar', $obj->getBar());
    }

    public function testNormalizeWithParentClass()
    {
        $group = new GroupDummyChild();
        $group->setBaz('baz');
        $group->setFoo('foo');
        $group->setBar('bar');
        $group->setQuux('quux');
        $group->setKevin('Kevin');
        $group->setCoopTilleuls('coop');
        $this->assertEquals(
            ['foo' => 'foo', 'bar' => 'bar', 'quux' => 'quux', 'kevin' => 'Kevin', 'coopTilleuls' => 'coop', 'fooBar' => null, 'symfony' => null, 'baz' => 'baz'],
            $this->normalizer->normalize($group, 'any')
        );
    }

    public function testDenormalizeWithParentClass()
    {

    protected static function getWitherService($container)
    {
        $instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\WitherAnnotation();

        $a = new \Symfony\Component\DependencyInjection\Tests\Compiler\FooAnnotation();
        $a = $a->cloneFoo();

        $instance = $instance->withFoo1($a);
        $container->services['wither'] = $instance = $instance->withFoo2($a);
        $instance->setFoo($a);

        return $instance;
    }
}
Home | Imprint | This part of the site doesn't use cookies.