ItemObjectModelTest::testItemObjectModel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
rs 9.424
cc 2
nc 2
nop 0
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 © 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\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
    public function testItemObjectModel()
69
    {
70
        $this->expectException('Jkphl\Micrometa\Ports\Exceptions\OutOfBoundsException');
71
        $this->expectExceptionCode('1489268571');
72
        $xml = '<html/>';
73
        $dom = new \DOMDocument();
74
        $dom->loadXML($xml);
75
        $itemObjectModel = new ItemObjectModel($dom, $this->getItems());
76
        $this->assertInstanceOf(ItemObjectModel::class, $itemObjectModel);
77
78
        $itemObjectModelDom = $itemObjectModel->getDom();
79
        $this->assertInstanceOf(\DOMDocument::class, $itemObjectModelDom);
80
        $this->assertEquals($xml, $itemObjectModelDom->saveXML($itemObjectModelDom->documentElement));
81
82
        // Test all LinkType items
83
        $links = $itemObjectModel->link();
84
        $this->assertEquals(2, count($links));
85
        foreach ($links as $link) {
86
            $this->assertInstanceOf(ItemInterface::class, $link);
87
        }
88
89
        // Test the second LinkType item
90
        $secondLink = $itemObjectModel->link(null, 1);
91
        $this->assertInstanceOf(Item::class, $secondLink);
92
        $this->assertEquals(
93
            [new Iri(LinkType::HTML_PROFILE_URI, 'alternate')],
94
            $secondLink->getType()
95
        );
96
97
        $this->runStylesheetTests($itemObjectModel);
98
    }
99
100
    /**
101
     * Return a list of 3 items
102
     *
103
     * @return Item[] Items
104
     */
105
    protected function getItems()
106
    {
107
        $items = ItemFactory::createFromApplicationItems(
108
            [
109
                // Stylesheet link item
110
                new ApplicationItem(
111
                    LinkType::FORMAT,
112
                    new PropertyListFactory(),
113
                    (object)['profile' => LinkType::HTML_PROFILE_URI, 'name' => 'stylesheet'],
114
                    [
115
                        (object)[
116
                            'profile' => LinkType::HTML_PROFILE_URI,
117
                            'name'    => 'type',
118
                            'values'  => [
119
                                new StringValue('text/css')
120
                            ]
121
                        ],
122
                        (object)[
123
                            'profile' => LinkType::HTML_PROFILE_URI,
124
                            'name'    => 'href',
125
                            'values'  => [
126
                                new StringValue('style.css')
127
                            ]
128
                        ]
129
                    ],
130
                    [],
131
                    'main-stylesheet'
132
                ),
133
134
                // Atom feed link item
135
                new ApplicationItem(
136
                    LinkType::FORMAT,
137
                    new PropertyListFactory(),
138
                    (object)['profile' => LinkType::HTML_PROFILE_URI, 'name' => 'alternate'],
139
                    [
140
                        (object)[
141
                            'profile' => LinkType::HTML_PROFILE_URI,
142
                            'name'    => 'type',
143
                            'values'  => [
144
                                new StringValue('application/atom+xml')
145
                            ]
146
                        ],
147
                        (object)[
148
                            'profile' => LinkType::HTML_PROFILE_URI,
149
                            'name'    => 'href',
150
                            'values'  => [
151
                                new StringValue('http://example.com/blog.atom')
152
                            ]
153
                        ],
154
                        (object)[
155
                            'profile' => LinkType::HTML_PROFILE_URI,
156
                            'name'    => 'title',
157
                            'values'  => [
158
                                new StringValue('Atom feed')
159
                            ]
160
                        ],
161
                        (object)[
162
                            'profile' => 'http://example.com/test-ns',
163
                            'name'    => 'prop',
164
                            'values'  => [
165
                                new StringValue('arbitrary')
166
                            ]
167
                        ]
168
                    ],
169
                    [],
170
                    'main-stylesheet'
171
                ),
172
            ]
173
        );
174
175
        $items[] = $this->getFeedItem();
176
177
        return $items;
178
    }
179
180
    /**
181
     * Test the stylesheet items
182
     *
183
     * @param ItemObjectModel $itemObjectModel Item object model
184
     */
185
    protected function runStylesheetTests(ItemObjectModel $itemObjectModel)
186
    {
187
        // Test all stylesheet LinkType items
188
        $stylesheetLinks = $itemObjectModel->link('stylesheet');
189
        $this->assertInstanceOf(ItemList::class, $stylesheetLinks);
190
        $this->assertEquals(1, count($stylesheetLinks));
191
        $this->assertInstanceOf(Item::class, $stylesheetLinks[0]);
192
        $this->assertEquals(
193
            [new Iri(LinkType::HTML_PROFILE_URI, 'stylesheet')],
194
            $stylesheetLinks[0]->getType()
195
        );
196
197
        // Test the first stylesheet LinkType item
198
        $firstStylesheetLink = $itemObjectModel->link('stylesheet', 0);
199
        $this->assertInstanceOf(Item::class, $firstStylesheetLink);
200
        $this->assertEquals(
201
            [new Iri(LinkType::HTML_PROFILE_URI, 'stylesheet')],
202
            $firstStylesheetLink->getType()
203
        );
204
205
        // Test an invalid item index
206
        $itemObjectModel->link('stylesheet', 1);
207
    }
208
}
209