JsonDecodeTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Tests\Encoder;
  11. use Symfony\Component\Serializer\Encoder\JsonDecode;
  12. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  13. class JsonDecodeTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /** @var \Symfony\Component\Serializer\Encoder\JsonDecode */
  16. private $decode;
  17. protected function setUp()
  18. {
  19. $this->decode = new JsonDecode();
  20. }
  21. public function testSupportsDecoding()
  22. {
  23. $this->assertTrue($this->decode->supportsDecoding(JsonEncoder::FORMAT));
  24. $this->assertFalse($this->decode->supportsDecoding('foobar'));
  25. }
  26. /**
  27. * @dataProvider decodeProvider
  28. */
  29. public function testDecode($toDecode, $expected, $context)
  30. {
  31. $this->assertEquals(
  32. $expected,
  33. $this->decode->decode($toDecode, JsonEncoder::FORMAT, $context)
  34. );
  35. }
  36. public function decodeProvider()
  37. {
  38. $stdClass = new \stdClass();
  39. $stdClass->foo = 'bar';
  40. $assoc = array('foo' => 'bar');
  41. return array(
  42. array('{"foo": "bar"}', $stdClass, array()),
  43. array('{"foo": "bar"}', $assoc, array('json_decode_associative' => true)),
  44. );
  45. }
  46. /**
  47. * @requires function json_last_error_msg
  48. * @dataProvider decodeProviderException
  49. * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
  50. */
  51. public function testDecodeWithException($value)
  52. {
  53. $this->decode->decode($value, JsonEncoder::FORMAT);
  54. }
  55. public function decodeProviderException()
  56. {
  57. return array(
  58. array("{'foo': 'bar'}"),
  59. array('kaboom!'),
  60. );
  61. }
  62. }