LampagerPaginatorTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
App::uses('LampagerTestCase', 'Lampager.Test/Case');
4
App::uses('LampagerPaginator', 'Lampager.Model');
5
6
class LampagerPaginatorTest extends LampagerTestCase
7
{
8
    /** @var Model */
9
    protected $Post;
10
11
    /** @var string[] */
12
    public $fixtures = [
13
        'plugin.Lampager.Post',
14
    ];
15
16
    public function setUp()
17
    {
18
        parent::setUp();
19
        $this->Post = ClassRegistry::init('Post');
0 ignored issues
show
Documentation Bug introduced by
It seems like ClassRegistry::init('Post') can also be of type boolean. However, the property $Post is declared as type Model. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
20
    }
21
22
    /**
23
     * Test LampagerPaginator::order
24
     *
25
     * @dataProvider queryProvider
26
     */
27
    public function testFromQuery(array $query, array $expected)
28
    {
29
        $expected = (object)$expected;
30
        $paginator = LampagerPaginator::create($this->Post, $query);
31
        $this->assertSame($expected->orders, $paginator->orders);
32
        $this->assertSame($expected->limit, $paginator->limit);
33
        $this->assertSame($expected->backward, $paginator->backward);
34
        $this->assertSame($expected->exclusive, $paginator->exclusive);
35
        $this->assertSame($expected->seekable, $paginator->seekable);
36
    }
37
38
    public function queryProvider()
39
    {
40
        yield 'Order by Model.column => order' => [
41
            [
42
                'order' => [
43
                    'Post.id' => 'ASC',
44
                    'Post.modified' => 'DESC',
45
                ],
46
                'limit' => 15,
47
                'backward' => true,
48
                'exclusive' => true,
49
                'seekable' => true,
50
            ],
51
            [
52
                'orders' => [
53
                    ['Post.id', 'asc'],
54
                    ['Post.modified', 'desc'],
55
                ],
56
                'limit' => 15,
57
                'backward' => true,
58
                'exclusive' => true,
59
                'seekable' => true,
60
            ],
61
        ];
62
63
        yield 'Order by Model.column order' => [
64
            [
65
                'order' => [
66
                    'Post.id ASC',
67
                    'Post.modified DESC',
68
                ],
69
                'limit' => 15,
70
                'backward' => true,
71
                'exclusive' => true,
72
                'seekable' => true,
73
            ],
74
            [
75
                'orders' => [
76
                    ['Post.id', 'asc'],
77
                    ['Post.modified', 'desc'],
78
                ],
79
                'limit' => 15,
80
                'backward' => true,
81
                'exclusive' => true,
82
                'seekable' => true,
83
            ],
84
        ];
85
86
        yield 'Order by column => order' => [
87
            [
88
                'order' => [
89
                    'id' => 'ASC',
90
                    'modified' => 'DESC',
91
                ],
92
                'limit' => 15,
93
                'backward' => true,
94
                'exclusive' => true,
95
                'seekable' => true,
96
            ],
97
            [
98
                'orders' => [
99
                    ['Post.id', 'asc'],
100
                    ['Post.modified', 'desc'],
101
                ],
102
                'limit' => 15,
103
                'backward' => true,
104
                'exclusive' => true,
105
                'seekable' => true,
106
            ],
107
        ];
108
109
        yield 'Order by column order' => [
110
            [
111
                'order' => [
112
                    'id ASC',
113
                    'modified DESC',
114
                ],
115
                'limit' => 15,
116
                'backward' => true,
117
                'exclusive' => true,
118
                'seekable' => true,
119
            ],
120
            [
121
                'orders' => [
122
                    ['Post.id', 'asc'],
123
                    ['Post.modified', 'desc'],
124
                ],
125
                'limit' => 15,
126
                'backward' => true,
127
                'exclusive' => true,
128
                'seekable' => true,
129
            ],
130
        ];
131
    }
132
}
133