JsonEncodeTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\JsonEncode;
  12. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  13. class JsonEncodeTest extends \PHPUnit_Framework_TestCase
  14. {
  15. private $encoder;
  16. protected function setUp()
  17. {
  18. $this->encode = new JsonEncode();
  19. }
  20. public function testSupportsEncoding()
  21. {
  22. $this->assertTrue($this->encode->supportsEncoding(JsonEncoder::FORMAT));
  23. $this->assertFalse($this->encode->supportsEncoding('foobar'));
  24. }
  25. /**
  26. * @dataProvider encodeProvider
  27. */
  28. public function testEncode($toEncode, $expected, $context)
  29. {
  30. $this->assertEquals(
  31. $expected,
  32. $this->encode->encode($toEncode, JsonEncoder::FORMAT, $context)
  33. );
  34. }
  35. public function encodeProvider()
  36. {
  37. return array(
  38. array(array(), '[]', array()),
  39. array(array(), '{}', array('json_encode_options' => JSON_FORCE_OBJECT)),
  40. );
  41. }
  42. /**
  43. * @requires function json_last_error_msg
  44. * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
  45. */
  46. public function testEncodeWithError()
  47. {
  48. $this->encode->encode("\xB1\x31", JsonEncoder::FORMAT);
  49. }
  50. }