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

PropertyListTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 109
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testPropertyList() 0 10 1
A runPropertyListAdditionTests() 0 19 2
A runProfiledPropertyTests() 0 16 1
A runUnprofiledPropertyTests() 0 11 1
A testUnprofiledInvalidProperty() 0 6 1
A testProfiledInvalidProperty() 0 6 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\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
44
/**
45
 * Property list tests
46
 *
47
 * @package Jkphl\Micrometa
48
 * @subpackage Jkphl\Micrometa\Tests
49
 */
50
class PropertyListTest extends \PHPUnit_Framework_TestCase
51
{
52
    /**
53
     * Test the property list
54
     *
55
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\ErrorException
56
     * @expectedExceptionCode 1489784392
57
     */
58
    public function testPropertyList()
59
    {
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(
103
            (object)['name' => 'name', 'profile' => MicroformatsFactory::MF2_PROFILE_URI]
104
        );
105
        $this->assertTrue(is_array($unprofiledProperty));
106
        $this->assertInstanceOf(StringValue::class, $unprofiledProperty[1]);
107
        $this->assertEquals('John Doe', $unprofiledProperty[1]);
108
109
        // Get a profiled property via IRI
110
        $unprofiledProperty = $propertyList->offsetGet(new Iri(MicroformatsFactory::MF2_PROFILE_URI, 'name'));
111
        $this->assertTrue(is_array($unprofiledProperty));
112
        $this->assertInstanceOf(StringValue::class, $unprofiledProperty[1]);
113
        $this->assertEquals('John Doe', $unprofiledProperty[1]);
114
    }
115
116
    /**
117
     * Test unprofiled properties
118
     *
119
     * @param PropertyList $propertyList Property list
120
     */
121
    protected function runUnprofiledPropertyTests(PropertyList $propertyList)
122
    {
123
        // Get an unprofiled property
124
        $unprofiledProperty = $propertyList->offsetGet('name');
125
        $this->assertTrue(is_array($unprofiledProperty));
126
        $this->assertInstanceOf(StringValue::class, $unprofiledProperty[0]);
127
        $this->assertEquals('John Doe', $unprofiledProperty[0]);
128
129
        // Test unprofiled invalid property
130
        unset($propertyList['forbidden']);
131
    }
132
133
    /**
134
     * Test an unprofiled invalid property
135
     *
136
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException
137
     * @expectedExceptionCode 1488315604
138
     */
139
    public function testUnprofiledInvalidProperty()
140
    {
141
        $propertyList = new PropertyList();
142
        $this->assertInstanceOf(PropertyList::class, $propertyList);
143
        $propertyList['invalid'];
144
    }
145
146
    /**
147
     * Test an profiled invalid property
148
     *
149
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException
150
     * @expectedExceptionCode 1488315604
151
     */
152
    public function testProfiledInvalidProperty()
153
    {
154
        $propertyList = new PropertyList();
155
        $this->assertInstanceOf(PropertyList::class, $propertyList);
156
        $propertyList->offsetGet((object)['name' => 'invalid', 'profile' => MicroformatsFactory::MF2_PROFILE_URI]);
157
    }
158
}
159