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

ItemObjectModelTest::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 74
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 74
c 2
b 0
f 1
rs 9.0335
cc 1
eloc 44
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\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\Domain\Item\Iri;
43
use Jkphl\Micrometa\Infrastructure\Factory\ItemFactory;
44
use Jkphl\Micrometa\Infrastructure\Parser\LinkType;
45
use Jkphl\Micrometa\Ports\Item\Item;
46
use Jkphl\Micrometa\Ports\Item\ItemInterface;
47
use Jkphl\Micrometa\Ports\Item\ItemList;
48
use Jkphl\Micrometa\Ports\Item\ItemObjectModel;
49
use Jkphl\Micrometa\Tests\AbstractTestBase;
50
use Jkphl\Micrometa\Tests\MicroformatsFeedTrait;
51
52
/**
53
 * Item object model tests
54
 *
55
 * @package Jkphl\Micrometa
56
 * @subpackage Jkphl\Micrometa\Tests
57
 */
58
class ItemObjectModelTest extends AbstractTestBase
59
{
60
    /**
61
     * Use the Microformats feed method
62
     */
63
    use MicroformatsFeedTrait;
64
65
    /**
66
     * Test the item object model
67
     *
68
     * @expectedException \Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException
69
     * @expectedExceptionCode 1489268571
70
     */
71
    public function testItemObjectModel()
72
    {
73
        $itemObjectModel = new ItemObjectModel($this->getItems());
74
        $this->assertInstanceOf(ItemObjectModel::class, $itemObjectModel);
75
76
        // Test all LinkType items
77
        $links = $itemObjectModel->link();
78
        $this->assertEquals(2, count($links));
79
        foreach ($links as $link) {
80
            $this->assertInstanceOf(ItemInterface::class, $link);
81
        }
82
83
        // Test the second LinkType item
84
        $secondLink = $itemObjectModel->link(null, 1);
85
        $this->assertInstanceOf(Item::class, $secondLink);
86
        $this->assertEquals(
87
            [new Iri(LinkType::HTML_PROFILE_URI, 'alternate')],
88
            $secondLink->getType()
89
        );
90
91
        $this->runStylesheetTests($itemObjectModel);
92
    }
93
94
    /**
95
     * Test the stylesheet items
96
     *
97
     * @param ItemObjectModel $itemObjectModel Item object model
98
     */
99
    protected function runStylesheetTests(ItemObjectModel $itemObjectModel) {
100
        // Test all stylesheet LinkType items
101
        $stylesheetLinks = $itemObjectModel->link('stylesheet');
102
        $this->assertInstanceOf(ItemList::class, $stylesheetLinks);
103
        $this->assertEquals(1, count($stylesheetLinks));
104
        $this->assertInstanceOf(Item::class, $stylesheetLinks[0]);
105
        $this->assertEquals(
106
            [new Iri(LinkType::HTML_PROFILE_URI, 'stylesheet')],
107
            $stylesheetLinks[0]->getType()
108
        );
109
110
        // Test the first stylesheet LinkType item
111
        $firstStylesheetLink = $itemObjectModel->link('stylesheet', 0);
112
        $this->assertInstanceOf(Item::class, $firstStylesheetLink);
113
        $this->assertEquals(
114
            [new Iri(LinkType::HTML_PROFILE_URI, 'stylesheet')],
115
            $firstStylesheetLink->getType()
116
        );
117
118
        // Test an invalid item index
119
        $itemObjectModel->link('stylesheet', 1);
120
    }
121
122
    /**
123
     * Return a list of 3 items
124
     *
125
     * @return Item[] Items
126
     */
127
    protected function getItems()
128
    {
129
        $items = ItemFactory::createFromApplicationItems(
130
            [
131
                // Stylesheet link item
132
                new ApplicationItem(
133
                    LinkType::FORMAT,
134
                    new PropertyListFactory(),
135
                    (object)['profile' => LinkType::HTML_PROFILE_URI, 'name' => 'stylesheet'],
136
                    [
137
                        (object)[
138
                            'profile' => LinkType::HTML_PROFILE_URI,
139
                            'name' => 'type',
140
                            'values' => [
141
                                new StringValue('text/css')
142
                            ]
143
                        ],
144
                        (object)[
145
                            'profile' => LinkType::HTML_PROFILE_URI,
146
                            'name' => 'href',
147
                            'values' => [
148
                                new StringValue('style.css')
149
                            ]
150
                        ]
151
                    ],
152
                    [],
153
                    'main-stylesheet'
154
                ),
155
156
                // Atom feed link item
157
                new ApplicationItem(
158
                    LinkType::FORMAT,
159
                    new PropertyListFactory(),
160
                    (object)['profile' => LinkType::HTML_PROFILE_URI, 'name' => 'alternate'],
161
                    [
162
                        (object)[
163
                            'profile' => LinkType::HTML_PROFILE_URI,
164
                            'name' => 'type',
165
                            'values' => [
166
                                new StringValue('application/atom+xml')
167
                            ]
168
                        ],
169
                        (object)[
170
                            'profile' => LinkType::HTML_PROFILE_URI,
171
                            'name' => 'href',
172
                            'values' => [
173
                                new StringValue('http://example.com/blog.atom')
174
                            ]
175
                        ],
176
                        (object)[
177
                            'profile' => LinkType::HTML_PROFILE_URI,
178
                            'name' => 'title',
179
                            'values' => [
180
                                new StringValue('Atom feed')
181
                            ]
182
                        ],
183
                        (object)[
184
                            'profile' => 'http://example.com/test-ns',
185
                            'name' => 'prop',
186
                            'values' => [
187
                                new StringValue('arbitrary')
188
                            ]
189
                        ]
190
                    ],
191
                    [],
192
                    'main-stylesheet'
193
                ),
194
            ]
195
        );
196
197
        $items[] = $this->getFeedItem();
198
199
        return $items;
200
    }
201
}
202