1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineORMModuleTest\Paginator; |
4
|
|
|
|
5
|
|
|
use DoctrineORMModuleTest\Framework\TestCase; |
6
|
|
|
use DoctrineORMModuleTest\Assets\Fixture\TestFixture; |
7
|
|
|
|
8
|
|
|
use Doctrine\Common\DataFixtures\Loader as FixtureLoader; |
9
|
|
|
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
10
|
|
|
use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
11
|
|
|
|
12
|
|
|
use Doctrine\ORM\QueryBuilder; |
13
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator; |
14
|
|
|
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as PaginatorAdapter; |
15
|
|
|
|
16
|
|
|
class AdapterTestIgnore extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var QueryBuilder |
20
|
|
|
*/ |
21
|
|
|
protected $qb; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var PaginatorAdapter |
25
|
|
|
*/ |
26
|
|
|
protected $paginatorAdapter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var DoctrinePaginator |
30
|
|
|
*/ |
31
|
|
|
protected $paginator; |
32
|
|
|
|
33
|
|
|
public function setUp() |
34
|
|
|
{ |
35
|
|
|
parent::setUp(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$this->createDb(); |
38
|
|
|
$loader = new FixtureLoader(); |
39
|
|
|
$loader->addFixture(new TestFixture()); |
40
|
|
|
$purger = new ORMPurger(); |
41
|
|
|
$executor = new ORMExecutor($this->getEntityManager(), $purger); |
42
|
|
|
$executor->execute($loader->getFixtures()); |
43
|
|
|
|
44
|
|
|
$this->qb = $this |
45
|
|
|
->getEntityManager() |
46
|
|
|
->createQueryBuilder() |
47
|
|
|
->select('t') |
48
|
|
|
->from(\DoctrineORMModuleTest\Assets\Entity\Test::class, 't') |
49
|
|
|
->orderBy('t.id', 'ASC'); |
50
|
|
|
|
51
|
|
|
$this->paginator = new DoctrinePaginator($this->qb); |
52
|
|
|
$this->paginatorAdapter = new PaginatorAdapter($this->paginator); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testCanSetPaginator() |
56
|
|
|
{ |
57
|
|
|
$this->assertSame($this->paginator, $this->paginatorAdapter->getPaginator()); |
|
|
|
|
58
|
|
|
$doctrinePaginator = new DoctrinePaginator($this->getEntityManager()->createQuery('')); |
59
|
|
|
$this->paginatorAdapter->setPaginator($doctrinePaginator); |
60
|
|
|
$this->assertSame($doctrinePaginator, $this->paginatorAdapter->getPaginator()); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testContainedItemsCount() |
64
|
|
|
{ |
65
|
|
|
$itemsCount = $this->qb->select('COUNT(t)')->getQuery()->getSingleScalarResult(); |
66
|
|
|
|
67
|
|
|
$this->assertEquals($itemsCount, $this->paginatorAdapter->count()); |
|
|
|
|
68
|
|
|
$this->assertEquals($itemsCount, count($this->paginatorAdapter->getItems(0, $itemsCount + 100))); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGetsItemsAtOffsetZero() |
72
|
|
|
{ |
73
|
|
|
$expected = $this->qb->setMaxResults(10)->getQuery()->getResult(); |
74
|
|
|
$actual = $this->paginatorAdapter->getItems(0, 10); |
75
|
|
|
|
76
|
|
|
foreach ($expected as $key => $expectedItem) { |
77
|
|
|
$this->assertEquals($expectedItem, $actual[$key]); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testGetsItemsAtOffsetTen() |
82
|
|
|
{ |
83
|
|
|
$expected = $this->qb->setFirstResult(10)->setMaxResults(10)->getQuery()->getResult(); |
84
|
|
|
$actual = $this->paginatorAdapter->getItems(10, 10); |
85
|
|
|
|
86
|
|
|
foreach ($expected as $key => $expectedItem) { |
87
|
|
|
$this->assertEquals($expectedItem, $actual[$key]); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: