ContextTest::testAddingLevelChildren()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 17
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\Application;
38
39
use Jkphl\RdfaLiteMicrodata\Application\Context\RdfaLiteContext;
40
use Jkphl\RdfaLiteMicrodata\Domain\Thing\Thing;
41
use Jkphl\RdfaLiteMicrodata\Domain\Type\Type;
42
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\Vocabulary;
43
use Jkphl\RdfaLiteMicrodata\Tests\Domain\VocabularyTest;
44
45
/**
46
 * Parser context tests
47
 *
48
 * @package Jkphl\RdfaLiteMicrodata
49
 * @subpackage Jkphl\RdfaLiteMicrodata\Tests
50
 */
51
class ContextTest extends \PHPUnit_Framework_TestCase
52
{
53
    /**
54
     * Test parser context instantiation
55
     */
56
    public function testContext()
57
    {
58
        $context = new RdfaLiteContext();
59
        $this->assertInstanceOf(RdfaLiteContext::class, $context);
60
        $this->assertTrue($context->hasVocabulary('schema'));
61
62
        // Test vocabulary retrieval
63
        $schemaOrgVocabulary = $context->getVocabulary('schema');
64
        $this->assertInstanceOf(Vocabulary::class, $schemaOrgVocabulary);
65
        $this->assertEquals($schemaOrgVocabulary, new Vocabulary(VocabularyTest::SCHEMA_ORG_URI));
66
    }
67
68
    /**
69
     * Test the registration of a vocabulary
70
     */
71
    public function testContextVocabRegistration()
72
    {
73
        $randomPrefix = 'random'.rand();
74
        $context = new RdfaLiteContext();
75
        $this->assertInstanceOf(RdfaLiteContext::class, $context);
76
        $this->assertFalse($context->hasVocabulary($randomPrefix));
77
        $contextHash = spl_object_hash($context);
78
79
        $sameContext = $context->registerVocabulary('schema', VocabularyTest::SCHEMA_ORG_URI);
80
        $this->assertEquals($contextHash, spl_object_hash($sameContext));
81
82
        $newContext = $sameContext->registerVocabulary($randomPrefix, 'http://example.com');
83
        $this->assertNotEquals($contextHash, spl_object_hash($newContext));
84
        $this->assertEquals(new Vocabulary('http://example.com'), $newContext->getVocabulary($randomPrefix));
85
    }
86
87
    /**
88
     * Test an invalid vocabulary prefix
89
     *
90
     * @expectedException \Jkphl\RdfaLiteMicrodata\Application\Exceptions\RuntimeException
91
     * @expectedExceptionCode 1486927326
92
     */
93
    public function testInvalidVocabularyPrefix()
94
    {
95
        $context = new RdfaLiteContext();
96
        $this->assertInstanceOf(RdfaLiteContext::class, $context);
97
        $context->registerVocabulary('', null);
98
    }
99
100
    /**
101
     * Test an invalid vocabulary URI
102
     *
103
     * @expectedException \Jkphl\RdfaLiteMicrodata\Domain\Exceptions\RuntimeException
104
     * @expectedExceptionCode 1486823170
105
     */
106
    public function testInvalidVocabularyUri()
107
    {
108
        $context = new RdfaLiteContext();
109
        $this->assertInstanceOf(RdfaLiteContext::class, $context);
110
        $context->registerVocabulary('test', null);
111
    }
112
113
    /**
114
     * Test an invalid vocabulary URI
115
     *
116
     * @expectedException \Jkphl\RdfaLiteMicrodata\Application\Exceptions\OutOfBoundsException
117
     * @expectedExceptionCode 1486928423
118
     */
119
    public function testUnknownVocabularyPrefix()
120
    {
121
        $context = new RdfaLiteContext();
122
        $this->assertInstanceOf(RdfaLiteContext::class, $context);
123
        $context->getVocabulary('random'.rand());
124
    }
125
126
    /**
127
     * Test the default vocabulary
128
     */
129
    public function testDefaultVocabulary()
130
    {
131
        $context = new RdfaLiteContext();
132
        $this->assertInstanceOf(RdfaLiteContext::class, $context);
133
        $vocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG_URI);
134
        $newContext = $context->setDefaultVocabulary($vocabulary);
135
        $this->assertEquals($vocabulary, $newContext->getDefaultVocabulary());
136
        $this->assertNotEquals(spl_object_hash($context), spl_object_hash($newContext));
137
        $this->assertEquals(
138
            spl_object_hash($newContext),
139
            spl_object_hash($newContext->setDefaultVocabulary($vocabulary))
140
        );
141
    }
142
143
    /**
144
     * Test the parent thing
145
     */
146
    public function testParentThing()
147
    {
148
        $context = new RdfaLiteContext();
149
        $this->assertInstanceOf(RdfaLiteContext::class, $context);
150
        $vocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG_URI);
151
        $type = new Type('Person', $vocabulary);
152
        $thing = new Thing($type);
153
        $newContext = $context->setParentThing($thing);
154
        $this->assertEquals($thing, $newContext->getParentThing());
155
        $this->assertNotEquals(spl_object_hash($context), spl_object_hash($newContext));
156
        $this->assertEquals(
157
            spl_object_hash($newContext),
158
            spl_object_hash($newContext->setParentThing($thing))
159
        );
160
    }
161
162
    /**
163
     * Test adding children
164
     */
165
    public function testAddingLevelChildren()
166
    {
167
        $context = new RdfaLiteContext();
168
        $this->assertInstanceOf(RdfaLiteContext::class, $context);
169
        $vocabulary = new Vocabulary(VocabularyTest::SCHEMA_ORG_URI);
170
171
        $type = new Type('Person', $vocabulary);
172
        $thing = new Thing($type);
173
        $context->addChild($thing);
174
        $this->assertEquals([$thing], $context->getChildren());
175
        $context->addChild($thing);
176
        $this->assertEquals([$thing], $context->getChildren());
177
178
        $parent = new Thing($type);
179
        $newContext = $context->setParentThing($parent);
180
        $this->assertEquals([$thing], $newContext->getChildren());
181
        $newContext->addChild($thing);
182
        $this->assertEquals([$thing], $newContext->getChildren());
183
        $newContext->addChild($thing);
184
        $this->assertEquals([$thing], $newContext->getChildren());
185
    }
186
}
187