Completed
Push — master ( c8af52...234fe1 )
by
unknown
11s
created

ConcatenationAdapterTest::testGetResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Pagerfanta\Tests\Adapter;
4
5
use Pagerfanta\Adapter\ArrayAdapter;
6
use Pagerfanta\Adapter\CallbackAdapter;
7
use Pagerfanta\Adapter\ConcatenationAdapter;
8
use Pagerfanta\Adapter\FixedAdapter;
9
use Pagerfanta\Adapter\NullAdapter;
10
use PHPUnit\Framework\TestCase;
11
12
class ConcatenationAdapterTest extends TestCase
13
{
14
    public function testConstructor()
15
    {
16
        new ConcatenationAdapter(array(
17
            new ArrayAdapter(array()),
18
            new NullAdapter(),
19
            new FixedAdapter(0, array())
20
        ));
21
22
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
23
            '\Pagerfanta\Exception\InvalidArgumentException',
24
            'Argument $adapters[1] expected to be a \Pagerfanta\Adapter\AdapterInterface instance, a string given'
25
        );
26
        new ConcatenationAdapter(array(
0 ignored issues
show
Documentation introduced by
array(new \Pagerfanta\Ad...dapter(array()), 'foo') is of type array<integer,object<Pag...dapter>","1":"string"}>, but the function expects a array<integer,object<Pag...pter\AdapterInterface>>.

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...
27
            new ArrayAdapter(array()),
28
            'foo'
29
        ));
30
    }
31
32
    public function testGetNbResults()
33
    {
34
        $adapter = new ConcatenationAdapter(array(
35
            new ArrayAdapter(array('foo', 'bar', 'baz'))
36
        ));
37
        $this->assertEquals(3, $adapter->getNbResults());
38
39
        $adapter = new ConcatenationAdapter(array(
40
            new ArrayAdapter(array_fill(0, 4, 'foo')),
41
            new ArrayAdapter(array_fill(0, 6, 'bar')),
42
            new ArrayAdapter(array('baq'))
43
        ));
44
        $this->assertEquals(11, $adapter->getNbResults());
45
46
        $adapter = new ConcatenationAdapter(array());
47
        $this->assertEquals(0, $adapter->getNbResults());
48
    }
49
50
    public function testGetResults()
51
    {
52
        $adapter = new ConcatenationAdapter(array(
53
            new ArrayAdapter(array(1, 2, 3, 4, 5, 6)),
54
            new ArrayAdapter(array(7, 8, 9, 10, 11, 12, 13, 14)),
55
            new ArrayAdapter(array(15, 16, 17))
56
        ));
57
        $this->assertEquals(array(8, 9, 10), $adapter->getSlice(7, 3));
58
        $this->assertEquals(array(5, 6, 7, 8), $adapter->getSlice(4, 4));
59
        $this->assertEquals(array(6, 7, 8, 9, 10, 11, 12, 13, 14, 15), $adapter->getSlice(5, 10));
60
        $this->assertEquals(array(16, 17), $adapter->getSlice(15, 5));
61
    }
62
63
    public function testWithTraversableAdapter()
64
    {
65
        $adapter = new ConcatenationAdapter(array(
66
            new CallbackAdapter(
67
                function () {
68
                    return 5;
69
                },
70
                function ($offset, $length) {
71
                    return new \ArrayIterator(array_slice(array(1, 2, 3, 4, 5), $offset, $length));
72
                }
73
            ),
74
            new CallbackAdapter(
75
                function () {
76
                    return 3;
77
                },
78
                function ($offset, $length) {
79
                    return new \ArrayIterator(array_slice(array(6, 7, 8), $offset, $length));
80
                }
81
            )
82
        ));
83
        $this->assertEquals(array(2, 3), $adapter->getSlice(1, 2));
84
        $this->assertEquals(array(4, 5, 6), $adapter->getSlice(3, 3));
85
    }
86
}
87