testGetResultsShouldPassTheOffsetAndLengthToTheCollectionSlice()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Pagerfanta\Tests\Adapter;
4
5
use Pagerfanta\Adapter\DoctrineCollectionAdapter;
6
use PHPUnit\Framework\TestCase;
7
8
class DoctrineCollectionAdapterTest extends TestCase
9
{
10
    private $collection;
11
    /**
12
     * @var DoctrineCollectionAdapter
13
     */
14
    private $adapter;
15
16
    protected function setUp()
17
    {
18
        if ($this->collectionIsNotAvailable()) {
19
            $this->markTestSkipped('Doctrine Collection is not available');
20
        }
21
22
        $this->collection = $this->createCollectionMock();
23
        $this->adapter = new DoctrineCollectionAdapter($this->collection);
0 ignored issues
show
Documentation introduced by
$this->collection is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Common\Collections\Collection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24
    }
25
26
    private function collectionIsNotAvailable()
27
    {
28
        return !interface_exists($this->getCollectionInterface());
29
    }
30
31
    private function createCollectionMock()
32
    {
33
        return $this
34
            ->getMockBuilder($this->getCollectionInterface())
35
            ->disableOriginalConstructor()
36
            ->getMock();
37
    }
38
39
    private function getCollectionInterface()
40
    {
41
        return 'Doctrine\Common\Collections\Collection';
42
    }
43
44
    public function testGetCollectionShouldReturnTheCollection()
45
    {
46
        $this->assertSame($this->collection, $this->adapter->getCollection());
47
    }
48
49
    public function testGetNbResultsShouldResultTheCollectionCount()
50
    {
51
        $this->collection
52
            ->expects($this->once())
53
            ->method('count')
54
            ->will($this->returnValue(120));
55
56
        $this->assertSame(120, $this->adapter->getNbResults());
57
    }
58
59
    public function testGetResultsShouldReturnTheCollectionSliceReturnValue()
60
    {
61
        $results = new \ArrayObject();
62
        $this->collection
63
            ->expects($this->once())
64
            ->method('slice')
65
            ->will($this->returnValue($results));
66
67
        $this->assertSame($results, $this->adapter->getSlice(1, 1));
68
    }
69
70
    public function testGetResultsShouldPassTheOffsetAndLengthToTheCollectionSlice()
71
    {
72
        $this->collection
73
            ->expects($this->once())
74
            ->method('slice')
75
            ->with(5, 12);
76
77
        $this->adapter->getSlice(5, 12);
78
    }
79
}
80