Completed
Push — master ( 342540...c82ee6 )
by Joschi
02:27
created

ThingTest::testAnonymousThing()   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-microdata
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\RdfaLiteMicrodata
8
 * @subpackage Jkphl\RdfaLiteMicrodata\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\RdfaLiteMicrodata\Tests\Domain;
38
39
use Jkphl\RdfaLiteMicrodata\Application\Parser\NullVocabulary;
40
use Jkphl\RdfaLiteMicrodata\Domain\Property\Property;
41
use Jkphl\RdfaLiteMicrodata\Domain\Property\PropertyInterface;
42
use Jkphl\RdfaLiteMicrodata\Domain\Thing\Thing;
43
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface;
44
use Jkphl\RdfaLiteMicrodata\Domain\Type\Type;
45
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\Vocabulary;
46
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
47
48
/**
49
 * Thing tests
50
 *
51
 * @package Jkphl\RdfaLiteMicrodata
52
 * @subpackage Jkphl\RdfaLiteMicrodata\Tests
53
 */
54
class ThingTest extends \PHPUnit_Framework_TestCase
55
{
56
    /**
57
     * schema.org vocabulary
58
     *
59
     * @var Vocabulary
60
     */
61
    protected static $schemaOrgVocabulary;
62
63
    /**
64
     * Setup all tests
65
     */
66
    public static function setUpBeforeClass()
67
    {
68
        self::$schemaOrgVocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG_URI);
69
    }
70
71
    /**
72
     * Test the instantiation of a minimum thing
73
     */
74
    public function testMinimumThing()
75
    {
76
        $type = new Type('Person', self::$schemaOrgVocabulary);
77
        $thing = new Thing($type);
78
        $this->assertInstanceOf(Thing::class, $thing);
79
        $this->assertEquals([$type], $thing->getTypes());
80
        $this->assertNull($thing->getResourceId());
81
        $this->assertTrue(is_array($thing->getChildren()));
82
        $this->assertEquals(0, count($thing->getChildren()));
83
        $this->assertTrue(is_array($thing->getProperties()));
84
        $this->assertEquals(0, count($thing->getProperties()));
85
    }
86
87
    /**
88
     * Test the resource ID
89
     */
90
    public function testResourceId()
91
    {
92
        $type = new Type('Person', self::$schemaOrgVocabulary);
93
        $thing = new Thing($type, 'bob');
94
        $this->assertInstanceOf(Thing::class, $thing);
95
        $this->assertEquals('bob', $thing->getResourceId());
96
    }
97
98
    /**
99
     * Test the thing instantiation with an invalid type
100
     *
101
     * @expectedException \Jkphl\RdfaLiteMicrodata\Domain\Exceptions\RuntimeException
102
     * @expectedExceptionCode 1487435964
103
     */
104
    public function testInvalidTypeThing()
105
    {
106
        new Thing([null]);
1 ignored issue
show
Documentation introduced by
array(null) is of type array<integer,null,{"0":"null"}>, but the function expects a object<Jkphl\RdfaLiteMic...in\Type\TypeInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
    }
108
109
    /**
110
     * Test the instantiation of an anonymous thing
111
     */
112
    public function testAnonymousThing()
113
    {
114
        $thing = new Thing([]);
115
        $this->assertInstanceOf(Thing::class, $thing);
116
        $this->assertEquals([], $thing->getTypes());
117
    }
118
119
    /**
120
     * Test adding a property
121
     */
122
    public function testAddProperty()
123
    {
124
        $type = new Type('Person', self::$schemaOrgVocabulary);
125
        $thing = new Thing($type);
126
        $this->assertInstanceOf(Thing::class, $thing);
127
128
        $vocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG_URI);
129
        $property1 = new Property('test1', $vocabulary, 'value1');
130
        $property2 = new Property('test1', $vocabulary, 'value2');
131
        $property3 = new Property('test2', $vocabulary, 'value1');
132
        $thing->addProperty($property1);
133
        $thing->addProperty($property2);
134
        $thing->addProperty($property3);
135
136
        $this->validateProperties($thing, $vocabulary);
137
        $this->validateProperty($thing, $vocabulary, $property1, $property2);
138
    }
139
140
    /**
141
     * Validate all properties
142
     *
143
     * @param ThingInterface $thing Thing
144
     * @param VocabularyInterface $vocabulary Vocabulary
145
     */
146
    protected function validateProperties(ThingInterface $thing, VocabularyInterface $vocabulary)
147
    {
148
        $properties = $thing->getProperties();
149
        $this->assertTrue(is_array($properties));
150
        $this->assertTrue(array_key_exists($vocabulary->expand('test1'), $properties));
151
        $this->assertTrue(array_key_exists($vocabulary->expand('test2'), $properties));
152
        $this->assertEquals(2, count($properties));
153
    }
154
155
    /**
156
     * Validate a single property
157
     *
158
     * @param ThingInterface $thing Thing
159
     * @param VocabularyInterface $vocabulary Vocabulary
160
     * @param PropertyInterface $property1 First property
161
     * @param PropertyInterface $property2 Second property
162
     */
163
    protected function validateProperty(
164
        ThingInterface $thing,
165
        VocabularyInterface $vocabulary,
166
        PropertyInterface $property1,
167
        PropertyInterface $property2
168
    ) {
169
        $test1Property = $thing->getProperty('test1', $vocabulary);
170
        $this->assertTrue(is_array($test1Property));
171
        $this->assertEquals(2, count($test1Property));
172
        $this->assertEquals([$property1, $property2], $test1Property);
173
    }
174
175
    /**
176
     * Test getting an invalid property name
177
     *
178
     * @expectedException \Jkphl\RdfaLiteMicrodata\Domain\Exceptions\OutOfBoundsException
179
     * @expectedExceptionCode 1486849016
180
     */
181
    public function testGetInvalidPropertyName()
182
    {
183
        $type = new Type('Person', self::$schemaOrgVocabulary);
184
        $thing = new Thing($type);
185
        $this->assertInstanceOf(Thing::class, $thing);
186
        $thing->getProperty('invalid', new NullVocabulary());
187
    }
188
189
    /**
190
     * Test adding children
191
     */
192
    public function testAddChild()
193
    {
194
        $type = new Type('Person', self::$schemaOrgVocabulary);
195
        $thing = new Thing($type);
196
        $this->assertInstanceOf(Thing::class, $thing);
197
198
        $child1 = new Thing($type);
199
        $child2 = new Thing($type);
200
        $thing->addChild($child1)->addChild($child2);
201
202
        $children = $thing->getChildren();
203
        $this->assertTrue(is_array($children));
204
        $this->assertEquals(2, count($children));
205
        $this->assertEquals([$child1, $child2], $children);
206
    }
207
}
208