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

Skip to content

Commit 133766f

Browse files
committed
Update the tests
1 parent 47adb72 commit 133766f

File tree

5 files changed

+25
-41
lines changed

5 files changed

+25
-41
lines changed

src/Symfony/Component/Serializer/Dumper/NormalizerDumper.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
namespace Symfony\Component\Serializer\Dumper;
1313

14-
use Symfony\Component\Serializer\NormalizerInterface;
15-
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
1614
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
15+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
16+
use Symfony\Component\Serializer\NormalizerInterface;
1717

1818
/**
1919
* @author Guilhem Niot <[email protected]>
@@ -61,7 +61,7 @@ class {$context['class']} implements NormalizerInterface, NormalizerAwareInterfa
6161
6262
use CircularReferenceTrait, NormalizerAwareTrait;
6363
64-
public function __construct(array \$defaultContext)
64+
public function __construct(array \$defaultContext = array())
6565
{
6666
\$this->defaultContext = array_merge(\$this->defaultContext, \$defaultContext);
6767
}
@@ -91,7 +91,7 @@ private function generateNormalizeMethodInner(\ReflectionClass $reflectionClass)
9191
$code = <<<EOL
9292
9393
if (\$this->isCircularReference(\$object, \$context)) {
94-
return \$this->handleCircularReference(\$object);
94+
return \$this->handleCircularReference(\$object, \$format, \$context);
9595
}
9696
9797
\$groups = isset(\$context[ObjectNormalizer::GROUPS]) && is_array(\$context[ObjectNormalizer::GROUPS]) ? \$context[ObjectNormalizer::GROUPS] : null;
@@ -113,10 +113,9 @@ private function generateNormalizeMethodInner(\ReflectionClass $reflectionClass)
113113
}
114114

115115
if ($maxDepthCode) {
116-
$maxDepthKey = ObjectNormalizer::ENABLE_MAX_DEPTH;
117116
$code .= <<<EOL
118117
119-
if (\$context[$maxDepthKey] ?? \$this->defaultContext[$maxDepthKey]) {{$maxDepthCode}
118+
if (\$context[ObjectNormalizer::ENABLE_MAX_DEPTH] ?? \$this->defaultContext[ObjectNormalizer::ENABLE_MAX_DEPTH]) {{$maxDepthCode}
120119
}
121120
122121
EOL;
@@ -125,7 +124,7 @@ private function generateNormalizeMethodInner(\ReflectionClass $reflectionClass)
125124
foreach ($attributesMetadata as $attributeMetadata) {
126125
$code .= <<<EOL
127126
128-
\$attributes = \$context['attributes'] ?? $this->defaultContext['attributes'] ?? null
127+
\$attributes = \$context[ObjectNormalizer::ATTRIBUTES] ?? \$this->defaultContext[ObjectNormalizer::ATTRIBUTES] ?? null;
129128
if ((null === \$groups
130129
EOL;
131130

@@ -134,7 +133,7 @@ private function generateNormalizeMethodInner(\ReflectionClass $reflectionClass)
134133
}
135134
$code .= ')';
136135

137-
$code .= " && (isset(\$attributes['{$attributeMetadata->name}']) || (is_array(\$attributes) && in_array('{$attributeMetadata->name}', \$attributes, true)))";
136+
$code .= " && (null === \$attributes || isset(\$attributes['{$attributeMetadata->name}']) || (is_array(\$attributes) && in_array('{$attributeMetadata->name}', \$attributes, true)))";
138137

139138
if (null !== $maxDepth = $attributeMetadata->getMaxDepth()) {
140139
$key = sprintf(ObjectNormalizer::DEPTH_KEY_PATTERN, $reflectionClass->name, $attributeMetadata->name);
@@ -151,10 +150,10 @@ private function generateNormalizeMethodInner(\ReflectionClass $reflectionClass)
151150
\$output['{$attributeMetadata->name}'] = \$value;
152151
} else {
153152
\$subContext = \$context;
154-
if (isset(\$context['attributes']['{$attributeMetadata->name}'])) {
155-
\$subContext['attributes'] = \$context['attributes']['{$attributeMetadata->name}'];
153+
if (isset(\$attributes['{$attributeMetadata->name}'])) {
154+
\$subContext[ObjectNormalizer::ATTRIBUTES] = \$attributes['{$attributeMetadata->name}'];
156155
} else {
157-
unset(\$subContext['attributes']);
156+
unset(\$subContext[ObjectNormalizer::ATTRIBUTES]);
158157
}
159158
160159
\$output['{$attributeMetadata->name}'] = \$this->normalizer->normalize(\$value, \$format, \$subContext);

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14-
use Symfony\Component\Serializer\Exception\CircularReferenceException;
1514
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1615
use Symfony\Component\Serializer\Exception\LogicException;
1716
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;

src/Symfony/Component/Serializer/Normalizer/GeneratedObjectNormalizer.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14-
use Symfony\Component\Config\ConfigCacheFactoryInterface;
1514
use Symfony\Component\Config\ConfigCacheFactory;
15+
use Symfony\Component\Config\ConfigCacheFactoryInterface;
1616
use Symfony\Component\Config\ConfigCacheInterface;
1717
use Symfony\Component\Config\Resource\ReflectionClassResource;
1818
use Symfony\Component\Serializer\Dumper\NormalizerDumper;
@@ -29,6 +29,7 @@ class GeneratedObjectNormalizer implements NormalizerInterface, NormalizerAwareI
2929
private $normalizerDumper;
3030
private $cacheDir;
3131
private $debug;
32+
protected $defaultContext;
3233

3334
/**
3435
* @var NormalizerInterface[]
@@ -40,29 +41,20 @@ class GeneratedObjectNormalizer implements NormalizerInterface, NormalizerAwareI
4041
*/
4142
private $configCacheFactory;
4243

43-
/**
44-
* @var int
45-
*/
46-
protected $circularReferenceLimit = 1;
47-
48-
/**
49-
* @var callable|null
50-
*/
51-
protected $circularReferenceHandler;
52-
53-
public function __construct(NormalizerDumper $normalizerDumper, string $cacheDir, bool $debug)
44+
public function __construct(NormalizerDumper $normalizerDumper, string $cacheDir, bool $debug, array $defaultContext = array())
5445
{
5546
$this->normalizerDumper = $normalizerDumper;
5647
$this->cacheDir = $cacheDir;
5748
$this->debug = $debug;
49+
$this->defaultContext = $defaultContext;
5850
}
5951

6052
/**
6153
* {@inheritdoc}
6254
*/
6355
public function normalize($object, $format = null, array $context = array())
6456
{
65-
$class = get_class($object);
57+
$class = \get_class($object);
6658
if (isset($this->normalizers[$class])) {
6759
return $this->normalizers[$class]->normalize($object, $format, $context);
6860
}
@@ -82,12 +74,8 @@ function (ConfigCacheInterface $cache) use ($class, $normalizerClass) {
8274

8375
require_once $cache->getPath();
8476

85-
$this->normalizers[$class] = $normalizer = new $normalizerClass();
77+
$this->normalizers[$class] = $normalizer = new $normalizerClass($this->defaultContext);
8678
$normalizer->setNormalizer($this->normalizer);
87-
$normalizer->setCircularReferenceLimit($this->circularReferenceLimit);
88-
if (null !== $this->circularReferenceHandler) {
89-
$normalizer->setCircularReferenceHandler($this->circularReferenceHandler);
90-
}
9179

9280
return $normalizer->normalize($object, $format, $context);
9381
}

src/Symfony/Component/Serializer/Tests/Dumper/NormalizerDumperTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
namespace Symfony\Component\Serializer\Tests\Dumper;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
15-
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
15+
use Symfony\Component\Serializer\Dumper\NormalizerDumper;
1616
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
17+
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
1718
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1819
use Symfony\Component\Serializer\Tests\Normalizer\ObjectNormalizerTest;
19-
use Symfony\Component\Serializer\Dumper\NormalizerDumper;
2020

2121
class NormalizerDumperTest extends ObjectNormalizerTest
2222
{
23-
protected function getNormalizerFor(string $class): NormalizerInterface
23+
protected function getNormalizerFor(string $class, array $defaultContext = array()): NormalizerInterface
2424
{
2525
$normalizerName = 'Test'.md5($class).'Normalizer';
2626

@@ -31,7 +31,7 @@ protected function getNormalizerFor(string $class): NormalizerInterface
3131
eval('?>'.$dumper->dump($class, array('class' => $normalizerName)));
3232
}
3333

34-
$normalizer = new $normalizerName();
34+
$normalizer = new $normalizerName($defaultContext);
3535
$normalizer->setNormalizer($this->serializer);
3636

3737
return $normalizer;

src/Symfony/Component/Serializer/Tests/Normalizer/ObjectNormalizerTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ private function createNormalizer(array $defaultContext = array(), ClassMetadata
6161
$this->normalizer->setSerializer($this->serializer);
6262
}
6363

64-
protected function getNormalizerFor(string $class): NormalizerInterface
64+
protected function getNormalizerFor(string $class, array $defaultContext = array()): NormalizerInterface
6565
{
6666
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
6767

68-
$normalizer = new ObjectNormalizer($classMetadataFactory);
68+
$normalizer = new ObjectNormalizer($classMetadataFactory, null, null, null, null, null, $defaultContext);
6969
$normalizer->setSerializer($this->serializer);
7070

7171
return $normalizer;
@@ -607,13 +607,12 @@ public function testLegacyUnableToNormalizeCircularReference()
607607

608608
private function doTestUnableToNormalizeCircularReference(bool $legacy = false)
609609
{
610-
$legacy ? $this->normalizer->setCircularReferenceLimit(2) : $this->createNormalizer(array(ObjectNormalizer::CIRCULAR_REFERENCE_LIMIT => 2));
610+
$legacy ? $this->normalizer->setCircularReferenceLimit(2) : $this->normalizer = $this->getNormalizerFor(CircularReferenceDummy::class, array(ObjectNormalizer::CIRCULAR_REFERENCE_LIMIT => 2));
611611
$serializer = new Serializer(array($this->normalizer));
612-
$this->normalizer->setSerializer($serializer);
613612

614613
$obj = new CircularReferenceDummy();
615614

616-
$normalizer->normalize($obj);
615+
$this->normalizer->normalize($obj);
617616
}
618617

619618
public function testSiblingReference()
@@ -665,9 +664,8 @@ private function doTestCircularReferenceHandler(bool $legacy = false)
665664

666665
private function createNormalizerWithCircularReferenceHandler(callable $handler, bool $legacy)
667666
{
668-
$legacy ? $this->normalizer->setCircularReferenceHandler($handler) : $this->createNormalizer(array(ObjectNormalizer::CIRCULAR_REFERENCE_HANDLER => $handler));
667+
$legacy ? $this->normalizer->setCircularReferenceHandler($handler) : $this->normalizer = $this->getNormalizerFor(CircularReferenceDummy::class, array(ObjectNormalizer::CIRCULAR_REFERENCE_HANDLER => $handler));
669668
$this->serializer = new Serializer(array($this->normalizer));
670-
$this->normalizer->setSerializer($this->serializer);
671669
}
672670

673671
public function testDenormalizeNonExistingAttribute()

0 commit comments

Comments
 (0)