This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
/
ParserAbstractTest.php
176 lines (150 loc) · 6.93 KB
/
ParserAbstractTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace Saft\Data\Test;
use Saft\Rdf\ArrayStatementIteratorImpl;
use Saft\Rdf\BlankNodeImpl;
use Saft\Rdf\LiteralImpl;
use Saft\Rdf\NamedNodeImpl;
use Saft\Rdf\NodeFactoryImpl;
use Saft\Rdf\RdfHelpers;
use Saft\Rdf\StatementImpl;
use Saft\Test\TestCase;
/**
* @codeCoverageIgnore
*/
abstract class ParserAbstractTest extends TestCase
{
/**
* @return Parser
*/
abstract protected function newInstance($serialization);
/*
* Tests for __construct
*/
public function testConstructorInvalidSerialization()
{
$this->setExpectedException('\Exception');
$this->newInstance('unknown');
}
/*
* Tests for parseStreamToIterator
* we load here the content of a file and transform it into an StatementIterator instance.
* afterwards we check if the read data are the same as expected.
*/
public function testParseStreamToIteratorTurtleFile()
{
$this->fixture = $this->newInstance('turtle');
// load iterator for a turtle file
$inputStream = dirname(__FILE__) .'/../resources/example.ttl';
$iterator = $this->fixture->parseStreamToIterator($inputStream);
$statementIteratorToCheckAgainst = new ArrayStatementIteratorImpl(array(
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/'),
new NamedNodeImpl(new RdfHelpers(), 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Foo')
),
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/'),
new NamedNodeImpl(new RdfHelpers(), 'http://www.w3.org/2000/01/rdf-schema#label'),
new LiteralImpl(new RdfHelpers(), 'RDFS label')
),
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Foobar'),
new NamedNodeImpl(new RdfHelpers(), 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Bar')
),
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Foobar'),
new NamedNodeImpl(new RdfHelpers(), 'http://www.w3.org/2000/01/rdf-schema#label'),
new LiteralImpl(
new RdfHelpers(),
'RDFS label with language tag',
new NamedNodeImpl(new RdfHelpers(), 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString'),
'en'
)
),
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Foobar'),
new NamedNodeImpl(new RdfHelpers(), 'http://www.w3.org/2000/01/rdf-schema#comment'),
new LiteralImpl(new RdfHelpers(), "\n Multi line comment\n ")
),
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Foobar'),
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/component'),
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/geo')
),
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Foobar'),
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/component'),
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/time')
),
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/dataset'),
new NamedNodeImpl(new RdfHelpers(), 'http://www.w3.org/2000/01/rdf-schema#label'),
new LiteralImpl(
new RdfHelpers(),
"RDFS label with datatype",
new NamedNodeImpl(new RdfHelpers(), 'http://www.w3.org/2001/XMLSchema#string')
)
),
));
$this->assertEquals($statementIteratorToCheckAgainst, $iterator);
}
/*
* Tests for parseStringToIterator
*/
public function testParseStringToIteratorTurtleString()
{
$xsdString = new NamedNodeImpl(new RdfHelpers(), 'http://www.w3.org/2001/XMLSchema#string');
$fixture = $this->newInstance('turtle');
$testString = '@prefix ex: <http://saft/example/> .
ex:Foo ex:knows ex:Bar ; ex:name "Foo"^^<'. $xsdString .'> .
ex:Bar ex:name "Bar"^^<'. $xsdString .'> .';
// build StatementIterator to check against
$statementIteratorToCheckAgainst = new ArrayStatementIteratorImpl(array(
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Foo'),
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/knows'),
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Bar')
),
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Foo'),
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/name'),
new LiteralImpl(new RdfHelpers(), 'Foo', $xsdString)
),
new StatementImpl(
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/Bar'),
new NamedNodeImpl(new RdfHelpers(), 'http://saft/example/name'),
new LiteralImpl(new RdfHelpers(), 'Bar', $xsdString)
),
));
$this->assertEquals($statementIteratorToCheckAgainst, $fixture->parseStringToIterator($testString));
}
// that test checks if the subject can be of type blank node
public function testParseStringToIteratorTurtleStringSubjectBlankNode()
{
$xsdString = new NamedNodeImpl(new RdfHelpers(), 'http://www.w3.org/2001/XMLSchema#string');
$fixture = $this->newInstance('turtle');
$testString = '@prefix ex: <http://saft/example/> .
_:foo ex:knows ex:Bar ;
ex:name "Foo"^^<'. $xsdString .'> .';
$result = $fixture->parseStringToIterator($testString);
$this->assertCountStatementIterator(2, $result);
// check generated triples
foreach ($result as $stmt) {
// subject needs to be a BlankNode
$this->assertTrue($stmt->getSubject()->isBlank());
// predicate check
$pUri = $stmt->getPredicate()->getUri();
$this->assertTrue(
'http://saft/example/knows' == $pUri || 'http://saft/example/name' == $pUri
);
// object check
$this->assertTrue($stmt->getObject()->isNamed() || $stmt->getObject()->isLiteral());
if ($stmt->getObject()->isNamed()) {
$this->assertEquals('http://saft/example/Bar', $stmt->getObject()->getUri());
} else { // isLiteral
$this->assertEquals('Foo', $stmt->getObject()->getValue());
}
}
}
}