public function testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookTableMap::clearInstancePool(); AuthorTableMap::clearInstancePool(); ReviewTableMap::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book'); $c->setFormatter(ModelCriteria::FORMAT_ON_DEMAND); $c->filterByTitle('The Tin Drum'); $c->join('Propel\\Tests\\Bookstore\\Book.Author'); $c->withColumn('Author.FirstName', 'AuthorName'); $c->withColumn('Author.LastName', 'AuthorName2'); $c->with('Author'); $c->limit(1); $con = Propel::getServiceContainer()->getConnection(BookTableMap::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, 'ObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getAuthor()->getFirstName(), 'ObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getVirtualColumn('AuthorName'), 'ObjectFormatter adds withColumns as virtual columns'); $this->assertEquals('Grass', $book->getVirtualColumn('AuthorName2'), 'ObjectFormatter correctly hydrates all virtual columns'); }
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(); BookTableMap::clearInstancePool(); ReviewTableMap::clearInstancePool(); $book = BookQuery::create()->findPk($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(ReviewTableMap::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(ReviewTableMap::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(); BookTableMap::clearInstancePool(); AuthorTableMap::clearInstancePool(); ReviewTableMap::clearInstancePool(); $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME); $book = BookQuery::create()->findOneByTitle('Harry Potter and the Order of the Phoenix', $con); $pk = $book->getPrimaryKey(); BookTableMap::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'); }
/** * Test the type sensitivity of the returning columns. * */ public function testTypeSensitive() { BookstoreDataPopulator::populate(); $book = BookQuery::create()->findOne(); $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. ReviewTableMap::clearInstancePool(); BookTableMap::clearInstancePool(); // reload and verify that the types are the same $r2 = ReviewQuery::create()->findPk($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('\\Propel\\Tests\\Bookstore\\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."); }