/** * This is run after each unit test. It empties the database. */ protected function tearDown() { BookstoreDataPopulator::depopulate(); $this->assertEquals(0, count(BookPeer::doSelect(new Criteria())), "Expect book table to be empty."); $this->assertEquals(0, count(AuthorPeer::doSelect(new Criteria())), "Expect author table to be empty."); $this->assertEquals(0, count(PublisherPeer::doSelect(new Criteria())), "Expect publisher table to be empty."); $this->assertEquals(0, count(ReviewPeer::doSelect(new Criteria())), "Expect review table to be empty."); $this->assertEquals(0, count(MediaPeer::doSelect(new Criteria())), "Expect media table to be empty."); $this->assertEquals(0, count(BookstoreEmployeePeer::doSelect(new Criteria())), "Expect bookstore_employee table to be empty."); $this->assertEquals(0, count(BookstoreEmployeeAccountPeer::doSelect(new Criteria())), "Expect bookstore_employee_account table to be empty."); $this->assertEquals(0, count(BookstoreSalePeer::doSelect(new Criteria())), "Expect bookstore_sale table to be empty."); BookPeer::clearInstancePool(); $this->assertEquals(0, count(BookPeer::$instances), "Expected 0 Book instances after clearInstancePool()"); AuthorPeer::clearInstancePool(); $this->assertEquals(0, count(AuthorPeer::$instances), "Expected 0 Author instances after clearInstancePool()"); PublisherPeer::clearInstancePool(); $this->assertEquals(0, count(PublisherPeer::$instances), "Expected 0 Publisher instances after clearInstancePool()"); ReviewPeer::clearInstancePool(); $this->assertEquals(0, count(ReviewPeer::$instances), "Expected 0 Review instances after clearInstancePool()"); MediaPeer::clearInstancePool(); $this->assertEquals(0, count(MediaPeer::$instances), "Expected 0 Media instances after clearInstancePool()"); BookstoreEmployeePeer::clearInstancePool(); $this->assertEquals(0, count(BookstoreEmployeePeer::$instances), "Expected 0 BookstoreEmployee instances after clearInstancePool()"); BookstoreEmployeeAccountPeer::clearInstancePool(); $this->assertEquals(0, count(BookstoreEmployeeAccountPeer::$instances), "Expected 0 BookstoreEmployeeAccount instances after clearInstancePool()"); BookstoreSalePeer::clearInstancePool(); $this->assertEquals(0, count(BookstoreSalePeer::$instances), "Expected 0 BookstoreSale instances after clearInstancePool()"); parent::tearDown(); }
public function testFindPkWithOneToMany() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $book = BookQuery::create()->findOneByTitle('Harry Potter and the Order of the Phoenix', $con); $pk = $book->getPrimaryKey(); BookPeer::clearInstancePool(); $book = BookQuery::create()->setFormatter(ModelCriteria::FORMAT_ARRAY)->joinWith('Review')->findPk($pk, $con); $reviews = $book['Reviews']; $this->assertEquals(2, count($reviews), 'Related objects are correctly hydrated'); }
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"); }
public function testFindPkWithOneToMany() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $book = BookQuery::create()->findOneByTitle('Harry Potter and the Order of the Phoenix', $con); $pk = $book->getPrimaryKey(); BookPeer::clearInstancePool(); $book = BookQuery::create()->joinWith('Review')->findPk($pk, $con); $count = $con->getQueryCount(); $reviews = $book->getReviews(); $this->assertEquals($count, $con->getQueryCount(), 'with() hydrates the related objects to save a query '); $this->assertEquals(2, count($reviews), 'Related objects are correctly hydrated'); }
public function testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Book'); $c->setFormatter(ModelCriteria::FORMAT_ON_DEMAND); $c->filterByTitle('The Tin Drum'); $c->join('Book.Author'); $c->withColumn('Author.FirstName', 'AuthorName'); $c->withColumn('Author.LastName', 'AuthorName2'); $c->with('Author'); $c->limit(1); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $books = $c->find($con); foreach ($books as $book) { break; } $this->assertTrue($book instanceof Book, 'withColumn() do not change the resulting model class'); $this->assertEquals('The Tin Drum', $book->getTitle()); $this->assertTrue($book->getAuthor() instanceof Author, 'PropelObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getAuthor()->getFirstName(), 'PropelObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getVirtualColumn('AuthorName'), 'PropelObjectFormatter adds withColumns as virtual columns'); $this->assertEquals('Grass', $book->getVirtualColumn('AuthorName2'), 'PropelObjectFormatter correctly hydrates all virtual columns'); }
/** * Test the type sensitivity of the resturning columns. * */ public function testTypeSensitive() { BookstoreDataPopulator::populate(); $book = BookPeer::doSelectOne(new Criteria()); $r = new Review(); $r->setReviewedBy("testTypeSensitive Tester"); $r->setReviewDate(time()); $r->setBook($book); $r->setRecommended(true); $r->save(); $id = $r->getId(); unset($r); // clear the instance cache to force reload from database. ReviewPeer::clearInstancePool(); BookPeer::clearInstancePool(); // reload and verify that the types are the same $r2 = ReviewPeer::retrieveByPK($id); $this->assertInternalType('integer', $r2->getId(), "Expected getId() to return an integer."); $this->assertInternalType('string', $r2->getReviewedBy(), "Expected getReviewedBy() to return a string."); $this->assertInternalType('boolean', $r2->getRecommended(), "Expected getRecommended() to return a boolean."); $this->assertInstanceOf('Book', $r2->getBook(), "Expected getBook() to return a Book."); $this->assertInternalType('float', $r2->getBook()->getPrice(), "Expected Book->getPrice() to return a float."); $this->assertInstanceOf('DateTime', $r2->getReviewDate(null), "Expected Book->getReviewDate() to return a DateTime."); }
/** * Method perform a DELETE on the database, given a Review or Criteria object OR a primary key value. * * @param mixed $values Criteria or Review object or primary key or array of primary keys * which is used to create the DELETE statement * @param PropelPDO $con the connection to use * @return int The number of affected rows (if supported by underlying database driver). This includes CASCADE-related rows * if supported by native driver or if emulated using Propel. * @throws PropelException Any exceptions caught during processing will be * rethrown wrapped into a PropelException. */ public static function doDelete($values, PropelPDO $con = null) { if ($con === null) { $con = Propel::getConnection(ReviewPeer::DATABASE_NAME, Propel::CONNECTION_WRITE); } if ($values instanceof Criteria) { // invalidate the cache for all objects of this type, since we have no // way of knowing (without running a query) what objects should be invalidated // from the cache based on this Criteria. ReviewPeer::clearInstancePool(); // rename for clarity $criteria = clone $values; } elseif ($values instanceof Review) { // it's a model object // invalidate the cache for this single object ReviewPeer::removeInstanceFromPool($values); // create criteria based on pk values $criteria = $values->buildPkeyCriteria(); } else { // it's a primary key, or an array of pks $criteria = new Criteria(self::DATABASE_NAME); $criteria->add(ReviewPeer::ID, (array) $values, Criteria::IN); // invalidate the cache for this object(s) foreach ((array) $values as $singleval) { ReviewPeer::removeInstanceFromPool($singleval); } } // Set the correct dbName $criteria->setDbName(self::DATABASE_NAME); $affectedRows = 0; // initialize var to track total num of affected rows try { // use transaction because $criteria could contain info // for more than one table or we could emulating ON DELETE CASCADE, etc. $con->beginTransaction(); $affectedRows += BasePeer::doDelete($criteria, $con); ReviewPeer::clearRelatedInstancePool(); $con->commit(); return $affectedRows; } catch (PropelException $e) { $con->rollBack(); throw $e; } }