MongoAdapterTest::isMongoNotAvailable()   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 Pagerfanta\Adapter\MongoAdapter;
6
use PHPUnit\Framework\TestCase;
7
8
class MongoAdapterTest extends TestCase
9
{
10
    protected $cursor;
11
    /**
12
     * @var MongoAdapter
13
     */
14
    protected $adapter;
15
16
    protected function setUp()
17
    {
18
        if ($this->isMongoNotAvailable()) {
19
            $this->markTestSkipped('Mongo is not available.');
20
        }
21
22
        $this->cursor = $this->createCursorMock();
23
        $this->adapter = new MongoAdapter($this->cursor);
0 ignored issues
show
Documentation introduced by
$this->cursor is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<MongoCursor>.

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 isMongoNotAvailable()
27
    {
28
        return !extension_loaded('mongo');
29
    }
30
31
    private function createCursorMock()
32
    {
33
        return $this
34
            ->getMockBuilder('\MongoCursor')
35
            ->disableOriginalConstructor()
36
            ->getMock();
37
    }
38
39
    public function testGetCursor()
40
    {
41
        $this->assertSame($this->cursor, $this->adapter->getCursor());
42
    }
43
44
    public function testGetNbResultsShouldReturnTheCursorCount()
45
    {
46
        $this->cursor
47
            ->expects($this->once())
48
            ->method('count')
49
            ->will($this->returnValue(100));
50
51
        $this->assertSame(100, $this->adapter->getNbResults());
52
    }
53
54
    public function testGetSliceShouldPassTheOffsetAndLengthToTheCursor()
55
    {
56
        $offset = 12;
57
        $length = 16;
58
59
        $this->cursor
60
            ->expects($this->once())
61
            ->method('limit')
62
            ->with($length);
63
        $this->cursor
64
            ->expects($this->once())
65
            ->method('skip')
66
            ->with($offset);
67
68
        $this->adapter->getSlice($offset, $length);
69
    }
70
71
    public function testGetSliceShouldReturnTheCursor()
72
    {
73
        $this->cursor
74
            ->expects($this->any())
75
            ->method('limit');
76
        $this->cursor
77
            ->expects($this->any())
78
            ->method('skip');
79
80
        $this->assertSame($this->cursor, $this->adapter->getSlice(1, 1));
81
    }
82
}
83