Completed
Push — v2 ( 7fc49e...7442ee )
by Joschi
04:33
created

PropertyListTest::testProfiledInvalidProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 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 © 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\Factory\AliasFactory;
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(new AliasFactory());
61
        $this->assertInstanceOf(PropertyList::class, $propertyList);
62
        $this->assertEquals(0, count($propertyList));
63
64
        // Test adding a property
65
        $property = (object)[
66
            'name' => 'name',
67
            'profile' => MicroformatsFactory::MF2_PROFILE_URI,
68
            'values' => [new StringValue('John Doe')],
69
        ];
70
        $propertyList->add($property);
71
        $propertyList->add($property);
72
        $this->assertEquals(1, count($propertyList));
73
74
        // Iterate over all properties
75
        foreach ($propertyList as $propertyName => $propertyValues) {
76
            $this->assertInstanceOf(\stdClass::class, $propertyName);
77
            $this->assertTrue(is_array($propertyValues));
78
            $this->assertEquals(2, count($propertyValues));
79
        }
80
81
        // Test the array export
82
        $this->assertEquals(
83
            [
84
                MicroformatsFactory::MF2_PROFILE_URI.'name' => [
85
                    new StringValue('John Doe'),
86
                    new StringValue('John Doe')
87
                ]
88
            ], $propertyList->toArray()
89
        );
90
91
        // Get an unprofiled property
92
        $unprofiledProperty = $propertyList->offsetGet('name');
93
        $this->assertTrue(is_array($unprofiledProperty));
94
        $this->assertInstanceOf(StringValue::class, $unprofiledProperty[0]);
95
        $this->assertEquals('John Doe', $unprofiledProperty[0]);
96
97
        // Get an profiled property
98
        $unprofiledProperty = $propertyList->offsetGet(
99
            (object)['name' => 'name', 'profile' => MicroformatsFactory::MF2_PROFILE_URI]
100
        );
101
        $this->assertTrue(is_array($unprofiledProperty));
102
        $this->assertInstanceOf(StringValue::class, $unprofiledProperty[1]);
103
        $this->assertEquals('John Doe', $unprofiledProperty[1]);
104
105
        // Test unprofiled invalid property
106
        unset($propertyList['forbidden']);
107
    }
108
109
    /**
110
     * Test an unprofiled invalid property
111
     *
112
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException
113
     * @expectedExceptionCode 1488315604
114
     */
115
    public function testUnprofiledInvalidProperty()
116
    {
117
        $propertyList = new PropertyList(new AliasFactory());
118
        $this->assertInstanceOf(PropertyList::class, $propertyList);
119
        $propertyList['invalid'];
120
    }
121
122
    /**
123
     * Test an profiled invalid property
124
     *
125
     * @expectedException \Jkphl\Micrometa\Domain\Exceptions\OutOfBoundsException
126
     * @expectedExceptionCode 1488315604
127
     */
128
    public function testProfiledInvalidProperty()
129
    {
130
        $propertyList = new PropertyList(new AliasFactory());
131
        $this->assertInstanceOf(PropertyList::class, $propertyList);
132
        $propertyList->offsetGet((object)['name' => 'invalid', 'profile' => MicroformatsFactory::MF2_PROFILE_URI]);
133
    }
134
}
135