Completed
Push — master ( da3279...199ae8 )
by Joschi
02:50
created

ThingTest::testAddProperty()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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