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

Skip to content

[Serializer] add missing unit tests related to Encoder #16557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2015
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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);
}
}
129 changes: 129 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/ChainEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;
}
}
56 changes: 43 additions & 13 deletions src/Symfony/Component/Serializer/Tests/Encoder/JsonDecodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!'),
);
}
}
59 changes: 59 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/JsonEncodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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);
}
}