作者:RafalFilipe
项目:Propel
public function testFrom()
{
$q = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book');
$expected = new BookQuery();
$this->assertEquals($expected, $q, 'from() returns a Model query instance based on the model name');
$q = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book b');
$expected = new BookQuery();
$expected->setModelAlias('b');
$this->assertEquals($expected, $q, 'from() sets the model alias if found after the blank');
$q = PropelQuery::from('\\Propel\\Tests\\Runtime\\Query\\myBook');
$expected = new myBookQuery();
$this->assertEquals($expected, $q, 'from() can find custom query classes');
try {
$q = PropelQuery::from('Foo');
$this->fail('PropelQuery::from() throws an exception when called on a non-existing query class');
} catch (PropelException $e) {
$this->assertTrue(true, 'PropelQuery::from() throws an exception when called on a non-existing query class');
}
}
作者:kalaspuffa
项目:php-orm-benchmar
public function testSerializeHydratedObject()
{
$book = new Book();
$book->setTitle('Foo3');
$book->setISBN('1234');
$book->save();
BookTableMap::clearInstancePool();
$book = BookQuery::create()->findOneByTitle('Foo3');
$sb = serialize($book);
$this->assertEquals($book, unserialize($sb));
}
作者:rozwel
项目:Propel
public function testMagicFindByObject()
{
$con = Propel::getServiceContainer()->getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author');
$testAuthor = $c->findOne();
$q = BookQuery::create()->findByAuthor($testAuthor);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=" . $testAuthor->getId();
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXX($value) is turned into findBy(XXX, $value)');
$c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author');
$testAuthor = $c->findOne();
$q = BookQuery::create()->findByAuthorAndISBN($testAuthor, 1234);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=" . $testAuthor->getId() . " AND book.ISBN=1234";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXXAndYYY($value) is turned into findBy(array(XXX, YYY), $value)');
}
作者:dracon
项目:forked-php-orm-benchmar
public function testMagicRequireOneWithAndThrowsException()
{
$this->setExpectedException('\\Propel\\Runtime\\Exception\\EntityNotFoundException', 'Book could not be found');
BookQuery::create()->requireOneByTitleAndId('Not Existing Book', -1337);
}
作者:diside
项目:Propel
public function testDebugLog()
{
$con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME);
// save data to return to normal state after test
$logger = $con->getLogger();
$logMethods = $con->getLogMethods();
$testLog = new Logger('debug');
$handler = new LastMessageHandler();
$testLog->pushHandler($handler);
$con->setLogger($testLog);
$con->setLogMethods(['exec', 'query', 'execute', 'beginTransaction', 'commit', 'rollBack']);
$con->useDebug(true);
$con->beginTransaction();
// test transaction log
$this->assertEquals('Begin transaction', $handler->latestMessage, 'PropelPDO logs begin transaction in debug mode');
$con->commit();
$this->assertEquals('Commit transaction', $handler->latestMessage, 'PropelPDO logs commit transaction in debug mode');
$con->beginTransaction();
$con->rollBack();
$this->assertEquals('Rollback transaction', $handler->latestMessage, 'PropelPDO logs rollback transaction in debug mode');
$con->beginTransaction();
$handler->latestMessage = '';
$con->beginTransaction();
$this->assertEquals('', $handler->latestMessage, 'PropelPDO does not log nested begin transaction in debug mode');
$con->commit();
$this->assertEquals('', $handler->latestMessage, 'PropelPDO does not log nested commit transaction in debug mode');
$con->beginTransaction();
$con->rollBack();
$this->assertEquals('', $handler->latestMessage, 'PropelPDO does not log nested rollback transaction in debug mode');
$con->rollback();
// test query log
$con->beginTransaction();
$c = new Criteria();
$c->add(BookTableMap::COL_TITLE, 'Harry%s', Criteria::LIKE);
$books = BookQuery::create(null, $c)->find($con);
$latestExecutedQuery = $this->getSql("SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM book WHERE book.title LIKE 'Harry%s'");
$this->assertEquals($latestExecutedQuery, $handler->latestMessage, 'PropelPDO logs queries and populates bound parameters in debug mode');
BookTableMap::doDeleteAll($con);
$latestExecutedQuery = $this->getSql("DELETE FROM book");
$this->assertEquals($latestExecutedQuery, $handler->latestMessage, 'PropelPDO logs deletion queries in debug mode');
$latestExecutedQuery = 'DELETE FROM book WHERE 1=1';
$con->exec($latestExecutedQuery);
$this->assertEquals($latestExecutedQuery, $handler->latestMessage, 'PropelPDO logs exec queries in debug mode');
$con->commit();
// return to normal state after test
$con->setLogger($logger);
$con->setLogMethods($logMethods);
}
作者:norfi
项目:Propel
public function testGetResultsRespectsFormatter()
{
$this->createBooks(5);
$query = BookQuery::create();
$query->setFormatter(ModelCriteria::FORMAT_ARRAY);
$pager = new PropelModelPager($query, 4);
$pager->setPage(1);
$pager->init();
$this->assertTrue($pager->getResults() instanceof ArrayCollection, 'getResults() returns a PropelArrayCollection if the query uses array hydration');
}
作者:a-melnichu
项目:Movies-Dem
public function testGroupBy()
{
$params = array();
$c = BookQuery::create()->joinReview()->withColumn('COUNT(Review.Id)', 'Count')->groupById();
$result = $c->createSelectSql($params);
if ($this->runningOnPostgreSQL()) {
$sql = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, COUNT(review.ID) AS Count FROM book LEFT JOIN review ON (book.ID=review.BOOK_ID) GROUP BY book.ID,book.TITLE,book.ISBN,book.PRICE,book.PUBLISHER_ID,book.AUTHOR_ID';
} else {
$sql = $this->getSql('SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, COUNT(review.ID) AS Count FROM `book` LEFT JOIN `review` ON (book.ID=review.BOOK_ID) GROUP BY book.ID');
}
$this->assertEquals($sql, $result);
}
作者:KyleGosla
项目:Huge-Prope
public function testSubQueryCount()
{
$subCriteria = new BookQuery();
$c = new BookQuery();
$c->addSelectQuery($subCriteria, 'subCriteriaAlias');
$c->filterByPrice(20, Criteria::LESS_THAN);
$nbBooks = $c->count();
$query = Propel::getConnection()->getLastExecutedQuery();
$sql = $this->getSql("SELECT COUNT(*) FROM (SELECT subCriteriaAlias.id, subCriteriaAlias.title, subCriteriaAlias.isbn, subCriteriaAlias.price, subCriteriaAlias.publisher_id, subCriteriaAlias.author_id FROM (SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM book) AS subCriteriaAlias WHERE subCriteriaAlias.price<20) propelmatch4cnt");
$this->assertEquals($sql, $query, 'addSelectQuery() doCount is defined as complexQuery');
}
作者:bondarovic
项目:Propel
public function testCountableInterface()
{
BookQuery::create()->deleteAll();
$pager = $this->getPager(10);
$this->assertCount(0, $pager);
$this->createBooks(15);
$pager = $this->getPager(10);
$this->assertCount(10, $pager);
$pager = $this->getPager(10, 2);
$this->assertCount(5, $pager);
}
作者:nald
项目:cyberde
public function testGroupBy()
{
$params = [];
$c = BookQuery::create()->joinReview()->withColumn('COUNT(Review.id)', 'Count')->groupById();
$result = $c->createSelectSql($params);
if ($this->runningOnPostgreSQL()) {
$sql = 'SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id, COUNT(review.id) AS Count FROM book LEFT JOIN review ON (book.id=review.book_id) GROUP BY book.id,book.title,book.isbn,book.price,book.publisher_id,book.author_id';
} else {
$sql = $this->getSql('SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id, COUNT(review.id) AS Count FROM book LEFT JOIN review ON (book.id=review.book_id) GROUP BY book.id');
}
$this->assertEquals($sql, $result);
}
作者:dracon
项目:forked-php-orm-benchmar
public function testPopulateRelationManyToOne()
{
$con = Propel::getServiceContainer()->getReadConnection(BookTableMap::DATABASE_NAME);
AuthorTableMap::clearInstancePool();
BookTableMap::clearInstancePool();
$books = BookQuery::create()->find($con);
$count = $con->getQueryCount();
$books->populateRelation('Author', null, $con);
foreach ($books as $book) {
$author = $book->getAuthor();
}
$this->assertEquals($count + 1, $con->getQueryCount(), 'populateRelation() populates a many-to-one relationship with a single supplementary query');
}
作者:diside
项目:Propel
public function testRemoveObjectOneToManyWithFkRequired()
{
BookSummaryQuery::create()->deleteAll();
BookQuery::create()->deleteAll();
$bookSummary = new BookSummary();
$bookSummary->setSummary('summary Propel Book');
$bookSummary2 = new BookSummary();
$bookSummary2->setSummary('summary2 Propel Book');
$book = new Book();
$book->setTitle('Propel Book');
$book->setISBN('01234');
$book->addBookSummary($bookSummary);
$book->addBookSummary($bookSummary2);
$this->assertCount(2, $book->getBookSummaries());
$book->removeBookSummary($bookSummary);
$bookSummaries = $book->getBookSummaries();
$this->assertCount(1, $bookSummaries);
$this->assertEquals('summary2 Propel Book', $bookSummaries->getFirst()->getSummary());
$book->save();
$bookSummary2->save();
$this->assertEquals(1, BookQuery::create()->count(), 'One Book');
$this->assertEquals(1, BookSummaryQuery::create()->count(), 'One Summary');
$this->assertEquals(1, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
$book->addBookSummary($bookSummary);
$bookSummary->save();
$book->save();
$this->assertEquals(2, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
$book->removeBookSummary($bookSummary2);
$book->save();
$this->assertEquals(1, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
$this->assertEquals(1, BookSummaryQuery::create()->count(), 'One Book summary because FK is required so book summary is deleted when book is saved');
}
作者:KyleGosla
项目:Huge-Prope
public function testMagicFindByObject()
{
$con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author');
$testAuthor = $c->findOne();
$q = BookQuery::create()->findByAuthor($testAuthor);
$expectedSQL = $this->getSql("SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM book WHERE book.author_id=" . $testAuthor->getId());
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXX($value) is turned into findBy(XXX, $value)');
$c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author');
$testAuthor = $c->findOne();
$q = BookQuery::create()->findByAuthorAndISBN($testAuthor, 1234);
$expectedSQL = $this->getSql("SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM book WHERE book.author_id=" . $testAuthor->getId() . " AND book.isbn=1234");
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXXAndYYY($value) is turned into findBy(array(XXX, YYY), $value)');
}
作者:rouff
项目:Propel
public function testSetterOneToManyReplacesOldObjectsByNewObjects()
{
// Ensure no data
BookQuery::create()->deleteAll();
AuthorQuery::create()->deleteAll();
$books = new ObjectCollection();
foreach (array('foo', 'bar') as $title) {
$b = new Book();
$b->setTitle($title);
$books[] = $b;
}
$a = new Author();
$a->setBooks($books);
$a->save();
$books = $a->getBooks();
$this->assertEquals('foo', $books[0]->getTitle());
$this->assertEquals('bar', $books[1]->getTitle());
$books = new ObjectCollection();
foreach (array('bam', 'bom') as $title) {
$b = new Book();
$b->setTitle($title);
$books[] = $b;
}
$a->setBooks($books);
$a->save();
$books = $a->getBooks();
$this->assertEquals('bam', $books[0]->getTitle());
$this->assertEquals('bom', $books[1]->getTitle());
$this->assertEquals(1, AuthorQuery::create()->count());
$this->assertEquals(2, BookQuery::create()->count());
}
作者:dracon
项目:forked-php-orm-benchmar
public function testLobSetting_WriteMode()
{
$blob_path = $this->getLobFile('tin_drum.gif');
$blob2_path = $this->getLobFile('propel.gif');
$clob_path = $this->getLobFile('tin_drum.txt');
$book = BookQuery::create()->findOne();
$m1 = new Media();
$m1->setBook($book);
$m1->setCoverImage(file_get_contents($blob_path));
$m1->setExcerpt(file_get_contents($clob_path));
$m1->save();
MediaTableMap::clearInstancePool();
// make sure we have the latest from the db:
$m2 = MediaQuery::create()->findPk($m1->getId());
// now attempt to assign a temporary stream, opened in 'w' mode, to the db
$stream = fopen("php://memory", 'w');
fwrite($stream, file_get_contents($blob2_path));
$m2->setCoverImage($stream);
$m2->save();
fclose($stream);
$m2->reload();
$this->assertEquals(md5(file_get_contents($blob2_path)), md5(stream_get_contents($m2->getCoverImage())), "Expected contents to match when setting stream w/ 'w' mode");
$stream2 = fopen("php://memory", 'w+');
fwrite($stream2, file_get_contents($blob_path));
rewind($stream2);
$this->assertEquals(md5(file_get_contents($blob_path)), md5(stream_get_contents($stream2)), "Expecting setup to be correct");
$m2->setCoverImage($stream2);
$m2->save();
$m2->reload();
$this->assertEquals(md5(file_get_contents($blob_path)), md5(stream_get_contents($m2->getCoverImage())), "Expected contents to match when setting stream w/ 'w+' mode");
}
作者:dracon
项目:forked-php-orm-benchmar
public function testToArrayIncludesForeignObjects()
{
BookstoreDataPopulator::populate();
BookTableMap::clearInstancePool();
AuthorTableMap::clearInstancePool();
PublisherTableMap::clearInstancePool();
$c = new Criteria();
$c->add(BookTableMap::COL_TITLE, 'Don Juan');
$books = BookQuery::create(null, $c)->joinWith('Author')->find();
$book = $books[0];
$arr1 = $book->toArray(TableMap::TYPE_PHPNAME, null, array(), true);
$expectedKeys = array('Id', 'Title', 'ISBN', 'Price', 'PublisherId', 'AuthorId', 'Author');
$this->assertEquals($expectedKeys, array_keys($arr1), 'toArray() can return sub arrays for hydrated related objects');
$this->assertEquals('George', $arr1['Author']['FirstName'], 'toArray() can return sub arrays for hydrated related objects');
}
作者:diside
项目:Propel
public function testFindOneWithLeftJoinWithOneToManyAndNullObjectsAndWithAdditionalJoins()
{
BookTableMap::clearInstancePool();
AuthorTableMap::clearInstancePool();
BookOpinionTableMap::clearInstancePool();
BookReaderTableMap::clearInstancePool();
$freud = new Author();
$freud->setFirstName("Sigmund");
$freud->setLastName("Freud");
$freud->save($this->con);
$publisher = new Publisher();
$publisher->setName('Psycho Books');
$publisher->save();
$book = new Book();
$book->setAuthor($freud);
$book->setTitle('Weirdness');
$book->setIsbn('abc123456');
$book->setPrice('14.99');
$book->setPublisher($publisher);
$book->save();
$query = BookQuery::create()->filterByTitle('Weirdness')->innerJoinAuthor()->useBookOpinionQuery(null, Criteria::LEFT_JOIN)->leftJoinBookReader()->endUse()->with('Author')->with('BookOpinion')->with('BookReader');
$books = $query->find($this->con)->get(0);
$this->assertEquals(0, count($books->getBookOpinions()));
}
作者:kalaspuffa
项目:php-orm-benchmar
public function testSelectArrayPaginate()
{
BookstoreDataPopulator::depopulate($this->con);
BookstoreDataPopulator::populate($this->con);
$pager = BookQuery::create()->select(array('Id', 'Title', 'ISBN', 'Price'))->paginate(1, 10, $this->con);
$this->assertInstanceOf('Propel\\Runtime\\Util\\PropelModelPager', $pager);
foreach ($pager as $result) {
$this->assertEquals(array('Id', 'Title', 'ISBN', 'Price'), array_keys($result));
}
}
作者:rouff
项目:Propel
public function testFindPkWithOneToMany()
{
BookstoreDataPopulator::populate();
BookPeer::clearInstancePool();
AuthorPeer::clearInstancePool();
ReviewPeer::clearInstancePool();
$con = Propel::getServiceContainer()->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');
}
作者:robin85
项目:Propel
public function testSetterOneToManyReplacesOldObjectsByNewObjectsWithFkRequired()
{
// Ensure no data
BookSummaryQuery::create()->deleteAll();
BookQuery::create()->deleteAll();
$bookSummaries = new ObjectCollection();
foreach (array('foo', 'bar') as $summary) {
$s = new BookSummary();
$s->setSummary($summary);
$bookSummaries[] = $s;
}
$b = new Book();
$b->setTitle('Hello');
$b->setBookSummaries($bookSummaries);
$b->save();
$bookSummaries = $b->getBookSummaries();
$this->assertEquals('foo', $bookSummaries[0]->getSummary());
$this->assertEquals('bar', $bookSummaries[1]->getSummary());
$bookSummaries = new ObjectCollection();
foreach (array('bam', 'bom') as $summary) {
$s = new BookSummary();
$s->setSummary($summary);
$bookSummaries[] = $s;
}
$b->setBookSummaries($bookSummaries);
$b->save();
$bookSummaries = $b->getBookSummaries();
$this->assertEquals('bam', $bookSummaries[0]->getSummary());
$this->assertEquals('bom', $bookSummaries[1]->getSummary());
$this->assertEquals(1, BookQuery::create()->count());
$this->assertEquals(2, BookSummaryQuery::create()->count());
}