forked from jpfuentes2/php-activerecord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActiveRecordTest.php
575 lines (482 loc) · 16.4 KB
/
ActiveRecordTest.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?php
class ActiveRecordTest extends DatabaseTest
{
public function set_up($connection_name=null)
{
parent::set_up($connection_name);
$this->options = array('conditions' => 'blah', 'order' => 'blah');
}
public function test_options_is_not()
{
$this->assert_false(Author::is_options_hash(null));
$this->assert_false(Author::is_options_hash(''));
$this->assert_false(Author::is_options_hash('tito'));
$this->assert_false(Author::is_options_hash(array()));
$this->assert_false(Author::is_options_hash(array(1,2,3)));
}
/**
* @expectedException ActiveRecord\ActiveRecordException
*/
public function test_options_hash_with_unknown_keys() {
$this->assert_false(Author::is_options_hash(array('conditions' => 'blah', 'sharks' => 'laserz', 'dubya' => 'bush')));
}
public function test_options_is_hash()
{
$this->assert_true(Author::is_options_hash($this->options));
}
public function test_extract_and_validate_options() {
$args = array('first',$this->options);
$this->assert_equals($this->options,Author::extract_and_validate_options($args));
$this->assert_equals(array('first'),$args);
}
public function test_extract_and_validate_options_with_array_in_args() {
$args = array('first',array(1,2),$this->options);
$this->assert_equals($this->options,Author::extract_and_validate_options($args));
}
public function test_extract_and_validate_options_removes_options_hash() {
$args = array('first',$this->options);
Author::extract_and_validate_options($args);
$this->assert_equals(array('first'),$args);
}
public function test_extract_and_validate_options_nope() {
$args = array('first');
$this->assert_equals(array(),Author::extract_and_validate_options($args));
$this->assert_equals(array('first'),$args);
}
public function test_extract_and_validate_options_nope_because_wasnt_at_end() {
$args = array('first',$this->options,array(1,2));
$this->assert_equals(array(),Author::extract_and_validate_options($args));
}
/**
* @expectedException ActiveRecord\UndefinedPropertyException
*/
public function test_invalid_attribute()
{
$author = Author::find('first',array('conditions' => 'author_id=1'));
$author->some_invalid_field_name;
}
public function test_invalid_attributes()
{
$book = Book::find(1);
try {
$book->update_attributes(array('name' => 'new name', 'invalid_attribute' => true , 'another_invalid_attribute' => 'something'));
} catch (ActiveRecord\UndefinedPropertyException $e) {
$exceptions = explode("\r\n", $e->getMessage());
}
$this->assert_equals(1, substr_count($exceptions[0], 'invalid_attribute'));
$this->assert_equals(1, substr_count($exceptions[1], 'another_invalid_attribute'));
}
public function test_getter_undefined_property_exception_includes_model_name()
{
$this->assert_exception_message_contains("Author->this_better_not_exist",function()
{
$author = new Author();
$author->this_better_not_exist;
});
}
public function test_mass_assignment_undefined_property_exception_includes_model_name()
{
$this->assert_exception_message_contains("Author->this_better_not_exist",function()
{
new Author(array("this_better_not_exist" => "hi"));
});
}
public function test_setter_undefined_property_exception_includes_model_name()
{
$this->assert_exception_message_contains("Author->this_better_not_exist",function()
{
$author = new Author();
$author->this_better_not_exist = "hi";
});
}
public function test_get_values_for()
{
$book = Book::find_by_name('Ancient Art of Main Tanking');
$ret = $book->get_values_for(array('book_id','author_id'));
$this->assert_equals(array('book_id','author_id'),array_keys($ret));
$this->assert_equals(array(1,1),array_values($ret));
}
public function test_hyphenated_column_names_to_underscore()
{
if ($this->conn instanceof ActiveRecord\OciAdapter)
return;
$keys = array_keys(RmBldg::first()->attributes());
$this->assert_true(in_array('rm_name',$keys));
}
public function test_column_names_with_spaces()
{
if ($this->conn instanceof ActiveRecord\OciAdapter)
return;
$keys = array_keys(RmBldg::first()->attributes());
$this->assert_true(in_array('space_out',$keys));
}
public function test_mixed_case_column_name()
{
$keys = array_keys(Author::first()->attributes());
$this->assert_true(in_array('mixedcasefield',$keys));
}
public function test_mixed_case_primary_key_save()
{
$venue = Venue::find(1);
$venue->name = 'should not throw exception';
$venue->save();
$this->assert_equals($venue->name,Venue::find(1)->name);
}
public function test_reload()
{
$venue = Venue::find(1);
$this->assert_equals('NY', $venue->state);
$venue->state = 'VA';
$this->assert_equals('VA', $venue->state);
$venue->reload();
$this->assert_equals('NY', $venue->state);
}
public function test_reload_protected_attribute()
{
$book = BookAttrAccessible::find(1);
$book->name = "Should not stay";
$book->reload();
$this->assert_not_equals("Should not stay", $book->name);
}
public function test_active_record_model_home_not_set()
{
$home = ActiveRecord\Config::instance()->get_model_directory();
ActiveRecord\Config::instance()->set_model_directory(__FILE__);
$this->assert_equals(false,class_exists('TestAutoload'));
ActiveRecord\Config::instance()->set_model_directory($home);
}
public function test_auto_load_with_namespaced_model()
{
$this->assert_true(class_exists('NamespaceTest\Book'));
}
public function test_namespace_gets_stripped_from_table_name()
{
$model = new NamespaceTest\Book();
$this->assert_equals('books',$model->table()->table);
}
public function test_namespace_gets_stripped_from_inferred_foreign_key()
{
$model = new NamespaceTest\Book();
$table = ActiveRecord\Table::load(get_class($model));
$this->assert_equals($table->get_relationship('parent_book')->foreign_key[0], 'book_id');
$this->assert_equals($table->get_relationship('parent_book_2')->foreign_key[0], 'book_id');
$this->assert_equals($table->get_relationship('parent_book_3')->foreign_key[0], 'book_id');
}
public function test_namespaced_relationship_associates_correctly()
{
$model = new NamespaceTest\Book();
$table = ActiveRecord\Table::load(get_class($model));
$this->assert_not_null($table->get_relationship('parent_book'));
$this->assert_not_null($table->get_relationship('parent_book_2'));
$this->assert_not_null($table->get_relationship('parent_book_3'));
$this->assert_not_null($table->get_relationship('pages'));
$this->assert_not_null($table->get_relationship('pages_2'));
$this->assert_null($table->get_relationship('parent_book_4'));
$this->assert_null($table->get_relationship('pages_3'));
// Should refer to the same class
$this->assert_same(
ltrim($table->get_relationship('parent_book')->class_name, '\\'),
ltrim($table->get_relationship('parent_book_2')->class_name, '\\')
);
// Should refer to different classes
$this->assert_not_same(
ltrim($table->get_relationship('parent_book_2')->class_name, '\\'),
ltrim($table->get_relationship('parent_book_3')->class_name, '\\')
);
// Should refer to the same class
$this->assert_same(
ltrim($table->get_relationship('pages')->class_name, '\\'),
ltrim($table->get_relationship('pages_2')->class_name, '\\')
);
}
public function test_should_have_all_column_attributes_when_initializing_with_array()
{
$author = new Author(array('name' => 'Tito'));
$this->assert_true(count(array_keys($author->attributes())) >= 9);
}
public function test_defaults()
{
$author = new Author();
$this->assert_equals('default_name',$author->name);
}
public function test_alias_attribute_getter()
{
$venue = Venue::find(1);
$this->assert_equals($venue->marquee, $venue->name);
$this->assert_equals($venue->mycity, $venue->city);
}
public function test_alias_attribute_setter()
{
$venue = Venue::find(1);
$venue->marquee = 'new name';
$this->assert_equals($venue->marquee, 'new name');
$this->assert_equals($venue->marquee, $venue->name);
$venue->name = 'another name';
$this->assert_equals($venue->name, 'another name');
$this->assert_equals($venue->marquee, $venue->name);
}
public function test_alias_from_mass_attributes()
{
$venue = new Venue(array('marquee' => 'meme', 'id' => 123));
$this->assert_equals('meme',$venue->name);
$this->assert_equals($venue->marquee,$venue->name);
}
public function test_gh18_isset_on_aliased_attribute()
{
$this->assert_true(isset(Venue::first()->marquee));
}
public function test_attr_accessible()
{
$book = new BookAttrAccessible(array('name' => 'should not be set', 'author_id' => 1));
$this->assert_null($book->name);
$this->assert_equals(1,$book->author_id);
$book->name = 'test';
$this->assert_equals('test', $book->name);
}
public function test_attr_protected()
{
$book = new BookAttrAccessible(array('book_id' => 999));
$this->assert_null($book->book_id);
$book->book_id = 999;
$this->assert_equals(999, $book->book_id);
}
public function test_isset()
{
$book = new Book();
$this->assert_true(isset($book->name));
$this->assert_false(isset($book->sharks));
}
public function test_readonly_only_halt_on_write_method()
{
$book = Book::first(array('readonly' => true));
$this->assert_true($book->is_readonly());
try {
$book->save();
$this->fail('expected exception ActiveRecord\ReadonlyException');
} catch (ActiveRecord\ReadonlyException $e) {
}
$book->name = 'some new name';
$this->assert_equals($book->name, 'some new name');
}
public function test_cast_when_using_setter()
{
$book = new Book();
$book->book_id = '1';
$this->assert_same(1,$book->book_id);
}
public function test_cast_when_loading()
{
$book = Book::find(1);
$this->assert_same(1,$book->book_id);
$this->assert_same('Ancient Art of Main Tanking',$book->name);
}
public function test_cast_defaults()
{
$book = new Book();
$this->assert_same(0.0,$book->special);
}
public function test_transaction_committed()
{
$original = Author::count();
$ret = Author::transaction(function() { Author::create(array("name" => "blah")); });
$this->assert_equals($original+1,Author::count());
$this->assert_true($ret);
}
public function test_transaction_committed_when_returning_true()
{
$original = Author::count();
$ret = Author::transaction(function() { Author::create(array("name" => "blah")); return true; });
$this->assert_equals($original+1,Author::count());
$this->assert_true($ret);
}
public function test_transaction_rolledback_by_returning_false()
{
$original = Author::count();
$ret = Author::transaction(function()
{
Author::create(array("name" => "blah"));
return false;
});
$this->assert_equals($original,Author::count());
$this->assert_false($ret);
}
public function test_transaction_rolledback_by_throwing_exception()
{
$original = Author::count();
$exception = null;
try
{
Author::transaction(function()
{
Author::create(array("name" => "blah"));
throw new Exception("blah");
});
}
catch (Exception $e)
{
$exception = $e;
}
$this->assert_not_null($exception);
$this->assert_equals($original,Author::count());
}
public function test_delegate()
{
$event = Event::first();
$this->assert_equals($event->venue->state,$event->state);
$this->assert_equals($event->venue->address,$event->address);
}
public function test_delegate_prefix()
{
$event = Event::first();
$this->assert_equals($event->host->name,$event->woot_name);
}
public function test_delegate_returns_null_if_relationship_does_not_exist()
{
$event = new Event();
$this->assert_null($event->state);
}
public function test_delegate_set_attribute()
{
$event = Event::first();
$event->state = 'MEXICO';
$this->assert_equals('MEXICO',$event->venue->state);
}
public function test_delegate_getter_gh_98()
{
Venue::$use_custom_get_state_getter = true;
$event = Event::first();
$this->assert_equals('ny', $event->venue->state);
$this->assert_equals('ny', $event->state);
Venue::$use_custom_get_state_getter = false;
}
public function test_delegate_setter_gh_98()
{
Venue::$use_custom_set_state_setter = true;
$event = Event::first();
$event->state = 'MEXICO';
$this->assert_equals('MEXICO#',$event->venue->state);
Venue::$use_custom_set_state_setter = false;
}
public function test_table_name_with_underscores()
{
$this->assert_not_null(AwesomePerson::first());
}
public function test_model_should_default_as_new_record()
{
$author = new Author();
$this->assert_true($author->is_new_record());
}
public function test_setter()
{
$author = new Author();
$author->password = 'plaintext';
$this->assert_equals(md5('plaintext'),$author->encrypted_password);
}
public function test_setter_with_same_name_as_an_attribute()
{
$author = new Author();
$author->name = 'bob';
$this->assert_equals('BOB',$author->name);
}
public function test_getter()
{
$book = Book::first();
$this->assert_equals(strtoupper($book->name), $book->upper_name);
}
public function test_getter_with_same_name_as_an_attribute()
{
Book::$use_custom_get_name_getter = true;
$book = new Book;
$book->name = 'bob';
$this->assert_equals('BOB', $book->name);
Book::$use_custom_get_name_getter = false;
}
public function test_setting_invalid_date_should_set_date_to_null()
{
$author = new Author();
$author->created_at = 'CURRENT_TIMESTAMP';
$this->assertNull($author->created_at);
}
public function test_table_name()
{
$this->assert_equals('authors',Author::table_name());
}
/**
* @expectedException ActiveRecord\ActiveRecordException
*/
public function test_undefined_instance_method()
{
Author::first()->find_by_name('sdf');
}
public function test_clear_cache_for_specific_class()
{
$book_table1 = ActiveRecord\Table::load('Book');
$book_table2 = ActiveRecord\Table::load('Book');
ActiveRecord\Table::clear_cache('Book');
$book_table3 = ActiveRecord\Table::load('Book');
$this->assert_true($book_table1 === $book_table2);
$this->assert_true($book_table1 !== $book_table3);
}
public function test_flag_dirty()
{
$author = new Author();
$author->flag_dirty('some_date');
$this->assert_has_keys('some_date', $author->dirty_attributes());
$this->assert_true($author->attribute_is_dirty('some_date'));
$author->save();
$this->assert_false($author->attribute_is_dirty('some_date'));
}
public function test_flag_dirty_attribute_which_does_not_exit()
{
$author = new Author();
$author->flag_dirty('some_inexistant_property');
$this->assert_null($author->dirty_attributes());
$this->assert_false($author->attribute_is_dirty('some_inexistant_property'));
}
public function test_gh245_dirty_attribute_should_not_raise_php_notice_if_not_dirty()
{
$event = new Event(array('title' => "Fun"));
$this->assert_false($event->attribute_is_dirty('description'));
$this->assert_true($event->attribute_is_dirty('title'));
}
public function test_assigning_php_datetime_gets_converted_to_date_class_with_defaults()
{
$author = new Author();
$author->created_at = $now = new \DateTime();
$this->assert_is_a("ActiveRecord\\DateTime", $author->created_at);
$this->assert_datetime_equals($now,$author->created_at);
}
public function test_assigning_php_datetime_gets_converted_to_date_class_with_custom_date_class()
{
ActiveRecord\Config::instance()->set_date_class('\\DateTime'); // use PHP built-in DateTime
$author = new Author();
$author->created_at = $now = new \DateTime();
$this->assert_is_a("DateTime", $author->created_at);
$this->assert_datetime_equals($now,$author->created_at);
}
public function test_assigning_from_mass_assignment_php_datetime_gets_converted_to_ar_datetime()
{
$author = new Author(array('created_at' => new \DateTime()));
$this->assert_is_a("ActiveRecord\\DateTime",$author->created_at);
}
public function test_get_real_attribute_name()
{
$venue = new Venue();
$this->assert_equals('name', $venue->get_real_attribute_name('name'));
$this->assert_equals('name', $venue->get_real_attribute_name('marquee'));
$this->assert_equals(null, $venue->get_real_attribute_name('invalid_field'));
}
public function test_id_setter_works_with_table_without_pk_named_attribute()
{
$author = new Author(array('id' => 123));
$this->assert_equals(123,$author->author_id);
}
public function test_query()
{
$row = Author::query('SELECT COUNT(*) AS n FROM authors',null)->fetch();
$this->assert_true($row['n'] > 1);
$row = Author::query('SELECT COUNT(*) AS n FROM authors WHERE name=?',array('Tito'))->fetch();
$this->assert_equals(array('n' => 1), $row);
}
};
?>