Mock test for attachment retrieval

This commit is contained in:
lukas committed 2026-10-22 09:07:27 +00:00
1 parent bcfeedc703
commit 6f2fd40464
1 file changed
+28 -3
+28 -3
View File
@@ -17,12 +17,14 @@ class ObjectRetrieverMockTest extends \PHPUnit\Framework\TestCase {
private $retriever;
/**
* @param Response|\Throwable $response A response, or an exception to throw instead.
* @param Response|\Throwable ...$responses Responses in the order they are returned,
* or exceptions to throw instead.
*/
private function setMockResponse($response) {
$mock = new MockHandler([$response]);
private function setMockResponse(...$responses) : MockHandler {
$mock = new MockHandler($responses);
$handler = HandlerStack::create($mock);
$this->retriever->setClient(new Client(['handler' => $handler]));
return $mock;
}
protected function setUp(): void {
@@ -90,6 +92,29 @@ class ObjectRetrieverMockTest extends \PHPUnit\Framework\TestCase {
}
}
public function testGetAttachment() {
$mock = $this->setMockResponse(
new Response(200, ['Content-Type' => 'application/ld+json'],
'{"@id":"coid://cloudobjects.io/Test","coid://cloudobjects.io/isAtRevision":"1-abc"}'),
new Response(200, ['Content-Type' => 'text/plain'], 'Attachment content')
);
$content = $this->retriever->getAttachment(new IRI('coid://cloudobjects.io/Test'), 'file.txt');
$this->assertEquals('Attachment content', $content);
$this->assertStringEndsWith('cloudobjects.io/Test/file.txt', $mock->getLastRequest()->getUri()->getPath());
}
public function testGetMissingAttachment() {
$this->setMockResponse(
new Response(200, ['Content-Type' => 'application/ld+json'],
'{"@id":"coid://cloudobjects.io/Test","coid://cloudobjects.io/isAtRevision":"1-abc"}'),
new Response(404)
);
$content = $this->retriever->getAttachment(new IRI('coid://cloudobjects.io/Test'), 'missing.txt');
$this->assertNull($content);
}
private function createFallbackStoreDirectory() : string {
$directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'clobj-fallback-'.uniqid();
mkdir($directory);