This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Serializer/Tests/Encoder/JsonDecodeTest.php

75 lines
1.8 KiB
PHP
Raw Normal View History

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