1
0
mirror of synced 2024-12-05 03:06:05 +03:00

Add thorough functional tests for Paginator, expand existing ones.

This commit is contained in:
Bill Schaller 2015-03-19 21:59:15 -04:00 committed by Marco Pivetta
parent 06998d015f
commit 81ccd93b74
4 changed files with 313 additions and 29 deletions

View File

@ -0,0 +1,35 @@
<?php
/**
* Company.php
* Created by William Schaller
* Date: 3/19/2015
* Time: 9:39 PM
*/
namespace Doctrine\Tests\Models\Pagination;
/**
* Company
*
* @package Doctrine\Tests\Models\Pagination
*
* @Entity
* @Table(name="pagination_company")
*/
class Company
{
/**
* @Id @Column(type="integer")
* @GeneratedValue
*/
public $id;
/**
* @Column(type="string")
*/
public $name;
/**
* @OneToOne(targetEntity="Logo", mappedBy="company", cascade={"persist"}, orphanRemoval=true)
*/
public $logo;
}

View File

@ -0,0 +1,46 @@
<?php
/**
* Logo.php
* Created by William Schaller
* Date: 3/19/2015
* Time: 9:44 PM
*/
namespace Doctrine\Tests\Models\Pagination;
/**
* Logo
*
* @package Doctrine\Tests\Models\Pagination
*
* @Entity
* @Table(name="pagination_logo")
*/
class Logo
{
/**
* @Column(type="integer") @Id
* @GeneratedValue
*/
public $id;
/**
* @Column(type="string")
*/
public $image;
/**
* @Column(type="integer")
*/
public $image_height;
/**
* @Column(type="integer")
*/
public $image_width;
/**
* @OneToOne(targetEntity="Company", inversedBy="logo", cascade={"persist"})
* @JoinColumn(name="company_id")
*/
public $company;
}

View File

@ -2,15 +2,13 @@
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Query;
use Doctrine\Tests\Models\CMS\CmsEmail;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsGroup;
use Doctrine\Tests\Models\CMS\CmsArticle;
use Doctrine\Tests\Models\CMS\CmsComment;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Doctrine\Tests\Models\Pagination\Company;
use Doctrine\Tests\Models\Pagination\Logo;
use ReflectionMethod;
/**
@ -21,6 +19,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
protected function setUp()
{
$this->useModelSet('cms');
$this->useModelSet('pagination');
parent::setUp();
$this->populate();
}
@ -35,7 +34,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
$paginator = new Paginator($query);
$paginator->setUseOutputWalkers($useOutputWalkers);
$this->assertCount(3, $paginator);
$this->assertCount(9, $paginator);
}
/**
@ -48,7 +47,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
$paginator = new Paginator($query);
$paginator->setUseOutputWalkers($useOutputWalkers);
$this->assertCount(3, $paginator);
$this->assertCount(9, $paginator);
}
public function testCountComplexWithOutputWalker()
@ -58,33 +57,144 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
$paginator = new Paginator($query);
$paginator->setUseOutputWalkers(true);
$this->assertCount(3, $paginator);
}
/**
* @expectedException
*/
public function testCountComplexWithoutOutputWalker()
{
$dql = "SELECT g, COUNT(u.id) AS userCount FROM Doctrine\Tests\Models\CMS\CmsGroup g LEFT JOIN g.users u GROUP BY g HAVING COUNT(u.id) > 0";
$query = $this->_em->createQuery($dql);
$paginator = new Paginator($query);
$paginator->setUseOutputWalkers(false);
$this->setExpectedException(
'RuntimeException',
'Cannot count query that uses a HAVING clause. Use the output walkers for pagination'
);
$this->assertCount(3, $paginator);
}
/**
* @dataProvider useOutputWalkers
*/
public function testCountWithComplexScalarOrderBy($useOutputWalkers)
{
$dql = 'SELECT l FROM Doctrine\Tests\Models\Pagination\Logo l ORDER BY l.image_width * l.image_height DESC';
$query = $this->_em->createQuery($dql);
$paginator = new Paginator($query);
$paginator->setUseOutputWalkers($useOutputWalkers);
$this->assertCount(9, $paginator);
}
/**
* @dataProvider useOutputWalkers
* @dataProvider useOutputWalkersAndFetchJoinCollection
*/
public function testIterateSimpleWithoutJoinFetchJoinHandlingOff($useOutputWalkers)
public function testIterateSimpleWithoutJoin($useOutputWalkers, $fetchJoinCollection)
{
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u";
$query = $this->_em->createQuery($dql);
$paginator = new Paginator($query, false);
$paginator = new Paginator($query, $fetchJoinCollection);
$paginator->setUseOutputWalkers($useOutputWalkers);
$this->assertCount(9, $paginator->getIterator());
// Test with limit
$query->setMaxResults(3);
$paginator = new Paginator($query, $fetchJoinCollection);
$paginator->setUseOutputWalkers($useOutputWalkers);
$this->assertCount(3, $paginator->getIterator());
// Test with limit and offset
$query->setMaxResults(3)->setFirstResult(4);
$paginator = new Paginator($query, $fetchJoinCollection);
$paginator->setUseOutputWalkers($useOutputWalkers);
$this->assertCount(3, $paginator->getIterator());
}
/**
* @dataProvider useOutputWalkers
*/
public function testIterateSimpleWithoutJoinFetchJoinHandlingOn($useOutputWalkers)
private function iterateWithOrder($useOutputWalkers, $fetchJoinCollection, $baseDql, $checkField)
{
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u";
// Ascending
$dql = "$baseDql ASC";
$query = $this->_em->createQuery($dql);
$paginator = new Paginator($query, true);
$paginator = new Paginator($query, $fetchJoinCollection);
$paginator->setUseOutputWalkers($useOutputWalkers);
$this->assertCount(3, $paginator->getIterator());
$iter = $paginator->getIterator();
$this->assertCount(9, $iter);
$result = iterator_to_array($iter);
$this->assertEquals($checkField . "0", $result[0]->$checkField);
// With limit
$query->setMaxResults(3);
$paginator = new Paginator($query, $fetchJoinCollection);
$paginator->setUseOutputWalkers($useOutputWalkers);
$iter = $paginator->getIterator();
$this->assertCount(3, $iter);
$result = iterator_to_array($iter);
$this->assertEquals($checkField . "0", $result[0]->$checkField);
// With offset
$query->setMaxResults(3)->setFirstResult(3);
$paginator = new Paginator($query, $fetchJoinCollection);
$paginator->setUseOutputWalkers($useOutputWalkers);
$iter = $paginator->getIterator();
$this->assertCount(3, $iter);
$result = iterator_to_array($iter);
$this->assertEquals($checkField . "3", $result[0]->$checkField);
// Descending
$dql = "$baseDql DESC";
$query = $this->_em->createQuery($dql);
$paginator = new Paginator($query, $fetchJoinCollection);
$paginator->setUseOutputWalkers($useOutputWalkers);
$iter = $paginator->getIterator();
$this->assertCount(9, $iter);
$result = iterator_to_array($iter);
$this->assertEquals($checkField . "8", $result[0]->$checkField);
// With limit
$query->setMaxResults(3);
$paginator = new Paginator($query, $fetchJoinCollection);
$paginator->setUseOutputWalkers($useOutputWalkers);
$iter = $paginator->getIterator();
$this->assertCount(3, $iter);
$result = iterator_to_array($iter);
$this->assertEquals($checkField . "8", $result[0]->$checkField);
// With offset
$query->setMaxResults(3)->setFirstResult(3);
$paginator = new Paginator($query, $fetchJoinCollection);
$paginator->setUseOutputWalkers($useOutputWalkers);
$iter = $paginator->getIterator();
$this->assertCount(3, $iter);
$result = iterator_to_array($iter);
$this->assertEquals($checkField . "5", $result[0]->$checkField);
}
/**
* @dataProvider useOutputWalkersAndFetchJoinCollection
*/
public function testIterateSimpleWithoutJoinWithOrder($useOutputWalkers, $fetchJoinCollection)
{
// Ascending
$dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u ORDER BY u.username";
$this->iterateWithOrder($useOutputWalkers, $fetchJoinCollection, $dql, "username");
}
/**
* @dataProvider fetchJoinCollection
*/
public function testIterateSimpleWithOutputWalkerWithoutJoinWithComplexOrder($fetchJoinCollection)
{
// Ascending
$dql = "SELECT l FROM Doctrine\Tests\Models\Pagination\Logo l ORDER BY l.image_width * l.image_height";
$this->iterateWithOrder(true, $fetchJoinCollection, $dql, "image");
}
/**
@ -97,7 +207,54 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
$paginator = new Paginator($query, true);
$paginator->setUseOutputWalkers($useOutputWalkers);
$this->assertCount(3, $paginator->getIterator());
$this->assertCount(9, $paginator->getIterator());
}
/**
* @dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinWithOrder($useOutputWalkers)
{
$dql = 'SELECT u,g FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g ORDER BY u.username';
$this->iterateWithOrder($useOutputWalkers, true, $dql, "username");
}
/**
* @dataProvider useOutputWalkersAndFetchJoinCollection
*/
public function testIterateWithRegularJoinWithOrderByColumnFromJoined($useOutputWalkers, $fetchJoinCollection)
{
$dql = 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.email e ORDER BY e.email';
$this->iterateWithOrder($useOutputWalkers, $fetchJoinCollection, $dql, "username");
}
/**
* @dataProvider fetchJoinCollection
*/
public function testIterateWithOutputWalkersWithRegularJoinWithComplexOrderByReferencingJoined($fetchJoinCollection)
{
// long function name is loooooooooooong
$dql = "SELECT c FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.logo l ORDER BY l.image_height * l.image_width";
$this->iterateWithOrder(true, $fetchJoinCollection, $dql, "name");
}
/**
* @dataProvider useOutputWalkers
*/
public function testIterateWithFetchJoinWithOrderByColumnFromJoined($useOutputWalkers)
{
$dql = "SELECT u,g,e FROM Doctrine\Tests\Models\CMS\CmsUser u JOIN u.groups g JOIN u.email e ORDER BY e.email";
$this->iterateWithOrder($useOutputWalkers, true, $dql, "username");
}
/**
* @dataProvider fetchJoinCollection
*/
public function testIterateWithOutputWalkersWithFetchJoinWithComplexOrderByReferencingJoined($fetchJoinCollection)
{
$dql = "SELECT c,l FROM Doctrine\Tests\Models\Pagination\Company c JOIN c.logo l ORDER BY l.image_width * l.image_height";
$this->iterateWithOrder(true, $fetchJoinCollection, $dql, "name");
}
public function testIterateComplexWithOutputWalker()
@ -107,7 +264,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
$paginator = new Paginator($query);
$paginator->setUseOutputWalkers(true);
$this->assertCount(9, $paginator->getIterator());
$this->assertCount(3, $paginator->getIterator());
}
public function testDetectOutputWalker()
@ -170,7 +327,7 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
$getCountQuery->setAccessible(true);
$this->assertCount(2, $getCountQuery->invoke($paginator)->getParameters());
$this->assertCount(3, $paginator);
$this->assertCount(9, $paginator);
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Doctrine\ORM\Query\SqlWalker');
@ -179,25 +336,44 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
// if select part of query is replaced with count(...) paginator should remove
// parameters from query object not used in new query.
$this->assertCount(1, $getCountQuery->invoke($paginator)->getParameters());
$this->assertCount(3, $paginator);
$this->assertCount(9, $paginator);
}
public function populate()
{
for ($i = 0; $i < 3; $i++) {
$groups = [];
for ($j = 0; $j < 3; $j++) {;
$group = new CmsGroup();
$group->name = "group$j";
$groups[] = $group;
$this->_em->persist($group);
}
for ($i = 0; $i < 9; $i++) {
$user = new CmsUser();
$user->name = "Name$i";
$user->username = "username$i";
$user->status = "active";
$this->_em->persist($user);
for ($j = 0; $j < 3; $j++) {;
$group = new CmsGroup();
$group->name = "group$j";
$user->addGroup($group);
$this->_em->persist($group);
$user->email = new CmsEmail();
$user->email->user = $user;
$user->email->email = "email$i";
for ($j = 0; $j < 3; $j++) {
$user->addGroup($groups[$j]);
}
$this->_em->persist($user);
}
for ($i = 0; $i < 9; $i++) {
$company = new Company();
$company->name = "name$i";
$company->logo = new Logo();
$company->logo->image = "image$i";
$company->logo->image_width = 100 + $i;
$company->logo->image_height = 100 + $i;
$company->logo->company = $company;
$this->_em->persist($company);
}
$this->_em->flush();
}
@ -208,6 +384,24 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase
array(false),
);
}
public function fetchJoinCollection()
{
return array(
array(true),
array(false),
);
}
public function useOutputWalkersAndFetchJoinCollection()
{
return array(
array(true, false),
array(true, true),
array(false, false),
array(false, true),
);
}
}
class CustomPaginationTestTreeWalker extends Query\TreeWalkerAdapter

View File

@ -267,6 +267,10 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
'Doctrine\Tests\Models\CustomType\CustomIdObjectTypeParent',
'Doctrine\Tests\Models\CustomType\CustomIdObjectTypeChild',
),
'pagination' => array(
'Doctrine\Tests\Models\Pagination\Company',
'Doctrine\Tests\Models\Pagination\Logo',
),
);
/**
@ -515,6 +519,11 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
$conn->executeUpdate('DELETE FROM custom_id_type_parent');
}
if (isset($this->_usedModelSets['pagination'])) {
$conn->executeUpdate('DELETE FROM pagination_logo');
$conn->executeUpdate('DELETE FROM pagination_company');
}
$this->_em->clear();
}