2015-11-18 08:21:04 +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-18 08:21:04 +00:00
|
|
|
use Symfony\Component\Serializer\Encoder\JsonDecode;
|
2015-11-16 10:02:30 +00:00
|
|
|
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
2015-11-18 08:21:04 +00:00
|
|
|
|
2017-02-18 17:42:54 +00:00
|
|
|
class JsonDecodeTest extends TestCase
|
2015-11-18 08:21:04 +00:00
|
|
|
{
|
|
|
|
/** @var \Symfony\Component\Serializer\Encoder\JsonDecode */
|
2015-11-16 10:02:30 +00:00
|
|
|
private $decode;
|
2015-11-18 08:21:04 +00:00
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
2015-11-16 10:02:30 +00:00
|
|
|
$this->decode = new JsonDecode();
|
2015-11-18 08:21:04 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 10:02:30 +00:00
|
|
|
public function testSupportsDecoding()
|
2015-11-18 08:21:04 +00:00
|
|
|
{
|
2015-11-16 10:02:30 +00:00
|
|
|
$this->assertTrue($this->decode->supportsDecoding(JsonEncoder::FORMAT));
|
|
|
|
$this->assertFalse($this->decode->supportsDecoding('foobar'));
|
2015-11-18 08:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-16 10:02:30 +00:00
|
|
|
* @dataProvider decodeProvider
|
2015-11-18 08:21:04 +00:00
|
|
|
*/
|
2015-11-16 10:02:30 +00:00
|
|
|
public function testDecode($toDecode, $expected, $context)
|
2015-11-18 08:21:04 +00:00
|
|
|
{
|
2015-11-16 10:02:30 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
$expected,
|
|
|
|
$this->decode->decode($toDecode, JsonEncoder::FORMAT, $context)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function decodeProvider()
|
|
|
|
{
|
|
|
|
$stdClass = new \stdClass();
|
|
|
|
$stdClass->foo = 'bar';
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$assoc = ['foo' => 'bar'];
|
2015-11-16 10:02:30 +00:00
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
return [
|
|
|
|
['{"foo": "bar"}', $stdClass, []],
|
|
|
|
['{"foo": "bar"}', $assoc, ['json_decode_associative' => true]],
|
|
|
|
];
|
2015-11-16 10:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider decodeProviderException
|
2016-09-24 10:47:20 +01:00
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
2015-11-16 10:02:30 +00:00
|
|
|
*/
|
|
|
|
public function testDecodeWithException($value)
|
|
|
|
{
|
2017-01-21 16:59:38 +00:00
|
|
|
$this->decode->decode($value, JsonEncoder::FORMAT);
|
2015-11-16 10:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function decodeProviderException()
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
return [
|
|
|
|
["{'foo': 'bar'}"],
|
|
|
|
['kaboom!'],
|
|
|
|
];
|
2015-11-18 08:21:04 +00:00
|
|
|
}
|
|
|
|
}
|