/
BookstoreTest.php
732 lines (580 loc) · 27 KB
/
BookstoreTest.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<?php
/**
* This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\Tests;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Tests\Bookstore\Author;
use Propel\Tests\Bookstore\AuthorQuery;
use Propel\Tests\Bookstore\Book;
use Propel\Tests\Bookstore\BookQuery;
use Propel\Tests\Bookstore\BookClubList;
use Propel\Tests\Bookstore\BookListRelQuery;
use Propel\Tests\Bookstore\BookClubListQuery;
use Propel\Tests\Bookstore\BookListRel;
use Propel\Tests\Bookstore\Publisher;
use Propel\Tests\Bookstore\PublisherQuery;
use Propel\Tests\Bookstore\Map\AuthorTableMap;
use Propel\Tests\Bookstore\Map\BookTableMap;
use Propel\Tests\Bookstore\Map\BookClubListTableMap;
use Propel\Tests\Bookstore\Map\PublisherTableMap;
use Propel\Tests\Bookstore\Media;
use Propel\Tests\Bookstore\MediaQuery;
use Propel\Tests\Bookstore\Review;
use Propel\Tests\Bookstore\ReviewQuery;
use Propel\Tests\Helpers\Bookstore\BookstoreEmptyTestBase;
use \DateTime;
/**
* Tests a functional scenario using the Bookstore model
*
* @author Francois Zaninotto
* @author Hans Lellelid <hans@xmpl.org>
*
* @group database
*/
class BookstoreTest extends BookstoreEmptyTestBase
{
public function testScenario()
{
// Add publisher records
// ---------------------
try {
$scholastic = new Publisher();
$scholastic->setName("Scholastic");
// do not save, will do later to test cascade
$morrow = new Publisher();
$morrow->setName("William Morrow");
$morrow->save();
$morrow_id = $morrow->getId();
$penguin = new Publisher();
$penguin->setName("Penguin");
$penguin->save();
$penguin_id = $penguin->getId();
$vintage = new Publisher();
$vintage->setName("Vintage");
$vintage->save();
$vintage_id = $vintage->getId();
$this->assertTrue(true, 'Save Publisher records');
} catch (Exception $e) {
$this->fail('Save publisher records');
}
// Add author records
// ------------------
try {
$rowling = new Author();
$rowling->setFirstName("J.K.");
$rowling->setLastName("Rowling");
// do not save, will do later to test cascade
$stephenson = new Author();
$stephenson->setFirstName("Neal");
$stephenson->setLastName("Stephenson");
$stephenson->save();
$stephenson_id = $stephenson->getId();
$byron = new Author();
$byron->setFirstName("George");
$byron->setLastName("Byron");
$byron->save();
$byron_id = $byron->getId();
$grass = new Author();
$grass->setFirstName("Gunter");
$grass->setLastName("Grass");
$grass->save();
$grass_id = $grass->getId();
$this->assertTrue(true, 'Save Author records');
} catch (Exception $e) {
$this->fail('Save Author records');
}
// Add book records
// ----------------
try {
$phoenix = new Book();
$phoenix->setTitle("Harry Potter and the Order of the Phoenix");
$phoenix->setISBN("043935806X");
$phoenix->setAuthor($rowling);
$phoenix->setPublisher($scholastic);
$phoenix->save();
$phoenix_id = $phoenix->getId();
$this->assertFalse($rowling->isNew(), 'saving book also saves related author');
$this->assertFalse($scholastic->isNew(), 'saving book also saves related publisher');
$qs = new Book();
$qs->setISBN("0380977427");
$qs->setTitle("Quicksilver");
$qs->setAuthor($stephenson);
$qs->setPublisher($morrow);
$qs->save();
$qs_id = $qs->getId();
$dj = new Book();
$dj->setISBN("0140422161");
$dj->setTitle("Don Juan");
$dj->setAuthor($byron);
$dj->setPublisher($penguin);
$dj->save();
$dj_id = $qs->getId();
$td = new Book();
$td->setISBN("067972575X");
$td->setTitle("The Tin Drum");
$td->setAuthor($grass);
$td->setPublisher($vintage);
$td->save();
$td_id = $td->getId();
$this->assertTrue(true, 'Save Book records');
} catch (Exception $e) {
$this->fail('Save Author records');
}
// Add review records
// ------------------
try {
$r1 = new Review();
$r1->setBook($phoenix);
$r1->setReviewedBy("Washington Post");
$r1->setRecommended(true);
$r1->setReviewDate(time());
$r1->save();
$r1_id = $r1->getId();
$r2 = new Review();
$r2->setBook($phoenix);
$r2->setReviewedBy("New York Times");
$r2->setRecommended(false);
$r2->setReviewDate(time());
$r2->save();
$r2_id = $r2->getId();
$this->assertTrue(true, 'Save Review records');
} catch (Exception $e) {
$this->fail('Save Review records');
}
// Perform a "complex" search
// --------------------------
$results = BookQuery::create()->filterByTitle('Harry%', Criteria::LIKE)->find();
$this->assertEquals(1, count($results));
$results = BookQuery::create()->filterByISBN(array("0380977427", "0140422161"), Criteria::IN)->find();
$this->assertEquals(2, count($results));
// Perform a "limit" search
// ------------------------
$results = BookQuery::create()->limit(2)->offset(1)->orderByTitle()->find();
$this->assertEquals(2, count($results));
// we ordered on book title, so we expect to get
$this->assertEquals("Harry Potter and the Order of the Phoenix", $results[0]->getTitle());
$this->assertEquals("Quicksilver", $results[1]->getTitle());
// Perform a lookup & update!
// --------------------------
// Updating just-created book title
// First finding book by PK (=$qs_id) ....
$qs_lookup = BookQuery::create()->findPk($qs_id);
$this->assertNotNull($qs_lookup, 'just-created book can be found by pk');
$new_title = "Quicksilver (".crc32(uniqid(rand())).")";
// Attempting to update found object
$qs_lookup->setTitle($new_title);
$qs_lookup->save();
// Making sure object was correctly updated: ";
$qs_lookup2 = BookQuery::create()->findPk($qs_id);
$this->assertEquals($new_title, $qs_lookup2->getTitle());
// Test some basic DATE / TIME stuff
// ---------------------------------
// that's the control timestamp.
$control = strtotime('2004-02-29 00:00:00');
// should be two in the db
$r = ReviewQuery::create()->findOne();
$r_id = $r->getId();
$r->setReviewDate($control);
$r->save();
$r2 = ReviewQuery::create()->findPk($r_id);
$this->assertEquals(new DateTime('2004-02-29 00:00:00'), $r2->getReviewDate(null), 'ability to fetch DateTime');
$this->assertEquals($control, $r2->getReviewDate('U'), 'ability to fetch native unix timestamp');
$this->assertEquals('2-29-2004', $r2->getReviewDate('n-j-Y'), 'ability to use date() formatter');
// Handle BLOB/CLOB Columns
// ------------------------
$blob_path = __DIR__ . '/../../Fixtures/etc/lob/tin_drum.gif';
$blob2_path = __DIR__ . '/../../Fixtures/etc/lob/propel.gif';
$clob_path = __DIR__ . '/../../Fixtures/etc/lob/tin_drum.txt';
$m1 = new Media();
$m1->setBook($phoenix);
$m1->setCoverImage(file_get_contents($blob_path));
$m1->setExcerpt(file_get_contents($clob_path));
$m1->save();
$m1_id = $m1->getId();
$m1_lookup = MediaQuery::create()->findPk($m1_id);
$this->assertNotNull($m1_lookup, 'Can find just-created media item');
$this->assertEquals(md5(file_get_contents($blob_path)), md5(stream_get_contents($m1_lookup->getCoverImage())), 'BLOB was correctly updated');
$this->assertEquals(file_get_contents($clob_path), (string) $m1_lookup->getExcerpt(), 'CLOB was correctly updated');
// now update the BLOB column and save it & check the results
$m1_lookup->setCoverImage(file_get_contents($blob2_path));
$m1_lookup->save();
$m2_lookup = MediaQuery::create()->findPk($m1_id);
$this->assertNotNull($m2_lookup, 'Can find just-created media item');
$this->assertEquals(md5(file_get_contents($blob2_path)), md5(stream_get_contents($m2_lookup->getCoverImage())), 'BLOB was correctly overwritten');
// Testing doCount() functionality
// -------------------------------
$records = BookQuery::create()->find();
$count = BookQuery::create()->count();
$this->assertEquals($count, count($records), 'correct number of results');
// Test many-to-many relationships
// ---------------
// init book club list 1 with 2 books
$blc1 = new BookClubList();
$blc1->setGroupLeader("Crazyleggs");
$blc1->setTheme("Happiness");
$brel1 = new BookListRel();
$brel1->setBook($phoenix);
$brel2 = new BookListRel();
$brel2->setBook($dj);
$blc1->addBookListRel($brel1);
$blc1->addBookListRel($brel2);
$blc1->save();
$this->assertNotNull($blc1->getId(), 'BookClubList 1 was saved');
// init book club list 2 with 1 book
$blc2 = new BookClubList();
$blc2->setGroupLeader("John Foo");
$blc2->setTheme("Default");
$brel3 = new BookListRel();
$brel3->setBook($phoenix);
$blc2->addBookListRel($brel3);
$blc2->save();
$this->assertNotNull($blc2->getId(), 'BookClubList 2 was saved');
// re-fetch books and lists from db to be sure that nothing is cached
$crit = new Criteria();
$crit->add(BookTableMap::COL_ID, $phoenix->getId());
$phoenix = BookQuery::create(null, $crit)->findOne();
$this->assertNotNull($phoenix, "book 'phoenix' has been re-fetched from db");
$crit = new Criteria();
$crit->add(BookClubListTableMap::COL_ID, $blc1->getId());
$blc1 = BookClubListQuery::create(null, $crit)->findOne();
$this->assertNotNull($blc1, 'BookClubList 1 has been re-fetched from db');
$crit = new Criteria();
$crit->add(BookClubListTableMap::COL_ID, $blc2->getId());
$blc2 = BookClubListQuery::create(null, $crit)->findOne();
$this->assertNotNull($blc2, 'BookClubList 2 has been re-fetched from db');
$relCount = $phoenix->countBookListRels();
$this->assertEquals(2, $relCount, "book 'phoenix' has 2 BookListRels");
$relCount = $blc1->countBookListRels();
$this->assertEquals(2, $relCount, 'BookClubList 1 has 2 BookListRels');
$relCount = $blc2->countBookListRels();
$this->assertEquals(1, $relCount, 'BookClubList 2 has 1 BookListRel');
// Cleanup (tests DELETE)
// ----------------------
// Removing books that were just created
// First finding book by PK (=$phoenix_id) ....
$hp = BookQuery::create()->findPk($phoenix_id);
$this->assertNotNull($hp, 'Could find just-created book');
// Attempting to delete [multi-table] by found pk
$c = new Criteria();
$c->add(BookTableMap::COL_ID, $hp->getId());
// The only way for cascading to work currently
// is to specify the author_id and publisher_id (i.e. the fkeys
// have to be in the criteria).
$c->add(AuthorTableMap::COL_ID, $hp->getAuthor()->getId());
$c->add(PublisherTableMap::COL_ID, $hp->getPublisher()->getId());
$c->setSingleRecord(true);
BookTableMap::doDelete($c);
// Checking to make sure correct records were removed.
$this->assertEquals(3, AuthorQuery::create()->count(), 'Correct records were removed from author table');
$this->assertEquals(3, PublisherQuery::create()->count(), 'Correct records were removed from publisher table');
$this->assertEquals(3, BookQuery::create()->count(), 'Correct records were removed from book table');
// Attempting to delete books by complex criteria
$c = new Criteria();
$cn = $c->getNewCriterion(BookTableMap::COL_ISBN, "043935806X");
$cn->addOr($c->getNewCriterion(BookTableMap::COL_ISBN, "0380977427"));
$cn->addOr($c->getNewCriterion(BookTableMap::COL_ISBN, "0140422161"));
$c->add($cn);
BookTableMap::doDelete($c);
// Attempting to delete book [id = $td_id]
$td->delete();
// Attempting to delete authors
AuthorTableMap::doDelete($stephenson_id);
AuthorTableMap::doDelete($byron_id);
$grass->delete();
// Attempting to delete publishers
PublisherTableMap::doDelete($morrow_id);
PublisherTableMap::doDelete($penguin_id);
$vintage->delete();
// These have to be deleted manually also since we have onDelete
// set to SETNULL in the foreign keys in book. Is this correct?
$rowling->delete();
$scholastic->delete();
$blc1->delete();
$blc2->delete();
$this->assertCount(0, AuthorQuery::create()->find(), 'no records in [author] table');
$this->assertCount(0, PublisherQuery::create()->find(), 'no records in [publisher] table');
$this->assertCount(0, BookQuery::create()->find(), 'no records in [book] table');
$this->assertCount(0, ReviewQuery::create()->find(), 'no records in [review] table');
$this->assertCount(0, MediaQuery::create()->find(), 'no records in [media] table');
$this->assertCount(0, BookClubListQuery::create()->find(), 'no records in [book_club_list] table');
$this->assertCount(0, BookListRelQuery::create()->find(), 'no records in [book_x_list] table');
}
public function testScenarioUsingQuery()
{
// Add publisher records
// ---------------------
try {
$scholastic = new Publisher();
$scholastic->setName("Scholastic");
// do not save, will do later to test cascade
$morrow = new Publisher();
$morrow->setName("William Morrow");
$morrow->save();
$morrow_id = $morrow->getId();
$penguin = new Publisher();
$penguin->setName("Penguin");
$penguin->save();
$penguin_id = $penguin->getId();
$vintage = new Publisher();
$vintage->setName("Vintage");
$vintage->save();
$vintage_id = $vintage->getId();
$this->assertTrue(true, 'Save Publisher records');
} catch (Exception $e) {
$this->fail('Save publisher records');
}
// Add author records
// ------------------
try {
$rowling = new Author();
$rowling->setFirstName("J.K.");
$rowling->setLastName("Rowling");
// do not save, will do later to test cascade
$stephenson = new Author();
$stephenson->setFirstName("Neal");
$stephenson->setLastName("Stephenson");
$stephenson->save();
$stephenson_id = $stephenson->getId();
$byron = new Author();
$byron->setFirstName("George");
$byron->setLastName("Byron");
$byron->save();
$byron_id = $byron->getId();
$grass = new Author();
$grass->setFirstName("Gunter");
$grass->setLastName("Grass");
$grass->save();
$grass_id = $grass->getId();
$this->assertTrue(true, 'Save Author records');
} catch (Exception $e) {
$this->fail('Save Author records');
}
// Add book records
// ----------------
try {
$phoenix = new Book();
$phoenix->setTitle("Harry Potter and the Order of the Phoenix");
$phoenix->setISBN("043935806X");
$phoenix->setAuthor($rowling);
$phoenix->setPublisher($scholastic);
$phoenix->save();
$phoenix_id = $phoenix->getId();
$this->assertFalse($rowling->isNew(), 'saving book also saves related author');
$this->assertFalse($scholastic->isNew(), 'saving book also saves related publisher');
$qs = new Book();
$qs->setISBN("0380977427");
$qs->setTitle("Quicksilver");
$qs->setAuthor($stephenson);
$qs->setPublisher($morrow);
$qs->save();
$qs_id = $qs->getId();
$dj = new Book();
$dj->setISBN("0140422161");
$dj->setTitle("Don Juan");
$dj->setAuthor($byron);
$dj->setPublisher($penguin);
$dj->save();
$dj_id = $qs->getId();
$td = new Book();
$td->setISBN("067972575X");
$td->setTitle("The Tin Drum");
$td->setAuthor($grass);
$td->setPublisher($vintage);
$td->save();
$td_id = $td->getId();
$this->assertTrue(true, 'Save Book records');
} catch (Exception $e) {
$this->fail('Save Author records');
}
// Add review records
// ------------------
try {
$r1 = new Review();
$r1->setBook($phoenix);
$r1->setReviewedBy("Washington Post");
$r1->setRecommended(true);
$r1->setReviewDate(time());
$r1->save();
$r1_id = $r1->getId();
$r2 = new Review();
$r2->setBook($phoenix);
$r2->setReviewedBy("New York Times");
$r2->setRecommended(false);
$r2->setReviewDate(time());
$r2->save();
$r2_id = $r2->getId();
$this->assertTrue(true, 'Save Review records');
} catch (Exception $e) {
$this->fail('Save Review records');
}
// Perform a "complex" search
// --------------------------
$results = BookQuery::create()
->filterByTitle('Harry%')
->find();
$this->assertEquals(1, count($results));
$results = BookQuery::create()
->where('Book.ISBN IN ?', array("0380977427", "0140422161"))
->find();
$this->assertEquals(2, count($results));
// Perform a "limit" search
// ------------------------
$results = BookQuery::create()
->limit(2)
->offset(1)
->orderByTitle()
->find();
$this->assertEquals(2, count($results));
// we ordered on book title, so we expect to get
$this->assertEquals("Harry Potter and the Order of the Phoenix", $results[0]->getTitle());
$this->assertEquals("Quicksilver", $results[1]->getTitle());
// Perform a lookup & update!
// --------------------------
// Updating just-created book title
// First finding book by PK (=$qs_id) ....
$qs_lookup = BookQuery::create()->findPk($qs_id);
$this->assertNotNull($qs_lookup, 'just-created book can be found by pk');
$new_title = "Quicksilver (".crc32(uniqid(rand())).")";
// Attempting to update found object
$qs_lookup->setTitle($new_title);
$qs_lookup->save();
// Making sure object was correctly updated: ";
$qs_lookup2 = BookQuery::create()->findPk($qs_id);
$this->assertEquals($new_title, $qs_lookup2->getTitle());
// Test some basic DATE / TIME stuff
// ---------------------------------
// that's the control timestamp.
$control = strtotime('2004-02-29 00:00:00');
// should be two in the db
$r = ReviewQuery::create()->findOne();
$r_id = $r->getId();
$r->setReviewDate($control);
$r->save();
$r2 = ReviewQuery::create()->findPk($r_id);
$this->assertEquals(new DateTime('2004-02-29 00:00:00'), $r2->getReviewDate(null), 'ability to fetch DateTime');
$this->assertEquals($control, $r2->getReviewDate('U'), 'ability to fetch native unix timestamp');
$this->assertEquals('2-29-2004', $r2->getReviewDate('n-j-Y'), 'ability to use date() formatter');
// Handle BLOB/CLOB Columns
// ------------------------
$blob_path = __DIR__ . '/../../Fixtures/etc/lob/tin_drum.gif';
$blob2_path = __DIR__ . '/../../Fixtures/etc/lob/propel.gif';
$clob_path = __DIR__ . '/../../Fixtures/etc/lob/tin_drum.txt';
$m1 = new Media();
$m1->setBook($phoenix);
$m1->setCoverImage(file_get_contents($blob_path));
$m1->setExcerpt(file_get_contents($clob_path));
$m1->save();
$m1_id = $m1->getId();
$m1_lookup = MediaQuery::create()->findPk($m1_id);
$this->assertNotNull($m1_lookup, 'Can find just-created media item');
$this->assertEquals(md5(file_get_contents($blob_path)), md5(stream_get_contents($m1_lookup->getCoverImage())), 'BLOB was correctly updated');
$this->assertEquals(file_get_contents($clob_path), (string) $m1_lookup->getExcerpt(), 'CLOB was correctly updated');
// now update the BLOB column and save it & check the results
$m1_lookup->setCoverImage(file_get_contents($blob2_path));
$m1_lookup->save();
$m2_lookup = MediaQuery::create()->findPk($m1_id);
$this->assertNotNull($m2_lookup, 'Can find just-created media item');
$this->assertEquals(md5(file_get_contents($blob2_path)), md5(stream_get_contents($m2_lookup->getCoverImage())), 'BLOB was correctly overwritten');
// Testing count() functionality
// -------------------------------
$records = BookQuery::create()->find();
$count = BookQuery::create()->count();
$this->assertEquals($count, count($records), 'correct number of results');
// Test many-to-many relationships
// ---------------
// init book club list 1 with 2 books
$blc1 = new BookClubList();
$blc1->setGroupLeader("Crazyleggs");
$blc1->setTheme("Happiness");
$brel1 = new BookListRel();
$brel1->setBook($phoenix);
$brel2 = new BookListRel();
$brel2->setBook($dj);
$blc1->addBookListRel($brel1);
$blc1->addBookListRel($brel2);
$blc1->save();
$this->assertNotNull($blc1->getId(), 'BookClubList 1 was saved');
// init book club list 2 with 1 book
$blc2 = new BookClubList();
$blc2->setGroupLeader("John Foo");
$blc2->setTheme("Default");
$brel3 = new BookListRel();
$brel3->setBook($phoenix);
$blc2->addBookListRel($brel3);
$blc2->save();
$this->assertNotNull($blc2->getId(), 'BookClubList 2 was saved');
// re-fetch books and lists from db to be sure that nothing is cached
$crit = new Criteria();
$crit->add(BookTableMap::COL_ID, $phoenix->getId());
$phoenix = BookQuery::create(null, $crit)->findOne();
$this->assertNotNull($phoenix, "book 'phoenix' has been re-fetched from db");
$crit = new Criteria();
$crit->add(BookClubListTableMap::COL_ID, $blc1->getId());
$blc1 = BookClubListQuery::create(null, $crit)->findOne();
$this->assertNotNull($blc1, 'BookClubList 1 has been re-fetched from db');
$crit = new Criteria();
$crit->add(BookClubListTableMap::COL_ID, $blc2->getId());
$blc2 = BookClubListQuery::create(null, $crit)->findOne();
$this->assertNotNull($blc2, 'BookClubList 2 has been re-fetched from db');
$relCount = $phoenix->countBookListRels();
$this->assertEquals(2, $relCount, "book 'phoenix' has 2 BookListRels");
$relCount = $blc1->countBookListRels();
$this->assertEquals(2, $relCount, 'BookClubList 1 has 2 BookListRels');
$relCount = $blc2->countBookListRels();
$this->assertEquals(1, $relCount, 'BookClubList 2 has 1 BookListRel');
// Cleanup (tests DELETE)
// ----------------------
// Removing books that were just created
// First finding book by PK (=$phoenix_id) ....
$hp = BookQuery::create()->findPk($phoenix_id);
$this->assertNotNull($hp, 'Could find just-created book');
// Attempting to delete [multi-table] by found pk
$c = new Criteria();
$c->add(BookTableMap::COL_ID, $hp->getId());
// The only way for cascading to work currently
// is to specify the author_id and publisher_id (i.e. the fkeys
// have to be in the criteria).
$c->add(AuthorTableMap::COL_ID, $hp->getAuthor()->getId());
$c->add(PublisherTableMap::COL_ID, $hp->getPublisher()->getId());
$c->setSingleRecord(true);
BookTableMap::doDelete($c);
// Checking to make sure correct records were removed.
$this->assertEquals(3, AuthorQuery::create()->count(), 'Correct records were removed from author table');
$this->assertEquals(3, PublisherQuery::create()->count(), 'Correct records were removed from publisher table');
$this->assertEquals(3, BookQuery::create()->count(), 'Correct records were removed from book table');
// Attempting to delete books by complex criteria
BookQuery::create()
->filterByISBN("043935806X")
->_or()->where('Book.ISBN = ?', "0380977427")
->_or()->where('Book.ISBN = ?', "0140422161")
->delete();
// Attempting to delete book [id = $td_id]
$td->delete();
// Attempting to delete authors
AuthorQuery::create()->filterById($stephenson_id)->delete();
AuthorQuery::create()->filterById($byron_id)->delete();
$grass->delete();
// Attempting to delete publishers
PublisherQuery::create()->filterById($morrow_id)->delete();
PublisherQuery::create()->filterById($penguin_id)->delete();
$vintage->delete();
// These have to be deleted manually also since we have onDelete
// set to SETNULL in the foreign keys in book. Is this correct?
$rowling->delete();
$scholastic->delete();
$blc1->delete();
$blc2->delete();
$this->assertCount(0, AuthorQuery::create()->find(), 'no records in [author] table');
$this->assertCount(0, PublisherQuery::create()->find(), 'no records in [publisher] table');
$this->assertCount(0, BookQuery::create()->find(), 'no records in [book] table');
$this->assertCount(0, ReviewQuery::create()->find(), 'no records in [review] table');
$this->assertCount(0, MediaQuery::create()->find(), 'no records in [media] table');
$this->assertCount(0, BookClubListQuery::create()->find(), 'no records in [book_club_list] table');
$this->assertCount(0, BookListRelQuery::create()->find(), 'no records in [book_x_list] table');
}
}