ItemTest::testInvalidPropertyStructure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
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 © 2018 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 © 2018 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\Iri;
41
use Jkphl\Micrometa\Domain\Item\Item;
42
use Jkphl\Micrometa\Domain\Item\PropertyListInterface;
43
use Jkphl\Micrometa\Domain\Value\ValueInterface;
44
use Jkphl\Micrometa\Tests\AbstractTestBase;
45
46
/**
47
 * Item tests
48
 *
49
 * @package    Jkphl\Micrometa
50
 * @subpackage Jkphl\Micrometa\Tests
51
 */
52
class ItemTest extends AbstractTestBase
53
{
54
    /**
55
     * Public function test the item creation
56
     *
57
     * @param string|\stdClass|\stdClass[] $type Item type(s)
58
     * @param array $properties                  Item properties
59
     * @param $itemId                            Item id
60
     * @param $itemLanguage                      Item language
61
     * @param array $expectedTypes               Expected item types
62
     * @param array $expectedProperties          Expected item properties
63
     * @param string $expectedId                 Expected item id
64
     * @param string $expectedLanguage           Expected language
65
     *
66
     * @dataProvider creationArgumentProvider
67
     */
68
    public function testItemCreation(
69
        $type,
70
        array $properties,
71
        $itemId,
72
        $itemLanguage,
73
        array $expectedTypes,
74
        array $expectedProperties,
75
        $expectedId,
76
        $expectedLanguage = null
77
    ) {
78
        $item = new Item($type, $properties, $itemId, $itemLanguage);
79
        $this->assertInstanceOf(Item::class, $item);
80
        $this->assertEquals($expectedTypes, $item->getType());
81
        $this->assertEquals($expectedProperties, $this->convertPropertyListToArray($item->getProperties()));
82
        $this->assertEquals($expectedId, $item->getId());
83
        $this->assertEquals($expectedLanguage, $item->getLanguage());
84
    }
85
86
    /**
87
     * Convert a property list to a plain array
88
     *
89
     * @param PropertyListInterface $propertyList Property list
90
     *
91
     * @return array Property list array
92
     */
93
    protected function convertPropertyListToArray(PropertyListInterface $propertyList)
94
    {
95
        $propertyListValues = [];
96
        foreach ($propertyList as $iri => $values) {
97
            $propertyListValues[$iri->profile.$iri->name] = $values;
98
        }
99
100
        return $propertyListValues;
101
    }
102
103
    /**
104
     * Data provider for item creation tests
105
     *
106
     * @return array Item creation arguments
107
     */
108
    public function creationArgumentProvider()
109
    {
110
        $item  = new Item('test');
111
        $testT = $this->typ('test');
112
        $nvP   = $this->prp('name1', 'value1');
113
114
        return [
115
            ['test', [], null, null, [$testT], [], null],
116
            [$this->typ('test', 'a'), [], null, null, [$this->typ('test', 'a')], [], null],
117
            [['test'], [], null, null, [$testT], [], null],
118
            [['test', 'lorem'], [], null, null, [$testT, $this->typ('lorem')], [], null],
119
            [['test', '', 'lorem'], [], null, null, [$testT, $this->typ('lorem')], [], null],
120
            ['test', [$nvP], null, null, [$testT], ['name1' => [$this->str('value1')]], null],
121
            ['test', [$this->prp('name1', '')], null, null, [$testT], [], null],
122
            ['test', [$this->prp('name1', [])], null, null, [$testT], [], null],
123
            [
124
                'test',
125
                [$this->prp('name1', 'value1', 'profile1/')],
126
                null,
127
                null,
128
                [$testT],
129
                ['profile1/name1' => [$this->str('value1')]],
130
                null
131
            ],
132
            ['test', [$nvP], null, null, [$testT], ['name1' => [$this->str('value1')]], null],
133
            [
134
                'test',
135
                [$nvP, $this->prp('name1', 'value2')],
136
                null,
137
                null,
138
                [$testT],
139
                ['name1' => [$this->str('value1'), $this->str('value2')]],
140
                null
141
            ],
142
            [
143
                'test',
144
                [$nvP, $this->prp('name2', 'value2')],
145
                null,
146
                null,
147
                [$testT],
148
                ['name1' => [$this->str('value1')], 'name2' => [$this->str('value2')]],
149
                null
150
            ],
151
            ['test', [$this->prp('name', [$item])], null, null, [$testT], ['name' => [$item]], null],
152
            ['test', [], 'id', null, [$testT], [], 'id'],
153
            ['test', [], null, 'en', [$testT], [], null, 'en'],
154
        ];
155
    }
156
157
    /**
158
     * Create a type object
159
     *
160
     * @param string $name    Type name
161
     * @param string $profile Type profile
162
     *
163
     * @return object Type object
164
     */
165
    protected function typ($name, $profile = '')
166
    {
167
        return new Iri($profile, $name);
168
    }
169
170
    /**
171
     * Create a property object
172
     *
173
     * @param string $name    Property name
174
     * @param mixed $str      Property value(s)
175
     * @param string $profile Property profile
176
     *
177
     * @return \stdClass Property object
178
     */
179
    protected function prp($name, $str, $profile = '')
180
    {
181
        $values = array_map([$this, 'str'], (array)$str);
182
183
        return (object)['profile' => $profile, 'name' => $name, 'values' => $values];
184
    }
185
186
    /**
187
     * Create a string value
188
     *
189
     * @param string $str Value
190
     *
191
     * @return ValueInterface String value
192
     */
193
    protected function str($str)
194
    {
195
        return ($str instanceof ValueInterface) ? $str : new StringValue($str);
196
    }
197
198
    /**
199
     * Test the item creation with an empty types list
200
     */
201
    public function testEmptyTypesList()
202
    {
203
        $this->expectException('Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException');
204
        $this->expectExceptionCode('1490814631');
205
        new Item(null);
206
    }
207
208
    /**
209
     * Test the item creation with an empty types list
210
     */
211
    public function testEmptyTypeName()
212
    {
213
        $this->expectException('Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException');
214
        $this->expectExceptionCode('1488314667');
215
        new Item('');
216
    }
217
218
    /**
219
     * Test the item creation with an empty property name
220
     */
221
    public function testEmptyPropertyName()
222
    {
223
        $this->expectException('Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException');
224
        $this->expectExceptionCode('1488314921');
225
        new Item('type', [$this->prp('', 'value')]);
226
    }
227
228
    /**
229
     * Test empty property value list
230
     */
231
    public function testInvalidPropertyStructure()
232
    {
233
        $this->expectException('Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException');
234
        $this->expectExceptionCode('1490814554');
235
        new Item('type', [(object)['invalid' => 'structure']]);
236
    }
237
238
    /**
239
     * Test the item creation with an invalid property value
240
     */
241
    public function testInvalidPropertyValue()
242
    {
243
        $this->expectException('Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException');
244
        $this->expectExceptionCode('1488315339');
245
        new Item('type', [(object)['profile' => '', 'name' => 'test', 'values' => [123]]]);
246
    }
247
248
    /**
249
     * Test the item creation with an invalid property value
250
     */
251
    public function testUnknownPropertyName()
252
    {
253
        $this->expectException('Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException');
254
        $this->expectExceptionCode('1488315604');
255
        $item = new Item('type');
256
        $item->getProperty('name');
257
    }
258
259
    /**
260
     * Test the item property getter with an unprofiled property
261
     */
262
    public function testItemUnprofiledProperty()
263
    {
264
        $item = new Item('type', [$this->prp('name', 123)]);
265
        $this->assertEquals([new StringValue('123')], $item->getProperty('name'));
266
    }
267
268
    /**
269
     * Test the item property getter with a profiled property
270
     */
271
    public function testItemProfiledProperty()
272
    {
273
        $item  = new Item('type', [$this->prp('name', 123, 'profile')]);
274
        $value = [new StringValue('123')];
275
        $this->assertEquals($value, $item->getProperty('name'));
276
        $this->assertEquals($value, $item->getProperty('name', 'profile'));
277
        $this->assertEquals($value, $item->getProperty((object)['name' => 'name', 'profile' => 'profile']));
278
        $this->assertEquals($value, $item->getProperty(new Iri('profile', 'name')));
279
    }
280
}
281