forked from jpfuentes2/php-activerecord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActiveRecordWriteTest.php
444 lines (373 loc) · 12 KB
/
ActiveRecordWriteTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
use ActiveRecord\DateTime;
class DirtyAuthor extends ActiveRecord\Model
{
static $table = 'authors';
static $before_save = 'before_save';
public function before_save()
{
$this->name = 'i saved';
}
};
class AuthorWithoutSequence extends ActiveRecord\Model
{
static $table = 'authors';
static $sequence = 'invalid_seq';
}
class AuthorExplicitSequence extends ActiveRecord\Model
{
static $sequence = 'blah_seq';
}
class ActiveRecordWriteTest extends DatabaseTest
{
private function make_new_book_and($save=true)
{
$book = new Book();
$book->name = 'rivers cuomo';
$book->special = 1;
if ($save)
$book->save();
return $book;
}
public function test_save()
{
$venue = new Venue(array('name' => 'Tito'));
$venue->save();
}
public function test_insert()
{
$author = new Author(array('name' => 'Blah Blah'));
$author->save();
$this->assert_not_null(Author::find($author->id));
}
/**
* @expectedException ActiveRecord\DatabaseException
*/
public function test_insert_with_no_sequence_defined()
{
if (!$this->conn->supports_sequences())
throw new ActiveRecord\DatabaseException('');
AuthorWithoutSequence::create(array('name' => 'Bob!'));
}
public function test_insert_should_quote_keys()
{
$author = new Author(array('name' => 'Blah Blah'));
$author->save();
$this->assert_true(strpos($author->connection()->last_query,$author->connection()->quote_name('updated_at')) !== false);
}
public function test_save_auto_increment_id()
{
$venue = new Venue(array('name' => 'Bob'));
$venue->save();
$this->assert_true($venue->id > 0);
}
public function test_sequence_was_set()
{
if ($this->conn->supports_sequences())
$this->assert_equals($this->conn->get_sequence_name('authors','author_id'),Author::table()->sequence);
else
$this->assert_null(Author::table()->sequence);
}
public function test_sequence_was_explicitly_set()
{
if ($this->conn->supports_sequences())
$this->assert_equals(AuthorExplicitSequence::$sequence,AuthorExplicitSequence::table()->sequence);
else
$this->assert_null(Author::table()->sequence);
}
public function test_delete()
{
$author = Author::find(1);
$author->delete();
$this->assert_false(Author::exists(1));
}
public function test_delete_by_find_all()
{
$books = Book::all();
foreach ($books as $model)
$model->delete();
$res = Book::all();
$this->assert_equals(0,count($res));
}
public function test_update()
{
$book = Book::find(1);
$new_name = 'new name';
$book->name = $new_name;
$book->save();
$this->assert_same($new_name, $book->name);
$this->assert_same($new_name, $book->name, Book::find(1)->name);
}
public function test_update_should_quote_keys()
{
$book = Book::find(1);
$book->name = 'new name';
$book->save();
$this->assert_true(strpos($book->connection()->last_query,$book->connection()->quote_name('name')) !== false);
}
public function test_update_attributes()
{
$book = Book::find(1);
$new_name = 'How to lose friends and alienate people'; // jax i'm worried about you
$attrs = array('name' => $new_name);
$book->update_attributes($attrs);
$this->assert_same($new_name, $book->name);
$this->assert_same($new_name, $book->name, Book::find(1)->name);
}
/**
* @expectedException ActiveRecord\UndefinedPropertyException
*/
public function test_update_attributes_undefined_property()
{
$book = Book::find(1);
$book->update_attributes(array('name' => 'new name', 'invalid_attribute' => true , 'another_invalid_attribute' => 'blah'));
}
public function test_update_attribute()
{
$book = Book::find(1);
$new_name = 'some stupid self-help book';
$book->update_attribute('name', $new_name);
$this->assert_same($new_name, $book->name);
$this->assert_same($new_name, $book->name, Book::find(1)->name);
}
/**
* @expectedException ActiveRecord\UndefinedPropertyException
*/
public function test_update_attribute_undefined_property()
{
$book = Book::find(1);
$book->update_attribute('invalid_attribute', true);
}
public function test_save_null_value()
{
$book = Book::first();
$book->name = null;
$book->save();
$this->assert_same(null,Book::find($book->id)->name);
}
public function test_save_blank_value()
{
// oracle doesn't do blanks. probably an option to enable?
if ($this->conn instanceof ActiveRecord\OciAdapter)
return;
$book = Book::find(1);
$book->name = '';
$book->save();
$this->assert_same('',Book::find(1)->name);
}
public function test_dirty_attributes()
{
$book = $this->make_new_book_and(false);
$this->assert_equals(array('name','special'),array_keys($book->dirty_attributes()));
}
public function test_dirty_attributes_cleared_after_saving()
{
$book = $this->make_new_book_and();
$this->assert_true(strpos($book->table()->last_sql,'name') !== false);
$this->assert_true(strpos($book->table()->last_sql,'special') !== false);
$this->assert_equals(null,$book->dirty_attributes());
}
public function test_dirty_attributes_cleared_after_inserting()
{
$book = $this->make_new_book_and();
$this->assert_equals(null,$book->dirty_attributes());
}
public function test_no_dirty_attributes_but_still_insert_record()
{
$book = new Book;
$this->assert_equals(null,$book->dirty_attributes());
$book->save();
$this->assert_equals(null,$book->dirty_attributes());
$this->assert_not_null($book->id);
}
public function test_dirty_attributes_cleared_after_updating()
{
$book = Book::first();
$book->name = 'rivers cuomo';
$book->save();
$this->assert_equals(null,$book->dirty_attributes());
}
public function test_dirty_attributes_after_reloading()
{
$book = Book::first();
$book->name = 'rivers cuomo';
$book->reload();
$this->assert_equals(null,$book->dirty_attributes());
}
public function test_dirty_attributes_with_mass_assignment()
{
$book = Book::first();
$book->set_attributes(array('name' => 'rivers cuomo'));
$this->assert_equals(array('name'), array_keys($book->dirty_attributes()));
}
public function test_timestamps_set_before_save()
{
$author = new Author;
$author->save();
$this->assert_not_null($author->created_at, $author->updated_at);
$author->reload();
$this->assert_not_null($author->created_at, $author->updated_at);
}
public function test_timestamps_updated_at_only_set_before_update()
{
$author = new Author();
$author->save();
$created_at = $author->created_at;
$updated_at = $author->updated_at;
sleep(1);
$author->name = 'test';
$author->save();
$this->assert_not_null($author->updated_at);
$this->assert_same($created_at, $author->created_at);
$this->assert_not_equals($updated_at, $author->updated_at);
}
public function test_create()
{
$author = Author::create(array('name' => 'Blah Blah'));
$this->assert_not_null(Author::find($author->id));
}
public function test_create_should_set_created_at()
{
$author = Author::create(array('name' => 'Blah Blah'));
$this->assert_not_null($author->created_at);
}
/**
* @expectedException ActiveRecord\ActiveRecordException
*/
public function test_update_with_no_primary_key_defined()
{
Author::table()->pk = array();
$author = Author::first();
$author->name = 'blahhhhhhhhhh';
$author->save();
}
/**
* @expectedException ActiveRecord\ActiveRecordException
*/
public function test_delete_with_no_primary_key_defined()
{
Author::table()->pk = array();
$author = author::first();
$author->delete();
}
public function test_inserting_with_explicit_pk()
{
$author = Author::create(array('author_id' => 9999, 'name' => 'blah'));
$this->assert_equals(9999,$author->author_id);
}
/**
* @expectedException ActiveRecord\ReadOnlyException
*/
public function test_readonly()
{
$author = Author::first(array('readonly' => true));
$author->save();
}
public function test_modified_attributes_in_before_handlers_get_saved()
{
$author = DirtyAuthor::first();
$author->encrypted_password = 'coco';
$author->save();
$this->assert_equals('i saved',DirtyAuthor::find($author->id)->name);
}
public function test_is_dirty()
{
$author = Author::first();
$this->assert_equals(false,$author->is_dirty());
$author->name = 'coco';
$this->assert_equals(true,$author->is_dirty());
}
public function test_set_date_flags_dirty()
{
$author = Author::create(array('some_date' => new DateTime()));
$author = Author::find($author->id);
$author->some_date->setDate(2010,1,1);
$this->assert_has_keys('some_date', $author->dirty_attributes());
}
public function test_set_date_flags_dirty_with_php_datetime()
{
$author = Author::create(array('some_date' => new \DateTime()));
$author = Author::find($author->id);
$author->some_date->setDate(2010,1,1);
$this->assert_has_keys('some_date', $author->dirty_attributes());
}
public function test_delete_all_with_conditions_as_string()
{
$num_affected = Author::delete_all(array('conditions' => 'parent_author_id = 2'));
$this->assert_equals(2, $num_affected);
}
public function test_delete_all_with_conditions_as_hash()
{
$num_affected = Author::delete_all(array('conditions' => array('parent_author_id' => 2)));
$this->assert_equals(2, $num_affected);
}
public function test_delete_all_with_conditions_as_array()
{
$num_affected = Author::delete_all(array('conditions' => array('parent_author_id = ?', 2)));
$this->assert_equals(2, $num_affected);
}
public function test_delete_all_with_limit_and_order()
{
if (!$this->conn->accepts_limit_and_order_for_update_and_delete())
$this->mark_test_skipped('Only MySQL & Sqlite accept limit/order with UPDATE clause');
$num_affected = Author::delete_all(array('conditions' => array('parent_author_id = ?', 2), 'limit' => 1, 'order' => 'name asc'));
$this->assert_equals(1, $num_affected);
$this->assert_true(strpos(Author::table()->last_sql, 'ORDER BY name asc LIMIT 1') !== false);
}
public function test_update_all_with_set_as_string()
{
$num_affected = Author::update_all(array('set' => 'parent_author_id = 2'));
$this->assert_equals(2, $num_affected);
$this->assert_equals(4, Author::count_by_parent_author_id(2));
}
public function test_update_all_with_set_as_hash()
{
$num_affected = Author::update_all(array('set' => array('parent_author_id' => 2)));
$this->assert_equals(2, $num_affected);
}
/**
* TODO: not implemented
public function test_update_all_with_set_as_array()
{
$num_affected = Author::update_all(array('set' => array('parent_author_id = ?', 2)));
$this->assert_equals(2, $num_affected);
}
*/
public function test_update_all_with_conditions_as_string()
{
$num_affected = Author::update_all(array('set' => 'parent_author_id = 2', 'conditions' => 'name = "Tito"'));
$this->assert_equals(1, $num_affected);
}
public function test_update_all_with_conditions_as_hash()
{
$num_affected = Author::update_all(array('set' => 'parent_author_id = 2', 'conditions' => array('name' => "Tito")));
$this->assert_equals(1, $num_affected);
}
public function test_update_all_with_conditions_as_array()
{
$num_affected = Author::update_all(array('set' => 'parent_author_id = 2', 'conditions' => array('name = ?', "Tito")));
$this->assert_equals(1, $num_affected);
}
public function test_update_all_with_limit_and_order()
{
if (!$this->conn->accepts_limit_and_order_for_update_and_delete())
$this->mark_test_skipped('Only MySQL & Sqlite accept limit/order with UPDATE clause');
$num_affected = Author::update_all(array('set' => 'parent_author_id = 2', 'limit' => 1, 'order' => 'name asc'));
$this->assert_equals(1, $num_affected);
$this->assert_true(strpos(Author::table()->last_sql, 'ORDER BY name asc LIMIT 1') !== false);
}
public function test_update_native_datetime()
{
$author = Author::create(array('name' => 'Blah Blah'));
$native_datetime = new \DateTime('1983-12-05');
$author->some_date = $native_datetime;
$this->assert_false($native_datetime === $author->some_date);
}
public function test_update_our_datetime()
{
$author = Author::create(array('name' => 'Blah Blah'));
$our_datetime = new DateTime('1983-12-05');
$author->some_date = $our_datetime;
$this->assert_true($our_datetime === $author->some_date);
}
};