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