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

Skip to content

Commit b6857ba

Browse files
committed
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
2 parents 4e1fffe + 7418d29 commit b6857ba

File tree

4 files changed

+308
-13
lines changed

4 files changed

+308
-13
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Encoder;
13+
14+
use Symfony\Component\Serializer\Encoder\ChainDecoder;
15+
16+
class ChainDecoderTest extends \PHPUnit_Framework_TestCase
17+
{
18+
const FORMAT_1 = 'format1';
19+
const FORMAT_2 = 'format2';
20+
const FORMAT_3 = 'format3';
21+
22+
private $chainDecoder;
23+
private $decoder1;
24+
private $decoder2;
25+
26+
protected function setUp()
27+
{
28+
$this->decoder1 = $this
29+
->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
30+
->getMock();
31+
32+
$this->decoder1
33+
->method('supportsDecoding')
34+
->will($this->returnValueMap(array(
35+
array(self::FORMAT_1, true),
36+
array(self::FORMAT_2, false),
37+
array(self::FORMAT_3, false),
38+
)));
39+
40+
$this->decoder2 = $this
41+
->getMockBuilder('Symfony\Component\Serializer\Encoder\DecoderInterface')
42+
->getMock();
43+
44+
$this->decoder2
45+
->method('supportsDecoding')
46+
->will($this->returnValueMap(array(
47+
array(self::FORMAT_1, false),
48+
array(self::FORMAT_2, true),
49+
array(self::FORMAT_3, false),
50+
)));
51+
52+
$this->chainDecoder = new ChainDecoder(array($this->decoder1, $this->decoder2));
53+
}
54+
55+
public function testSupportsDecoding()
56+
{
57+
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_1));
58+
$this->assertTrue($this->chainDecoder->supportsDecoding(self::FORMAT_2));
59+
$this->assertFalse($this->chainDecoder->supportsDecoding(self::FORMAT_3));
60+
}
61+
62+
public function testDecode()
63+
{
64+
$this->decoder1->expects($this->never())->method('decode');
65+
$this->decoder2->expects($this->once())->method('decode');
66+
67+
$this->chainDecoder->decode('string_to_decode', self::FORMAT_2);
68+
}
69+
70+
/**
71+
* @expectedException Symfony\Component\Serializer\Exception\RuntimeException
72+
*/
73+
public function testDecodeUnsupportedFormat()
74+
{
75+
$this->chainDecoder->decode('string_to_decode', self::FORMAT_3);
76+
}
77+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Encoder;
13+
14+
use Symfony\Component\Serializer\Encoder\ChainEncoder;
15+
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
16+
17+
class ChainEncoderTest extends \PHPUnit_Framework_TestCase
18+
{
19+
const FORMAT_1 = 'format1';
20+
const FORMAT_2 = 'format2';
21+
const FORMAT_3 = 'format3';
22+
23+
private $chainEncoder;
24+
private $encoder1;
25+
private $encoder2;
26+
27+
protected function setUp()
28+
{
29+
$this->encoder1 = $this
30+
->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
31+
->getMock();
32+
33+
$this->encoder1
34+
->method('supportsEncoding')
35+
->will($this->returnValueMap(array(
36+
array(self::FORMAT_1, true),
37+
array(self::FORMAT_2, false),
38+
array(self::FORMAT_3, false),
39+
)));
40+
41+
$this->encoder2 = $this
42+
->getMockBuilder('Symfony\Component\Serializer\Encoder\EncoderInterface')
43+
->getMock();
44+
45+
$this->encoder2
46+
->method('supportsEncoding')
47+
->will($this->returnValueMap(array(
48+
array(self::FORMAT_1, false),
49+
array(self::FORMAT_2, true),
50+
array(self::FORMAT_3, false),
51+
)));
52+
53+
$this->chainEncoder = new ChainEncoder(array($this->encoder1, $this->encoder2));
54+
}
55+
56+
public function testSupportsEncoding()
57+
{
58+
$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_1));
59+
$this->assertTrue($this->chainEncoder->supportsEncoding(self::FORMAT_2));
60+
$this->assertFalse($this->chainEncoder->supportsEncoding(self::FORMAT_3));
61+
}
62+
63+
public function testEncode()
64+
{
65+
$this->encoder1->expects($this->never())->method('encode');
66+
$this->encoder2->expects($this->once())->method('encode');
67+
68+
$this->chainEncoder->encode(array('foo' => 123), self::FORMAT_2);
69+
}
70+
71+
/**
72+
* @expectedException Symfony\Component\Serializer\Exception\RuntimeException
73+
*/
74+
public function testEncodeUnsupportedFormat()
75+
{
76+
$this->chainEncoder->encode(array('foo' => 123), self::FORMAT_3);
77+
}
78+
79+
public function testNeedsNormalizationBasic()
80+
{
81+
$this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_1));
82+
$this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_2));
83+
}
84+
85+
/**
86+
* @dataProvider booleanProvider
87+
*/
88+
public function testNeedsNormalizationChainNormalizationAware($bool)
89+
{
90+
$chainEncoder = $this
91+
->getMockBuilder('Symfony\Component\Serializer\Tests\Encoder\ChainNormalizationAwareEncoder')
92+
->getMock();
93+
94+
$chainEncoder->method('supportsEncoding')->willReturn(true);
95+
$chainEncoder->method('needsNormalization')->willReturn($bool);
96+
97+
$sut = new ChainEncoder(array($chainEncoder));
98+
99+
$this->assertEquals($bool, $sut->needsNormalization(self::FORMAT_1));
100+
}
101+
102+
public function testNeedsNormalizationNormalizationAware()
103+
{
104+
$encoder = new NormalizationAwareEncoder();
105+
$sut = new ChainEncoder(array($encoder));
106+
107+
$this->assertFalse($sut->needsNormalization(self::FORMAT_1));
108+
}
109+
110+
public function booleanProvider()
111+
{
112+
return array(
113+
array(true),
114+
array(false),
115+
);
116+
}
117+
}
118+
119+
class ChainNormalizationAwareEncoder extends ChainEncoder implements NormalizationAwareInterface
120+
{
121+
}
122+
123+
class NormalizationAwareEncoder implements NormalizationAwareInterface
124+
{
125+
public function supportsEncoding($format)
126+
{
127+
return true;
128+
}
129+
}

src/Symfony/Component/Serializer/Tests/Encoder/JsonDecodeTest.php

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,63 @@
1212
namespace Symfony\Component\Serializer\Tests\Encoder;
1313

1414
use Symfony\Component\Serializer\Encoder\JsonDecode;
15+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
1516

1617
class JsonDecodeTest extends \PHPUnit_Framework_TestCase
1718
{
1819
/** @var \Symfony\Component\Serializer\Encoder\JsonDecode */
19-
private $decoder;
20+
private $decode;
2021

2122
protected function setUp()
2223
{
23-
$this->decoder = new JsonDecode(true);
24+
$this->decode = new JsonDecode();
2425
}
2526

26-
public function testDecodeWithValidData()
27+
public function testSupportsDecoding()
2728
{
28-
$json = json_encode(array(
29-
'hello' => 'world',
30-
));
31-
$result = $this->decoder->decode($json, 'json');
32-
$this->assertEquals(array(
33-
'hello' => 'world',
34-
), $result);
29+
$this->assertTrue($this->decode->supportsDecoding(JsonEncoder::FORMAT));
30+
$this->assertFalse($this->decode->supportsDecoding('foobar'));
3531
}
3632

3733
/**
38-
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
34+
* @dataProvider decodeProvider
3935
*/
40-
public function testDecodeWithInvalidData()
36+
public function testDecode($toDecode, $expected, $context)
4137
{
42-
$result = $this->decoder->decode('kaboom!', 'json');
38+
$this->assertEquals(
39+
$expected,
40+
$this->decode->decode($toDecode, JsonEncoder::FORMAT, $context)
41+
);
42+
}
43+
44+
public function decodeProvider()
45+
{
46+
$stdClass = new \stdClass();
47+
$stdClass->foo = 'bar';
48+
49+
$assoc = array('foo' => 'bar');
50+
51+
return array(
52+
array('{"foo": "bar"}', $stdClass, array()),
53+
array('{"foo": "bar"}', $assoc, array('json_decode_associative' => true)),
54+
);
55+
}
56+
57+
/**
58+
* @requires function json_last_error_msg
59+
* @dataProvider decodeProviderException
60+
* @expectedException Symfony\Component\Serializer\Exception\UnexpectedValueException
61+
*/
62+
public function testDecodeWithException($value)
63+
{
64+
$this->decode->decode($value, JsonEncoder::FORMAT);
65+
}
66+
67+
public function decodeProviderException()
68+
{
69+
return array(
70+
array("{'foo': 'bar'}"),
71+
array('kaboom!'),
72+
);
4373
}
4474
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Encoder;
13+
14+
use Symfony\Component\Serializer\Encoder\JsonEncode;
15+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
16+
17+
class JsonEncodeTest extends \PHPUnit_Framework_TestCase
18+
{
19+
private $encoder;
20+
21+
protected function setUp()
22+
{
23+
$this->encode = new JsonEncode();
24+
}
25+
26+
public function testSupportsEncoding()
27+
{
28+
$this->assertTrue($this->encode->supportsEncoding(JsonEncoder::FORMAT));
29+
$this->assertFalse($this->encode->supportsEncoding('foobar'));
30+
}
31+
32+
/**
33+
* @dataProvider encodeProvider
34+
*/
35+
public function testEncode($toEncode, $expected, $context)
36+
{
37+
$this->assertEquals(
38+
$expected,
39+
$this->encode->encode($toEncode, JsonEncoder::FORMAT, $context)
40+
);
41+
}
42+
43+
public function encodeProvider()
44+
{
45+
return array(
46+
array(array(), '[]', array()),
47+
array(array(), '{}', array('json_encode_options' => JSON_FORCE_OBJECT)),
48+
);
49+
}
50+
51+
/**
52+
* @requires function json_last_error_msg
53+
* @expectedException Symfony\Component\Serializer\Exception\UnexpectedValueException
54+
*/
55+
public function testEncodeWithError()
56+
{
57+
$this->encode->encode("\xB1\x31", JsonEncoder::FORMAT);
58+
}
59+
}

0 commit comments

Comments
 (0)