bookstore-packaged-test.php 21 KB

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