minor #16557 [Serializer] add missing unit tests related to Encoder (FlorianLB)
This PR was merged into the 2.8 branch.
Discussion
----------
[Serializer] add missing unit tests related to Encoder
Add some missing unit tests on the Serializer component.
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Commits
-------
7418d29
[Serializer] add missing unit tests related to Encoder
This commit is contained in:
commit
b6857ba405
@ -0,0 +1,77 @@
|
||||
<?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;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\ChainDecoder;
|
||||
|
||||
class ChainDecoderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
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')
|
||||
->will($this->returnValueMap(array(
|
||||
array(self::FORMAT_1, true),
|
||||
array(self::FORMAT_2, false),
|
||||
array(self::FORMAT_3, false),
|
||||
)));
|
||||
|
||||
$this->decoder2 = $this
|
||||
->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
|
||||
->getMock();
|
||||
|
||||
$this->decoder2
|
||||
->method('supportsDecoding')
|
||||
->will($this->returnValueMap(array(
|
||||
array(self::FORMAT_1, false),
|
||||
array(self::FORMAT_2, true),
|
||||
array(self::FORMAT_3, false),
|
||||
)));
|
||||
|
||||
$this->chainDecoder = new ChainDecoder(array($this->decoder1, $this->decoder2));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Serializer\Exception\RuntimeException
|
||||
*/
|
||||
public function testDecodeUnsupportedFormat()
|
||||
{
|
||||
$this->chainDecoder->decode('string_to_decode', self::FORMAT_3);
|
||||
}
|
||||
}
|
@ -0,0 +1,129 @@
|
||||
<?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;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\ChainEncoder;
|
||||
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
|
||||
|
||||
class ChainEncoderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
const FORMAT_1 = 'format1';
|
||||
const FORMAT_2 = 'format2';
|
||||
const FORMAT_3 = 'format3';
|
||||
|
||||
private $chainEncoder;
|
||||
private $encoder1;
|
||||
private $encoder2;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->encoder1 = $this
|
||||
->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
|
||||
->getMock();
|
||||
|
||||
$this->encoder1
|
||||
->method('supportsEncoding')
|
||||
->will($this->returnValueMap(array(
|
||||
array(self::FORMAT_1, true),
|
||||
array(self::FORMAT_2, false),
|
||||
array(self::FORMAT_3, false),
|
||||
)));
|
||||
|
||||
$this->encoder2 = $this
|
||||
->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
|
||||
->getMock();
|
||||
|
||||
$this->encoder2
|
||||
->method('supportsEncoding')
|
||||
->will($this->returnValueMap(array(
|
||||
array(self::FORMAT_1, false),
|
||||
array(self::FORMAT_2, true),
|
||||
array(self::FORMAT_3, false),
|
||||
)));
|
||||
|
||||
$this->chainEncoder = new ChainEncoder(array($this->encoder1, $this->encoder2));
|
||||
}
|
||||
|
||||
public function testSupportsEncoding()
|
||||
{
|
||||
$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_1));
|
||||
$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_2));
|
||||
$this->assertFalse($this->chainEncoder->supportsEncoding(self::FORMAT_3));
|
||||
}
|
||||
|
||||
public function testEncode()
|
||||
{
|
||||
$this->encoder1->expects($this->never())->method('encode');
|
||||
$this->encoder2->expects($this->once())->method('encode');
|
||||
|
||||
$this->chainEncoder->encode(array('foo' => 123), self::FORMAT_2);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Serializer\Exception\RuntimeException
|
||||
*/
|
||||
public function testEncodeUnsupportedFormat()
|
||||
{
|
||||
$this->chainEncoder->encode(array('foo' => 123), self::FORMAT_3);
|
||||
}
|
||||
|
||||
public function testNeedsNormalizationBasic()
|
||||
{
|
||||
$this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_1));
|
||||
$this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider booleanProvider
|
||||
*/
|
||||
public function testNeedsNormalizationChainNormalizationAware($bool)
|
||||
{
|
||||
$chainEncoder = $this
|
||||
->getMockBuilder('Symfony\Component\Serializer\Tests\Encoder\ChainNormalizationAwareEncoder')
|
||||
->getMock();
|
||||
|
||||
$chainEncoder->method('supportsEncoding')->willReturn(true);
|
||||
$chainEncoder->method('needsNormalization')->willReturn($bool);
|
||||
|
||||
$sut = new ChainEncoder(array($chainEncoder));
|
||||
|
||||
$this->assertEquals($bool, $sut->needsNormalization(self::FORMAT_1));
|
||||
}
|
||||
|
||||
public function testNeedsNormalizationNormalizationAware()
|
||||
{
|
||||
$encoder = new NormalizationAwareEncoder();
|
||||
$sut = new ChainEncoder(array($encoder));
|
||||
|
||||
$this->assertFalse($sut->needsNormalization(self::FORMAT_1));
|
||||
}
|
||||
|
||||
public function booleanProvider()
|
||||
{
|
||||
return array(
|
||||
array(true),
|
||||
array(false),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChainNormalizationAwareEncoder extends ChainEncoder implements NormalizationAwareInterface
|
||||
{
|
||||
}
|
||||
|
||||
class NormalizationAwareEncoder implements NormalizationAwareInterface
|
||||
{
|
||||
public function supportsEncoding($format)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
@ -12,33 +12,63 @@
|
||||
namespace Symfony\Component\Serializer\Tests\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\JsonDecode;
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
|
||||
class JsonDecodeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var \Symfony\Component\Serializer\Encoder\JsonDecode */
|
||||
private $decoder;
|
||||
private $decode;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->decoder = new JsonDecode(true);
|
||||
$this->decode = new JsonDecode();
|
||||
}
|
||||
|
||||
public function testDecodeWithValidData()
|
||||
public function testSupportsDecoding()
|
||||
{
|
||||
$json = json_encode(array(
|
||||
'hello' => 'world',
|
||||
));
|
||||
$result = $this->decoder->decode($json, 'json');
|
||||
$this->assertEquals(array(
|
||||
'hello' => 'world',
|
||||
), $result);
|
||||
$this->assertTrue($this->decode->supportsDecoding(JsonEncoder::FORMAT));
|
||||
$this->assertFalse($this->decode->supportsDecoding('foobar'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
* @dataProvider decodeProvider
|
||||
*/
|
||||
public function testDecodeWithInvalidData()
|
||||
public function testDecode($toDecode, $expected, $context)
|
||||
{
|
||||
$result = $this->decoder->decode('kaboom!', 'json');
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->decode->decode($toDecode, JsonEncoder::FORMAT, $context)
|
||||
);
|
||||
}
|
||||
|
||||
public function decodeProvider()
|
||||
{
|
||||
$stdClass = new \stdClass();
|
||||
$stdClass->foo = 'bar';
|
||||
|
||||
$assoc = array('foo' => 'bar');
|
||||
|
||||
return array(
|
||||
array('{"foo": "bar"}', $stdClass, array()),
|
||||
array('{"foo": "bar"}', $assoc, array('json_decode_associative' => true)),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires function json_last_error_msg
|
||||
* @dataProvider decodeProviderException
|
||||
* @expectedException Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testDecodeWithException($value)
|
||||
{
|
||||
$this->decode->decode($value, JsonEncoder::FORMAT);
|
||||
}
|
||||
|
||||
public function decodeProviderException()
|
||||
{
|
||||
return array(
|
||||
array("{'foo': 'bar'}"),
|
||||
array('kaboom!'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,59 @@
|
||||
<?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;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncode;
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
|
||||
class JsonEncodeTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $encoder;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->encode = new JsonEncode();
|
||||
}
|
||||
|
||||
public function testSupportsEncoding()
|
||||
{
|
||||
$this->assertTrue($this->encode->supportsEncoding(JsonEncoder::FORMAT));
|
||||
$this->assertFalse($this->encode->supportsEncoding('foobar'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider encodeProvider
|
||||
*/
|
||||
public function testEncode($toEncode, $expected, $context)
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->encode->encode($toEncode, JsonEncoder::FORMAT, $context)
|
||||
);
|
||||
}
|
||||
|
||||
public function encodeProvider()
|
||||
{
|
||||
return array(
|
||||
array(array(), '[]', array()),
|
||||
array(array(), '{}', array('json_encode_options' => JSON_FORCE_OBJECT)),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @requires function json_last_error_msg
|
||||
* @expectedException Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function testEncodeWithError()
|
||||
{
|
||||
$this->encode->encode("\xB1\x31", JsonEncoder::FORMAT);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user