Completed
Push — master ( acb0bf...7018dc )
by Joschi
02:52
created

ThingTest::testGetInvalidPropertyName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 * rdfa-lite
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Rdfalite
8
 * @subpackage Jkphl\Rdfalite\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\Rdfalite\Tests;
38
39
use Jkphl\Rdfalite\Domain\Thing;
40
use Jkphl\Rdfalite\Domain\Vocabulary;
41
42
/**
43
 * Thing tests
44
 *
45
 * @package Jkphl\Rdfalite
46
 * @subpackage Jkphl\Rdfalite\Tests
47
 */
48
class ThingTest extends \PHPUnit_Framework_TestCase
49
{
50
    /**
51
     * schema.org vocabulary
52
     *
53
     * @var Vocabulary
54
     */
55
    protected static $schemaOrgVocabulary;
56
57
    /**
58
     * Setup all tests
59
     */
60
    public static function setUpBeforeClass()
61
    {
62
        self::$schemaOrgVocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG);
63
    }
64
65
    /**
66
     * Test the instantiation of a minimum thing
67
     */
68
    public function testMinimumThing()
69
    {
70
        $type = 'Person';
71
        $thing = new Thing($type, self::$schemaOrgVocabulary);
72
        $this->assertInstanceOf(Thing::class, $thing);
73
        $this->assertEquals(VocabularyTest::SCHEMA_ORG.'/'.$type, $thing->getType());
74
        $this->assertEquals(self::$schemaOrgVocabulary, $thing->getVocabulary());
75
        $this->assertNull($thing->getId());
76
        $this->assertTrue(is_array($thing->getChildren()));
77
        $this->assertEquals(0, count($thing->getChildren()));
78
        $this->assertTrue(is_array($thing->getProperties()));
79
        $this->assertEquals(0, count($thing->getProperties()));
80
    }
81
82
    /**
83
     * Test the thing instantiation with an invalid type
84
     *
85
     * @expectedException \Jkphl\Rdfalite\Domain\RuntimeException
86
     * @expectedExceptionCode 1486823588
87
     */
88
    public function testInvalidTypeThing()
89
    {
90
        new Thing('', self::$schemaOrgVocabulary);
91
    }
92
93
    /**
94
     * Test adding a property
95
     */
96
    public function testAddProperty()
97
    {
98
        $thing = new Thing('Person', self::$schemaOrgVocabulary);
99
        $this->assertInstanceOf(Thing::class, $thing);
100
101
        $thing->addProperty('test1', 'value1');
102
        $thing->addProperty('test1', 'value2');
103
        $thing->addProperty('test2', 'value1');
104
105
        $properties = $thing->getProperties();
106
        $this->assertTrue(is_array($properties));
107
        $this->assertTrue(array_key_exists('test1', $properties));
108
        $this->assertTrue(array_key_exists('test2', $properties));
109
        $this->assertEquals(2, count($properties));
110
111
        $test1Property = $thing->getProperty('test1');
112
        $this->assertTrue(is_array($test1Property));
113
        $this->assertEquals(2, count($test1Property));
114
        $this->assertEquals(['value1', 'value2'], $test1Property);
115
    }
116
117
    /**
118
     * Test setting an invalid property name
119
     *
120
     * @expectedException \Jkphl\Rdfalite\Domain\RuntimeException
121
     * @expectedExceptionCode 1486848618
122
     */
123
    public function testSetInvalidPropertyName()
124
    {
125
        $thing = new Thing('Person', self::$schemaOrgVocabulary);
126
        $this->assertInstanceOf(Thing::class, $thing);
127
        $thing->addProperty('', null);
128
    }
129
130
    /**
131
     * Test getting an invalid property name
132
     *
133
     * @expectedException \Jkphl\Rdfalite\Domain\OutOfBoundsException
134
     * @expectedExceptionCode 1486849016
135
     */
136
    public function testGetInvalidPropertyName()
137
    {
138
        $thing = new Thing('Person', self::$schemaOrgVocabulary);
139
        $this->assertInstanceOf(Thing::class, $thing);
140
        $thing->getProperty('invalid');
141
    }
142
143
    /**
144
     * Test adding children
145
     */
146
    public function testAddChild() {
147
        $thing = new Thing('Person', self::$schemaOrgVocabulary);
148
        $this->assertInstanceOf(Thing::class, $thing);
149
150
        $child1 = new Thing('Person', self::$schemaOrgVocabulary);
151
        $child2 = new Thing('Person', self::$schemaOrgVocabulary);
152
        $thing->addChild($child1)->addChild($child2);
153
154
        $children = $thing->getChildren();
155
        $this->assertTrue(is_array($children));
156
        $this->assertEquals(2, count($children));
157
        $this->assertEquals([$child1, $child2], $children);
158
    }
159
}
160