/**
  * Test deleting an object using the delete() method.
  */
 public function testDelete()
 {
     BookstoreDataPopulator::populate();
     // 1) grab an arbitrary object
     $book = BookPeer::doSelectOne(new Criteria());
     $bookId = $book->getId();
     // 2) delete it
     $book->delete();
     // 3) make sure it can't be save()d now that it's deleted
     try {
         $book->setTitle("Will Fail");
         $book->save();
         $this->fail("Expect an exception to be thrown when attempting to save() a deleted object.");
     } catch (PropelException $e) {
     }
     // 4) make sure that it doesn't exist in db
     $book = BookPeer::retrieveByPK($bookId);
     $this->assertNull($book, "Expect NULL from retrieveByPK on deleted Book.");
 }
 public function testObjectInstances()
 {
     $sample = BookPeer::doSelectOne(new Criteria());
     $samplePk = $sample->getPrimaryKey();
     // 1) make sure consecutive calls to retrieveByPK() return the same object.
     $b1 = BookPeer::retrieveByPK($samplePk);
     $b2 = BookPeer::retrieveByPK($samplePk);
     $sampleval = md5(microtime());
     $this->assertTrue($b1 === $b2, "Expected object instances to match for calls with same retrieveByPK() method signature.");
     // 2) make sure that calls to doSelect also return references to the same objects.
     $allbooks = BookPeer::doSelect(new Criteria());
     foreach ($allbooks as $testb) {
         if ($testb->getPrimaryKey() == $b1->getPrimaryKey()) {
             $this->assertTrue($testb === $b1, "Expected same object instance from doSelect() as from retrieveByPK()");
         }
     }
     // 3) test fetching related objects
     $book = BookPeer::retrieveByPK($samplePk);
     $bookauthor = $book->getAuthor();
     $author = AuthorPeer::retrieveByPK($bookauthor->getId());
     $this->assertTrue($bookauthor === $author, "Expected same object instance when calling fk object accessor as retrieveByPK()");
     // 4) test a doSelectJoin()
     $morebooks = BookPeer::doSelectJoinAuthor(new Criteria());
     for ($i = 0, $j = 0; $j < count($morebooks); $i++, $j++) {
         $testb1 = $allbooks[$i];
         $testb2 = $allbooks[$j];
         $this->assertTrue($testb1 === $testb2, "Expected the same objects from consecutive doSelect() calls.");
         // we could probably also test this by just verifying that $book & $testb are the same
         if ($testb1->getPrimaryKey() === $book) {
             $this->assertTrue($book->getAuthor() === $testb1->getAuthor(), "Expected same author object in calls to pkey-matching books.");
         }
     }
     // 5) test creating a new object, saving it, and then retrieving that object (should all be same instance)
     $b = new BookstoreEmployee();
     $b->setName("Testing");
     $b->setJobTitle("Testing");
     $b->save();
     $empId = $b->getId();
     $this->assertSame($b, BookstoreEmployeePeer::retrieveByPK($empId), "Expected newly saved object to be same instance as pooled.");
 }
Beispiel #3
0
 public function testCountRefFk()
 {
     $book = new Book();
     $book->setTitle("Test Book");
     $book->setISBN("TT-EE-SS-TT");
     $num = 5;
     for ($i = 2; $i < $num + 2; $i++) {
         $r = new Review();
         $r->setReviewedBy('Hans ' . $num);
         $dt = new DateTime("now");
         $dt->modify("-" . $i . " weeks");
         $r->setReviewDate($dt);
         $r->setRecommended($i % 2 == 0);
         $book->addReview($r);
     }
     $this->assertEquals($num, $book->countReviews(), "Expected countReviews to return {$num}");
     $this->assertEquals($num, count($book->getReviews()), "Expected getReviews to return {$num} reviews");
     $book->save();
     BookPeer::clearInstancePool();
     ReviewPeer::clearInstancePool();
     $book = BookPeer::retrieveByPK($book->getId());
     $this->assertEquals($num, $book->countReviews(), "Expected countReviews() to return {$num} (after save)");
     $this->assertEquals($num, count($book->getReviews()), "Expected getReviews() to return {$num} (after save)");
     // Now set different criteria and expect different results
     $c = new Criteria();
     $c->add(ReviewPeer::RECOMMENDED, false);
     $this->assertEquals(floor($num / 2), $book->countReviews($c), "Expected " . floor($num / 2) . " results from countReviews(recomm=false)");
     // Change Criteria, run again -- expect different.
     $c = new Criteria();
     $c->add(ReviewPeer::RECOMMENDED, true);
     $this->assertEquals(ceil($num / 2), count($book->getReviews($c)), "Expected " . ceil($num / 2) . " results from getReviews(recomm=true)");
     $this->assertEquals($num, $book->countReviews(), "Expected countReviews to return {$num} with new empty Criteria");
 }
 /**
  * @see        testDoDeleteCompositePK()
  */
 private function createBookWithId($id)
 {
     $con = Propel::getServiceContainer()->getConnection(BookPeer::DATABASE_NAME);
     $b = BookPeer::retrieveByPK($id);
     if (!$b) {
         $b = new Book();
         $b->setTitle("Book{$id}")->setISBN("BookISBN{$id}")->save();
         $b1Id = $b->getId();
         $sql = "UPDATE " . BookPeer::TABLE_NAME . " SET id = ? WHERE id = ?";
         $stmt = $con->prepare($sql);
         $stmt->bindValue(1, $id);
         $stmt->bindValue(2, $b1Id);
         $stmt->execute();
     }
 }