Completed
Push — v2 ( dad8a3...5e546d )
by Joschi
06:58
created

ItemTest.php ➔ s()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 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 © 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
42
/**
43
 * Item tests
44
 *
45
 * @package Jkphl\Micrometa
46
 * @subpackage Jkphl\Micrometa\Tests
47
 */
48
class ItemTest extends \PHPUnit_Framework_TestCase
49
{
50
    /**
51
     * Public function test the item creation
52
     *
53
     * @param string|array $type Item type(s)
54
     * @param array $properties Item properties
55
     * @param $itemId Item id
56
     * @param array $expectedTypes Expected item types
57
     * @param array $expectedProperties Expected item properties
58
     * @param string $expectedId Expected item id
59
     * @dataProvider creationArgumentProvider
60
     */
61
    public function testItemCreation(
62
        $type,
63
        array $properties,
64
        $itemId,
65
        array $expectedTypes,
66
        array $expectedProperties,
67
        $expectedId
68
    ) {
69
        $item = new Item($type, $properties, $itemId);
70
        $this->assertInstanceOf(Item::class, $item);
71
        $this->assertEquals($expectedTypes, $item->getType());
72
        $this->assertEquals($expectedProperties, $item->getProperties());
73
        $this->assertEquals($expectedId, $item->getId());
74
    }
75
76
    /**
77
     * Data provider for item creation tests
78
     *
79
     * @return array Item creation arguments
80
     */
81
    public function creationArgumentProvider()
82
    {
83
        function s($s)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
84
        {
85
            return new StringValue($s);
86
        }
87
88
        $item = new Item('test');
89
        return [
90
            ['test', [], null, ['test'], [], null],
91
            [['test'], [], null, ['test'], [], null],
92
            [['test', 'lorem'], [], null, ['test', 'lorem'], [], null],
93
            [['test', '', 'lorem'], [], null, ['test', 'lorem'], [], null],
94
            ['test', ['name1' => s('value1')], null, ['test'], ['name1' => [s('value1')]], null],
95
            ['test', ['name1' => [s('value1')]], null, ['test'], ['name1' => [s('value1')]], null],
96
            [
97
                'test',
98
                ['name1' => [s('value1'), s('value2')]],
99
                null,
100
                ['test'],
101
                ['name1' => [s('value1'), s('value2')]],
102
                null
103
            ],
104
            [
105
                'test',
106
                ['name1' => [s('value1'), s(''), s('value2')]],
107
                null,
108
                ['test'],
109
                ['name1' => [s('value1'), s('value2')]],
110
                null
111
            ],
112
            [
113
                'test',
114
                ['name1' => s('value1'), 'name2' => [s('value2')]],
115
                null,
116
                ['test'],
117
                ['name1' => [s('value1')], 'name2' => [s('value2')]],
118
                null
119
            ],
120
            ['test', ['name' => $item], null, ['test'], ['name' => [$item]], null],
121
            ['test', [], 'id', ['test'], [], 'id'],
122
        ];
123
    }
124
125
    /**
126
     * Test the item creation with an empty types list
127
     *
128
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException
129
     * @expectedExceptionCode 1488314667
130
     */
131
    public function testEmptyTypesList()
132
    {
133
        new Item(null);
134
    }
135
136
    /**
137
     * Test the item creation with an empty property name
138
     *
139
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException
140
     * @expectedExceptionCode 1488314921
141
     */
142
    public function testEmptyPropertyName()
143
    {
144
        new Item('type', ['' => ['value']]);
145
    }
146
147
    /**
148
     * Test the item creation with an invalid property value
149
     *
150
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\InvalidArgumentException
151
     * @expectedExceptionCode 1488315339
152
     */
153
    public function testInvalidPropertyValue()
154
    {
155
        new Item('type', ['name' => [123]]);
156
    }
157
158
    /**
159
     * Test the item creation with an invalid property value
160
     *
161
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException
162
     * @expectedExceptionCode 1488315604
163
     */
164
    public function testUnknownPropertyName()
165
    {
166
        $item = new Item('type');
167
        $item->getProperty('name');
168
    }
169
170
    /**
171
     * Test the item property getter
172
     */
173
    public function testItemPropertyGetter()
174
    {
175
        $item = new Item('type', ['name' => [new StringValue('123')]]);
176
        $this->assertEquals([new StringValue('123')], $item->getProperty('name'));
177
    }
178
}
179