ItemTest::testItemUnprofiledProperty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.568
cc 1
nc 1
nop 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 © 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\Ports;
38
39
use Jkphl\Micrometa\Application\Item\PropertyList;
40
use Jkphl\Micrometa\Application\Value\StringValue;
41
use Jkphl\Micrometa\Infrastructure\Factory\MicroformatsFactory;
42
use Jkphl\Micrometa\Ports\Item\Item;
43
use Jkphl\Micrometa\Ports\Item\ItemInterface;
44
use Jkphl\Micrometa\Ports\Item\ItemList;
45
use Jkphl\Micrometa\Tests\AbstractTestBase;
46
use Jkphl\Micrometa\Tests\MicroformatsFeedTrait;
47
48
/**
49
 * Parser factory tests
50
 *
51
 * @package    Jkphl\Micrometa
52
 * @subpackage Jkphl\Micrometa\Tests
53
 */
54
class ItemTest extends AbstractTestBase
55
{
56
    /**
57
     * Use the Microformats feed method
58
     */
59
    use MicroformatsFeedTrait;
60
61
    /**
62
     * Test an item
63
     */
64
    public function testItemTypes()
65
    {
66
        $feedItem = $this->getFeedItem();
67
        $this->assertInstanceOf(Item::class, $feedItem);
68
69
        // Test the item type
70
        $this->assertTrue($feedItem->isOfType('h-feed'));
71
        $this->assertTrue($feedItem->isOfType('h-feed', MicroformatsFactory::MF2_PROFILE_URI));
72
        $this->assertFalse($feedItem->isOfType('invalid', MicroformatsFactory::MF2_PROFILE_URI));
73
74
        // Test other item properties
75
        $this->assertEquals('feed-id', $feedItem->getId());
76
        $this->assertEquals('feed-language', $feedItem->getLanguage());
77
        $this->assertEquals('feed-value', $feedItem->getValue());
78
    }
79
80
    /**
81
     * Test the item properties
82
     */
83
    public function testItemProperties()
84
    {
85
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException');
86
        $this->expectExceptionCode('1491672553');
87
        $feedItem = $this->getFeedItem();
88
        $this->assertInstanceOf(Item::class, $feedItem);
89
90
        $properties = $feedItem->getProperties();
91
        $this->assertInstanceOf(PropertyList::class, $properties);
92
        $this->assertEquals(3, count($properties));
93
94
        // Get an unknown property
95
        $feedItem->getProperty('name', null, 2);
96
    }
97
98
    /**
99
     * Test the item export
100
     */
101
    public function testItemExport()
102
    {
103
        $feedItem = $this->getFeedItem();
104
        $this->assertInstanceOf(Item::class, $feedItem);
105
106
        $export = $feedItem->toObject();
107
        $this->assertInstanceOf(\stdClass::class, $export);
108
        foreach (['format', 'types', 'properties', 'items'] as $property) {
109
            $this->assertTrue(isset($export->$property));
110
        }
111
    }
112
113
    /**
114
     * Test an unprofiled property
115
     */
116
    public function testItemUnprofiledProperty()
117
    {
118
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException');
119
        $this->expectExceptionCode('1488315604');
120
        $feedItem = $this->getFeedItem();
121
        $this->assertInstanceOf(Item::class, $feedItem);
122
123
        // Test the item name as an unprofiled property value list
124
        $feedNameList = $feedItem->getProperty('name');
125
        $this->assertTrue(is_array($feedNameList));
126
        $this->assertEquals(1, count($feedNameList));
127
        $this->assertInstanceOf(StringValue::class, $feedNameList[0]);
128
        $this->assertEquals('John Doe\'s Blog', strval($feedNameList[0]));
129
130
        // Test the item name as an unprofiled single property value
131
        $feedName = $feedItem->getProperty('name', null, 0);
132
        $this->assertInstanceOf(StringValue::class, $feedName);
133
        $this->assertEquals('John Doe\'s Blog', strval($feedName));
134
135
        // Test an invalid unprofiled property
136
        $feedItem->getProperty('invalid');
137
    }
138
139
    /**
140
     * Test a profiled property
141
     */
142
    public function testItemProfiledProperty()
143
    {
144
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException');
145
        $this->expectExceptionCode('1488315604');
146
        $feedItem = $this->getFeedItem();
147
        $this->assertInstanceOf(Item::class, $feedItem);
148
149
        // Test the item name as an unprofiled property value list
150
        $feedNameList = $feedItem->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI);
151
        $this->assertTrue(is_array($feedNameList));
152
        $this->assertEquals(1, count($feedNameList));
153
        $this->assertInstanceOf(StringValue::class, $feedNameList[0]);
154
        $this->assertEquals('John Doe\'s Blog', strval($feedNameList[0]));
155
156
        // Test the item name as an unprofiled single property value
157
        $feedName = $feedItem->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI, 0);
158
        $this->assertInstanceOf(StringValue::class, $feedName);
159
        $this->assertEquals('John Doe\'s Blog', strval($feedName));
160
161
        // Test an invalid unprofiled property
162
        $feedItem->getProperty('invalid', MicroformatsFactory::MF2_PROFILE_URI);
163
    }
164
165
    /**
166
     * Test an unprofiled property
167
     */
168
    public function testItemAliasedProperty()
169
    {
170
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException');
171
        $this->expectExceptionCode('1488315604');
172
        $feedItem = $this->getFeedItem();
173
        $this->assertInstanceOf(Item::class, $feedItem);
174
175
        // Run the custom item property tests
176
        $this->runCustomItemPropertyTests($feedItem);
177
178
        // Test an invalid property
179
        $feedItem->invalidProperty;
0 ignored issues
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...
180
    }
181
182
    /**
183
     * Run the custom item property tests
184
     *
185
     * @param Item $feedItem Feed item
186
     */
187
    protected function runCustomItemPropertyTests(Item $feedItem)
188
    {
189
        // Test the custom item property as an unprofiled property value list
190
        $feedCustomPropList = $feedItem->getProperty('custom-property');
191
        $this->assertTrue(is_array($feedCustomPropList));
192
        $this->assertEquals(1, count($feedCustomPropList));
193
        $this->assertInstanceOf(StringValue::class, $feedCustomPropList[0]);
194
        $this->assertEquals('Property for alias testing', strval($feedCustomPropList[0]));
195
196
        // Test the custom item property as an unprofiled single property value
197
        $feedCustomProp = $feedItem->getProperty('custom-property', null, 0);
198
        $this->assertInstanceOf(StringValue::class, $feedCustomProp);
199
        $this->assertEquals('Property for alias testing', strval($feedCustomProp));
200
201
        // Test the custom item property via the convenience getter
202
        $feedCustomProp = $feedItem->customProperty;
0 ignored issues
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...
203
        $this->assertInstanceOf(StringValue::class, $feedCustomProp);
204
        $this->assertEquals('Property for alias testing', strval($feedCustomProp));
205
    }
206
207
    /**
208
     * Test a property stack
209
     */
210
    public function testItemPropertyStack()
211
    {
212
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException');
213
        $this->expectExceptionCode('1488315604');
214
        $feedItem = $this->getFeedItem();
215
        $this->assertInstanceOf(Item::class, $feedItem);
216
217
        // Request a valid property stack
218
        $propertyValues = $feedItem->getFirstProperty('photo', MicroformatsFactory::MF2_PROFILE_URI, 'name');
219
        $this->assertEquals(['John Doe\'s Blog'], $propertyValues);
220
221
        // Request unknown properties only
222
        $feedItem->getFirstProperty('photo', MicroformatsFactory::MF2_PROFILE_URI, 'invalid');
223
    }
224
225
    /**
226
     * Test a property item
227
     */
228
    public function testItemPropertyItem()
229
    {
230
        $feedItem = $this->getFeedItem();
231
        $this->assertInstanceOf(Item::class, $feedItem);
232
233
        // Request a valid property stack
234
        /** @var ItemInterface[] $authors */
235
        $authors = $feedItem->getFirstProperty('author');
236
        $this->assertTrue(is_array($authors));
237
        $this->assertInstanceOf(ItemInterface::class, $authors[0]);
238
239
        // Test the author name as an unprofiled single property value
240
        $authorName = $authors[0]->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI, 0);
241
        $this->assertInstanceOf(StringValue::class, $authorName);
242
        $this->assertEquals('John Doe', strval($authorName));
243
    }
244
245
    /**
246
     * Test nested items
247
     */
248
    public function testItemNestedItems()
249
    {
250
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\InvalidArgumentException');
251
        $this->expectExceptionCode('1492418709');
252
        $feedItem = $this->getFeedItem();
253
        $this->assertInstanceOf(Item::class, $feedItem);
254
255
        // Test the number of nested items
256
        $this->assertEquals(2, count($feedItem));
257
        $this->assertEquals(2, count($feedItem->getItems()));
258
        foreach ($feedItem as $itemIndex => $entryItem) {
259
            $this->assertInstanceOf(ItemInterface::class, $entryItem);
260
            $this->assertTrue(is_int($itemIndex));
261
        }
262
        $this->assertInstanceOf(ItemInterface::class, $feedItem->getFirstItem('h-entry'));
263
        $this->assertInstanceOf(
264
            ItemInterface::class,
265
            $feedItem->getFirstItem('h-entry', MicroformatsFactory::MF2_PROFILE_URI)
266
        );
267
268
        // Test the second entry item
269
        $this->runFeedNestedEntryItemTest($feedItem);
270
    }
271
272
    /**
273
     * Test a nested entry item
274
     *
275
     * @param ItemInterface $feedItem Feed item
276
     */
277
    protected function runFeedNestedEntryItemTest(ItemInterface $feedItem)
278
    {
279
        $entryItem = $feedItem->getItems('h-entry')[1];
280
        $this->assertInstanceOf(ItemInterface::class, $entryItem);
281
282
        // Test the magic item getter / item type aliases
283
        /** @noinspection PhpUndefinedMethodInspection */
284
        $entryItem = $feedItem->hEntry(0);
0 ignored issues
show
Bug introduced by
The method hEntry() does not seem to exist on object<Jkphl\Micrometa\Ports\Item\ItemInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
285
        $this->assertInstanceOf(ItemInterface::class, $entryItem);
286
        /** @noinspection PhpUndefinedMethodInspection */
287
        $feedItem->hEntry(-1);
0 ignored issues
show
Bug introduced by
The method hEntry() does not seem to exist on object<Jkphl\Micrometa\Ports\Item\ItemInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
288
    }
289
290
    /**
291
     * Test non-existent nested item
292
     */
293
    public function testItemNonExistentNestedItems()
294
    {
295
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException');
296
        $this->expectExceptionCode('1492418999');
297
        $feedItem = $this->getFeedItem();
298
        /** @noinspection PhpUndefinedMethodInspection */
299
        $this->assertEquals('John Doe', $feedItem->hEntry()->author->name);
0 ignored issues
show
Documentation Bug introduced by
The method hEntry does not exist on object<Jkphl\Micrometa\Ports\Item\Item>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
300
        /** @noinspection PhpUndefinedMethodInspection */
301
        $feedItem->hEntry(2);
0 ignored issues
show
Documentation Bug introduced by
The method hEntry does not exist on object<Jkphl\Micrometa\Ports\Item\Item>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
302
    }
303
304
    /**
305
     * Test the item list export
306
     */
307
    public function testItemListExport()
308
    {
309
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException');
310
        $this->expectExceptionCode('1492030227');
311
        $feedItem = $this->getFeedItem();
312
        $itemList = new ItemList([$feedItem]);
313
        $this->assertInstanceOf(ItemList::class, $itemList);
314
315
        $export = $itemList->toObject();
316
        $this->assertInstanceOf(\stdClass::class, $export);
317
        $this->assertTrue(isset($export->items));
318
        $this->assertTrue(is_array($export->items));
319
        $this->assertEquals($feedItem->toObject(), current($export->items));
320
321
        $itemList->getFirstItem('invalid');
322
    }
323
324
    /**
325
     * Test the item list immutability
326
     */
327
    public function testItemListImmutabilitySet()
328
    {
329
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\RuntimeException');
330
        $this->expectExceptionCode('1495988721');
331
        $feedItem = $this->getFeedItem();
332
        $itemList = new ItemList([$feedItem]);
333
        $this->assertInstanceOf(ItemList::class, $itemList);
334
        $this->assertEquals($feedItem, $itemList[0]);
335
        $itemList[1] = $feedItem;
336
    }
337
338
    /**
339
     * Test the item list immutability
340
     */
341
    public function testItemListImmutabilityUnset()
342
    {
343
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\RuntimeException');
344
        $this->expectExceptionCode('1495988721');
345
        $feedItem = $this->getFeedItem();
346
        $itemList = new ItemList([$feedItem]);
347
        $this->assertInstanceOf(ItemList::class, $itemList);
348
        $this->assertEquals($feedItem, $itemList[0]);
349
        unset($itemList[0]);
350
    }
351
}
352