1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DoctrineModuleTest\Paginator\Adapter; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use DoctrineModule\Paginator\Adapter\Collection as CollectionAdapter; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use function range; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Tests for the Collection pagination adapter |
14
|
|
|
* |
15
|
|
|
* @link http://www.doctrine-project.org/ |
16
|
|
|
*/ |
17
|
|
|
class CollectionAdapterTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** @var CollectionAdapter */ |
20
|
|
|
protected $adapter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritDoc}. |
24
|
|
|
*/ |
25
|
|
|
protected function setUp() : void |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
|
|
|
|
28
|
|
|
$this->adapter = new CollectionAdapter(new ArrayCollection(range(1, 101))); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testGetsItemsAtOffsetZero() : void |
32
|
|
|
{ |
33
|
|
|
$expected = range(1, 10); |
34
|
|
|
$actual = $this->adapter->getItems(0, 10); |
35
|
|
|
$this->assertEquals($expected, $actual); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetsItemsAtOffsetTen() : void |
39
|
|
|
{ |
40
|
|
|
$expected = range(11, 20); |
41
|
|
|
$actual = $this->adapter->getItems(10, 10); |
42
|
|
|
$this->assertEquals($expected, $actual); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testReturnsCorrectCount() : void |
46
|
|
|
{ |
47
|
|
|
$this->assertEquals(101, $this->adapter->count()); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testEmptySet() : void |
51
|
|
|
{ |
52
|
|
|
$adapter = new CollectionAdapter(new ArrayCollection()); |
53
|
|
|
$actual = $adapter->getItems(0, 10); |
54
|
|
|
$this->assertEquals([], $actual); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
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: