Completed
Push — v2 ( ad2ba8...88b419 )
by Joschi
04:59
created

ItemTest::creationArgumentProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 45
nc 1
nop 0
dl 0
loc 60
rs 9.5555
c 0
b 0
f 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
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Tests
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Micrometa\Tests\Domain;
38
39
use Jkphl\Micrometa\Application\Value\StringValue;
40
use Jkphl\Micrometa\Domain\Item\Item;
41
use Jkphl\Micrometa\Domain\Value\ValueInterface;
42
43
/**
44
 * Item tests
45
 *
46
 * @package Jkphl\Micrometa
47
 * @subpackage Jkphl\Micrometa\Tests
48
 */
49
class ItemTest extends \PHPUnit_Framework_TestCase
50
{
51
    /**
52
     * Public function test the item creation
53
     *
54
     * @param string|array $type Item type(s)
55
     * @param array $properties Item properties
56
     * @param $itemId Item id
57
     * @param array $expectedTypes Expected item types
58
     * @param array $expectedProperties Expected item properties
59
     * @param string $expectedId Expected item id
60
     * @dataProvider creationArgumentProvider
61
     */
62
    public function testItemCreation(
63
        $type,
64
        array $properties,
65
        $itemId,
66
        array $expectedTypes,
67
        array $expectedProperties,
68
        $expectedId
69
    ) {
70
        $item = new Item($type, $properties, $itemId);
0 ignored issues
show
Bug introduced by
It seems like $type defined by parameter $type on line 63 can also be of type array; however, Jkphl\Micrometa\Domain\Item\Item::__construct() does only seem to accept string|object<stdClass>|...teger,object<stdClass>>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
71
        $this->assertInstanceOf(Item::class, $item);
72
        $this->assertEquals($expectedTypes, $item->getType());
73
        $this->assertEquals($expectedProperties, $item->getProperties()->toArray());
74
        $this->assertEquals($expectedId, $item->getId());
75
    }
76
77
    /**
78
     * Data provider for item creation tests
79
     *
80
     * @return array Item creation arguments
81
     */
82
    public function creationArgumentProvider()
83
    {
84
        $item = new Item('test');
85
        return [
86
            ['test', [], null, [$this->t('test')], [], null],
87
            [$this->t('test', 'a'), [], null, [$this->t('test', 'a')], [], null],
88
            [['test'], [], null, [$this->t('test')], [], null],
89
            [['test', 'lorem'], [], null, [$this->t('test'), $this->t('lorem')], [], null],
90
            [['test', '', 'lorem'], [], null, [$this->t('test'), $this->t('lorem')], [], null],
91
            [
92
                'test',
93
                [$this->p('name1', 'value1')],
94
                null,
95
                [$this->t('test')],
96
                ['name1' => [$this->s('value1')]],
97
                null
98
            ],
99
            [
100
                'test',
101
                [$this->p('name1', 'value1', 'profile1/')],
102
                null,
103
                [$this->t('test')],
104
                ['profile1/name1' => [$this->s('value1')]],
105
                null
106
            ],
107
            [
108
                'test',
109
                [$this->p('name1', 'value1')],
110
                null,
111
                [$this->t('test')],
112
                ['name1' => [$this->s('value1')]],
113
                null
114
            ],
115
            [
116
                'test',
117
                [$this->p('name1', 'value1'), $this->p('name1', 'value2')],
118
                null,
119
                [$this->t('test')],
120
                ['name1' => [$this->s('value1'), $this->s('value2')]],
121
                null
122
            ],
123
            [
124
                'test',
125
                [$this->p('name1', 'value1'), $this->p('name2', 'value2')],
126
                null,
127
                [$this->t('test')],
128
                ['name1' => [$this->s('value1')], 'name2' => [$this->s('value2')]],
129
                null
130
            ],
131
[
132
    'test',
133
    [$this->p('name', [$item])],
134
    null,
135
    [$this->t('test')],
136
    ['name' => [$item]],
137
    null
138
],
139
            ['test', [], 'id', [$this->t('test')], [], 'id'],
140
        ];
141
    }
142
143
    /**
144
     * Create a property object
145
     *
146
     * @param string $n Property name
147
     * @param mixed $s Property value(s)
148
     * @param string $p Property profiles
149
     * @return object Property object
150
     */
151
    protected function p($n, $s, $p = '')
152
    {
153
        $values = array_map([$this, 's'], (array)$s);
154
        return (object)['profile' => $p, 'name' => $n, 'values' => $values];
155
    }
156
157
    /**
158
     * Create a type object
159
     *
160
     * @param string $n Type name
161
     * @param string $p Type profile
162
     * @return object Type object
163
     */
164
    protected function t($n, $p = '')
165
    {
166
        return (object)['profile' => $p, 'name' => $n];
167
    }
168
169
    /**
170
     * Test the item creation with an empty types list
171
     *
172
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException
173
     * @expectedExceptionCode 1490814631
174
     */
175
    public function testEmptyTypesList()
176
    {
177
        new Item(null);
178
    }
179
180
    /**
181
     * Test the item creation with an empty property name
182
     *
183
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException
184
     * @expectedExceptionCode 1488314921
185
     */
186
    public function testEmptyPropertyName()
187
    {
188
        new Item('type', [$this->p('', 'value')]);
0 ignored issues
show
Documentation introduced by
array($this->p('', 'value')) is of type array<integer,object,{"0":"object"}>, but the function expects a array<integer,array>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
189
    }
190
191
    /**
192
     * Test the item creation with an invalid property value
193
     *
194
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException
195
     * @expectedExceptionCode 1488315339
196
     */
197
    public function testInvalidPropertyValue()
198
    {
199
        new Item('type', [(object)['profile' => '', 'name' => 'test', 'values' => [123]]]);
200
    }
201
202
    /**
203
     * Test the item creation with an invalid property value
204
     *
205
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException
206
     * @expectedExceptionCode 1488315604
207
     */
208
    public function testUnknownPropertyName()
209
    {
210
        $item = new Item('type');
211
        $item->getProperty('name');
212
    }
213
214
    /**
215
     * Test the item property getter
216
     */
217
    public function testItemPropertyGetter()
218
    {
219
        $item = new Item('type', [$this->p('name', 123)]);
0 ignored issues
show
Documentation introduced by
array($this->p('name', 123)) is of type array<integer,object,{"0":"object"}>, but the function expects a array<integer,array>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
220
        $this->assertEquals([new StringValue('123')], $item->getProperty('name'));
221
    }
222
223
    /**
224
     * Create a string value
225
     *
226
     * @param string $s Value
227
     * @return ValueInterface String value
228
     */
229
    protected function s($s)
230
    {
231
        return ($s instanceof ValueInterface) ? $s : new StringValue($s);
232
    }
233
}
234