JsonSerializationTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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['readonly'] = '{"id":123,"full_name":"Ruud Kamphuis"}';
  27. $outputs['string'] = '"foo"';
  28. $outputs['boolean_true'] = 'true';
  29. $outputs['boolean_false'] = 'false';
  30. $outputs['integer'] = '1';
  31. $outputs['float'] = '4.533';
  32. $outputs['float_trailing_zero'] = '1';
  33. $outputs['simple_object'] = '{"foo":"foo","moo":"bar","camel_case":"boo"}';
  34. $outputs['circular_reference'] = '{"collection":[{"name":"child1"},{"name":"child2"}],"another_collection":[{"name":"child1"},{"name":"child2"}]}';
  35. $outputs['array_strings'] = '["foo","bar"]';
  36. $outputs['array_booleans'] = '[true,false]';
  37. $outputs['array_integers'] = '[1,3,4]';
  38. $outputs['array_floats'] = '[1.34,3,6.42]';
  39. $outputs['array_objects'] = '[{"foo":"foo","moo":"bar","camel_case":"boo"},{"foo":"baz","moo":"boo","camel_case":"boo"}]';
  40. $outputs['array_mixed'] = '["foo",1,true,{"foo":"foo","moo":"bar","camel_case":"boo"},[1,3,true]]';
  41. $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"}}';
  42. $outputs['price'] = '{"price":3}';
  43. $outputs['currency_aware_price'] = '{"currency":"EUR","amount":2.34}';
  44. $outputs['order'] = '{"cost":{"price":12.34}}';
  45. $outputs['order_with_currency_aware_price'] = '{"cost":{"currency":"EUR","amount":1.23}}';
  46. $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"}]}';
  47. $outputs['lifecycle_callbacks'] = '{"name":"Foo Bar"}';
  48. $outputs['form_errors'] = '["This is the form error","Another error"]';
  49. $outputs['nested_form_errors'] = '{"errors":["This is the form error"],"children":{"bar":{"errors":["Error of the child form"]}}}';
  50. $outputs['constraint_violation'] = '{"property_path":"foo","message":"Message of violation"}';
  51. $outputs['constraint_violation_list'] = '[{"property_path":"foo","message":"Message of violation"},{"property_path":"bar","message":"Message of another violation"}]';
  52. $outputs['article'] = '{"custom":"serialized"}';
  53. $outputs['orm_proxy'] = '{"foo":"foo","moo":"bar","camel_case":"proxy-boo"}';
  54. $outputs['custom_accessor'] = '{"comments":{"Foo":{"comments":[{"author":{"full_name":"Foo"},"text":"foo"},{"author":{"full_name":"Foo"},"text":"bar"}],"count":2}}}';
  55. $outputs['mixed_access_types'] = '{"id":1,"name":"Johannes","read_only_property":42}';
  56. $outputs['accessor_order_child'] = '{"c":"c","d":"d","a":"a","b":"b"}';
  57. $outputs['accessor_order_parent'] = '{"a":"a","b":"b"}';
  58. $outputs['inline'] = '{"c":"c","a":"a","b":"b","d":"d"}';
  59. $outputs['groups_all'] = '{"foo":"foo","foobar":"foobar","bar":"bar","none":"none"}';
  60. $outputs['groups_foo'] = '{"foo":"foo","foobar":"foobar"}';
  61. $outputs['groups_foobar'] = '{"foo":"foo","foobar":"foobar","bar":"bar"}';
  62. $outputs['virtual_properties'] = '{"exist_field":"value","virtual_value":"value","test":"other-name"}';
  63. $outputs['virtual_properties_low'] = '{"low":1}';
  64. $outputs['virtual_properties_high'] = '{"high":8}';
  65. $outputs['virtual_properties_all'] = '{"low":1,"high":8}';
  66. }
  67. if (!isset($outputs[$key])) {
  68. throw new RuntimeException(sprintf('The key "%s" is not supported.', $key));
  69. }
  70. return $outputs[$key];
  71. }
  72. protected function getFormat()
  73. {
  74. return 'json';
  75. }
  76. }