Added permanent store feature

This commit is contained in:
lukas committed 2026-10-18 17:10:08 +00:00
1 parent 29e59c2b3c
commit 1f938cf19d
3 files changed
+200 -17

No files matched your search

@@ -266,4 +266,104 @@ class ObjectRetrieverMockTest extends \PHPUnit\Framework\TestCase {
}
}
private function createRetrieverWithPermanentObject(string $directory, array $options = []) {
$this->retriever = new ObjectRetriever(array_merge([
'permanent_store_directory' => $directory,
'permanent_store_scope' => '*',
], $options));
mkdir($directory.'/cloudobjects.io/Test', 0777, true);
file_put_contents($directory.'/cloudobjects.io/Test/object.jsonld',
'{"@id":"coid://cloudobjects.io/Test","http://www.w3.org/2000/01/rdf-schema#label":"Stored"}');
}
public function testPermanentStoreSavesObjectInScope() {
$directory = $this->createFallbackStoreDirectory();
$this->retriever = new ObjectRetriever([
'permanent_store_directory' => $directory,
'permanent_store_scope' => 'example.com, cloudobjects.io',
]);
$body = '{"@id":"coid://cloudobjects.io/Test","http://www.w3.org/2000/01/rdf-schema#label":"Test"}';
$this->setMockResponse(new Response(200, ['Content-Type' => 'application/ld+json'], $body));
try {
$this->retriever->getObjectNode(new IRI('coid://cloudobjects.io/Test'));
$location = $directory.'/cloudobjects.io/Test/object.jsonld';
$this->assertFileExists($location);
$this->assertEquals($body, file_get_contents($location));
} finally {
$this->removeDirectory($directory);
}
}
public function testPermanentStoreSkipsObjectOutOfScope() {
$directory = $this->createFallbackStoreDirectory();
$this->retriever = new ObjectRetriever([
'permanent_store_directory' => $directory,
'permanent_store_scope' => [ 'example.com' ],
]);
$this->setMockResponse(new Response(200, ['Content-Type' => 'application/ld+json'],
'{"@id":"coid://cloudobjects.io/Test"}'));
try {
$this->retriever->getObjectNode(new IRI('coid://cloudobjects.io/Test'));
$this->assertFileDoesNotExist($directory.'/cloudobjects.io/Test/object.jsonld');
} finally {
$this->removeDirectory($directory);
}
}
public function testPermanentStoreUsedWithoutContactingAPI() {
$directory = $this->createFallbackStoreDirectory();
$this->createRetrieverWithPermanentObject($directory);
$mock = new MockHandler([ new Response(500) ]);
$this->retriever->setClient(new Client(['handler' => HandlerStack::create($mock)]));
try {
$object = $this->retriever->getObjectNode(new IRI('coid://cloudobjects.io/Test'));
$this->assertNotNull($object);
$this->assertEquals('Stored', $object->getProperty('http://www.w3.org/2000/01/rdf-schema#label')->getValue());
// The mocked response has not been consumed
$this->assertCount(1, $mock);
} finally {
$this->removeDirectory($directory);
}
}
public function testPermanentStoreNotOverwritten() {
$directory = $this->createFallbackStoreDirectory();
$this->createRetrieverWithPermanentObject($directory);
$this->setMockResponse(new Response(200, ['Content-Type' => 'application/ld+json'],
'[{"@id":"coid://cloudobjects.io/Test","http://www.w3.org/2000/01/rdf-schema#label":"Updated"}]'));
try {
$this->retriever->fetchAllObjectsInNamespace(new IRI('coid://cloudobjects.io'));
$this->assertStringContainsString('Stored',
file_get_contents($directory.'/cloudobjects.io/Test/object.jsonld'));
} finally {
$this->removeDirectory($directory);
}
}
public function testPermanentStoreTakesPrecedenceOverFallbackStore() {
$permanentDirectory = $this->createFallbackStoreDirectory();
$fallbackDirectory = $this->createFallbackStoreDirectory();
$this->createRetrieverWithPermanentObject($permanentDirectory, [
'fallback_store_directory' => $fallbackDirectory,
'fallback_store_scope' => '*',
]);
mkdir($fallbackDirectory.'/cloudobjects.io/Test', 0777, true);
file_put_contents($fallbackDirectory.'/cloudobjects.io/Test/object.jsonld',
'{"@id":"coid://cloudobjects.io/Test","http://www.w3.org/2000/01/rdf-schema#label":"Fallback"}');
$this->setMockResponse(new Response(500));
try {
$object = $this->retriever->getObjectNode(new IRI('coid://cloudobjects.io/Test'));
$this->assertEquals('Stored', $object->getProperty('http://www.w3.org/2000/01/rdf-schema#label')->getValue());
} finally {
$this->removeDirectory($permanentDirectory);
$this->removeDirectory($fallbackDirectory);
}
}
}
@@ -26,4 +26,30 @@ class ObjectRetrieverTest extends \PHPUnit\Framework\TestCase {
]);
}
public function testPermanentStoreWithExistingDirectory() {
$retriever = new ObjectRetriever([
'permanent_store_directory' => sys_get_temp_dir(),
'permanent_store_scope' => '*',
]);
$this->assertInstanceOf(ObjectRetriever::class, $retriever);
}
public function testPermanentStoreWithMissingDirectory() {
$this->expectException(InvalidSDKConfigurationException::class);
new ObjectRetriever([
'permanent_store_directory' => sys_get_temp_dir().'/does-not-exist-'.uniqid(),
'permanent_store_scope' => '*',
]);
}
public function testPermanentStoreWithSameDirectoryAsFallbackStore() {
$this->expectException(InvalidSDKConfigurationException::class);
new ObjectRetriever([
'fallback_store_directory' => sys_get_temp_dir(),
'fallback_store_scope' => '*',
'permanent_store_directory' => sys_get_temp_dir(),
'permanent_store_scope' => '*',
]);
}
}