/** * 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 testFormat() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); BookstoreEmployeePeer::clearInstancePool(); $stmt = $con->query('SELECT * FROM bookstore_employee'); $formatter = new PropelObjectFormatter(); $formatter->init(new ModelCriteria('bookstore', 'BookstoreEmployee')); $emps = $formatter->format($stmt); $expectedClass = array('b1' => 'BookstoreEmployee', 'b2' => 'BookstoreManager', 'b3' => 'BookstoreCashier'); foreach ($emps as $emp) { $this->assertEquals($expectedClass[$emp->getName()], get_class($emp), 'format() creates objects of the correct class when using inheritance'); } }
public function testFindOneWithRelationName() { BookstoreDataPopulator::populate(); BookstoreEmployeePeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'BookstoreEmployee'); $c->setFormatter(ModelCriteria::FORMAT_ARRAY); $c->join('BookstoreEmployee.Supervisor s'); $c->with('s'); $c->where('s.Name = ?', 'John'); $emp = $c->findOne(); $this->assertEquals($emp['Name'], 'Pieter', 'Main object is correctly hydrated'); $sup = $emp['Supervisor']; $this->assertEquals($sup['Name'], 'John', 'Related object is correctly hydrated'); }
/** * Test foreign key relationships based on references to unique cols but not PK. * @link http://trac.propelorm.org/ticket/691 */ public function testUniqueFkRel() { BookstoreEmployeeAccountPeer::doDeleteAll(); $employee = new BookstoreEmployee(); $employee->setName("Johnny Walker"); $acct = new BookstoreEmployeeAccount(); $acct->setBookstoreEmployee($employee); $acct->setLogin("test-login"); $acct->save(); $acctId = $acct->getEmployeeId(); $al = new AcctAuditLog(); $al->setBookstoreEmployeeAccount($acct); $al->save(); $alId = $al->getId(); BookstoreEmployeePeer::clearInstancePool(); BookstoreEmployeeAccountPeer::clearInstancePool(); AcctAuditLogPeer::clearInstancePool(); $al2 = AcctAuditLogPeer::retrieveByPK($alId); /* @var $al2 AcctAuditLog */ $mapacct = $al2->getBookstoreEmployeeAccount(); $lookupacct = BookstoreEmployeeAccountPeer::retrieveByPK($acctId); $logs = $lookupacct->getAcctAuditLogs(); $this->assertTrue(count($logs) == 1, "Expected 1 audit log result."); $this->assertEquals($logs[0]->getId(), $al->getId(), "Expected returned audit log to match created audit log."); }
public function testFindOneWithRelationName() { BookstoreDataPopulator::populate(); BookstoreEmployeePeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'BookstoreEmployee'); $c->join('BookstoreEmployee.Supervisor s'); $c->with('s'); $c->where('s.Name = ?', 'John'); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $emp = $c->findOne($con); $count = $con->getQueryCount(); $this->assertEquals($emp->getName(), 'Pieter', 'Main object is correctly hydrated'); $sup = $emp->getSupervisor(); $this->assertEquals($count, $con->getQueryCount(), 'with() hydrates the related objects to save a query'); $this->assertEquals($sup->getName(), 'John', 'Related object is correctly hydrated'); }
public function testJoinAliasQuery() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Book', 'b'); $c->join('b.Author a'); $c->where('a.FirstName = ?', 'Leo'); $books = BookPeer::doSelect($c, $con); $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo'"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in where()'); $c = new ModelCriteria('bookstore', 'BookstoreEmployee', 'be'); $c->join('be.Supervisor sup'); $c->join('sup.Subordinate sub'); $c->where('sub.Name = ?', 'Foo'); $employees = BookstoreEmployeePeer::doSelect($c, $con); $expectedSQL = "SELECT bookstore_employee.ID, bookstore_employee.CLASS_KEY, bookstore_employee.NAME, bookstore_employee.JOB_TITLE, bookstore_employee.SUPERVISOR_ID FROM `bookstore_employee` INNER JOIN `bookstore_employee` `sup` ON (bookstore_employee.SUPERVISOR_ID=sup.ID) INNER JOIN `bookstore_employee` `sub` ON (sup.ID=sub.SUPERVISOR_ID) WHERE sub.NAME = 'Foo'"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in further joins()'); }
public function testFormatSingleTableInheritanceManyResults() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); BookstoreDataPopulator::populate($con); $stmt = $con->query('SELECT * FROM bookstore_employee'); $formatter = new PropelOnDemandFormatter(); $formatter->init(new ModelCriteria('bookstore', 'BookstoreEmployee')); $employees = $formatter->format($stmt); foreach ($employees as $employee) { $row = array(); $row[1] = $employee->getClassKey(); $omClass = BookstoreEmployeePeer::getOMClass($row, 0, false); $actualClass = get_class($employee); $this->assertEquals($omClass, $actualClass, 'PropelOnDemandFormatter::format() should handle single table inheritance'); } }
/** * Test hydration of joined rows that contain lazy load columns. * @link http://propel.phpdb.org/trac/ticket/464 */ public function testHydrationJoinLazyLoad() { BookstoreEmployeeAccountPeer::doDeleteAll(); BookstoreEmployeePeer::doDeleteAll(); AcctAccessRolePeer::doDeleteAll(); $bemp2 = new BookstoreEmployee(); $bemp2->setName("Pieter"); $bemp2->setJobTitle("Clerk"); $bemp2->save(); $role = new AcctAccessRole(); $role->setName("Admin"); $bempacct = new BookstoreEmployeeAccount(); $bempacct->setBookstoreEmployee($bemp2); $bempacct->setAcctAccessRole($role); $bempacct->setLogin("john"); $bempacct->setPassword("johnp4ss"); $bempacct->save(); $c = new Criteria(); $results = BookstoreEmployeeAccountPeer::doSelectJoinAll($c); $o = $results[0]; $this->assertEquals('Admin', $o->getAcctAccessRole()->getName()); }
public function testGetRelation() { set_include_path(get_include_path() . PATH_SEPARATOR . "fixtures/bookstore/build/classes"); Propel::init('fixtures/bookstore/build/conf/bookstore-conf.php'); $bookTable = BookPeer::getTableMap(); $titleColumn = $bookTable->getColumn('TITLE'); $this->assertNull($titleColumn->getRelation(), 'getRelation() returns null for non-foreign key columns'); $publisherColumn = $bookTable->getColumn('PUBLISHER_ID'); $this->assertEquals($publisherColumn->getRelation(), $bookTable->getRelation('Publisher'), 'getRelation() returns the RelationMap object for this foreign key'); $bookstoreTable = BookstoreEmployeePeer::getTableMap(); $supervisorColumn = $bookstoreTable->getColumn('SUPERVISOR_ID'); $this->assertEquals($supervisorColumn->getRelation(), $supervisorColumn->getRelation('Supervisor'), 'getRelation() returns the RelationMap object even whit ha specific refPhpName'); }
public function testGetRelation() { $bookTable = BookPeer::getTableMap(); $titleColumn = $bookTable->getColumn('title'); $this->assertNull($titleColumn->getRelation(), 'getRelation() returns null for non-foreign key columns'); $publisherColumn = $bookTable->getColumn('publisher_id'); $this->assertEquals($publisherColumn->getRelation(), $bookTable->getRelation('Publisher'), 'getRelation() returns the RelationMap object for this foreign key'); $bookstoreTable = BookstoreEmployeePeer::getTableMap(); $supervisorColumn = $bookstoreTable->getColumn('supervisor_id'); $this->assertEquals($supervisorColumn->getRelation(), $supervisorColumn->getRelation('Supervisor'), 'getRelation() returns the RelationMap object even without specific refPhpName'); }
/** * Test copyInto method. */ public function testCopyInto_Deep() { BookstoreDataPopulator::populate(); // Test a "normal" object $c = new Criteria(); $c->add(BookPeer::TITLE, 'Harry%', Criteria::LIKE); $book = BookPeer::doSelectOne($c); $reviews = $book->getReviews(); $b2 = $book->copy(true); $this->assertInstanceOf('Book', $b2); $this->assertNull($b2->getId()); $r2 = $b2->getReviews(); $this->assertEquals(count($reviews), count($r2)); // Test a one-to-one object $emp = BookstoreEmployeePeer::doSelectOne(new Criteria()); $e2 = $emp->copy(true); $this->assertInstanceOf('BookstoreEmployee', $e2); $this->assertNull($e2->getId()); $this->assertEquals($emp->getBookstoreEmployeeAccount()->getLogin(), $e2->getBookstoreEmployeeAccount()->getLogin()); }
public static function depopulate() { AcctAccessRolePeer::doDeleteAll(); AuthorPeer::doDeleteAll(); BookstorePeer::doDeleteAll(); BookstoreContestPeer::doDeleteAll(); BookstoreContestEntryPeer::doDeleteAll(); BookstoreEmployeePeer::doDeleteAll(); BookstoreEmployeeAccountPeer::doDeleteAll(); BookstoreSalePeer::doDeleteAll(); BookClubListPeer::doDeleteAll(); BookOpinionPeer::doDeleteAll(); BookReaderPeer::doDeleteAll(); BookListRelPeer::doDeleteAll(); BookPeer::doDeleteAll(); ContestPeer::doDeleteAll(); CustomerPeer::doDeleteAll(); MediaPeer::doDeleteAll(); PublisherPeer::doDeleteAll(); ReaderFavoritePeer::doDeleteAll(); ReviewPeer::doDeleteAll(); }
public function testJoinAliasQuery() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Book', 'b'); $c->join('b.Author a'); $c->where('a.FirstName = ?', 'Leo'); $books = BookPeer::doSelect($c, $con); $expectedSQL = "SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM `book` INNER JOIN `author` `a` ON (book.author_id=a.id) WHERE a.first_name = 'Leo'"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in where()'); $c = new ModelCriteria('bookstore', 'BookstoreEmployee', 'be'); $c->join('be.Supervisor sup'); $c->join('sup.Subordinate sub'); $c->where('sub.Name = ?', 'Foo'); $employees = BookstoreEmployeePeer::doSelect($c, $con); $expectedSQL = "SELECT bookstore_employee.id, bookstore_employee.class_key, bookstore_employee.name, bookstore_employee.job_title, bookstore_employee.supervisor_id FROM `bookstore_employee` INNER JOIN `bookstore_employee` `sup` ON (bookstore_employee.supervisor_id=sup.id) INNER JOIN `bookstore_employee` `sub` ON (sup.id=sub.supervisor_id) WHERE sub.name = 'Foo'"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in further joins()'); }
public static function depopulate($con = null) { if ($con === null) { $con = Propel::getConnection(BookPeer::DATABASE_NAME); } $con->beginTransaction(); AuthorPeer::doDeleteAll($con); BookstorePeer::doDeleteAll($con); BookstoreContestPeer::doDeleteAll($con); BookstoreContestEntryPeer::doDeleteAll($con); BookstoreEmployeePeer::doDeleteAll($con); BookstoreEmployeeAccountPeer::doDeleteAll($con); BookstoreSalePeer::doDeleteAll($con); BookClubListPeer::doDeleteAll($con); BookOpinionPeer::doDeleteAll($con); BookReaderPeer::doDeleteAll($con); BookListRelPeer::doDeleteAll($con); BookPeer::doDeleteAll($con); ContestPeer::doDeleteAll($con); CustomerPeer::doDeleteAll($con); MediaPeer::doDeleteAll($con); PublisherPeer::doDeleteAll($con); ReaderFavoritePeer::doDeleteAll($con); ReviewPeer::doDeleteAll($con); $con->commit(); }