Passed
Pull Request — master (#7)
by Chito
02:51
created

LampagerColumnAccessTest::getProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 68
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 29
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 68
rs 9.456

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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