SerializationTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. include 'helpers/config.php';
  3. require '../lib/Serialization.php';
  4. use ActiveRecord\DateTime;
  5. class SerializationTest extends DatabaseTest
  6. {
  7. public function tear_down()
  8. {
  9. parent::tear_down();
  10. ActiveRecord\ArraySerializer::$include_root = false;
  11. ActiveRecord\JsonSerializer::$include_root = false;
  12. }
  13. public function _a($options=array(), $model=null)
  14. {
  15. if (!$model)
  16. $model = Book::find(1);
  17. $s = new ActiveRecord\JsonSerializer($model,$options);
  18. return $s->to_a();
  19. }
  20. public function test_only()
  21. {
  22. $this->assert_has_keys('name', 'special', $this->_a(array('only' => array('name', 'special'))));
  23. }
  24. public function test_only_not_array()
  25. {
  26. $this->assert_has_keys('name', $this->_a(array('only' => 'name')));
  27. }
  28. public function test_only_should_only_apply_to_attributes()
  29. {
  30. $this->assert_has_keys('name','author', $this->_a(array('only' => 'name', 'include' => 'author')));
  31. $this->assert_has_keys('book_id','upper_name', $this->_a(array('only' => 'book_id', 'methods' => 'upper_name')));
  32. }
  33. public function test_only_overrides_except()
  34. {
  35. $this->assert_has_keys('name', $this->_a(array('only' => 'name', 'except' => 'name')));
  36. }
  37. public function test_except()
  38. {
  39. $this->assert_doesnt_has_keys('name', 'special', $this->_a(array('except' => array('name','special'))));
  40. }
  41. public function test_except_takes_a_string()
  42. {
  43. $this->assert_doesnt_has_keys('name', $this->_a(array('except' => 'name')));
  44. }
  45. public function test_methods()
  46. {
  47. $a = $this->_a(array('methods' => array('upper_name')));
  48. $this->assert_equals('ANCIENT ART OF MAIN TANKING', $a['upper_name']);
  49. }
  50. public function test_methods_takes_a_string()
  51. {
  52. $a = $this->_a(array('methods' => 'upper_name'));
  53. $this->assert_equals('ANCIENT ART OF MAIN TANKING', $a['upper_name']);
  54. }
  55. // methods added last should we shuld have value of the method in our json
  56. // rather than the regular attribute value
  57. public function test_methods_method_same_as_attribute()
  58. {
  59. $a = $this->_a(array('methods' => 'name'));
  60. $this->assert_equals('ancient art of main tanking', $a['name']);
  61. }
  62. public function test_include()
  63. {
  64. $a = $this->_a(array('include' => array('author')));
  65. $this->assert_has_keys('parent_author_id', $a['author']);
  66. }
  67. public function test_include_nested_with_nested_options()
  68. {
  69. $a = $this->_a(
  70. array('include' => array('events' => array('except' => 'title', 'include' => array('host' => array('only' => 'id'))))),
  71. Host::find(4));
  72. $this->assert_equals(3, count($a['events']));
  73. $this->assert_doesnt_has_keys('title', $a['events'][0]);
  74. $this->assert_equals(array('id' => 4), $a['events'][0]['host']);
  75. }
  76. public function test_datetime_values_get_converted_to_strings()
  77. {
  78. $now = new DateTime();
  79. $a = $this->_a(array('only' => 'created_at'),new Author(array('created_at' => $now)));
  80. $this->assert_equals($now->format(ActiveRecord\Serialization::$DATETIME_FORMAT),$a['created_at']);
  81. }
  82. public function test_to_json()
  83. {
  84. $book = Book::find(1);
  85. $json = $book->to_json();
  86. $this->assert_equals($book->attributes(),(array)json_decode($json));
  87. }
  88. public function test_to_json_include_root()
  89. {
  90. ActiveRecord\JsonSerializer::$include_root = true;
  91. $this->assert_not_null(json_decode(Book::find(1)->to_json())->book);
  92. }
  93. public function test_to_xml_include()
  94. {
  95. $xml = Host::find(4)->to_xml(array('include' => 'events'));
  96. $decoded = get_object_vars(new SimpleXMLElement($xml));
  97. $this->assert_equals(3, count($decoded['events']->event));
  98. }
  99. public function test_to_xml()
  100. {
  101. $book = Book::find(1);
  102. $this->assert_equals($book->attributes(),get_object_vars(new SimpleXMLElement($book->to_xml())));
  103. }
  104. public function test_to_array()
  105. {
  106. $book = Book::find(1);
  107. $array = $book->to_array();
  108. $this->assert_equals($book->attributes(), $array);
  109. }
  110. public function test_to_array_include_root()
  111. {
  112. ActiveRecord\ArraySerializer::$include_root = true;
  113. $book = Book::find(1);
  114. $array = $book->to_array();
  115. $book_attributes = array('book' => $book->attributes());
  116. $this->assert_equals($book_attributes, $array);
  117. }
  118. public function test_to_array_except()
  119. {
  120. $book = Book::find(1);
  121. $array = $book->to_array(array('except' => array('special')));
  122. $book_attributes = $book->attributes();
  123. unset($book_attributes['special']);
  124. $this->assert_equals($book_attributes, $array);
  125. }
  126. public function test_works_with_datetime()
  127. {
  128. Author::find(1)->update_attribute('created_at',new DateTime());
  129. $this->assert_reg_exp('/<updated_at>[0-9]{4}-[0-9]{2}-[0-9]{2}/',Author::find(1)->to_xml());
  130. $this->assert_reg_exp('/"updated_at":"[0-9]{4}-[0-9]{2}-[0-9]{2}/',Author::find(1)->to_json());
  131. }
  132. public function test_to_xml_skip_instruct()
  133. {
  134. $this->assert_same(false,strpos(Book::find(1)->to_xml(array('skip_instruct' => true)),'<?xml version'));
  135. $this->assert_same(0, strpos(Book::find(1)->to_xml(array('skip_instruct' => false)),'<?xml version'));
  136. }
  137. public function test_only_method()
  138. {
  139. $this->assert_contains('<sharks>lasers</sharks>', Author::first()->to_xml(array('only_method' => 'return_something')));
  140. }
  141. public function test_to_csv()
  142. {
  143. $book = Book::find(1);
  144. $this->assert_equals('1,1,2,"Ancient Art of Main Tanking",0,0',$book->to_csv());
  145. }
  146. public function test_to_csv_only_header()
  147. {
  148. $book = Book::find(1);
  149. $this->assert_equals('book_id,author_id,secondary_author_id,name,numeric_test,special',
  150. $book->to_csv(array('only_header'=>true))
  151. );
  152. }
  153. public function test_to_csv_only_method()
  154. {
  155. $book = Book::find(1);
  156. $this->assert_equals('2,"Ancient Art of Main Tanking"',
  157. $book->to_csv(array('only'=>array('name','secondary_author_id')))
  158. );
  159. }
  160. public function test_to_csv_only_method_on_header()
  161. {
  162. $book = Book::find(1);
  163. $this->assert_equals('secondary_author_id,name',
  164. $book->to_csv(array('only'=>array('secondary_author_id','name'),
  165. 'only_header'=>true))
  166. );
  167. }
  168. public function test_to_csv_with_custom_delimiter()
  169. {
  170. $book = Book::find(1);
  171. ActiveRecord\CsvSerializer::$delimiter=';';
  172. $this->assert_equals('1;1;2;"Ancient Art of Main Tanking";0;0',$book->to_csv());
  173. }
  174. public function test_to_csv_with_custom_enclosure()
  175. {
  176. $book = Book::find(1);
  177. ActiveRecord\CsvSerializer::$delimiter=',';
  178. ActiveRecord\CsvSerializer::$enclosure="'";
  179. $this->assert_equals("1,1,2,'Ancient Art of Main Tanking',0,0",$book->to_csv());
  180. }
  181. };
  182. ?>