Completed
Push — master ( c82ee6...a08768 )
by Joschi
03:01
created

ThingTest::testAddChild()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 11
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->getProperties()));
82
        $this->assertEquals(0, count($thing->getProperties()));
83
    }
84
85
    /**
86
     * Test the resource ID
87
     */
88
    public function testResourceId()
89
    {
90
        $type = new Type('Person', self::$schemaOrgVocabulary);
91
        $thing = new Thing($type, 'bob');
92
        $this->assertInstanceOf(Thing::class, $thing);
93
        $this->assertEquals('bob', $thing->getResourceId());
94
    }
95
96
    /**
97
     * Test the thing instantiation with an invalid type
98
     *
99
     * @expectedException \Jkphl\RdfaLiteMicrodata\Domain\Exceptions\RuntimeException
100
     * @expectedExceptionCode 1487435964
101
     */
102
    public function testInvalidTypeThing()
103
    {
104
        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...
105
    }
106
107
    /**
108
     * Test the instantiation of an anonymous thing
109
     */
110
    public function testAnonymousThing()
111
    {
112
        $thing = new Thing([]);
113
        $this->assertInstanceOf(Thing::class, $thing);
114
        $this->assertEquals([], $thing->getTypes());
115
    }
116
117
    /**
118
     * Test adding a property
119
     */
120
    public function testAddProperty()
121
    {
122
        $type = new Type('Person', self::$schemaOrgVocabulary);
123
        $thing = new Thing($type);
124
        $this->assertInstanceOf(Thing::class, $thing);
125
126
        $vocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG_URI);
127
        $property1 = new Property('test1', $vocabulary, 'value1');
128
        $property2 = new Property('test1', $vocabulary, 'value2');
129
        $property3 = new Property('test2', $vocabulary, 'value1');
130
        $thing->addProperty($property1);
131
        $thing->addProperty($property2);
132
        $thing->addProperty($property3);
133
134
        $this->validateProperties($thing, $vocabulary);
135
        $this->validateProperty($thing, $vocabulary, $property1, $property2);
136
    }
137
138
    /**
139
     * Validate all properties
140
     *
141
     * @param ThingInterface $thing Thing
142
     * @param VocabularyInterface $vocabulary Vocabulary
143
     */
144
    protected function validateProperties(ThingInterface $thing, VocabularyInterface $vocabulary)
145
    {
146
        $properties = $thing->getProperties();
147
        $this->assertTrue(is_array($properties));
148
        $this->assertTrue(array_key_exists($vocabulary->expand('test1'), $properties));
149
        $this->assertTrue(array_key_exists($vocabulary->expand('test2'), $properties));
150
        $this->assertEquals(2, count($properties));
151
    }
152
153
    /**
154
     * Validate a single property
155
     *
156
     * @param ThingInterface $thing Thing
157
     * @param VocabularyInterface $vocabulary Vocabulary
158
     * @param PropertyInterface $property1 First property
159
     * @param PropertyInterface $property2 Second property
160
     */
161
    protected function validateProperty(
162
        ThingInterface $thing,
163
        VocabularyInterface $vocabulary,
164
        PropertyInterface $property1,
165
        PropertyInterface $property2
166
    ) {
167
        $test1Property = $thing->getProperty('test1', $vocabulary);
168
        $this->assertTrue(is_array($test1Property));
169
        $this->assertEquals(2, count($test1Property));
170
        $this->assertEquals([$property1, $property2], $test1Property);
171
    }
172
173
    /**
174
     * Test getting an invalid property name
175
     *
176
     * @expectedException \Jkphl\RdfaLiteMicrodata\Domain\Exceptions\OutOfBoundsException
177
     * @expectedExceptionCode 1486849016
178
     */
179
    public function testGetInvalidPropertyName()
180
    {
181
        $type = new Type('Person', self::$schemaOrgVocabulary);
182
        $thing = new Thing($type);
183
        $this->assertInstanceOf(Thing::class, $thing);
184
        $thing->getProperty('invalid', new NullVocabulary());
185
    }
186
}
187