JsonSerializationTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Tests\Serializer;
  18. use JMS\SerializerBundle\Exception\RuntimeException;
  19. use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
  20. class JsonSerializationTest extends BaseSerializationTest
  21. {
  22. protected function getContent($key)
  23. {
  24. static $outputs = array();
  25. if (!$outputs) {
  26. $outputs['string'] = '"foo"';
  27. $outputs['boolean_true'] = 'true';
  28. $outputs['boolean_false'] = 'false';
  29. $outputs['integer'] = '1';
  30. $outputs['float'] = '4.533';
  31. $outputs['float_trailing_zero'] = '1';
  32. $outputs['simple_object'] = '{"foo":"foo","moo":"bar","camel_case":"boo"}';
  33. $outputs['circular_reference'] = '{"collection":[{"name":"child1"},{"name":"child2"}],"another_collection":[{"name":"child1"},{"name":"child2"}]}';
  34. $outputs['array_strings'] = '["foo","bar"]';
  35. $outputs['array_booleans'] = '[true,false]';
  36. $outputs['array_integers'] = '[1,3,4]';
  37. $outputs['array_floats'] = '[1.34,3,6.42]';
  38. $outputs['array_objects'] = '[{"foo":"foo","moo":"bar","camel_case":"boo"},{"foo":"baz","moo":"boo","camel_case":"boo"}]';
  39. $outputs['array_mixed'] = '["foo",1,true,{"foo":"foo","moo":"bar","camel_case":"boo"},[1,3,true]]';
  40. $outputs['blog_post'] = '{"title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"comments":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"author":{"full_name":"Foo Bar"}}';
  41. $outputs['price'] = '"3"';
  42. $outputs['log'] = '{"author_list":[{"full_name":"Johannes Schmitt"},{"full_name":"John Doe"}],"comments":[{"author":{"full_name":"Foo Bar"},"text":"foo"},{"author":{"full_name":"Foo Bar"},"text":"bar"},{"author":{"full_name":"Foo Bar"},"text":"baz"}]}';
  43. $outputs['lifecycle_callbacks'] = '{"name":"Foo Bar"}';
  44. $outputs['form_errors'] = '["This is the form error","Another error"]';
  45. $outputs['nested_form_errors'] = '{"errors":["This is the form error"],"children":{"bar":{"errors":["Error of the child form"]}}}';
  46. $outputs['constraint_violation'] = '{"property_path":"foo","message":"Message of violation"}';
  47. $outputs['constraint_violation_list'] = '[{"property_path":"foo","message":"Message of violation"},{"property_path":"bar","message":"Message of another violation"}]';
  48. $outputs['article'] = '{"custom":"serialized"}';
  49. }
  50. if (!isset($outputs[$key])) {
  51. throw new RuntimeException(sprintf('The key "%s" is not supported.', $key));
  52. }
  53. return $outputs[$key];
  54. }
  55. protected function getFormat()
  56. {
  57. return 'json';
  58. }
  59. }