Completed
Push — v2 ( 7442ee...58f91a )
by Joschi
04:41
created

ItemTest::testAliasedProperty()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
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\AliasFactory;
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
46
/**
47
 * Parser factory tests
48
 *
49
 * @package Jkphl\Micrometa
50
 * @subpackage Jkphl\Micrometa\Tests
51
 */
52
class ItemTest extends \PHPUnit_Framework_TestCase
53
{
54
    /**
55
     * Test an item
56
     */
57
    public function testItemTypes()
58
    {
59
        $feedItem = $this->getFeedItem();
60
        $this->assertInstanceOf(Item::class, $feedItem);
61
62
        // Test the item type
63
        $this->assertTrue($feedItem->isOfType('h-feed'));
64
        $this->assertTrue($feedItem->isOfType('h-feed', MicroformatsFactory::MF2_PROFILE_URI));
65
    }
66
67
    /**
68
     * Create and return an h-feed Microformats item
69
     *
70
     * @return Item h-feed item
71
     */
72
    protected function getFeedItem()
73
    {
74
        $authorItem = new ApplicationItem(
75
            Microformats::FORMAT,
76
            new AliasFactory(),
77
            (object)['profile' => MicroformatsFactory::MF2_PROFILE_URI, 'name' => 'h-card'],
78
            [
79
                (object)[
80
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
81
                    'name' => 'name',
82
                    'values' => [
83
                        new StringValue('John Doe')
84
                    ]
85
                ],
86
                (object)[
87
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
88
                    'name' => 'email',
89
                    'values' => [
90
                        new StringValue('[email protected]')
91
                    ]
92
                ]
93
            ]
94
        );
95
96
97
        $entryItem = new ApplicationItem(
98
            Microformats::FORMAT,
99
            new AliasFactory(),
100
            (object)['profile' => MicroformatsFactory::MF2_PROFILE_URI, 'name' => 'h-entry'],
101
            [
102
                (object)[
103
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
104
                    'name' => 'name',
105
                    'values' => [
106
                        new StringValue('Famous blog post')
107
                    ]
108
                ],
109
                (object)[
110
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
111
                    'name' => 'author',
112
                    'values' => [
113
                        $authorItem
114
                    ]
115
                ]
116
            ]
117
        );
118
119
120
        $feedItem = new ApplicationItem(
121
            Microformats::FORMAT,
122
            new AliasFactory(),
123
            (object)['profile' => MicroformatsFactory::MF2_PROFILE_URI, 'name' => 'h-feed'],
124
            [
125
                (object)[
126
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
127
                    'name' => 'name',
128
                    'values' => [
129
                        new StringValue('John Doe\'s Blog')
130
                    ]
131
                ],
132
                (object)[
133
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
134
                    'name' => 'author',
135
                    'values' => [
136
                        $authorItem
137
                    ]
138
                ],
139
                (object)[
140
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
141
                    'name' => 'custom-property',
142
                    'values' => [
143
                        new StringValue('Property for alias testing')
144
                    ]
145
                ],
146
            ],
147
            [$entryItem, $entryItem]
148
        );
149
150
        return new Item($feedItem);
151
    }
152
153
    /**
154
     * Test an unprofiled property
155
     *
156
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
157
     * @expectedExceptionCode 1488315604
158
     */
159
    public function testUnprofiledProperty()
160
    {
161
        $feedItem = $this->getFeedItem();
162
        $this->assertInstanceOf(Item::class, $feedItem);
163
164
        // Test the item name as an unprofiled property value list
165
        $feedNameList = $feedItem->getProperty('name');
166
        $this->assertTrue(is_array($feedNameList));
167
        $this->assertEquals(1, count($feedNameList));
168
        $this->assertInstanceOf(StringValue::class, $feedNameList[0]);
169
        $this->assertEquals('John Doe\'s Blog', strval($feedNameList[0]));
170
171
        // Test the item name as an unprofiled single property value
172
        $feedName = $feedItem->getProperty('name', null, 0);
173
        $this->assertInstanceOf(StringValue::class, $feedName);
174
        $this->assertEquals('John Doe\'s Blog', strval($feedName));
175
176
        // Test an invalid unprofiled property
177
        $feedItem->getProperty('invalid');
178
    }
179
180
    /**
181
     * Test a profiled property
182
     *
183
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
184
     * @expectedExceptionCode 1488315604
185
     */
186
    public function testProfiledProperty()
187
    {
188
        $feedItem = $this->getFeedItem();
189
        $this->assertInstanceOf(Item::class, $feedItem);
190
191
        // Test the item name as an unprofiled property value list
192
        $feedNameList = $feedItem->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI);
193
        $this->assertTrue(is_array($feedNameList));
194
        $this->assertEquals(1, count($feedNameList));
195
        $this->assertInstanceOf(StringValue::class, $feedNameList[0]);
196
        $this->assertEquals('John Doe\'s Blog', strval($feedNameList[0]));
197
198
        // Test the item name as an unprofiled single property value
199
        $feedName = $feedItem->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI, 0);
200
        $this->assertInstanceOf(StringValue::class, $feedName);
201
        $this->assertEquals('John Doe\'s Blog', strval($feedName));
202
203
        // Test an invalid unprofiled property
204
        $feedItem->getProperty('invalid', MicroformatsFactory::MF2_PROFILE_URI);
205
    }
206
207
    /**
208
     * Test an unprofiled property
209
     *
210
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
211
     * @expectedExceptionCode 1488315604
212
     */
213
    public function testAliasedProperty()
214
    {
215
        $feedItem = $this->getFeedItem();
216
        $this->assertInstanceOf(Item::class, $feedItem);
217
218
        // Test the custom item property as an unprofiled property value list
219
        $feedCustomPropList = $feedItem->getProperty('custom-property');
220
        $this->assertTrue(is_array($feedCustomPropList));
221
        $this->assertEquals(1, count($feedCustomPropList));
222
        $this->assertInstanceOf(StringValue::class, $feedCustomPropList[0]);
223
        $this->assertEquals('Property for alias testing', strval($feedCustomPropList[0]));
224
225
        // Test the custom item property as an unprofiled single property value
226
        $feedCustomProp = $feedItem->getProperty('custom-property', null, 0);
227
        $this->assertInstanceOf(StringValue::class, $feedCustomProp);
228
        $this->assertEquals('Property for alias testing', strval($feedCustomProp));
229
230
        // Test the custom item property via the convenience getter
231
        $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...
232
        $this->assertInstanceOf(StringValue::class, $feedCustomProp);
233
        $this->assertEquals('Property for alias testing', strval($feedCustomProp));
234
235
        // Test an invalid property
236
        $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...
237
    }
238
239
    /**
240
     * Test a property stack
241
     *
242
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
243
     * @expectedExceptionCode 1488315604
244
     */
245
    public function testPropertyStack()
246
    {
247
        $feedItem = $this->getFeedItem();
248
        $this->assertInstanceOf(Item::class, $feedItem);
249
250
        // Request a valid property stack
251
        $propertyValues = $feedItem->getFirstProperty('photo', MicroformatsFactory::MF2_PROFILE_URI, 'name');
252
        $this->assertEquals([new StringValue('John Doe\'s Blog')], $propertyValues);
253
254
        // Request unknown properties only
255
        $feedItem->getFirstProperty('photo', MicroformatsFactory::MF2_PROFILE_URI, 'invalid');
256
    }
257
}
258