Completed
Push — v2 ( b41681...2062ad )
by Joschi
04:41
created

ItemObjectModelTest::runStylesheetTests()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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