Completed
Push — v2 ( c6f6d7...e81eda )
by Joschi
05:09
created

ItemTest::testNonExistentNestedItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Infrastructure
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\Ports;
38
39
use Jkphl\Micrometa\Application\Factory\PropertyListFactory;
40
use Jkphl\Micrometa\Application\Item\Item as ApplicationItem;
41
use Jkphl\Micrometa\Application\Value\StringValue;
42
use Jkphl\Micrometa\Infrastructure\Factory\MicroformatsFactory;
43
use Jkphl\Micrometa\Infrastructure\Parser\Microformats;
44
use Jkphl\Micrometa\Ports\Item\Item;
45
use Jkphl\Micrometa\Ports\Item\ItemInterface;
46
47
/**
48
 * Parser factory tests
49
 *
50
 * @package Jkphl\Micrometa
51
 * @subpackage Jkphl\Micrometa\Tests
52
 */
53
class ItemTest extends \PHPUnit_Framework_TestCase
54
{
55
    /**
56
     * Test an item
57
     */
58
    public function testItemTypes()
59
    {
60
        $feedItem = $this->getFeedItem();
61
        $this->assertInstanceOf(Item::class, $feedItem);
62
63
        // Test the item type
64
        $this->assertTrue($feedItem->isOfType('h-feed'));
65
        $this->assertTrue($feedItem->isOfType('h-feed', MicroformatsFactory::MF2_PROFILE_URI));
66
    }
67
68
    /**
69
     * Create and return an h-feed Microformats item
70
     *
71
     * @return Item h-feed item
72
     */
73
    protected function getFeedItem()
74
    {
75
        $authorItem = new ApplicationItem(
76
            Microformats::FORMAT,
77
            new PropertyListFactory(),
78
            (object)['profile' => MicroformatsFactory::MF2_PROFILE_URI, 'name' => 'h-card'],
79
            [
80
                (object)[
81
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
82
                    'name' => 'name',
83
                    'values' => [
84
                        new StringValue('John Doe')
85
                    ]
86
                ],
87
                (object)[
88
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
89
                    'name' => 'email',
90
                    'values' => [
91
                        new StringValue('[email protected]')
92
                    ]
93
                ]
94
            ]
95
        );
96
97
98
        $entryItem = new ApplicationItem(
99
            Microformats::FORMAT,
100
            new PropertyListFactory(),
101
            (object)['profile' => MicroformatsFactory::MF2_PROFILE_URI, 'name' => 'h-entry'],
102
            [
103
                (object)[
104
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
105
                    'name' => 'name',
106
                    'values' => [
107
                        new StringValue('Famous blog post')
108
                    ]
109
                ],
110
                (object)[
111
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
112
                    'name' => 'author',
113
                    'values' => [
114
                        $authorItem
115
                    ]
116
                ]
117
            ]
118
        );
119
120
121
        $feedItem = new ApplicationItem(
122
            Microformats::FORMAT,
123
            new PropertyListFactory(),
124
            (object)['profile' => MicroformatsFactory::MF2_PROFILE_URI, 'name' => 'h-feed'],
125
            [
126
                (object)[
127
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
128
                    'name' => 'name',
129
                    'values' => [
130
                        new StringValue('John Doe\'s Blog')
131
                    ]
132
                ],
133
                (object)[
134
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
135
                    'name' => 'author',
136
                    'values' => [
137
                        $authorItem
138
                    ]
139
                ],
140
                (object)[
141
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
142
                    'name' => 'custom-property',
143
                    'values' => [
144
                        new StringValue('Property for alias testing')
145
                    ]
146
                ],
147
            ],
148
            [$entryItem, $entryItem]
149
        );
150
151
        return new Item($feedItem);
152
    }
153
154
    /**
155
     * Test an unprofiled property
156
     *
157
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
158
     * @expectedExceptionCode 1488315604
159
     */
160
    public function testUnprofiledProperty()
161
    {
162
        $feedItem = $this->getFeedItem();
163
        $this->assertInstanceOf(Item::class, $feedItem);
164
165
        // Test the item name as an unprofiled property value list
166
        $feedNameList = $feedItem->getProperty('name');
167
        $this->assertTrue(is_array($feedNameList));
168
        $this->assertEquals(1, count($feedNameList));
169
        $this->assertTrue(is_string($feedNameList[0]));
170
        $this->assertEquals('John Doe\'s Blog', $feedNameList[0]);
171
172
        // Test the item name as an unprofiled single property value
173
        $feedName = $feedItem->getProperty('name', null, 0);
174
        $this->assertTrue(is_string($feedName));
175
        $this->assertEquals('John Doe\'s Blog', $feedName);
176
177
        // Test an invalid unprofiled property
178
        $feedItem->getProperty('invalid');
179
    }
180
181
    /**
182
     * Test a profiled property
183
     *
184
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
185
     * @expectedExceptionCode 1488315604
186
     */
187
    public function testProfiledProperty()
188
    {
189
        $feedItem = $this->getFeedItem();
190
        $this->assertInstanceOf(Item::class, $feedItem);
191
192
        // Test the item name as an unprofiled property value list
193
        $feedNameList = $feedItem->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI);
194
        $this->assertTrue(is_array($feedNameList));
195
        $this->assertEquals(1, count($feedNameList));
196
        $this->assertTrue(is_string($feedNameList[0]));
197
        $this->assertEquals('John Doe\'s Blog', $feedNameList[0]);
198
199
        // Test the item name as an unprofiled single property value
200
        $feedName = $feedItem->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI, 0);
201
        $this->assertTrue(is_string($feedName));
202
        $this->assertEquals('John Doe\'s Blog', $feedName);
203
204
        // Test an invalid unprofiled property
205
        $feedItem->getProperty('invalid', MicroformatsFactory::MF2_PROFILE_URI);
206
    }
207
208
    /**
209
     * Test an unprofiled property
210
     *
211
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
212
     * @expectedExceptionCode 1488315604
213
     */
214
    public function testAliasedProperty()
215
    {
216
        $feedItem = $this->getFeedItem();
217
        $this->assertInstanceOf(Item::class, $feedItem);
218
219
        // Test the custom item property as an unprofiled property value list
220
        $feedCustomPropList = $feedItem->getProperty('custom-property');
221
        $this->assertTrue(is_array($feedCustomPropList));
222
        $this->assertEquals(1, count($feedCustomPropList));
223
        $this->assertTrue(is_string($feedCustomPropList[0]));
224
        $this->assertEquals('Property for alias testing', $feedCustomPropList[0]);
225
226
        // Test the custom item property as an unprofiled single property value
227
        $feedCustomProp = $feedItem->getProperty('custom-property', null, 0);
228
        $this->assertTrue(is_string($feedCustomProp));
229
        $this->assertEquals('Property for alias testing', $feedCustomProp);
230
231
        // Test the custom item property via the convenience getter
232
        $feedCustomProp = $feedItem->customProperty;
1 ignored issue
show
Bug introduced by
The property customProperty does not seem to exist in Jkphl\Micrometa\Ports\Item\Item.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
233
        $this->assertTrue(is_string($feedCustomProp));
234
        $this->assertEquals('Property for alias testing', $feedCustomProp);
235
236
        // Test an invalid property
237
        $feedItem->invalidProperty;
1 ignored issue
show
Documentation introduced by
The property invalidProperty does not exist on object<Jkphl\Micrometa\Ports\Item\Item>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
238
    }
239
240
    /**
241
     * Test a property stack
242
     *
243
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
244
     * @expectedExceptionCode 1488315604
245
     */
246
    public function testPropertyStack()
247
    {
248
        $feedItem = $this->getFeedItem();
249
        $this->assertInstanceOf(Item::class, $feedItem);
250
251
        // Request a valid property stack
252
        $propertyValues = $feedItem->getFirstProperty('photo', MicroformatsFactory::MF2_PROFILE_URI, 'name');
253
        $this->assertEquals(['John Doe\'s Blog'], $propertyValues);
254
255
        // Request unknown properties only
256
        $feedItem->getFirstProperty('photo', MicroformatsFactory::MF2_PROFILE_URI, 'invalid');
257
    }
258
259
    /**
260
     * Test a property item
261
     */
262
    public function testPropertyItem()
263
    {
264
        $feedItem = $this->getFeedItem();
265
        $this->assertInstanceOf(Item::class, $feedItem);
266
267
        // Request a valid property stack
268
        /** @var ItemInterface[] $authors */
269
        $authors = $feedItem->getFirstProperty('author');
270
        $this->assertTrue(is_array($authors));
271
        $this->assertInstanceOf(ItemInterface::class, $authors[0]);
272
273
        // Test the author name as an unprofiled single property value
274
        $authorName = $authors[0]->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI, 0);
275
        $this->assertTrue(is_string($authorName));
276
        $this->assertEquals('John Doe', $authorName);
277
    }
278
279
    /**
280
     * Test nested items
281
     *
282
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException
283
     * @expectedExceptionCode 1492418709
284
     */
285
    public function testNestedItems()
286
    {
287
        $feedItem = $this->getFeedItem();
288
        $this->assertInstanceOf(Item::class, $feedItem);
289
290
        // Test the number of nested items
291
        $this->assertEquals(2, count($feedItem));
292
        foreach ($feedItem as $entryItem) {
293
            $this->assertInstanceOf(ItemInterface::class, $entryItem);
294
        }
295
        $this->assertInstanceOf(ItemInterface::class, $feedItem->getFirstItem('h-entry'));
296
        $this->assertInstanceOf(
297
            ItemInterface::class, $feedItem->getFirstItem('h-entry', MicroformatsFactory::MF2_PROFILE_URI)
298
        );
299
300
        // Test the second entry item
301
        /** @var Item $entryItem */
302
        $entryItem = $feedItem->getItems('h-entry')[1];
303
        $this->assertInstanceOf(ItemInterface::class, $entryItem);
304
305
        // Test the magic item getter / item type aliases
306
        /** @noinspection PhpUndefinedMethodInspection */
307
        $entryItem = $feedItem->hEntry(0);
308
        $this->assertInstanceOf(ItemInterface::class, $entryItem);
309
        /** @noinspection PhpUndefinedMethodInspection */
310
        $feedItem->hEntry(-1);
311
    }
312
313
    /**
314
     * Test non-existent nested item
315
     *
316
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
317
     * @expectedExceptionCode 1492418999
318
     */
319
    public function testNonExistentNestedItems()
320
    {
321
        $feedItem = $this->getFeedItem();
322
        /** @noinspection PhpUndefinedMethodInspection */
323
        $this->assertEquals('John Doe', $feedItem->hEntry()->author->name);
324
        /** @noinspection PhpUndefinedMethodInspection */
325
        $feedItem->hEntry(2);
326
    }
327
}
328