$t->is($finder->getLatestQuery(), propel_sql('SELECT [P12article.ID, ]category.NAME AS "Category.Name", article.TITLE AS "Article.Title" FROM article INNER JOIN category ON (article.CATEGORY_ID=category.ID) LIMIT 1'), 'find() does not request twice the columns added by way of withColumn() and select()');
$expectedRow = array(
  'Article.Title' => 'art1',
  'Category.Name' => 'cat1'
);
$t->is_deeply($row, $expectedRow, 'select() can select columns added by way of withColumn()');

$finder = sfPropelFinder::from('Article')->
  join('Category')->
  withColumn('Category.Name', 'cname')->
  select(array('Article.Title', 'cname'));
$row = $finder->findOne();
$t->is($finder->getLatestQuery(), propel_sql('SELECT [P12article.ID, ]category.NAME AS cname, article.TITLE AS "Article.Title" FROM article INNER JOIN category ON (article.CATEGORY_ID=category.ID) LIMIT 1'), 'select() uses any alias specified in withColumn() in the query');
$expectedRow = array(
  'Article.Title' => 'art1',
  'cname'         => 'cat1'
);
$t->is_deeply($row, $expectedRow, 'select() uses any alias specified in withColumn() in the result');

$finder = sfPropelFinder::from('Article')->
  join('Category')->
  withColumn('Category.Name')->
  withColumn('Article.Title')->
  select(array('Article.Title', 'Category.Name'));
$row = $finder->findOne();
$t->is($finder->getLatestQuery(), propel_sql('SELECT [P12article.ID, ]category.NAME AS "Category.Name", article.TITLE AS "Article.Title" FROM article INNER JOIN category ON (article.CATEGORY_ID=category.ID) LIMIT 1'), 'select() can cope with only columns added with withColumn()');
$expectedRow = array(
  'Article.Title' => 'art1',
  'Category.Name' => 'cat1'
);
$t->is_deeply($row, $expectedRow, 'select() can cope with only columns added with withColumn()');
$finder = sfPropelFinder::from('Human')->
  join('Human father', 'Human.FatherId', 'father.Id', 'INNER JOIN')->
  join('Human mother', 'Human.MotherId', 'mother.Id', 'INNER JOIN')->
  where('father.Name', 'John')->
  where('mother.Name', 'Jane');
$nbHumans = $finder->count();
$t->is($nbHumans, 1, 'join() allows to join to the current table with several foreign keys using an alias');
$t->is($finder->getLatestQuery(), propel_sql('SELECT COUNT([P13*][P12human.ID]) FROM human INNER JOIN human father ON (human.FATHER_ID=father.ID) INNER JOIN human mother ON (human.MOTHER_ID=mother.ID) WHERE (father.NAME=\'John\' AND mother.NAME=\'Jane\')'), 'join() uses aliased table names when using an alias relation');

HumanPeer::doDeleteAll();
$human1 = new Human();
$human1->setName('John');
$human1->save();
$human2 = new Human();
$human2->setName('Albert');
$human2->setHumanRelatedByFatherId($human1);
$human2->save();
$human3 = new Human();
$human3->setName('Jane');
$human3->setHumanRelatedByFatherId($human2);
$human3->save();

$finder = sfPropelFinder::from('Human')->
  join('Human father', 'Human.FatherId', 'father.Id', 'INNER JOIN')->
  join('Human grandfather', 'father.FatherId', 'grandfather.Id', 'INNER JOIN')->
  where('grandfather.Name', 'John');
$nbHumans = $finder->count();
$t->is($nbHumans, 1, 'join() allows to join to the current table with several foreign keys using an alias');
$t->is($finder->getLatestQuery(), propel_sql('SELECT COUNT([P13*][P12human.ID]) FROM human INNER JOIN human father ON (human.FATHER_ID=father.ID) INNER JOIN human grandfather ON (father.FATHER_ID=grandfather.ID) WHERE grandfather.NAME=\'John\''), 'join() uses aliased table names when using an alias relation');
$t->isa_ok(@$articles[0], 'Article', 'sfPropelFinder::paginate() returns a pager object from which items can be retrieved');

$pager = sfPropelFinder::from('Article')->
  where('Title', 'like', 'tt%')->
  paginate(1, 2);
  
$t->is($pager->getNbResults(), 4, 'sfPropelFinder::paginate() sfPropelFinder::paginate() uses the internal conditions');
$t->is($pager->getLastPage(), 2, 'sfPropelFinder::paginate() sfPropelFinder::paginate() uses the internal conditions');
$t->is($pager->getFirstIndice(), 1, 'sfPropelFinder::paginate() sfPropelFinder::paginate() uses the internal conditions');

$t->diag('sfPropelFinderPager issues with GroupBy');
$finder = sfPropelFinder::from('Article')->groupBy('Title');
$pager = new sfPropelFinderPager('Article', 2);
$pager->setFinder($finder);
$pager->init();
$t->is($con->getLastExecutedQuery(), propel_sql('SELECT COUNT([P13*][P12article.ID]) FROM article'), 'sfPropelFinderPager::init() removes groupBy clauses and issues a COUNT');
$pager->getResults();
$t->is($con->getLastExecutedQuery(), 'SELECT article.ID, article.TITLE, article.CATEGORY_ID FROM article GROUP BY article.TITLE LIMIT 2', 'sfPropelFinderPager::getResults() does not remove groupBy clauses and issues a SELECT');

$t->diag('sfPropelFinderPager issues with object finders classes');
class ArticleFinder extends sfPropelFinder
{
  protected $class = 'Article';
}
$finder = new ArticleFinder();
try
{
  $pager = $finder->paginate();
  $t->pass('Children of sfPropelFinder can use paginate()');
}
catch(sfException $e)
$t->is($finder->set(array('Title' => 'updated title')), 2, 'set() returns the number of updated rows');
$t->is($finder->getLatestQuery(), propel_sql('UPDATE article SET TITLE[P12 ]=[P12 ]\'updated title\' WHERE 1=1'), 'set() issues an update query even when passed an empty finder');
$t->is(sfPropelFinder::from('Article')->where('Title', 'updated title')->count(), 2, 'set() updates all records when passed an empty finder');

ArticlePeer::doDeleteAll();

$article1 = new Article;
$article1->setTitle('foo');
$article1->save();
$article2 = new Article;
$article2->setTitle('bar');
$article2->save();

$finder = sfPropelFinder::from('Article')->where('Title', 'foo');
$t->is($finder->set(array('Title' => 'updated title')), 1, 'set() returns the number of updated rows');
$t->is($finder->getLatestQuery(), propel_sql('UPDATE article SET TITLE[P12 ]=[P12 ]\'updated title\' WHERE article.TITLE=\'foo\''), 'set() issues an Update query when passed a finder');
$t->is(sfPropelFinder::from('Article')->where('Title', 'updated title')->count(), 1, 'set() updates only the records found based on the array of values');

$finder = sfPropelFinder::from('Article')->where('Title', 'bar');
$t->is($finder->set(array('Title' => 'updated title'), true), 1, 'set() returns the number of updated rows, even with $forceIndividualSaves set to true');
$t->isnt($finder->getLatestQuery(), 'UPDATE article SET TITLE = \'updated title\' WHERE article.TITLE=\'bar\'', 'set() issues an Update query on every record when passed a finder with $forceIndividualSaves set to true');
$t->is(sfPropelFinder::from('Article')->where('Title', 'updated title')->count(), 2, 'set() updates only the records found based on the array of values, even with $forceIndividualSaves set to true');

try
{
  sfPropelFinder::from('Comment')->joinArticle()->where('Article_Title', 'updated title')->set(array('Title' => 3));
  $t->fail('set() throws an exception when called on a finder with join()');
}
catch( Exception $e )
{
  $t->pass('set() throws an exception when called on a finder with join()');