Completed
Push — master ( 2f5cae...701262 )
by Joschi
03:21
created

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