LampagerColumnAccessTest::withProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 10
1
<?php
2
3
App::uses('LampagerTestCase', 'Lampager.Test/Case');
4
App::uses('LampagerColumnAccess', 'Lampager.Model');
5
6
class LampagerColumnAccessTest 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 LampagerColumnAccess::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 LampagerColumnAccess($this->Post);
32
        $this->assertSame($expected, $access->get($data, $column));
33
    }
34
35
    /**
36
     * Test LampagerColumnAccess::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 LampagerColumnAccess($this->Post);
45
        $this->assertSame($expected, $access->has($data, $column));
46
    }
47
48
    /**
49
     * Test LampagerColumnAccess::with
50
     *
51
     * @param string $column
52
     * @param mixed  $value
53
     * @param mixed  $expected
54
     * @dataProvider withProvider
55
     */
56
    public function testWith($column, $value, $expected)
57
    {
58
        $access = new LampagerColumnAccess($this->Post);
59
        $this->assertSame($expected, $access->with($column, $value));
60
    }
61
62
    /**
63
     * Test LampagerColumnAccess::with
64
     *
65
     * @param mixed $expected
66
     * @dataProvider iterateProvider
67
     */
68
    public function testIterate(array $data, $expected)
69
    {
70
        $access = new LampagerColumnAccess($this->Post);
71
        $this->assertSame($expected, iterator_to_array($access->iterate($data)));
72
    }
73
74
    public function getProvider()
75
    {
76
        yield 'Multi-dimensional Model array from Model.column' => [
77
            [
78
                'Post' => [
79
                    'id' => 1,
80
                ],
81
            ],
82
            'Post.id',
83
            1,
84
        ];
85
86
        yield 'Multi-dimensional Model array from column' => [
87
            [
88
                'Post' => [
89
                    'id' => 1,
90
                ],
91
            ],
92
            'id',
93
            1,
94
        ];
95
96
        yield 'Single-dimensional Model.column array from Model.column' => [
97
            [
98
                'Post.id' => 1,
99
            ],
100
            'Post.id',
101
            1,
102
        ];
103
104
        yield 'Single-dimensional Model.column array from column' => [
105
            [
106
                'Post.id' => 1,
107
            ],
108
            'id',
109
            1,
110
        ];
111
112
        yield 'Single-dimensional column array from column' => [
113
            [
114
                'id' => 1,
115
            ],
116
            'id',
117
            1,
118
        ];
119
120
        yield 'Multi-dimensional Model array from non-existent column' => [
121
            [
122
                'Post' => [
123
                    'id' => 1,
124
                ],
125
            ],
126
            'modified',
127
            null,
128
        ];
129
130
        yield 'Single-dimensional Model.column array from non-existent Model.column' => [
131
            [
132
                'Post.id' => 1,
133
            ],
134
            'Post.modified',
135
            null,
136
        ];
137
138
        yield 'Empty array' => [
139
            [],
140
            'Post.id',
141
            null,
142
        ];
143
    }
144
145
    public function hasProvider()
146
    {
147
        yield 'Multi-dimensional Model array from Model.column' => [
148
            [
149
                'Post' => [
150
                    'id' => 1,
151
                ],
152
            ],
153
            'Post.id',
154
            true,
155
        ];
156
157
        yield 'Multi-dimensional Model array from column' => [
158
            [
159
                'Post' => [
160
                    'id' => 1,
161
                ],
162
            ],
163
            'id',
164
            true,
165
        ];
166
167
        yield 'Single-dimensional Model.column array from Model.column' => [
168
            [
169
                'Post.id' => 1,
170
            ],
171
            'Post.id',
172
            true,
173
        ];
174
175
        yield 'Single-dimensional Model.column array from column' => [
176
            [
177
                'Post.id' => 1,
178
            ],
179
            'id',
180
            true,
181
        ];
182
183
        yield 'Single-dimensional column array from column' => [
184
            [
185
                'id' => 1,
186
            ],
187
            'id',
188
            true,
189
        ];
190
191
        yield 'Multi-dimensional Model array from non-existent column' => [
192
            [
193
                'Post' => [
194
                    'id' => 1,
195
                ],
196
            ],
197
            'modified',
198
            false,
199
        ];
200
201
        yield 'Single-dimensional Model.column array from non-existent Model.column' => [
202
            [
203
                'Post.id' => 1,
204
            ],
205
            'Post.modified',
206
            false,
207
        ];
208
209
        yield 'Empty array' => [
210
            [],
211
            'Post.id',
212
            false,
213
        ];
214
    }
215
216
    public function withProvider()
217
    {
218
        yield 'Model.column to multi-dimensional Model array' => [
219
            'Post.id',
220
            1,
221
            [
222
                'Post' => [
223
                    'id' => 1,
224
                ],
225
            ],
226
        ];
227
228
        yield 'Column to multi-dimensional Model array' => [
229
            'id',
230
            1,
231
            [
232
                'Post' => [
233
                    'id' => 1,
234
                ],
235
            ],
236
        ];
237
    }
238
239
    public function iterateProvider()
240
    {
241
        yield 'Single-dimensional Model.column array' => [
242
            [
243
                'Post.id' => '1',
244
                'Post.modified' => '2017-01-01 10:00:00',
245
            ],
246
            [
247
                'Post.id' => '1',
248
                'Post.modified' => '2017-01-01 10:00:00',
249
            ],
250
        ];
251
252
        yield 'Multi-dimensional Model.column array' => [
253
            [
254
                'Post' => [
255
                    'id' => '1',
256
                    'modified' => '2017-01-01 10:00:00',
257
                ],
258
            ],
259
            [
260
                'Post.id' => '1',
261
                'Post.modified' => '2017-01-01 10:00:00',
262
            ],
263
        ];
264
    }
265
}
266