Completed
Push — v2 ( d5d6a3...7fc49e )
by Joschi
05:54
created

ItemTest::testProfiledProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 20
rs 9.4285
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\Item\Item as ApplicationItem;
40
use Jkphl\Micrometa\Application\Value\StringValue;
41
use Jkphl\Micrometa\Infrastructure\Factory\MicroformatsFactory;
42
use Jkphl\Micrometa\Infrastructure\Parser\Microformats;
43
use Jkphl\Micrometa\Ports\Item\Item;
44
45
/**
46
 * Parser factory tests
47
 *
48
 * @package Jkphl\Micrometa
49
 * @subpackage Jkphl\Micrometa\Tests
50
 */
51
class ItemTest extends \PHPUnit_Framework_TestCase
52
{
53
    /**
54
     * Test an item
55
     */
56
    public function testItem()
57
    {
58
        $feedItem = $this->getFeedItem();
59
        $this->assertInstanceOf(Item::class, $feedItem);
60
61
        // Test the item type
62
        $this->assertTrue($feedItem->isOfType('h-feed'));
63
        $this->assertTrue($feedItem->isOfType('h-feed', MicroformatsFactory::MF2_PROFILE_URI));
64
    }
65
66
    /**
67
     * Test an unprofiled property
68
     *
69
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
70
     * @expectedExceptionCode 1488315604
71
     */
72
    public function testUnprofiledProperty()
73
    {
74
        $feedItem = $this->getFeedItem();
75
        $this->assertInstanceOf(Item::class, $feedItem);
76
77
        // Test the item name as an unprofiled property value list
78
        $feedNameList = $feedItem->getProperty('name');
79
        $this->assertTrue(is_array($feedNameList));
80
        $this->assertEquals(1, count($feedNameList));
81
        $this->assertInstanceOf(StringValue::class, $feedNameList[0]);
82
        $this->assertEquals('John Doe\'s Blog', strval($feedNameList[0]));
83
84
        // Test the item name as an unprofiled single property value
85
        $feedName = $feedItem->getProperty('name', null, 0);
86
        $this->assertInstanceOf(StringValue::class, $feedName);
87
        $this->assertEquals('John Doe\'s Blog', strval($feedName));
88
89
        // Test an invalid unprofiled property
90
        $feedItem->getProperty('invalid');
91
    }
92
93
    /**
94
     * Test an profiled property
95
     *
96
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
97
     * @expectedExceptionCode 1488315604
98
     */
99
    public function testProfiledProperty()
100
    {
101
        $feedItem = $this->getFeedItem();
102
        $this->assertInstanceOf(Item::class, $feedItem);
103
104
        // Test the item name as an unprofiled property value list
105
        $feedNameList = $feedItem->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI);
106
        $this->assertTrue(is_array($feedNameList));
107
        $this->assertEquals(1, count($feedNameList));
108
        $this->assertInstanceOf(StringValue::class, $feedNameList[0]);
109
        $this->assertEquals('John Doe\'s Blog', strval($feedNameList[0]));
110
111
        // Test the item name as an unprofiled single property value
112
        $feedName = $feedItem->getProperty('name', MicroformatsFactory::MF2_PROFILE_URI, 0);
113
        $this->assertInstanceOf(StringValue::class, $feedName);
114
        $this->assertEquals('John Doe\'s Blog', strval($feedName));
115
116
        // Test an invalid unprofiled property
117
        $feedItem->getProperty('invalid', MicroformatsFactory::MF2_PROFILE_URI);
118
    }
119
120
    /**
121
     * Create and return an h-feed Microformats item
122
     *
123
     * @return Item h-feed item
124
     */
125
    protected function getFeedItem()
126
    {
127
        $authorItem = new ApplicationItem(
128
            Microformats::FORMAT,
129
            (object)['profile' => MicroformatsFactory::MF2_PROFILE_URI, 'name' => 'h-card'],
130
            [
131
                (object)[
132
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
133
                    'name' => 'name',
134
                    'values' => [
135
                        new StringValue('John Doe')
136
                    ]
137
                ],
138
                (object)[
139
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
140
                    'name' => 'email',
141
                    'values' => [
142
                        new StringValue('[email protected]')
143
                    ]
144
                ]
145
            ]
146
        );
147
148
149
        $entryItem = new ApplicationItem(
150
            Microformats::FORMAT,
151
            (object)['profile' => MicroformatsFactory::MF2_PROFILE_URI, 'name' => 'h-entry'],
152
            [
153
                (object)[
154
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
155
                    'name' => 'name',
156
                    'values' => [
157
                        new StringValue('Famous blog post')
158
                    ]
159
                ],
160
                (object)[
161
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
162
                    'name' => 'author',
163
                    'values' => [
164
                        $authorItem
165
                    ]
166
                ]
167
            ]
168
        );
169
170
171
        $feedItem = new ApplicationItem(
172
            Microformats::FORMAT,
173
            (object)['profile' => MicroformatsFactory::MF2_PROFILE_URI, 'name' => 'h-feed'],
174
            [
175
                (object)[
176
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
177
                    'name' => 'name',
178
                    'values' => [
179
                        new StringValue('John Doe\'s Blog')
180
                    ]
181
                ],
182
                (object)[
183
                    'profile' => MicroformatsFactory::MF2_PROFILE_URI,
184
                    'name' => 'author',
185
                    'values' => [
186
                        $authorItem
187
                    ]
188
                ]
189
            ],
190
            [$entryItem, $entryItem]
191
        );
192
193
        return new Item($feedItem);
194
    }
195
}
196