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

LampagerArrayCursorTest::hasProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 70

Duplication

Lines 70
Ratio 100 %

Importance

Changes 0
Metric Value
dl 70
loc 70
rs 8.6545
c 0
b 0
f 0
cc 1
nc 1
nop 0

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