isDoctrinePhpcrNotAvailable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Pagerfanta\Tests\Adapter;
4
5
use ArrayIterator;
6
use Pagerfanta\Adapter\DoctrineODMPhpcrAdapter;
7
use PHPUnit\Framework\TestCase;
8
9
class DoctrineODMPhpcrAdapterTest extends TestCase
10
{
11
    private $queryBuilder;
12
    private $query;
13
    /**
14
     * @var DoctrineODMPhpcrAdapter
15
     */
16
    private $adapter;
17
18
    protected function setUp()
19
    {
20
        if ($this->isDoctrinePhpcrNotAvailable()) {
21
            $this->markTestSkipped('Doctrine PHPCR-ODM is not available');
22
        }
23
24
        $this->queryBuilder = $this->createQueryBuilderMock();
25
        $this->query = $this->createQueryMock();
26
27
        $this->adapter = new DoctrineODMPhpcrAdapter($this->queryBuilder);
0 ignored issues
show
Documentation introduced by
$this->queryBuilder is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ODM\PHPC...y\Builder\QueryBuilder>.

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...
28
    }
29
30
    private function isDoctrinePhpcrNotAvailable()
31
    {
32
        return !class_exists('Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder');
33
    }
34
35
    private function createQueryBuilderMock()
36
    {
37
        return $this
38
            ->getMockBuilder('Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder')
39
            ->disableOriginalConstructor()
40
            ->getMock();
41
    }
42
43
    private function createQueryMock()
44
    {
45
        return $this
46
            ->getMockBuilder('Doctrine\ODM\PHPCR\Query\Query')
47
            ->disableOriginalConstructor()
48
            ->getMock()
49
        ;
50
    }
51
52
    public function testGetQueryBuilder()
53
    {
54
        $this->assertSame($this->queryBuilder, $this->adapter->getQueryBuilder());
55
    }
56
57
    public function testGetNbResultsShouldCreateTheQueryAndCount()
58
    {
59
        $this->queryBuilder
60
            ->expects($this->once())
61
            ->method('getQuery')
62
            ->will($this->returnValue($this->query))
63
        ;
64
65
        $queryResult = $this->getMockBuilder('Jackalope\Query\QueryResult')
66
            ->disableOriginalConstructor()
67
            ->getMock();
68
        $queryResult
69
            ->expects($this->once())
70
            ->method('getRows')
71
            ->will($this->returnValue(new ArrayIterator(array(1, 2, 3 , 4, 5, 6))));
72
73
        $this->query
74
            ->expects($this->once())
75
            ->method('execute')
76
            ->will($this->returnValue($queryResult))
77
        ;
78
79
        $this->assertSame(6, $this->adapter->getNbResults());
80
    }
81
82
    public function testGetSlice()
83
    {
84
        $offset = 10;
85
        $length = 15;
86
        $slice = new \ArrayIterator();
87
88
        $this->query
89
            ->expects($this->once())
90
            ->method('setMaxResults')
91
            ->with($length)
92
            ->will($this->returnValue($this->query))
93
        ;
94
        $this->query
95
            ->expects($this->once())
96
            ->method('setFirstResult')
97
            ->with($offset)
98
            ->will($this->returnValue($this->query))
99
        ;
100
        $this->queryBuilder
101
            ->expects($this->once())
102
            ->method('getQuery')
103
            ->will($this->returnValue($this->query))
104
        ;
105
        $this->query
106
            ->expects($this->once())
107
            ->method('execute')
108
            ->will($this->returnValue($slice))
109
        ;
110
111
        $this->assertSame($slice, $this->adapter->getSlice($offset, $length));
112
    }
113
}
114