Skip to content

Commit def62e9

Browse files
committed
Update the tests
1 parent 47adb72 commit def62e9

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -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/Tests/Dumper/NormalizerDumperTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
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

+5-7
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)