/** * @return bool * @throws Exception */ public function process() { $bookModel = new Book(); if (!$bookModel->load($this->data) || !$bookModel->validate()) { throw new Exception('Invalid data!'); } return $bookModel->delete(); }
function remove() { $id = $this->uri->segment(3); $obj = new Book(); $obj->where('id', $id)->get(); $obj->delete(); echo $obj->check_last_query(); }
function testDelete() { $title = "Anathem"; $test_book = new Book($title); $test_book->save(); $title2 = "Snow Crash"; $test_book2 = new Book($title2); $test_book2->save(); $test_book->delete(); $this->assertEquals([$test_book2], Book::getAll()); }
function test_delete() { $title = "Three Blind Mice"; $test_book = new Book($title); $test_book->save(); $title2 = "Chicken Dog"; $test_book2 = new Book($title2); $test_book2->save(); $test_book->delete(); $result = Book::getAll(); $this->assertEquals([$test_book2], $result); }
function test_delete() { //Arrange $title = "History of whatever"; $test_book = new Book($title); $test_book->save(); $title2 = "History of nothing"; $test_book2 = new Book($title2); $test_book2->save(); //Act $test_book->delete(); $result = Book::getAll(); //Assert $this->assertEquals([$test_book2], $result); }
function test_delete() { //Arrange $title = "Sea Wolf"; $test_book = new Book($title); $test_book->save(); $title2 = "Eye of the World"; $test_book2 = new Book($title2); $test_book2->save(); //Act $test_book2->delete(); $result = Book::getall(); //Assert $this->assertEquals([$test_book], $result); }
/** * testDeleteDependentWithConditions method * * @return void */ public function testDeleteDependentWithConditions() { $this->loadFixtures('Cd', 'Book', 'OverallFavorite'); $Cd = new Cd(); $Book = new Book(); $OverallFavorite = new OverallFavorite(); $Cd->delete(1); $result = $OverallFavorite->find('all', array('fields' => array('model_type', 'model_id', 'priority'))); $expected = array(array('OverallFavorite' => array('model_type' => 'Book', 'model_id' => 1, 'priority' => 2))); $this->assertTrue(is_array($result)); $this->assertEquals($expected, $result); $Book->delete(1); $result = $OverallFavorite->find('all', array('fields' => array('model_type', 'model_id', 'priority'))); $expected = array(); $this->assertTrue(is_array($result)); $this->assertEquals($expected, $result); }
function test_delete() { //Arrange $title = "Revenge of the Martians"; $test_book = new Book($title); $test_book->save(); $title2 = "Star Wars"; $test_book2 = new Book($title2); $test_book2->save(); //Act $test_book->delete(); $result = Book::getAll(); //Assert $this->assertEquals([$test_book2], $result); }
function testDelete() { //Arrange $title = "Little Cat"; $id = 1; $test_book = new Book($title, $id); $test_book->save(); $name = "Ben"; $id = 1; $test_author = new Author($name, $id); $test_author->save(); //Act $test_book->addAuthor($test_author); $test_book->delete(); //Assert $this->assertEquals([], $test_author->getBooks()); }
public function testSpeed() { // Add publisher records // --------------------- $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(); // Add author records // ------------------ $rowling = new Author(); $rowling->setFirstName("J.K."); $rowling->setLastName("Rowling"); // no save() $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(); // Add book records // ---------------- $phoenix = new Book(); $phoenix->setTitle("Harry Potter and the Order of the Phoenix"); $phoenix->setISBN("043935806X"); // cascading save (Harry Potter) $phoenix->setAuthor($rowling); $phoenix->setPublisher($scholastic); $phoenix->save(); $phoenix_id = $phoenix->getId(); $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(); // Add review records // ------------------ $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(); // Perform a "complex" search // -------------------------- $crit = new Criteria(); $crit->add(BookPeer::TITLE, 'Harry%', Criteria::LIKE); $results = BookPeer::doSelect($crit); $crit2 = new Criteria(); $crit2->add(BookPeer::ISBN, array("0380977427", "0140422161"), Criteria::IN); $results = BookPeer::doSelect($crit2); // Perform a "limit" search // ------------------------ $crit = new Criteria(); $crit->setLimit(2); $crit->setOffset(1); $crit->addAscendingOrderByColumn(BookPeer::TITLE); $results = BookPeer::doSelect($crit); // Perform a lookup & update! // -------------------------- $qs_lookup = BookPeer::retrieveByPk($qs_id); $new_title = "Quicksilver (" . crc32(uniqid(rand())) . ")"; $qs_lookup->setTitle($new_title); $qs_lookup->save(); $qs_lookup2 = BookPeer::retrieveByPk($qs_id); // 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 = ReviewPeer::doSelectOne(new Criteria()); $r_id = $r->getId(); $r->setReviewDate($control); $r->save(); $r2 = ReviewPeer::retrieveByPk($r_id); // Testing the DATE/TIME columns // ----------------------------- // that's the control timestamp. $control = strtotime('2004-02-29 00:00:00'); // should be two in the db $r = ReviewPeer::doSelectOne(new Criteria()); $r_id = $r->getId(); $r->setReviewDate($control); $r->save(); $r2 = ReviewPeer::retrieveByPk($r_id); // Testing the column validators // ----------------------------- $bk1 = new Book(); $bk1->setTitle("12345"); // min length is 10 $ret = $bk1->validate(); // Unique validator $bk2 = new Book(); $bk2->setTitle("Don Juan"); $ret = $bk2->validate(); // Now trying some more complex validation. $auth1 = new Author(); $auth1->setFirstName("Hans"); // last name required; will fail $bk1->setAuthor($auth1); $rev1 = new Review(); $rev1->setReviewDate("08/09/2001"); // will fail: reviewed_by column required $bk1->addReview($rev1); $ret2 = $bk1->validate(); $bk2 = new Book(); $bk2->setTitle("12345678901"); // passes $auth2 = new Author(); $auth2->setLastName("Blah"); //passes $auth2->setEmail("*****@*****.**"); //passes $auth2->setAge(50); //passes $bk2->setAuthor($auth2); $rev2 = new Review(); $rev2->setReviewedBy("Me!"); // passes $rev2->setStatus("new"); // passes $bk2->addReview($rev2); $ret3 = $bk2->validate(); // Testing doCount() functionality // ------------------------------- $c = new Criteria(); $count = BookPeer::doCount($c); // Testing 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(); // 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(); // re-fetch books and lists from db to be sure that nothing is cached $crit = new Criteria(); $crit->add(BookPeer::ID, $phoenix->getId()); $phoenix = BookPeer::doSelectOne($crit); $crit = new Criteria(); $crit->add(BookClubListPeer::ID, $blc1->getId()); $blc1 = BookClubListPeer::doSelectOne($crit); $crit = new Criteria(); $crit->add(BookClubListPeer::ID, $blc2->getId()); $blc2 = BookClubListPeer::doSelectOne($crit); $relCount = $phoenix->countBookListRels(); $relCount = $blc1->countBookListRels(); $relCount = $blc2->countBookListRels(); // Removing books that were just created // ------------------------------------- $hp = BookPeer::retrieveByPk($phoenix_id); $c = new Criteria(); $c->add(BookPeer::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(AuthorPeer::ID, $hp->getId()); $c->add(PublisherPeer::ID, $hp->getId()); $c->setSingleRecord(true); BookPeer::doDelete($c); // Attempting to delete books by complex criteria $c = new Criteria(); $cn = $c->getNewCriterion(BookPeer::ISBN, "043935806X"); $cn->addOr($c->getNewCriterion(BookPeer::ISBN, "0380977427")); $cn->addOr($c->getNewCriterion(BookPeer::ISBN, "0140422161")); $c->add($cn); BookPeer::doDelete($c); $td->delete(); AuthorPeer::doDelete($stephenson_id); AuthorPeer::doDelete($byron_id); $grass->delete(); PublisherPeer::doDelete($morrow_id); PublisherPeer::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(); }
function testDelete() { //Arrange $book_title = "Snow Crash"; $id = null; $test_book = new Book($book_title, $id); $test_book->save(); $book_title2 = "Ready Player One"; $id2 = null; $test_book2 = new Book($book_title2, $id2); $test_book2->save(); //Act $test_book->delete(); //Assert $this->assertEquals([$test_book2], Book::getAll()); }
function test_delete() { //Arrange $title = "World War Z"; $genre = "Horror"; $test_book = new Book($title, $genre); $test_book->save(); $name2 = "Billy Bartle-Barnaby"; $genre2 = "2015-07-09"; $test_book2 = new Book($name2, $genre2); $test_book2->save(); //Act $test_book->delete(); //Assert $result = Book::getAll(); $this->assertEquals($test_book2, $result[0]); }
function book_delete_form_submit($data) { $book = new Book(); $delete = $book->delete($data['id']); if ($delete['code'] == 200) { return $data['id']; } else { return FALSE; } }
function testDelete() { //Arrange $book_name = "Intro to Art"; $test_book = new Book($book_name); $test_book->save(); $book_name2 = "Everybody Poops"; $test_book2 = new Book($book_name2); $test_book2->save(); $name = "Arty"; $test_author = new Author($name); $test_author->save(); //Act $test_book->addAuthor($test_author); $test_book->delete(); //Assert $this->assertEquals([], $test_author->getBooks()); }
/** * Saves the book into de database. * If the id isn't setted, automatically assigns one * **/ public function save(){ $update = false ; $hasErrors = false; if(!isset($this->userId)) $this->userId = 1; //TODO: Remove only for debugging user must be always setted (logged user) $sql = "INSERT INTO ".table('books'). " (BookId, BookName, UserId) VALUES ($this->bookId,'$this->bookName',$this->userId)"; if (isset($this->bookId)) { // Edit book.. //Check if the the id is correct.. $res = mysql_query("SELECT BookId FROM ".table('books'). " where BookId=$this->bookId"); if(!$res){ $error = new GsError(302,"Error loading book."); if($error->isDebugging()){ $error->addContentElement("BookId",$BookId); $err = str_replace("'", '"', mysql_error()); $error->addContentElement("MySql Error",$err); } throw $error; $hasErrors = true; } $row = mysql_fetch_object($res); if (!$row) { //ERROR: trying to save a book that does exist. Must have null value the bookid if(!mysql_query($sql)){ $error = new GsError(302,"Error saving book. Book don't exists"); if($error->isDebugging()){ $error->addContentElement("BookId",$BookId); $err = str_replace("'", '"', mysql_error()); $error->addContentElement("MySql Error",$err); } throw $error; } }else { // OK: Delete.. and save it again $update = true; mysql_query("START TRANSACTION"); $book_tmp = new Book(); $book_tmp->load($this->bookId); $book_tmp->delete(true); if(!mysql_query($sql)){ $error = new GsError(302,"Error saving book. Book don't exists"); if($error->isDebugging()){ $error->addContentElement("BookId",$BookId); $err = str_replace("'", '"', mysql_error()); $error->addContentElement("MySql Error",$err); } throw $error; $hasErrors = true; } } }else { //SAVE AS... $sql = "INSERT INTO ".table('books'). " (BookName, UserId) VALUES ('$this->bookName',$this->userId)"; $query = mysql_query($sql); if($query) $this->bookId= mysql_insert_id(); else{ $error = new GsError(302,"Error saving book."); if($error->isDebugging()){ $error->addContentElement("BookId",$BookId); $err = str_replace("'", '"', mysql_error()); $error->addContentElement("MySql Error",$err); } throw $error; $hasErrors = true; } } //COMMON CODE.. if(!$hasErrors){ foreach ($this->sheets as $sheet) { $sheet->bookId = $this->bookId; $hasErrors = $sheet->save(); } } if(!$hasErrors){ foreach ($this->fontStyles as $fontStyle) { $fontStyle->bookId = $this->bookId; $hasErrors = $fontStyle->save(); } } if(!$hasErrors){ foreach ($this->layoutStyles as $layoutStyle) { $layoutStyle->bookId = $this->bookId; $hasErrors = $layoutStyle->save(); } } if ($update) { //if update means that a transaction was started.. //so check for errors and commit if ok if (!$hasErrors) mysql_query("COMMIT"); else mysql_query("ROLLBACK") ; } return $hasErrors; }
if ($_POST['action'] == 'submit') { if (isset($_POST['usertext'])) { $text = $_POST['usertext']; $book = new Book(); $book->add($text); } else { echo "sorry you have to enter name and comment"; } } elseif ($_POST['action'] == 'like') { if ($_POST['l_id']) { $id = $_POST['l_id']; $book = new Book(); $book->like($id); } } elseif ($_POST['action'] == 'delete') { if ($_POST['d_id']) { $id = $_POST['d_id']; $book = new Book(); $book->delete($id); } } elseif ($_POST['action'] == 'update') { if ($_POST['up_id']) { if ($_POST['new_comment']) { $new_comment = $_POST['new_comment']; $id = $_POST['up_id']; $book = new Book(); $book->update($id, $new_comment); } } } }
function testDelete() { //Arrange $title = "Harry Potter"; $id = 1; $test_book = new Book($title, $id); $test_book->save(); $name = "JK Rowling"; $id = 1; $test_author = new Author($name, $id); $test_author->save(); //Act $test_book->addAuthor($test_author); $test_book->delete(); //Assert $this->assertEquals([], $test_author->getBooks()); }
function test_delete() { //Arrange $title = "Whimsical Fairytales...and other stories"; $genre = "Fantasy"; $test_book = new Book($title, $genre); $test_book->save(); $title2 = "The Secret Life of Garden Gnomes"; $genre2 = "Nonfiction"; $test_book2 = new Book($title2, $genre2); $test_book2->save(); //Act $test_book->delete(); //Assert $result = Book::getAll(); $this->assertEquals($test_book2, $result[0]); }
function testDelete() { //Arrange $title = "Where the Red Fern Grows"; $year_published = 1961; $id = null; $test_book = new Book($title, $year_published, $id); $test_book->save(); $title2 = "Where the Wild Things Are"; $year_published2 = 1964; $test_book2 = new Book($title2, $year_published2, $id); $test_book2->save(); //Act $test_book->delete(); //Assert $this->assertEquals([$test_book2], Book::getAll()); }
print "\tFrom publisher table: "; $res2 = PublisherPeer::doSelect(new Criteria()); print boolTest(count($res2) === 3); print "\tFrom book table: "; $res3 = BookPeer::doSelect(new Criteria()); print boolTest(count($res3) === 3); print "Attempting to delete books by complex criteria: "; $c = new Criteria(); $cn = $c->getNewCriterion(BookPeer::ISBN, "043935806X"); $cn->addOr($c->getNewCriterion(BookPeer::ISBN, "0380977427")); $cn->addOr($c->getNewCriterion(BookPeer::ISBN, "0140422161")); $c->add($cn); BookPeer::doDelete($c); print boolTest(true); print "Attempting to delete book [id = {$td_id}]: "; $td->delete(); print boolTest(true); print "Attempting to delete author [id = {$stephenson_id}]: "; AuthorPeer::doDelete($stephenson_id); print boolTest(true); print "Attempting to delete author [id = {$byron_id}]: "; AuthorPeer::doDelete($byron_id); print boolTest(true); print "Attempting to delete author [id = {$grass_id}]: "; $grass->delete(); print boolTest(true); print "Attempting to delete publisher [id = {$morrow_id}]: "; PublisherPeer::doDelete($morrow_id); print boolTest(true); print "Attempting to delete publisher [id = {$penguin_id}]: "; PublisherPeer::doDelete($penguin_id);
<?php session_start(); include 'library.php'; $op = $_GET['op']; $lib = new Library(); if ($op == "add") { $lib->addBook($_GET['title'], $_GET['author']); } else { if ($op == "add_existing") { $lib->addBook_existing($_GET['id']); } else { if ($op == "delete") { $book = new Book($_GET['copyId'], 'copy'); $book->delete(); } else { if ($op == "shelves") { $lib = new Library(); $html = $lib->get_shelves_tableview('librarian'); echo $html; } } } }
public function testCreateDelete() { $connection = $this->getT4Connection(); Book::setConnection($connection); $book = new Book(); $book->title = 'War and peace'; $book->author = 'Tolstoi'; $this->assertTrue($book->isNew()); $this->assertFalse($book->isDeleted()); $book->save(); $this->assertFalse($book->isNew()); $this->assertTrue($book->wasNew()); $this->assertFalse($book->isDeleted()); $this->assertEquals(1, $book->getPk()); $book1 = Book::findByPK(1); $this->assertEquals(1, $book1->getPk()); $this->assertEquals('War and peace', $book1->title); $this->assertEquals('Tolstoi', $book1->author); $book->delete(); $this->assertFalse($book->isNew()); $this->assertTrue($book->wasNew()); $this->assertTrue($book->isDeleted()); $count = $connection->query("SELECT COUNT(*) FROM `books`")->fetchScalar(); $this->assertEquals(0, $count); }
/** * method Delete() * Delete a record */ function Delete($param) { try { // get the parameter $key $key = $param['key']; // open a transaction with database 'library' TTransaction::open('library'); // instantiates object Book $object = new Book($key); // deletes the object from the database $object->delete(); // close the transaction TTransaction::close(); // reload the listing $this->onReload(); // shows the success message new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted')); } catch (Exception $e) { // shows the exception error message new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // undo all pending operations TTransaction::rollback(); } }
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 = dirname(__FILE__) . '/../../etc/lob/tin_drum.gif'; $blob2_path = dirname(__FILE__) . '/../../etc/lob/propel.gif'; $clob_path = dirname(__FILE__) . '/../../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'); // Test Validators // --------------- require_once 'tools/helpers/bookstore/validator/ISBNValidator.php'; $bk1 = new Book(); $bk1->setTitle("12345"); // min length is 10 $ret = $bk1->validate(); $this->assertFalse($ret, 'validation failed'); $failures = $bk1->getValidationFailures(); $this->assertEquals(1, count($failures), '1 validation message was returned'); $el = array_shift($failures); $this->assertContains("must be more than", $el->getMessage(), 'Expected validation message was returned'); $bk2 = new Book(); $bk2->setTitle("Don Juan"); $ret = $bk2->validate(); $this->assertFalse($ret, 'validation failed'); $failures = $bk2->getValidationFailures(); $this->assertEquals(1, count($failures), '1 validation message was returned'); $el = array_shift($failures); $this->assertContains("Book title already in database.", $el->getMessage(), 'Expected validation message was returned'); //Now trying some more complex validation. $auth1 = new Author(); $auth1->setFirstName("Hans"); // last name required; will fail $bk1->setAuthor($auth1); $rev1 = new Review(); $rev1->setReviewDate("08/09/2001"); // will fail: reviewed_by column required $bk1->addReview($rev1); $ret2 = $bk1->validate(); $this->assertFalse($ret2, 'validation failed'); $failures2 = $bk1->getValidationFailures(); $this->assertEquals(3, count($failures2), '3 validation messages were returned'); $expectedKeys = array(AuthorPeer::LAST_NAME, BookPeer::TITLE, ReviewPeer::REVIEWED_BY); $this->assertEquals($expectedKeys, array_keys($failures2), 'correct columns failed'); $bk2 = new Book(); $bk2->setTitle("12345678901"); // passes $auth2 = new Author(); $auth2->setLastName("Blah"); //passes $auth2->setEmail("*****@*****.**"); //passes $auth2->setAge(50); //passes $bk2->setAuthor($auth2); $rev2 = new Review(); $rev2->setReviewedBy("Me!"); // passes $rev2->setStatus("new"); // passes $bk2->addReview($rev2); $ret3 = $bk2->validate(); $this->assertTrue($ret3, 'complex validation can pass'); // Testing doCount() functionality // ------------------------------- // old way $c = new Criteria(); $records = BookPeer::doSelect($c); $count = BookPeer::doCount($c); $this->assertEquals($count, count($records), 'correct number of results'); // new way $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(BookPeer::ID, $phoenix->getId()); $phoenix = BookPeer::doSelectOne($crit); $this->assertNotNull($phoenix, "book 'phoenix' has been re-fetched from db"); $crit = new Criteria(); $crit->add(BookClubListPeer::ID, $blc1->getId()); $blc1 = BookClubListPeer::doSelectOne($crit); $this->assertNotNull($blc1, 'BookClubList 1 has been re-fetched from db'); $crit = new Criteria(); $crit->add(BookClubListPeer::ID, $blc2->getId()); $blc2 = BookClubListPeer::doSelectOne($crit); $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(BookPeer::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(AuthorPeer::ID, $hp->getAuthor()->getId()); $c->add(PublisherPeer::ID, $hp->getPublisher()->getId()); $c->setSingleRecord(true); BookPeer::doDelete($c); // Checking to make sure correct records were removed. $this->assertEquals(3, AuthorPeer::doCount(new Criteria()), 'Correct records were removed from author table'); $this->assertEquals(3, PublisherPeer::doCount(new Criteria()), 'Correct records were removed from publisher table'); $this->assertEquals(3, BookPeer::doCount(new Criteria()), 'Correct records were removed from book table'); // Attempting to delete books by complex criteria BookQuery::create()->filterByISBN("043935806X")->orWhere('Book.ISBN = ?', "0380977427")->orWhere('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->assertEquals(array(), AuthorPeer::doSelect(new Criteria()), 'no records in [author] table'); $this->assertEquals(array(), PublisherPeer::doSelect(new Criteria()), 'no records in [publisher] table'); $this->assertEquals(array(), BookPeer::doSelect(new Criteria()), 'no records in [book] table'); $this->assertEquals(array(), ReviewPeer::doSelect(new Criteria()), 'no records in [review] table'); $this->assertEquals(array(), MediaPeer::doSelect(new Criteria()), 'no records in [media] table'); $this->assertEquals(array(), BookClubListPeer::doSelect(new Criteria()), 'no records in [book_club_list] table'); $this->assertEquals(array(), BookListRelPeer::doSelect(new Criteria()), 'no records in [book_x_list] table'); }
function testDeleteBook() { //Arrange $id = null; $name = "A Series of Unfortunate Events"; $test_book = new Book($id, $name); $test_book->save(); $name2 = "Fresh Off the Boat"; $test_book2 = new Book($id, $name2); $test_book2->save(); //Act $test_book->delete(); //Assert $this->assertEquals([$test_book2], Book::getAll()); }