Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,11 @@ public function normalize(mixed $object, ?string $format = null, array $context
foreach ($stack as $attribute => $attributeValue) {
$attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context);

if (null === $attributeValue || \is_scalar($attributeValue)) {
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
continue;
}

if (!$this->serializer instanceof NormalizerInterface) {
if (null === $attributeValue || \is_scalar($attributeValue)) {
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
continue;
}
throw new LogicException(\sprintf('Cannot normalize attribute "%s" because the injected serializer is not a normalizer.', $attribute));
}

Expand Down Expand Up @@ -465,7 +464,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri

// This try-catch should cover all NotNormalizableValueException (and all return branches after the first
// exception) so we could try denormalizing all types of an union type. If the target type is not an union
// type, we will just re-throw the catched exception.
// type, we will just re-throw the caught exception.
// In the case of no denormalization succeeds with an union type, it will fall back to the default exception
// with the acceptable types list.
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class ScalarNormalizer implements NormalizerInterface
{
public function normalize(mixed $object, ?string $format = null, array $context = []): string
{
$data = $object;

if (!\is_string($data)) {
$data = (string) $object;
}

return strtoupper($data);
}

public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return \is_scalar($data);
}

public function getSupportedTypes(?string $format): array
{
return [
'native-boolean' => true,
'native-integer' => true,
'native-string' => true,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class StdClassNormalizer implements NormalizerInterface
{
public function normalize(mixed $object, ?string $format = null, array $context = []): string
{
return 'string_object';
}

public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
{
return $data instanceof \stdClass;
}

public function getSupportedTypes(?string $format): array
{
return [
\stdClass::class => true,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -960,15 +960,15 @@ protected function createChildContext(array $parentContext, string $attribute, ?
$this->assertSame($firstChildContextCacheKey, $secondChildContextCacheKey);
}

public function testChildContextKeepsOriginalContextCacheKey()
public function testChildContextChangesContextCacheKey()
{
$foobar = new Dummy();
$foobar->foo = new EmptyDummy();
$foobar->bar = 'bar';
$foobar->baz = 'baz';

$normalizer = new class extends AbstractObjectNormalizerDummy {
public $childContextCacheKey;
public array $childContextCacheKeys = [];

protected function extractAttributes(object $object, ?string $format = null, array $context = []): array
{
Expand All @@ -983,7 +983,7 @@ protected function getAttributeValue(object $object, string $attribute, ?string
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
{
$childContext = parent::createChildContext($parentContext, $attribute, $format);
$this->childContextCacheKey = $childContext['cache_key'];
$this->childContextCacheKeys[$attribute] = $childContext['cache_key'];

return $childContext;
}
Expand All @@ -992,7 +992,7 @@ protected function createChildContext(array $parentContext, string $attribute, ?
$serializer = new Serializer([$normalizer]);
$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/1']);

$this->assertSame('hardcoded-foo', $normalizer->childContextCacheKey);
$this->assertSame(['foo' => 'hardcoded-foo', 'bar' => 'hardcoded-bar', 'baz' => 'hardcoded-baz'], $normalizer->childContextCacheKeys);
}

public function testChildContextCacheKeyStaysFalseWhenOriginalCacheKeyIsFalse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Serializer\Tests\Normalizer;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
Expand All @@ -34,7 +33,9 @@
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\ClassWithIgnoreAttribute;
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\GroupDummy;
use Symfony\Component\Serializer\Tests\Fixtures\CircularReferenceDummy;
use Symfony\Component\Serializer\Tests\Fixtures\ScalarNormalizer;
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
use Symfony\Component\Serializer\Tests\Fixtures\StdClassNormalizer;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CacheableObjectAttributesTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CircularReferenceTestTrait;
Expand All @@ -61,7 +62,7 @@ class GetSetMethodNormalizerTest extends TestCase
use TypeEnforcementTestTrait;

private GetSetMethodNormalizer $normalizer;
private SerializerInterface&NormalizerInterface&MockObject $serializer;
private SerializerInterface&NormalizerInterface $serializer;

protected function setUp(): void
{
Expand All @@ -70,8 +71,8 @@ protected function setUp(): void

private function createNormalizer(array $defaultContext = []): void
{
$this->serializer = $this->createMock(SerializerNormalizer::class);
$this->normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext);
$this->serializer = new Serializer([$this->normalizer, new StdClassNormalizer()]);
$this->normalizer->setSerializer($this->serializer);
}

Expand All @@ -91,13 +92,6 @@ public function testNormalize()
$obj->setCamelCase('camelcase');
$obj->setObject($object);

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

$this->assertEquals(
[
'foo' => 'foo',
Expand All @@ -111,6 +105,29 @@ public function testNormalize()
);
}

public function testNormalizeWithoutSerializer()
{
$obj = new GetSetDummy();
$obj->setFoo('foo');
$obj->setBar('bar');
$obj->setBaz(true);
$obj->setCamelCase('camelcase');

$this->normalizer = new GetSetMethodNormalizer();

$this->assertEquals(
[
'foo' => 'foo',
'bar' => 'bar',
'baz' => true,
'fooBar' => 'foobar',
'camelCase' => 'camelcase',
'object' => null,
],
$this->normalizer->normalize($obj, 'any')
);
}

public function testDenormalize()
{
$obj = $this->normalizer->denormalize(
Expand Down Expand Up @@ -377,8 +394,7 @@ public function testUnableToNormalizeObjectAttribute()
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Cannot normalize attribute "object" because the injected serializer is not a normalizer');
$serializer = $this->createMock(SerializerInterface::class);
$this->normalizer->setSerializer($serializer);
$this->normalizer->setSerializer($this->createMock(SerializerInterface::class));

$obj = new GetSetDummy();
$object = new \stdClass();
Expand Down Expand Up @@ -523,6 +539,30 @@ public function testNormalizeWithMethodNamesSimilarToAccessors()
$this->assertSame(['class' => 'class', 123 => 123], $normalizer->normalize(new GetSetWithAccessorishMethod()));
}

public function testNormalizeWithScalarValueNormalizer()
{
$normalizer = new GetSetMethodNormalizer();
$normalizer->setSerializer(new Serializer([$normalizer, new ScalarNormalizer()]));

$obj = new GetSetDummy();
$obj->setFoo('foo');
$obj->setBar(10);
$obj->setBaz(true);
$obj->setCamelCase('camelcase');

$this->assertSame(
[
'foo' => 'FOO',
'bar' => '10',
'baz' => '1',
'fooBar' => 'FOO10',
'camelCase' => 'CAMELCASE',
'object' => null,
],
$normalizer->normalize($obj, 'any')
);
}

public function testDenormalizeWithDiscriminator()
{
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
Expand Down Expand Up @@ -701,10 +741,6 @@ public function otherMethod()
}
}

abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
{
}

class GetConstructorOptionalArgsDummy
{
protected $foo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Serializer\Tests\Normalizer;

use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyAccess\PropertyAccessorBuilder;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
Expand Down Expand Up @@ -49,6 +48,7 @@
use Symfony\Component\Serializer\Tests\Fixtures\Php74DummyPrivate;
use Symfony\Component\Serializer\Tests\Fixtures\Php80Dummy;
use Symfony\Component\Serializer\Tests\Fixtures\SiblingHolder;
use Symfony\Component\Serializer\Tests\Fixtures\StdClassNormalizer;
use Symfony\Component\Serializer\Tests\Normalizer\Features\AttributesTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CacheableObjectAttributesTestTrait;
use Symfony\Component\Serializer\Tests\Normalizer\Features\CallbacksTestTrait;
Expand Down Expand Up @@ -86,7 +86,7 @@ class ObjectNormalizerTest extends TestCase
use TypeEnforcementTestTrait;

private ObjectNormalizer $normalizer;
private SerializerInterface&NormalizerInterface&MockObject $serializer;
private SerializerInterface&NormalizerInterface $serializer;

protected function setUp(): void
{
Expand All @@ -95,8 +95,8 @@ protected function setUp(): void

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->serializer = new Serializer([new StdClassNormalizer(), $this->normalizer]);
$this->normalizer->setSerializer($this->serializer);
}

Expand All @@ -111,13 +111,6 @@ public function testNormalize()
$obj->setObject($object);
$obj->setGo(true);

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

$this->assertEquals(
[
'foo' => 'foo',
Expand All @@ -132,6 +125,32 @@ public function testNormalize()
);
}

public function testNormalizeWithoutSerializer()
{
$obj = new ObjectDummy();
$obj->setFoo('foo');
$obj->bar = 'bar';
$obj->setBaz(true);
$obj->setCamelCase('camelcase');
$obj->setObject(null);
$obj->setGo(true);

$this->normalizer = new ObjectNormalizer();

$this->assertEquals(
[
'foo' => 'foo',
'bar' => 'bar',
'baz' => true,
'fooBar' => 'foobar',
'camelCase' => 'camelcase',
'object' => null,
'go' => true,
],
$this->normalizer->normalize($obj, 'any')
);
}

public function testNormalizeObjectWithUninitializedProperties()
{
$obj = new Php74Dummy();
Expand Down Expand Up @@ -962,7 +981,7 @@ public function testObjectNormalizerWithAttributeLoaderAndObjectHasStaticPropert

protected function getNormalizerForAccessors($accessorPrefixes = null): ObjectNormalizer
{
$accessorPrefixes = $accessorPrefixes ?? ReflectionExtractor::$defaultAccessorPrefixes;
$accessorPrefixes ??= ReflectionExtractor::$defaultAccessorPrefixes;
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$propertyAccessorBuilder = (new PropertyAccessorBuilder())
->setReadInfoExtractor(
Expand Down
Loading