Completed
Branch master (6c7c3c)
by Joschi
02:33
created

ItemFactoryTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 145
rs 10
c 0
b 0
f 0

5 Methods

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