LampagerArrayCursorTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 73
c 1
b 0
f 0
dl 0
loc 181
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testGet() 0 4 1
A hasProvider() 0 68 1
A getProvider() 0 68 1
A testHas() 0 4 1
1
<?php
2
3
App::uses('LampagerArrayCursor', 'Lampager.Model');
4
App::uses('LampagerTestCase', 'Lampager.Test/Case');
5
6
class LampagerArrayCursorTest 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 LampagerArrayCursor::get
24
     *
25
     * @param string $column
26
     * @param mixed  $expected
27
     * @dataProvider getProvider
28
     */
29
    public function testGet(array $data, $column, $expected)
30
    {
31
        $access = new LampagerArrayCursor($this->Post, $data);
32
        $this->assertSame($expected, $access->get($column));
33
    }
34
35
    /**
36
     * Test LampagerArrayCursor::has
37
     *
38
     * @param string $column
39
     * @param mixed  $expected
40
     * @dataProvider hasProvider
41
     */
42
    public function testHas(array $data, $column, $expected)
43
    {
44
        $access = new LampagerArrayCursor($this->Post, $data);
45
        $this->assertSame($expected, $access->has($column));
46
    }
47
48
    public function getProvider()
49
    {
50
        yield 'Multi-dimensional Model array from Model.column' => [
51
            [
52
                'Post' => [
53
                    'id' => 1,
54
                ],
55
            ],
56
            'Post.id',
57
            1,
58
        ];
59
60
        yield 'Multi-dimensional Model array from column' => [
61
            [
62
                'Post' => [
63
                    'id' => 1,
64
                ],
65
            ],
66
            'id',
67
            1,
68
        ];
69
70
        yield 'Single-dimensional Model.column array from Model.column' => [
71
            [
72
                'Post.id' => 1,
73
            ],
74
            'Post.id',
75
            1,
76
        ];
77
78
        yield 'Single-dimensional Model.column array from column' => [
79
            [
80
                'Post.id' => 1,
81
            ],
82
            'id',
83
            1,
84
        ];
85
86
        yield 'Single-dimensional column array from column' => [
87
            [
88
                'id' => 1,
89
            ],
90
            'id',
91
            1,
92
        ];
93
94
        yield 'Multi-dimensional Model array from non-existent column' => [
95
            [
96
                'Post' => [
97
                    'id' => 1,
98
                ],
99
            ],
100
            'modified',
101
            null,
102
        ];
103
104
        yield 'Single-dimensional Model.column array from non-existent Model.column' => [
105
            [
106
                'Post.id' => 1,
107
            ],
108
            'Post.modified',
109
            null,
110
        ];
111
112
        yield 'Empty array' => [
113
            [],
114
            'Post.id',
115
            null,
116
        ];
117
    }
118
119
    public function hasProvider()
120
    {
121
        yield 'Multi-dimensional Model array from Model.column' => [
122
            [
123
                'Post' => [
124
                    'id' => 1,
125
                ],
126
            ],
127
            'Post.id',
128
            true,
129
        ];
130
131
        yield 'Multi-dimensional Model array from column' => [
132
            [
133
                'Post' => [
134
                    'id' => 1,
135
                ],
136
            ],
137
            'id',
138
            true,
139
        ];
140
141
        yield 'Single-dimensional Model.column array from Model.column' => [
142
            [
143
                'Post.id' => 1,
144
            ],
145
            'Post.id',
146
            true,
147
        ];
148
149
        yield 'Single-dimensional Model.column array from column' => [
150
            [
151
                'Post.id' => 1,
152
            ],
153
            'id',
154
            true,
155
        ];
156
157
        yield 'Single-dimensional column array from column' => [
158
            [
159
                'id' => 1,
160
            ],
161
            'id',
162
            true,
163
        ];
164
165
        yield 'Multi-dimensional Model array from non-existent column' => [
166
            [
167
                'Post' => [
168
                    'id' => 1,
169
                ],
170
            ],
171
            'modified',
172
            false,
173
        ];
174
175
        yield 'Single-dimensional Model.column array from non-existent Model.column' => [
176
            [
177
                'Post.id' => 1,
178
            ],
179
            'Post.modified',
180
            false,
181
        ];
182
183
        yield 'Empty array' => [
184
            [],
185
            'Post.id',
186
            null,
187
        ];
188
    }
189
}
190