2015-11-16 10:02:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Serializer\Tests\Encoder;
|
|
|
|
|
2017-02-18 17:42:54 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-11-16 10:02:30 +00:00
|
|
|
use Symfony\Component\Serializer\Encoder\ChainDecoder;
|
|
|
|
|
2017-02-18 17:42:54 +00:00
|
|
|
class ChainDecoderTest extends TestCase
|
2015-11-16 10:02:30 +00:00
|
|
|
{
|
|
|
|
const FORMAT_1 = 'format1';
|
|
|
|
const FORMAT_2 = 'format2';
|
|
|
|
const FORMAT_3 = 'format3';
|
|
|
|
|
|
|
|
private $chainDecoder;
|
|
|
|
private $decoder1;
|
|
|
|
private $decoder2;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->decoder1 = $this
|
|
|
|
->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->decoder1
|
|
|
|
->method('supportsDecoding')
|
2019-05-30 15:56:20 +01:00
|
|
|
->willReturnMap([
|
2019-01-16 09:39:14 +00:00
|
|
|
[self::FORMAT_1, [], true],
|
|
|
|
[self::FORMAT_2, [], false],
|
|
|
|
[self::FORMAT_3, [], false],
|
|
|
|
[self::FORMAT_3, ['foo' => 'bar'], true],
|
2019-05-30 15:56:20 +01:00
|
|
|
]);
|
2015-11-16 10:02:30 +00:00
|
|
|
|
|
|
|
$this->decoder2 = $this
|
|
|
|
->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->decoder2
|
|
|
|
->method('supportsDecoding')
|
2019-05-30 15:56:20 +01:00
|
|
|
->willReturnMap([
|
2019-01-16 09:39:14 +00:00
|
|
|
[self::FORMAT_1, [], false],
|
|
|
|
[self::FORMAT_2, [], true],
|
|
|
|
[self::FORMAT_3, [], false],
|
2019-05-30 15:56:20 +01:00
|
|
|
]);
|
2015-11-16 10:02:30 +00:00
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->chainDecoder = new ChainDecoder([$this->decoder1, $this->decoder2]);
|
2015-11-16 10:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsDecoding()
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_1));
|
|
|
|
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_2));
|
|
|
|
$this->assertFalse($this->chainDecoder->supportsDecoding(self::FORMAT_3));
|
2019-01-16 09:39:14 +00:00
|
|
|
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_3, ['foo' => 'bar']));
|
2015-11-16 10:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDecode()
|
|
|
|
{
|
|
|
|
$this->decoder1->expects($this->never())->method('decode');
|
|
|
|
$this->decoder2->expects($this->once())->method('decode');
|
|
|
|
|
|
|
|
$this->chainDecoder->decode('string_to_decode', self::FORMAT_2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-24 10:47:20 +01:00
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\RuntimeException
|
2015-11-16 10:02:30 +00:00
|
|
|
*/
|
|
|
|
public function testDecodeUnsupportedFormat()
|
|
|
|
{
|
|
|
|
$this->chainDecoder->decode('string_to_decode', self::FORMAT_3);
|
|
|
|
}
|
|
|
|
}
|