root/trunk/generator/test/bookstore-packaged-test.php

Revision 833, 21.2 kB (checked in by hans, 1 year ago)

Merging in changes from 1.3 branch. svn merge -r 534:832

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id Rev Date Author HeadURL Revision
Line 
1 <?php
2
3 /**
4  * Simple test script for Propel drivers.
5  *
6  * This script will no do in-depth testing, but is designed to test whether drivers
7  * are correctly performing basic operations -- SELECT, UPDATE, DELETE, limit support,
8  * prepared query emulation, etc.
9  *
10  * IMPORTANT:
11  *
12  * Use this script with a clean version of the [example] bookstore database.  If records
13  * already exist, an error will be displayed.
14  *
15  * TODO:
16  * A more advanced driver test system should be developed that could test capabilities
17  * of driver-specific things like callable statements (stored procedures), etc.  Perhaps break
18  * functionality into class & provide ability to subclass.
19  *
20  * @author     Hans Lellelid <hans@xmpl.org>
21  * @version    $Revision$
22  */
23
24 // Setup configuration.  It is expected that the bookstore-conf.php file exists in ../build/conf
25 //
26
27 error_reporting(E_ALL);
28
29 $conf_path = realpath(dirname(__FILE__) . '/../projects/bookstore-packaged/build/conf/bookstore-packaged-conf.php');
30 if (!file_exists($conf_path)) {
31     print "Make sure that you specify properties in conf/bookstore-packaged.properties and "
32     ."build propel before running this script.";
33     exit;
34 }
35
36 // Add PHP_CLASSPATH, if set
37 if (getenv("PHP_CLASSPATH")) {
38     set_include_path(getenv("PHP_CLASSPATH") . PATH_SEPARATOR . get_include_path());
39 }
40
41  // Add build/classes/ and classes/ to path
42 set_include_path(
43     realpath(dirname(__FILE__) . '/../projects/bookstore-packaged/build/classes') . PATH_SEPARATOR .
44     dirname(__FILE__) . '/../../runtime/classes' . PATH_SEPARATOR .
45     get_include_path()
46 );
47
48
49  // Require classes.
50  require_once 'propel/Propel.php';
51  require_once 'author/Author.php';
52  require_once 'publisher/Publisher.php';
53  require_once 'book/Book.php';
54  require_once 'review/Review.php';
55  include_once 'media/Media.php';
56  include_once 'log/BookstoreLog.php';
57  include_once 'book_club_list/BookClubList.php';
58  include_once 'book_club_list/BookListRel.php';
59
60  include_once 'Benchmark/Timer.php';
61
62  $timer = new Benchmark_Timer;
63
64  $timer->start();
65
66  // Some utility functions
67  function boolTest($cond) {
68      if ($cond) {
69          return "[OK]\n";
70      } else {
71         return "[FAILED]\n";
72     }
73  }
74
75  try {
76     // Initialize Propel
77      Propel::init($conf_path);
78  } catch (Exception $e) {
79      die("Error initializing propel: ". $e->__toString());
80  }
81
82 function check_tables_empty() {
83     try {
84
85         print "\nChecking to see that tables are empty\n";
86         print "-------------------------------------\n\n";
87
88         print "Ensuring that there are no records in [author] table: ";
89         $res = AuthorPeer::doSelect(new Criteria());
90         print boolTest(empty($res));
91
92         print "Ensuring that there are no records in [publisher] table: ";
93         $res2 = PublisherPeer::doSelect(new Criteria());
94         print boolTest(empty($res2));
95
96         print "Ensuring that there are no records in [book] table: ";
97         $res3 = AuthorPeer::doSelect(new Criteria());
98         print boolTest(empty($res3));
99
100         print "Ensuring that there are no records in [review] table: ";
101         $res4 = ReviewPeer::doSelect(new Criteria());
102         print boolTest(empty($res4));
103
104         print "Ensuring that there are no records in [media] table: ";
105         $res5 = MediaPeer::doSelect(new Criteria());
106         print boolTest(empty($res5));
107
108         print "Ensuring that there are no records in [book_club_list] table: ";
109         $res6 = BookClubListPeer::doSelect(new Criteria());
110         print boolTest(empty($res6));
111
112         print "Ensuring that there are no records in [book_x_list] table: ";
113         $res7 = BookListRelPeer::doSelect(new Criteria());
114         print boolTest(empty($res7));
115
116         return (empty($res) && empty($res2) && empty($res3) && empty($res4) && empty($res5));
117
118     } catch (Exception $e) {
119         die("Error ensuring tables were empty: " . $e->__toString());
120     }
121 }
122
123 // Check to see if records already exist in any of the three tables.  If so, display an error
124 // and exit.
125
126 if (!check_tables_empty()) {
127     die("Tables must be empty to perform these tests.");
128 }
129
130 // Add publisher records
131 // ---------------------
132
133 try {
134     print "\nAdding some new publishers to the list\n";
135     print "--------------------------------------\n\n";
136
137     $scholastic = new Publisher();
138     $scholastic->setName("Scholastic");
139     // do not save, will do later to test cascade
140     print "Added publisher \"Scholastic\" [not saved yet].\n";
141
142     $morrow = new Publisher();
143     $morrow->setName("William Morrow");
144     $morrow->save();
145     $morrow_id = $morrow->getId();
146     print "Added publisher \"William Morrow\" [id = $morrow_id].\n";
147
148     $penguin = new Publisher();
149     $penguin->setName("Penguin");
150     $penguin->save();
151     $penguin_id = $penguin->getId();
152     print "Added publisher \"Penguin\" [id = $penguin_id].\n";
153
154     $vintage = new Publisher();
155     $vintage->setName("Vintage");
156     $vintage->save();
157     $vintage_id = $vintage->getId();
158     print "Added publisher \"Vintage\" [id = $vintage_id].\n";
159
160 } catch (Exception $e) {
161     die("Error adding publisher: " . $e->__toString());
162 }
163
164 // Add author records
165 // ------------------
166
167 try {
168     print "\nAdding some new authors to the list\n";
169     print "--------------------------------------\n\n";
170
171     $rowling = new Author();
172     $rowling->setFirstName("J.K.");
173     $rowling->setLastName("Rowling");
174     // no save()
175     print "Added author \"J.K. Rowling\" [not saved yet].\n";
176
177     $stephenson = new Author();
178     $stephenson->setFirstName("Neal");
179     $stephenson->setLastName("Stephenson");
180     $stephenson->save();
181     $stephenson_id = $stephenson->getId();
182     print "Added author \"Neal Stephenson\" [id = $stephenson_id].\n";
183
184     $byron = new Author();
185     $byron->setFirstName("George");
186     $byron->setLastName("Byron");
187     $byron->save();
188     $byron_id = $byron->getId();
189     print "Added author \"George Byron\" [id = $byron_id].\n";
190
191
192     $grass = new Author();
193     $grass->setFirstName("Gunter");
194     $grass->setLastName("Grass");
195     $grass->save();
196     $grass_id = $grass->getId();
197     print "Added author \"Gunter Grass\" [id = $grass_id].\n";
198
199 } catch (Exception $e) {
200     die("Error adding author: " . $e->__toString());
201 }
202
203 // Add book records
204 // ----------------
205
206 try {
207
208     print "\nAdding some new books to the list\n";
209     print "-------------------------------------\n\n";
210
211     $phoenix = new Book();
212     $phoenix->setTitle("Harry Potter and the Order of the Phoenix");
213     $phoenix->setISBN("043935806X");
214
215     print "Trying cascading save (Harry Potter): ";
216     $phoenix->setAuthor($rowling);
217     $phoenix->setPublisher($scholastic);
218     $phoenix->save();
219     $phoenix_id = $phoenix->getId();
220     print boolTest(true);
221     print "Added book \"Harry Potter and the Order of the Phoenix\" [id = $phoenix_id].\n";
222
223     $qs = new Book();
224     $qs->setISBN("0380977427");
225     $qs->setTitle("Quicksilver");
226     $qs->setAuthor($stephenson);
227     $qs->setPublisher($morrow);
228     $qs->save();
229     $qs_id = $qs->getId();
230     print "Added book \"Quicksilver\" [id = $qs_id].\n";
231
232     $dj = new Book();
233     $dj->setISBN("0140422161");
234     $dj->setTitle("Don Juan");
235     $dj->setAuthor($byron);
236     $dj->setPublisher($penguin);
237     $dj->save();
238     $dj_id = $qs->getId();
239     print "Added book \"Don Juan\" [id = $dj_id].\n";
240
241     $td = new Book();
242     $td->setISBN("067972575X");
243     $td->setTitle("The Tin Drum");
244     $td->setAuthor($grass);
245     $td->setPublisher($vintage);
246     $td->save();
247     $td_id = $td->getId();
248     print "Added book \"The Tin Drum\" [id = $dj_id].\n";
249
250 } catch (Exception $e) {
251     die("Error saving book: " . $e->__toString());
252 }
253
254 // Add review records
255 // ------------------
256
257 try {
258
259     print "\nAdding some book reviews to the list\n";
260     print "------------------------------------\n\n";
261
262     $r1 = new Review();
263     $r1->setBook($phoenix);
264     $r1->setReviewedBy("Washington Post");
265     $r1->setRecommended(true);
266     $r1->setReviewDate(time());
267     $r1->save();
268     $r1_id = $r1->getId();
269     print "Added Washington Post book review  [id = $r1_id].\n";
270
271     $r2 = new Review();
272     $r2->setBook($phoenix);
273     $r2->setReviewedBy("New York Times");
274     $r2->setRecommended(false);
275     $r2->setReviewDate(time());
276     $r2->save();
277     $r2_id = $r2->getId();
278     print "Added New York Times book review  [id = $r2_id].\n";
279
280 } catch (Exception $e) {
281     die("Error saving book review: " . $e->__toString());
282 }
283
284 // Perform a "complex" search
285 // --------------------------
286
287 try {
288
289     print "\nDoing complex search on books\n";
290     print "-----------------------------\n\n";
291
292     $crit = new Criteria();
293     $crit->add(BookPeer::TITLE, 'Harry%', Criteria::LIKE);
294
295     print "Looking for \"Harry%\": ";
296     $results = BookPeer::doSelect($crit);
297     print boolTest(count($results) === 1);
298
299
300     $crit2 = new Criteria();
301     $crit2->add(BookPeer::ISBN, array("0380977427", "0140422161"), Criteria::IN);
302     $results = BookPeer::doSelect($crit2);
303     print "Looking for ISBN IN (\"0380977427\", \"0140422161\"): ";
304     print boolTest(count($results) === 2);
305
306 } catch (Exception $e) {
307     die("Error while performing complex query: " . $e->__toString());
308 }
309
310
311 // Perform a "limit" search
312 // ------------------------
313
314 try {
315
316     print "\nDoing LIMITed search on books\n";
317     print "-----------------------------\n\n";
318
319     $crit = new Criteria();
320     $crit->setLimit(2);
321     $crit->setOffset(1);
322     $crit->addAscendingOrderByColumn(BookPeer::TITLE);
323
324     print "Checking to make sure correct number returned: ";
325     $results = BookPeer::doSelect($crit);
326     print boolTest(count($results) === 2);
327
328     print "Checking to make sure correct books returned: ";
329     // we ordered on book title, so we expect to get
330     print boolTest( $results[0]->getTitle() == "Harry Potter and the Order of the Phoenix" && $results[1]->getTitle() == "Quicksilver"  );
331
332
333 } catch (Exception $e) {
334     die("Error while performing LIMIT query: " . $e->__toString());
335 }
336
337
338
339 // Perform a lookup & update!
340 // --------------------------
341
342 try {
343
344     print "\nUpdating just-created book title\n";
345     print "--------------------------------\n\n";
346
347     print "First finding book by PK (=$qs_id) .... ";
348
349     try {
350         $qs_lookup = BookPeer::retrieveByPk($qs_id);
351     } catch (Exception $e) {
352         print "ERROR!\n";
353         die("Error retrieving by pk: " . $e->__toString());
354     }
355
356     if ($qs_lookup) {
357         print "FOUND!\n";
358     } else {
359         print "NOT FOUND :(\n";
360         die("Couldn't find just-created book: book_id = $qs_id");
361     }
362
363     try {
364         $new_title = "Quicksilver (".crc32(uniqid(rand())).")";
365         print "Attempting to update found object (".$qs_lookup->getTitle()." -> ".$new_title."): ";
366         $qs_lookup->setTitle($new_title);
367         $qs_lookup->save();
368         print boolTest(true);
369     } catch (Exception $e) {
370         die("Error saving (updating) book: " . $e->__toString());
371     }
372
373     print "Making sure object was correctly updated: ";
374     $qs_lookup2 = BookPeer::retrieveByPk($qs_id);
375     print boolTest($qs_lookup2->getTitle() == $new_title);
376
377 } catch (Exception $e) {
378     die("Error updating book: " . $e->__toString());
379 }
380
381
382 // Test some basic DATE / TIME stuff
383 // ---------------------------------
384
385 try {
386     print "\nTesting the DATE/TIME columns\n";
387     print "-----------------------------\n\n";
388
389     // that's the control timestamp.
390     $control = strtotime('2004-02-29 00:00:00');
391
392     // should be two in the db
393     $r = ReviewPeer::doSelectOne(new Criteria());
394     $r_id = $r->getId();
395     $r->setReviewDate($control);
396     $r->save();
397
398     $r2 = ReviewPeer::retrieveByPk($r_id);
399
400     print "Checking ability to fetch native unix timestamp: ";
401     print boolTest($r2->getReviewDate(null) === $control);
402
403     print "Checking ability to use date() formatter: ";
404     print boolTest($r2->getReviewDate('n-j-Y') === '2-29-2004');
405
406     print "[FYI] Here's the strftime() formatter for current locale: " . $r2->getReviewDate('%x') . "\n";
407
408 } catch (Exception $e) {
409     die("Error test date/time: "  . $e->__toString());
410 }
411
412 // Handle BLOB/CLOB Columns
413 // ------------------------
414
415 try {
416     print "\nTesting the BLOB/CLOB columns\n";
417     print "-------------------------------\n\n";
418
419     $blob_path = dirname(__FILE__) . '/etc/lob/tin_drum.gif';
420     $blob2_path = dirname(__FILE__) . '/etc/lob/propel.gif';
421     $clob_path = dirname(__FILE__) . '/etc/lob/tin_drum.txt';
422
423     $m1 = new Media();
424     $m1->setBook($phoenix);
425     $m1->setCoverImage(file_get_contents($blob_path));
426     $m1->setExcerpt(file_get_contents($clob_path));
427     $m1->save();
428     $m1_id = $m1->getId();
429     print "Added Media collection [id = $m1_id].\n";
430
431     print "Looking for just-created mediat by PK (=$m1_id) .... ";
432
433     try {
434         $m1_lookup = MediaPeer::retrieveByPk($m1_id);
435     } catch (Exception $e) {
436         print "ERROR!\n";
437         die("Error retrieving media by pk: " . $e->__toString());
438     }
439
440     if ($m1_lookup) {
441         print "FOUND!\n";
442     } else {
443         print "NOT FOUND :(\n";
444         die("Couldn't find just-created media item: media_id = $m1_id");
445     }
446
447     print "Making sure BLOB was correctly updated: ";
448     print boolTest( $m1_lookup->getCoverImage()->getContents() === file_get_contents($blob_path));
449     print "Making sure CLOB was correctly updated: ";
450     print boolTest((string) $m1_lookup->getExcerpt()->getContents() === file_get_contents($clob_path));
451
452
453     // now update the BLOB column and save it & check the results
454
455     $b = $m1_lookup->getCoverImage();
456     $b->setContents(file_get_contents($blob2_path));
457     $m1_lookup->setCoverImage($b);
458     $m1_lookup->save();
459
460     try {
461         $m2_lookup = MediaPeer::retrieveByPk($m1_id);
462     } catch (Exception $e) {
463         print "ERROR!\n";
464         die("Error retrieving media by pk: " . $e->__toString());
465     }
466
467     print "Making sure BLOB was correctly overwritten: ";
468     print boolTest($m2_lookup->getCoverImage()->getContents() === file_get_contents($blob2_path));
469
470 } catch (Exception $e) {
471     die("Error doing blob/clob updates: " . $e->__toString());
472 }
473
474 // Test Validators
475 // ---------------
476
477 try {
478
479     print "\nTesting the column validators\n";
480     print "-----------------------------\n\n";
481
482     $bk1 = new Book();
483     $bk1->setTitle("12345"); // min length is 10
484     $ret = $bk1->validate();
485
486     print "Making sure validation failed: ";
487     print boolTest($ret !== true);
488
489     print "Making sure 1 validation message was returned: ";
490     print boolTest(count($ret) === 1);
491
492     print "Making sure expected validation message was returned: ";
493     $el = array_shift($ret);
494     print boolTest(stripos($el->getMessage(), "must be more than") !== false);
495
496     print "\n(Unique validator)\n";
497
498     $bk2 = new Book();
499     $bk2->setTitle("Don Juan");
500     $ret = $bk2->validate();
501
502     print "Making sure validation failed: ";
503     print boolTest($ret !== true);
504
505     print "Making sure 1 validation message was returned: ";
506     print boolTest(count($ret) === 1);
507
508     print "Making sure expected validation message was returned: ";
509     $el = array_shift($ret);
510     print boolTest(stripos($el->getMessage(), "Book title already in database.") !== false);
511
512     print "\n(Now trying some more complex validation.)\n";
513     $auth1 = new Author();
514     $auth1->setFirstName("Hans");
515     // last name required; will fail
516
517     $bk1->setAuthor($auth1);
518
519     $rev1 = new Review();
520     $rev1->setReviewDate("08/09/2001");
521      // will fail: reviewed_by column required
522
523     $bk1->addReview($rev1);
524
525     $ret2 = $bk1->validate();
526
527     print "Making sure 6 validation messages were returned: ";
528     print boolTest(count($ret2) === 6);
529
530     print "Making sure correct columns failed: ";
531     print boolTest(array_keys($ret2) === array(
532         AuthorPeer::LAST_NAME,
533         AuthorPeer::EMAIL,
534         AuthorPeer::AGE,
535         BookPeer::TITLE,
536         ReviewPeer::REVIEWED_BY,
537         ReviewPeer::STATUS
538     ));
539
540
541     $bk2 = new Book();
542     $bk2->setTitle("12345678901"); // passes
543
544     $auth2 = new Author();
545     $auth2->setLastName("Blah"); //passes
546     $auth2->setEmail("some@body.com"); //passes
547     $auth2->setAge(50); //passes
548     $bk2->setAuthor($auth2);
549
550     $rev2 = new Review();
551     $rev2->setReviewedBy("Me!"); // passes
552     $rev2->setStatus("new"); // passes
553     $bk2->addReview($rev2);
554
555     $ret3 = $bk2->validate();
556
557     print "Making sure complex validation can pass: ";
558     print boolTest($ret3 === true);
559
560 } catch (Exception $e) {
561     die("Error doing validation tests: " . $e->__toString());
562 }
563
564
565 // Test doCount()
566 //
567 try {
568
569     print "\nTesting doCount() functionality\n";
570     print "-------------------------------\n\n";
571
572     $c = new Criteria();
573     $records = BookPeer::doSelect($c