PropertyListTest::runProfiledPropertyTests()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.568
cc 1
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 © 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\Domain;
38
39
use Jkphl\Micrometa\Application\Value\StringValue;
40
use Jkphl\Micrometa\Domain\Item\Iri;
41
use Jkphl\Micrometa\Domain\Item\PropertyList;
42
use Jkphl\Micrometa\Infrastructure\Factory\MicroformatsFactory;
43
use Jkphl\Micrometa\Tests\AbstractTestBase;
44
45
/**
46
 * Property list tests
47
 *
48
 * @package    Jkphl\Micrometa
49
 * @subpackage Jkphl\Micrometa\Tests
50
 */
51
class PropertyListTest extends AbstractTestBase
52
{
53
    /**
54
     * Test the property list
55
     */
56
    public function testPropertyList()
57
    {
58
        $this->expectException('Jkphl\Micrometa\Domain\Exceptions\ErrorException');
59
        $this->expectExceptionCode('1489784392');
60
        $propertyList = new PropertyList();
61
        $this->assertInstanceOf(PropertyList::class, $propertyList);
62
        $this->assertEquals(0, count($propertyList));
63
64
        $this->runPropertyListAdditionTests($propertyList);
65
        $this->runProfiledPropertyTests($propertyList);
66
        $this->runUnprofiledPropertyTests($propertyList);
67
    }
68
69
    /**
70
     * Test adding properties
71
     *
72
     * @param PropertyList $propertyList Property list
73
     */
74
    protected function runPropertyListAdditionTests(PropertyList $propertyList)
75
    {
76
        // Test adding a property
77
        $property = (object)[
78
            'name'    => 'name',
79
            'profile' => MicroformatsFactory::MF2_PROFILE_URI,
80
            'values'  => [new StringValue('John Doe')],
81
        ];
82
        $propertyList->add($property);
83
        $propertyList->add($property);
84
        $this->assertEquals(1, count($propertyList));
85
86
        // Iterate over all properties
87
        foreach ($propertyList as $propertyName => $propertyValues) {
88
            $this->assertInstanceOf(Iri::class, $propertyName);
89
            $this->assertTrue(is_array($propertyValues));
90
            $this->assertEquals(2, count($propertyValues));
91
        }
92
    }
93
94
    /**
95
     * Test profiled properties
96
     *
97
     * @param PropertyList $propertyList Property list
98
     */
99
    protected function runProfiledPropertyTests(PropertyList $propertyList)
100
    {
101
        // Get a profiled property via object
102
        $unprofiledProperty = $propertyList->offsetGet(MicroformatsFactory::MF2_PROFILE_URI.'name');
103
        $this->assertTrue(is_array($unprofiledProperty));
104
        $this->assertInstanceOf(StringValue::class, $unprofiledProperty[1]);
105
        $this->assertEquals('John Doe', $unprofiledProperty[1]);
106
107
        // Get a profiled property via object
108
        $unprofiledProperty = $propertyList->offsetGet(
109
            (object)['name' => 'name', 'profile' => MicroformatsFactory::MF2_PROFILE_URI]
110
        );
111
        $this->assertTrue(is_array($unprofiledProperty));
112
        $this->assertInstanceOf(StringValue::class, $unprofiledProperty[1]);
113
        $this->assertEquals('John Doe', $unprofiledProperty[1]);
114
115
        // Get a profiled property via IRI
116
        $unprofiledProperty = $propertyList->offsetGet(new Iri(MicroformatsFactory::MF2_PROFILE_URI, 'name'));
117
        $this->assertTrue(is_array($unprofiledProperty));
118
        $this->assertInstanceOf(StringValue::class, $unprofiledProperty[1]);
119
        $this->assertEquals('John Doe', $unprofiledProperty[1]);
120
    }
121
122
    /**
123
     * Test unprofiled properties
124
     *
125
     * @param PropertyList $propertyList Property list
126
     */
127
    protected function runUnprofiledPropertyTests(PropertyList $propertyList)
128
    {
129
        // Get an unprofiled property
130
        $unprofiledProperty = $propertyList->offsetGet('name');
131
        $this->assertTrue(is_array($unprofiledProperty));
132
        $this->assertInstanceOf(StringValue::class, $unprofiledProperty[0]);
133
        $this->assertEquals('John Doe', $unprofiledProperty[0]);
134
135
        // Test unprofiled invalid property
136
        unset($propertyList['forbidden']);
137
    }
138
139
    /**
140
     * Test an unprofiled invalid property
141
     */
142
    public function testUnprofiledInvalidProperty()
143
    {
144
        $this->expectException('Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException');
145
        $this->expectExceptionCode('1488315604');
146
        $propertyList = new PropertyList();
147
        $this->assertInstanceOf(PropertyList::class, $propertyList);
148
        $propertyList['invalid'];
149
    }
150
151
    /**
152
     * Test an profiled invalid property
153
     */
154
    public function testProfiledInvalidProperty()
155
    {
156
        $this->expectException('Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException');
157
        $this->expectExceptionCode('1488315604');
158
        $propertyList = new PropertyList();
159
        $this->assertInstanceOf(PropertyList::class, $propertyList);
160
        $propertyList->offsetGet((object)['name' => 'invalid', 'profile' => MicroformatsFactory::MF2_PROFILE_URI]);
161
    }
162
}
163