root/branches/1.3/generator/test/bookstore-test.php

Revision 563, 20.3 kB (checked in by heltem, 2 years ago)

Cleanup:

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