Extend and reformat tests

This commit is contained in:
2026-05-15 10:41:38 +00:00
parent 6cb8a9d603
commit a9689d5a2b

View File

@@ -7,150 +7,180 @@
namespace CloudObjects\SDK; namespace CloudObjects\SDK;
use ML\IRI\IRI; use ML\IRI\IRI;
use ML\JsonLD\Node;
use GuzzleHttp\Client, GuzzleHttp\Handler\MockHandler, use GuzzleHttp\Client, GuzzleHttp\Handler\MockHandler,
GuzzleHttp\HandlerStack, GuzzleHttp\Psr7\Response; GuzzleHttp\HandlerStack, GuzzleHttp\Psr7\Response;
class NodeReaderMockTest extends \PHPUnit\Framework\TestCase { class NodeReaderMockTest extends \PHPUnit\Framework\TestCase {
private $retriever; private $retriever;
private $reader; private $reader;
private function setMockResponse(Response $response) { private function setMockResponse(Response $response) {
$mock = new MockHandler([$response]); $mock = new MockHandler([$response]);
$handler = HandlerStack::create($mock); $handler = HandlerStack::create($mock);
$this->retriever->setClient(new Client(['handler' => $handler])); $this->retriever->setClient(new Client(['handler' => $handler]));
} }
private function useRootResourceMock() { private function useRootResourceMock() {
$this->setMockResponse(new Response(200, $this->setMockResponse(new Response(200,
['Content-Type' => 'application/ld+json'], ['Content-Type' => 'application/ld+json'],
'{"@context":{"co":"coid:\/\/cloudobjects.io\/","rdf":"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#","agws":"coid:\/\/aauid.net\/","rdfs":"http:\/\/www.w3.org\/2000\/01\/rdf-schema#"},"@id":"coid:\/\/cloudobjects.io","@type":["agws:Service","co:Namespace"],"co:isAtRevision":"6-fbea0c90b2c5e5300e4039ed99be9b2d","co:isVisibleTo":{"@id":"co:Public"},"co:recommendsPrefix":"co","co:wasUpdatedAt":{"@type":"http:\/\/www.w3.org\/2001\/XMLSchema#dateTime","@value":"2017-01-16T17:29:22+00:00"},"rdfs:comment":"The CloudObjects namespace defines the essential objects.","rdfs:label":"CloudObjects"}')); '{"@context":{"co":"coid:\/\/cloudobjects.io\/","rdf":"http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#","agws":"coid:\/\/aauid.net\/","rdfs":"http:\/\/www.w3.org\/2000\/01\/rdf-schema#"},"@id":"coid:\/\/cloudobjects.io","@type":["agws:Service","co:Namespace"],"co:isAtRevision":"6-fbea0c90b2c5e5300e4039ed99be9b2d","co:isVisibleTo":{"@id":"co:Public"},"co:recommendsPrefix":"co","co:wasUpdatedAt":{"@type":"http:\/\/www.w3.org\/2001\/XMLSchema#dateTime","@value":"2017-01-16T17:29:22+00:00"},"rdfs:comment":"The CloudObjects namespace defines the essential objects.","rdfs:label":"CloudObjects"}'
} ));
}
protected function setUp(): void { protected function setUp(): void {
$this->retriever = new ObjectRetriever; $this->retriever = new ObjectRetriever;
$this->reader = new NodeReader([ $this->reader = new NodeReader([
'prefixes' => [ 'prefixes' => [
'co' => 'coid://cloudobjects.io/', 'co' => 'coid://cloudobjects.io/',
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#' 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#'
] ]
]); ]);
} $this->retriever->setDefaultReader($this->reader);
}
public function testHasType1() { public function testHasType1() {
$coid = new IRI('coid://cloudobjects.io'); $coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock(); $this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid); $object = $this->retriever->getObjectNode($coid);
$this->assertTrue($this->reader->hasType($object, 'coid://cloudobjects.io/Namespace')); $this->assertTrue($this->reader->hasType($object, 'coid://cloudobjects.io/Namespace'));
$this->assertTrue($this->reader->hasType($object, 'co:Namespace')); $this->assertTrue($this->reader->hasType($object, 'co:Namespace'));
$this->assertFalse($this->reader->hasType($object, 'coid://cloudobjects.io/MemberRole')); $this->assertFalse($this->reader->hasType($object, 'coid://cloudobjects.io/MemberRole'));
$this->assertFalse($this->reader->hasType($object, 'co:MemberRole')); $this->assertFalse($this->reader->hasType($object, 'co:MemberRole'));
} }
public function testHasPropertyValue1() { public function testHasPropertyValue1() {
$coid = new IRI('coid://cloudobjects.io'); $coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock(); $this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid); $object = $this->retriever->getObjectNode($coid);
$this->assertTrue($this->reader->hasPropertyValue($object, 'http://www.w3.org/2000/01/rdf-schema#label', 'CloudObjects')); $this->assertTrue($this->reader->hasPropertyValue($object, 'http://www.w3.org/2000/01/rdf-schema#label', 'CloudObjects'));
$this->assertTrue($this->reader->hasPropertyValue($object, 'rdfs:label', 'CloudObjects')); $this->assertTrue($this->reader->hasPropertyValue($object, 'rdfs:label', 'CloudObjects'));
} }
public function testGetFirstValueString1() { public function testGetFirstValueString1() {
$coid = new IRI('coid://cloudobjects.io'); $coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock(); $this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid); $object = $this->retriever->getObjectNode($coid);
$this->assertEquals('CloudObjects', $this->reader->getFirstValueString($object, 'http://www.w3.org/2000/01/rdf-schema#label')); $this->assertEquals('CloudObjects', $this->reader->getFirstValueString($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertEquals('CloudObjects', $this->reader->getFirstValueString($object, 'rdfs:label')); $this->assertEquals('CloudObjects', $this->reader->getFirstValueString($object, 'rdfs:label'));
$this->assertNull($this->reader->getFirstValueString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo')); $this->assertNull($this->reader->getFirstValueString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo'));
$this->assertNull($this->reader->getFirstValueString($object, 'co:makesTriplesVisibleTo')); $this->assertNull($this->reader->getFirstValueString($object, 'co:makesTriplesVisibleTo'));
$this->assertEquals('theDefaultValue', $this->reader->getFirstValueString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo', 'theDefaultValue')); $this->assertEquals('theDefaultValue', $this->reader->getFirstValueString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo', 'theDefaultValue'));
$this->assertEquals('theDefaultValue', $this->reader->getFirstValueString($object, 'co:makesTriplesVisibleTo', 'theDefaultValue')); $this->assertEquals('theDefaultValue', $this->reader->getFirstValueString($object, 'co:makesTriplesVisibleTo', 'theDefaultValue'));
}
public function testGetFirstValueIRI1() { $object = $this->retriever->getCloudObject($coid);
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertInstanceOf('ML\IRI\IRI', $this->reader->getFirstValueIRI($object, 'coid://cloudobjects.io/isVisibleTo')); $this->assertEquals('CloudObjects', $object->getString('http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertInstanceOf('ML\IRI\IRI', $this->reader->getFirstValueIRI($object, 'co:isVisibleTo')); $this->assertEquals('CloudObjects', $object->getString('rdfs:label'));
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getFirstValueIRI($object, 'coid://cloudobjects.io/isVisibleTo')); $this->assertNull($object->getString('coid://cloudobjects.io/makesTriplesVisibleTo'));
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getFirstValueIRI($object, 'co:isVisibleTo')); $this->assertNull($object->getString('co:makesTriplesVisibleTo'));
}
public function testGetFirstValueNode1() { $this->assertEquals('theDefaultValue', $object->getString('coid://cloudobjects.io/makesTriplesVisibleTo', 'theDefaultValue'));
$coid = new IRI('coid://cloudobjects.io'); $this->assertEquals('theDefaultValue', $object->getString('co:makesTriplesVisibleTo', 'theDefaultValue'));
$this->useRootResourceMock(); }
$object = $this->retriever->getObjectNode($coid);
$this->assertInstanceOf('ML\JsonLD\Node', $this->reader->getFirstValueNode($object, 'coid://cloudobjects.io/isVisibleTo')); public function testGetFirstValueIRI1() {
$this->assertInstanceOf('ML\JsonLD\Node', $this->reader->getFirstValueNode($object, 'co:isVisibleTo')); $coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getFirstValueNode($object, 'coid://cloudobjects.io/isVisibleTo')->getId()); $this->assertInstanceOf(IRI::class, $this->reader->getFirstValueIRI($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getFirstValueNode($object, 'co:isVisibleTo')->getId()); $this->assertInstanceOf(IRI::class, $this->reader->getFirstValueIRI($object, 'co:isVisibleTo'));
}
public function testGetAllValuesString1() { $this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getFirstValueIRI($object, 'coid://cloudobjects.io/isVisibleTo'));
$coid = new IRI('coid://cloudobjects.io'); $this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getFirstValueIRI($object, 'co:isVisibleTo'));
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertCount(1, $this->reader->getAllValuesString($object, 'http://www.w3.org/2000/01/rdf-schema#label')); $object = $this->retriever->getCloudObject($coid);
$this->assertCount(1, $this->reader->getAllValuesString($object, 'rdfs:label'));
$this->assertEquals('CloudObjects', $this->reader->getAllValuesString($object, 'http://www.w3.org/2000/01/rdf-schema#label')[0]); $this->assertInstanceOf(IRI::class, $object->getIRI('coid://cloudobjects.io/isVisibleTo'));
$this->assertEquals('CloudObjects', $this->reader->getAllValuesString($object, 'rdfs:label')[0]); $this->assertInstanceOf(IRI::class, $object->getIRI('co:isVisibleTo'));
$this->assertCount(0, $this->reader->getAllValuesString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo')); $this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $object->getIRI('coid://cloudobjects.io/isVisibleTo'));
$this->assertCount(0, $this->reader->getAllValuesString($object, 'co:makesTriplesVisibleTo')); $this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $object->getIRI('co:isVisibleTo'));
}
$this->assertCount(2, $this->reader->getAllValuesString($object, '@type')); public function testGetFirstValueNode1() {
} $coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
public function testGetAllValuesIRI1() { $this->assertInstanceOf(Node::class, $this->reader->getFirstValueNode($object, 'coid://cloudobjects.io/isVisibleTo'));
$coid = new IRI('coid://cloudobjects.io'); $this->assertInstanceOf(Node::class, $this->reader->getFirstValueNode($object, 'co:isVisibleTo'));
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertCount(0, $this->reader->getAllValuesIRI($object, 'http://www.w3.org/2000/01/rdf-schema#label')); $this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getFirstValueNode($object, 'coid://cloudobjects.io/isVisibleTo')->getId());
$this->assertCount(0, $this->reader->getAllValuesIRI($object, 'rdfs:label')); $this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getFirstValueNode($object, 'co:isVisibleTo')->getId());
$this->assertCount(1, $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo')); $object = $this->retriever->getCloudObject($coid);
$this->assertCount(1, $this->reader->getAllValuesIRI($object, 'co:isVisibleTo'));
$this->assertCount(2, $this->reader->getAllValuesIRI($object, '@type')); $this->assertInstanceOf(Node::class, $object->getNode('coid://cloudobjects.io/isVisibleTo'));
$this->assertInstanceOf(Node::class, $object->getNode('co:isVisibleTo'));
$this->assertInstanceOf('ML\IRI\IRI', $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo')[0]); $this->assertEquals('coid://cloudobjects.io/Public', $object->getNode('coid://cloudobjects.io/isVisibleTo')->getId());
$this->assertInstanceOf('ML\IRI\IRI', $this->reader->getAllValuesIRI($object, 'co:isVisibleTo')[0]); $this->assertEquals('coid://cloudobjects.io/Public', $object->getNode('co:isVisibleTo')->getId());
}
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo')[0]); public function testGetAllValuesString1() {
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getAllValuesIRI($object, 'co:isVisibleTo')[0]); $coid = new IRI('coid://cloudobjects.io');
} $this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
public function testGetAllValuesNode1() { $this->assertCount(1, $this->reader->getAllValuesString($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$coid = new IRI('coid://cloudobjects.io'); $this->assertCount(1, $this->reader->getAllValuesString($object, 'rdfs:label'));
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertCount(0, $this->reader->getAllValuesNode($object, 'http://www.w3.org/2000/01/rdf-schema#label')); $this->assertEquals('CloudObjects', $this->reader->getAllValuesString($object, 'http://www.w3.org/2000/01/rdf-schema#label')[0]);
$this->assertCount(0, $this->reader->getAllValuesNode($object, 'rdfs:label')); $this->assertEquals('CloudObjects', $this->reader->getAllValuesString($object, 'rdfs:label')[0]);
$this->assertCount(1, $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo')); $this->assertCount(0, $this->reader->getAllValuesString($object, 'coid://cloudobjects.io/makesTriplesVisibleTo'));
$this->assertCount(1, $this->reader->getAllValuesNode($object, 'co:isVisibleTo')); $this->assertCount(0, $this->reader->getAllValuesString($object, 'co:makesTriplesVisibleTo'));
$this->assertCount(2, $this->reader->getAllValuesNode($object, '@type')); $this->assertCount(2, $this->reader->getAllValuesString($object, '@type'));
}
$this->assertInstanceOf('ML\JsonLD\Node', $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo')[0]); public function testGetAllValuesIRI1() {
$this->assertInstanceOf('ML\JsonLD\Node', $this->reader->getAllValuesNode($object, 'co:isVisibleTo')[0]); $coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo')[0]->getId()); $this->assertCount(0, $this->reader->getAllValuesIRI($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getAllValuesNode($object, 'co:isVisibleTo')[0]->getId()); $this->assertCount(0, $this->reader->getAllValuesIRI($object, 'rdfs:label'));
}
$this->assertCount(1, $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertCount(1, $this->reader->getAllValuesIRI($object, 'co:isVisibleTo'));
$this->assertCount(2, $this->reader->getAllValuesIRI($object, '@type'));
$this->assertInstanceOf(IRI::class, $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo')[0]);
$this->assertInstanceOf(IRI::class, $this->reader->getAllValuesIRI($object, 'co:isVisibleTo')[0]);
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getAllValuesIRI($object, 'coid://cloudobjects.io/isVisibleTo')[0]);
$this->assertEquals(new IRI('coid://cloudobjects.io/Public'), $this->reader->getAllValuesIRI($object, 'co:isVisibleTo')[0]);
}
public function testGetAllValuesNode1() {
$coid = new IRI('coid://cloudobjects.io');
$this->useRootResourceMock();
$object = $this->retriever->getObjectNode($coid);
$this->assertCount(0, $this->reader->getAllValuesNode($object, 'http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertCount(0, $this->reader->getAllValuesNode($object, 'rdfs:label'));
$this->assertCount(1, $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo'));
$this->assertCount(1, $this->reader->getAllValuesNode($object, 'co:isVisibleTo'));
$this->assertCount(2, $this->reader->getAllValuesNode($object, '@type'));
$this->assertInstanceOf(Node::class, $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo')[0]);
$this->assertInstanceOf(Node::class, $this->reader->getAllValuesNode($object, 'co:isVisibleTo')[0]);
$this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getAllValuesNode($object, 'coid://cloudobjects.io/isVisibleTo')[0]->getId());
$this->assertEquals('coid://cloudobjects.io/Public', $this->reader->getAllValuesNode($object, 'co:isVisibleTo')[0]->getId());
}
} }