You are here

class JsonEncoderTest in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/serializer/Tests/Encoder/JsonEncoderTest.php \Symfony\Component\Serializer\Tests\Encoder\JsonEncoderTest
  2. 8 core/modules/serialization/tests/src/Unit/Encoder/JsonEncoderTest.php \Drupal\Tests\serialization\Unit\Encoder\JsonEncoderTest
Same name and namespace in other branches
  1. 8.0 vendor/symfony/serializer/Tests/Encoder/JsonEncoderTest.php \Symfony\Component\Serializer\Tests\Encoder\JsonEncoderTest

Hierarchy

  • class \Symfony\Component\Serializer\Tests\Encoder\JsonEncoderTest extends \Symfony\Component\Serializer\Tests\Encoder\PHPUnit_Framework_TestCase

Expanded class hierarchy of JsonEncoderTest

File

vendor/symfony/serializer/Tests/Encoder/JsonEncoderTest.php, line 18

Namespace

Symfony\Component\Serializer\Tests\Encoder
View source
class JsonEncoderTest extends \PHPUnit_Framework_TestCase {
  protected function setUp() {
    $this->encoder = new JsonEncoder();
    $this->serializer = new Serializer(array(
      new CustomNormalizer(),
    ), array(
      'json' => new JsonEncoder(),
    ));
  }
  public function testEncodeScalar() {
    $obj = new \stdClass();
    $obj->foo = 'foo';
    $expected = '{"foo":"foo"}';
    $this
      ->assertEquals($expected, $this->encoder
      ->encode($obj, 'json'));
  }
  public function testComplexObject() {
    $obj = $this
      ->getObject();
    $expected = $this
      ->getJsonSource();
    $this
      ->assertEquals($expected, $this->encoder
      ->encode($obj, 'json'));
  }
  public function testOptions() {
    $context = array(
      'json_encode_options' => JSON_NUMERIC_CHECK,
    );
    $arr = array();
    $arr['foo'] = '3';
    $expected = '{"foo":3}';
    $this
      ->assertEquals($expected, $this->serializer
      ->serialize($arr, 'json', $context));
    $arr = array();
    $arr['foo'] = '3';
    $expected = '{"foo":"3"}';
    $this
      ->assertEquals($expected, $this->serializer
      ->serialize($arr, 'json'), 'Context should not be persistent');
  }
  protected function getJsonSource() {
    return '{"foo":"foo","bar":["a","b"],"baz":{"key":"val","key2":"val","A B":"bar","item":[{"title":"title1"},{"title":"title2"}],"Barry":{"FooBar":{"Baz":"Ed","@id":1}}},"qux":"1"}';
  }
  protected function getObject() {
    $obj = new \stdClass();
    $obj->foo = 'foo';
    $obj->bar = array(
      'a',
      'b',
    );
    $obj->baz = array(
      'key' => 'val',
      'key2' => 'val',
      'A B' => 'bar',
      'item' => array(
        array(
          'title' => 'title1',
        ),
        array(
          'title' => 'title2',
        ),
      ),
      'Barry' => array(
        'FooBar' => array(
          'Baz' => 'Ed',
          '@id' => 1,
        ),
      ),
    );
    $obj->qux = '1';
    return $obj;
  }

}

Members