Skip to content
Merged
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
6 changes: 5 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/ChainDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ private function getDecoder(string $format, array $context): DecoderInterface
return $this->decoders[$this->decoderByFormat[$format]];
}

$cache = true;
foreach ($this->decoders as $i => $decoder) {
$cache = $cache && !$decoder instanceof ContextAwareDecoderInterface;
if ($decoder->supportsDecoding($format, $context)) {
$this->decoderByFormat[$format] = $i;
if ($cache) {
$this->decoderByFormat[$format] = $i;
}

return $decoder;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Serializer/Encoder/ChainEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ private function getEncoder(string $format, array $context): EncoderInterface
return $this->encoders[$this->encoderByFormat[$format]];
}

$cache = true;
foreach ($this->encoders as $i => $encoder) {
$cache = $cache && !$encoder instanceof ContextAwareEncoderInterface;
if ($encoder->supportsEncoding($format, $context)) {
$this->encoderByFormat[$format] = $i;
if ($cache) {
$this->encoderByFormat[$format] = $i;
}

return $encoder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Encoder\ChainDecoder;
use Symfony\Component\Serializer\Encoder\ContextAwareDecoderInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Exception\RuntimeException;

Expand All @@ -28,14 +29,15 @@ class ChainDecoderTest extends TestCase

protected function setUp(): void
{
$this->decoder1 = $this->createMock(DecoderInterface::class);
$this->decoder1 = $this->createMock(ContextAwareDecoderInterface::class);
$this->decoder1
->method('supportsDecoding')
->willReturnMap([
[self::FORMAT_1, [], true],
[self::FORMAT_2, [], false],
[self::FORMAT_3, [], false],
[self::FORMAT_3, ['foo' => 'bar'], true],
[self::FORMAT_3, ['foo' => 'bar2'], false],
]);

$this->decoder2 = $this->createMock(DecoderInterface::class);
Expand All @@ -45,17 +47,35 @@ protected function setUp(): void
[self::FORMAT_1, [], false],
[self::FORMAT_2, [], true],
[self::FORMAT_3, [], false],
[self::FORMAT_3, ['foo' => 'bar'], false],
[self::FORMAT_3, ['foo' => 'bar2'], true],
]);

$this->chainDecoder = new ChainDecoder([$this->decoder1, $this->decoder2]);
}

public function testSupportsDecoding()
{
$this->decoder1
->method('decode')
->willReturn('result1');
$this->decoder2
->method('decode')
->willReturn('result2');

$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_1));
$this->assertEquals('result1', $this->chainDecoder->decode('', self::FORMAT_1, []));

$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_2));
$this->assertEquals('result2', $this->chainDecoder->decode('', self::FORMAT_2, []));

$this->assertFalse($this->chainDecoder->supportsDecoding(self::FORMAT_3));

$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar']));
$this->assertEquals('result1', $this->chainDecoder->decode('', self::FORMAT_3, ['foo' => 'bar']));

$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar2']));
$this->assertEquals('result2', $this->chainDecoder->decode('', self::FORMAT_3, ['foo' => 'bar2']));
}

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

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Encoder\ChainEncoder;
use Symfony\Component\Serializer\Encoder\ContextAwareEncoderInterface;
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
use Symfony\Component\Serializer\Exception\RuntimeException;
Expand All @@ -29,14 +30,15 @@ class ChainEncoderTest extends TestCase

protected function setUp(): void
{
$this->encoder1 = $this->createMock(EncoderInterface::class);
$this->encoder1 = $this->createMock(ContextAwareEncoderInterface::class);
$this->encoder1
->method('supportsEncoding')
->willReturnMap([
[self::FORMAT_1, [], true],
[self::FORMAT_2, [], false],
[self::FORMAT_3, [], false],
[self::FORMAT_3, ['foo' => 'bar'], true],
[self::FORMAT_3, ['foo' => 'bar2'], false],
]);

$this->encoder2 = $this->createMock(EncoderInterface::class);
Expand All @@ -46,17 +48,35 @@ protected function setUp(): void
[self::FORMAT_1, [], false],
[self::FORMAT_2, [], true],
[self::FORMAT_3, [], false],
[self::FORMAT_3, ['foo' => 'bar'], false],
[self::FORMAT_3, ['foo' => 'bar2'], true],
]);

$this->chainEncoder = new ChainEncoder([$this->encoder1, $this->encoder2]);
}

public function testSupportsEncoding()
{
$this->encoder1
->method('encode')
->willReturn('result1');
$this->encoder2
->method('encode')
->willReturn('result2');

$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_1));
$this->assertEquals('result1', $this->chainEncoder->encode('', self::FORMAT_1, []));

$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_2));
$this->assertEquals('result2', $this->chainEncoder->encode('', self::FORMAT_2, []));

$this->assertFalse($this->chainEncoder->supportsEncoding(self::FORMAT_3));

$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_3, ['foo' => 'bar']));
$this->assertEquals('result1', $this->chainEncoder->encode('', self::FORMAT_3, ['foo' => 'bar']));

$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_3, ['foo' => 'bar2']));
$this->assertEquals('result2', $this->chainEncoder->encode('', self::FORMAT_3, ['foo' => 'bar2']));
}

public function testEncode()
Expand Down