Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty prefix support #183

Merged
merged 5 commits into from Jul 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/EasyRdf/Namespace.php
Expand Up @@ -130,7 +130,7 @@ public static function resetNamespaces()
*/
public static function get($prefix)
{
if (!is_string($prefix) or $prefix === null or $prefix === '') {
if (!is_string($prefix) or $prefix === null) {
throw new InvalidArgumentException(
"\$prefix should be a string and cannot be null or empty"
);
Expand Down Expand Up @@ -160,7 +160,7 @@ public static function get($prefix)
*/
public static function set($prefix, $long)
{
if (!is_string($prefix) or $prefix === null or $prefix === '') {
if (!is_string($prefix) or $prefix === null) {
throw new InvalidArgumentException(
"\$prefix should be a string and cannot be null or empty"
);
Expand Down Expand Up @@ -357,7 +357,7 @@ public static function expand($shortUri)
"\$shortUri should be a string and cannot be null or empty"
);
}

if ($shortUri === 'a') {
$namespaces = self::namespaces();
return $namespaces['rdf'] . 'type';
Expand Down
25 changes: 12 additions & 13 deletions test/EasyRdf/NamespaceTest.php
Expand Up @@ -116,15 +116,6 @@ public function testGetNullNamespace()
EasyRdf_Namespace::get(null);
}

public function testGetEmptyNamespace()
{
$this->setExpectedException(
'InvalidArgumentException',
'$prefix should be a string and cannot be null or empty'
);
EasyRdf_Namespace::get('');
}

public function testGetNonStringNamespace()
{
$this->setExpectedException(
Expand Down Expand Up @@ -172,10 +163,6 @@ public function testAddNamespaceShortNull()

public function testAddNamespaceShortEmpty()
{
$this->setExpectedException(
'InvalidArgumentException',
'$prefix should be a string and cannot be null or empty'
);
EasyRdf_Namespace::set('', 'http://purl.org/ontology/ko/');
}

Expand Down Expand Up @@ -485,6 +472,18 @@ public function testPrefixOfUnknownUrl()
);
}

public function testGetEmptyNamespace()
{
EasyRdf_Namespace::set('', 'http://xmlns.com/foaf/0.1/name');

$url = EasyRdf_Namespace::get('');

$this->assertSame(
'http://xmlns.com/foaf/0.1/name',
$url
);
}

public function testPrefixOfUriNull()
{
$this->setExpectedException(
Expand Down
19 changes: 13 additions & 6 deletions test/EasyRdf/Serialiser/JsonLdTest.php
Expand Up @@ -62,6 +62,7 @@ public function setUp()
$joe = $this->graph->resource('http://www.example.com/joe#me', 'foaf:Person');
$joe->set('foaf:name', new EasyRdf_Literal('Joe Bloggs', 'en'));
$joe->set('foaf:age', 59);
$joe->set('foaf:homepage', $this->graph->resource('http://foo/bar/me'));

$project = $this->graph->newBNode();
$project->add('foaf:name', 'Project Name');
Expand All @@ -71,6 +72,7 @@ public function setUp()
EasyRdf_Namespace::set('dc', 'http://purl.org/dc/elements/1.1/');
EasyRdf_Namespace::set('ex', 'http://example.org/vocab#');
EasyRdf_Namespace::set('xsd', 'http://www.w3.org/2001/XMLSchema#');
EasyRdf_Namespace::set('', 'http://foo/bar/');

$chapter=$this->graph->resource('http://example.org/library/the-republic#introduction', 'ex:Chapter');
$chapter->set('dc:description', new EasyRdf_Literal('An introductory chapter on The Republic.'));
Expand Down Expand Up @@ -116,6 +118,11 @@ public function testSerialiseJsonLd()
->getProperty('http://xmlns.com/foaf/0.1/name')
->getValue()
);

$this->assertEquals(
'http://foo/bar/me',
$node->getProperty('http://xmlns.com/foaf/0.1/homepage')->getId()
);
}

public function testSerialiseJsonLdOptions()
Expand All @@ -125,21 +132,21 @@ public function testSerialiseJsonLdOptions()
$decoded = json_decode($string, true);

$this->assertArrayNotHasKey('@graph', $decoded);
$this->assertInternalType('array', $decoded[7]["http://xmlns.com/foaf/0.1/age"][0]);
$this->assertSame(59, $decoded[7]["http://xmlns.com/foaf/0.1/age"][0]['@value']);
$this->assertArrayNotHasKey('@type', $decoded[7]["http://xmlns.com/foaf/0.1/age"][0]);
$this->assertInternalType('array', $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]);
$this->assertSame(59, $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]['@value']);
$this->assertArrayNotHasKey('@type', $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]);


// Expanded form + explicit types
$string = $this->serialiser->serialise($this->graph, 'jsonld', array('expand_native_types' => true));
$decoded = json_decode($string, true);

$this->assertArrayNotHasKey('@graph', $decoded);
$this->assertInternalType('array', $decoded[7]["http://xmlns.com/foaf/0.1/age"][0]);
$this->assertSame('59', $decoded[7]["http://xmlns.com/foaf/0.1/age"][0]['@value']);
$this->assertInternalType('array', $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]);
$this->assertSame('59', $decoded[8]["http://xmlns.com/foaf/0.1/age"][0]['@value']);
$this->assertSame(
'http://www.w3.org/2001/XMLSchema#integer',
$decoded[7]["http://xmlns.com/foaf/0.1/age"][0]['@type']
$decoded[8]["http://xmlns.com/foaf/0.1/age"][0]['@type']
);


Expand Down
6 changes: 6 additions & 0 deletions test/EasyRdf/Serialiser/JsonTest.php
Expand Up @@ -51,19 +51,25 @@ public function setUp()

public function testSerialiseJson()
{
\EasyRdf_Namespace::set('', 'http://foo/bar/');

$joe = $this->graph->resource('http://www.example.com/joe#me', 'foaf:Person');
$joe->set('foaf:name', new EasyRdf_Literal('Joe Bloggs', 'en'));
$joe->set('foaf:homepage', $this->graph->resource('http://foo/bar/me'));
$joe->set('foaf:age', 59);
$project = $this->graph->newBNode();
$project->add('foaf:name', 'Project Name');
$joe->add('foaf:project', $project);

$json = $this->serialiser->serialise($this->graph, 'json');

$this->assertSame(
'{"http:\/\/www.example.com\/joe#me":{'.
'"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#type":['.
'{"type":"uri","value":"http:\/\/xmlns.com\/foaf\/0.1\/Person"}],'.
'"http:\/\/xmlns.com\/foaf\/0.1\/name":['.
'{"type":"literal","value":"Joe Bloggs","lang":"en"}],'.
'"http:\/\/xmlns.com\/foaf\/0.1\/homepage":[{"type":"uri","value":"http:\/\/foo\/bar\/me"}],'.
'"http:\/\/xmlns.com\/foaf\/0.1\/age":['.
'{"type":"literal","value":"59","datatype":'.
'"http:\/\/www.w3.org\/2001\/XMLSchema#integer"}],'.
Expand Down
23 changes: 23 additions & 0 deletions test/EasyRdf/Serialiser/NtriplesTest.php
Expand Up @@ -221,6 +221,29 @@ public function testSerialiseDatatype()
);
}

public function testSerialiseEmptyPrefix()
{
\EasyRdf_Namespace::set('', 'http://foo/bar/');

$joe = $this->graph->resource(
'http://foo/bar/me'
);

$joe->set('foaf:name', 'Joe Bloggs');
$joe->set(
'foaf:homepage',
$this->graph->resource('http://example.com/joe/')
);

$ntriples = $this->serialiser->serialise($this->graph, 'ntriples');

$this->assertSame(
"<http://foo/bar/me> <http://xmlns.com/foaf/0.1/name> \"Joe Bloggs\" .\n" .
"<http://foo/bar/me> <http://xmlns.com/foaf/0.1/homepage> <http://example.com/joe/> .\n",
$ntriples
);
}

public function testSerialiseUnsupportedFormat()
{
$this->setExpectedException(
Expand Down
30 changes: 30 additions & 0 deletions test/EasyRdf/Serialiser/RdfXmlTest.php
Expand Up @@ -411,4 +411,34 @@ public function testSerialiseContainer()
$this->serialiser->serialise($this->graph, 'rdfxml')
);
}

public function testSerialiseEmptyPrefix()
{
\EasyRdf_Namespace::set('', 'http://foo/bar/');

$joe = $this->graph->resource(
'http://foo/bar/me',
'foaf:Person'
);

$joe->set('foaf:name', 'Joe Bloggs');
$joe->set(
'foaf:homepage',
$this->graph->resource('http://example.com/joe/')
);

$rdf = $this->serialiser->serialise($this->graph, 'rdfxml');

$this->assertSame(
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n".
"<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n".
" xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">\n\n".
" <foaf:Person rdf:about=\"http://foo/bar/me\">\n".
" <foaf:name>Joe Bloggs</foaf:name>\n".
" <foaf:homepage rdf:resource=\"http://example.com/joe/\"/>\n".
" </foaf:Person>\n\n".
"</rdf:RDF>\n",
$rdf
);
}
}
29 changes: 29 additions & 0 deletions test/EasyRdf/Serialiser/TurtleTest.php
Expand Up @@ -683,6 +683,35 @@ public function testSerialiseUnshortenableProperty()
);
}

public function testSerialiseEmptyPrefix()
{
\EasyRdf_Namespace::set('', 'http://foo/bar/');

$joe = $this->graph->resource(
'http://foo/bar/me',
'foaf:Person'
);

$joe->set('foaf:name', 'Joe Bloggs');
$joe->set(
'foaf:homepage',
$this->graph->resource('http://example.com/joe/')
);

$turtle = $this->serialiser->serialise($this->graph, 'turtle');

$this->assertSame(
"@prefix : <http://foo/bar/> .\n".
"@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n".
"\n".
":me\n".
" a foaf:Person ;\n".
" foaf:name \"Joe Bloggs\" ;\n".
" foaf:homepage <http://example.com/joe/> .\n\n",
$turtle
);
}

public function testSerialiseUnsupportedFormat()
{
$this->setExpectedException(
Expand Down