Completed
Push — master ( 754f5c...d85481 )
by Joschi
05:03
created

ItemTest::typ()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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