ItemFactoryTest::testInvalidItemPropertyList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
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\Application;
38
39
use Jkphl\Micrometa\Application\Factory\ItemFactory;
40
use Jkphl\Micrometa\Application\Item\Item;
41
use Jkphl\Micrometa\Application\Value\StringValue;
42
use Jkphl\Micrometa\Domain\Item\Iri;
43
use Jkphl\Micrometa\Infrastructure\Factory\MicroformatsFactory;
44
use Jkphl\Micrometa\Tests\AbstractTestBase;
45
46
/**
47
 * Item factory tests
48
 *
49
 * @package    Jkphl\Micrometa
50
 * @subpackage Jkphl\Micrometa\Tests
51
 */
52
class ItemFactoryTest extends AbstractTestBase
53
{
54
    /**
55
     * Test the item factory
56
     */
57
    public function testItemFactory()
58
    {
59
        $itemFactory = new ItemFactory(0);
60
        $rawItem     = (object)['type' => ['test']];
61
        $item        = $itemFactory($rawItem);
62
        $this->assertInstanceOf(Item::class, $item);
63
        $this->assertEquals([new Iri('', 'test')], $item->getType());
64
        $this->assertNull($item->getValue());
65
    }
66
67
    /**
68
     * Test an item property list with alias
69
     */
70
    public function testAliasedItemProperty()
71
    {
72
        $itemFactory = new ItemFactory(0);
73
        $rawItem     = (object)[
74
            'type'       => ['test'],
75
            'properties' => [
76
                (object)[
77
                    'name'    => 'alias-property',
78
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
79
                    'values'  => ['value']
80
                ]
81
            ],
82
            'children'   => [
83
                (object)[
84
                    'type' => ['test']
85
                ]
86
            ]
87
        ];
88
        $item        = $itemFactory($rawItem);
89
        $this->assertInstanceOf(Item::class, $item);
90
        $this->assertEquals(
91
            [MicroformatsFactory::MF2_PROFILE_URI.'alias-property' => [new StringValue('value')]],
92
            $item->getProperties()->export()
93
        );
94
        $propertyList = $item->getProperties();
95
        $this->assertTrue(
96
            $propertyList->offsetExists(
97
                (object)['name' => 'alias-property', 'profile' => MicroformatsFactory::MF2_PROFILE_URI]
98
            )
99
        );
100
        /** @noinspection PhpIllegalArrayKeyTypeInspection */
101
        $this->assertTrue(
102
            isset(
103
                $propertyList[(object)['name' => 'alias-property', 'profile' => MicroformatsFactory::MF2_PROFILE_URI]]
104
            )
105
        );
106
        $this->assertTrue(
107
            $propertyList->offsetExists(
108
                (object)['name' => 'aliasProperty', 'profile' => MicroformatsFactory::MF2_PROFILE_URI]
109
            )
110
        );
111
        $this->assertTrue(
112
            $propertyList->offsetExists('alias-property')
113
        );
114
        $this->assertFalse(
115
            $propertyList->offsetExists('invalid-alias-property')
116
        );
117
        $this->assertEquals(
118
            (object)[
119
                'format'     => 0,
120
                'id'         => null,
121
                'language'   => null,
122
                'types'      => ['test'],
123
                'properties' => [
124
                    MicroformatsFactory::MF2_PROFILE_URI.'alias-property' => ['value']
125
                ],
126
                'items'      => [
127
                    (object)[
128
                        'format'     => 0,
129
                        'id'         => null,
130
                        'language'   => null,
131
                        'types'      => ['test'],
132
                        'properties' => [],
133
                        'items'      => [],
134
                        'value'      => null
135
                    ]
136
                ],
137
                'value'      => null
138
            ],
139
            $item->export()
140
        );
141
    }
142
143
    /**
144
     * Test an invalid item property list
145
     */
146
    public function testInvalidItemPropertyList()
147
    {
148
        $itemFactory = new ItemFactory(0);
149
        $rawItem     = (object)['type' => ['test'], 'properties' => ['test' => false]];
150
        $item        = $itemFactory($rawItem);
151
        $this->assertInstanceOf(Item::class, $item);
152
        $this->assertEquals([], $item->getProperties()->export());
153
    }
154
155
    /**
156
     * Test an invalid item property value list
157
     */
158
    public function testInvalidItemPropertyValueList()
159
    {
160
        $itemFactory = new ItemFactory(0);
161
        $rawItem     = (object)[
162
            'type'       => ['test'],
163
            'properties' => [
164
                'test' => (object)[
165
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
166
                    'name'    => 'name',
167
                    'values'  => false
168
                ]
169
            ]
170
        ];
171
        $item        = $itemFactory($rawItem);
172
        $this->assertInstanceOf(Item::class, $item);
173
        $this->assertEquals([], $item->getProperties()->export());
174
    }
175
176
    /**
177
     * Test an invalid language tagged property value
178
     */
179
    public function testInvalidLanguageTaggedPropertyValue()
180
    {
181
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\RuntimeException');
182
        $this->expectExceptionCode('1495906369');
183
        $itemFactory = new ItemFactory(0);
184
        $rawItem     = (object)[
185
            'type'       => ['test'],
186
            'properties' => [
187
                'test' => (object)[
188
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
189
                    'name'    => 'name',
190
                    'values'  => [(object)['invalid' => true]]
191
                ]
192
            ]
193
        ];
194
        $itemFactory($rawItem);
195
    }
196
}
197