From ce77709d37488c092f3a9899d9998e6d27327780 Mon Sep 17 00:00:00 2001 From: Lukas Rosenstock Date: Mon, 15 Jun 2026 09:29:04 +0000 Subject: [PATCH] Added global function cloudobjects_get_object() --- CloudObjects/SDK/functions.php | 23 ++++++++++ composer.json | 5 ++- tests/OfflineTests/FunctionsTest.php | 63 ++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 CloudObjects/SDK/functions.php create mode 100644 tests/OfflineTests/FunctionsTest.php diff --git a/CloudObjects/SDK/functions.php b/CloudObjects/SDK/functions.php new file mode 100644 index 0000000..9c3e4cc --- /dev/null +++ b/CloudObjects/SDK/functions.php @@ -0,0 +1,23 @@ +setAccessible(true); + $prop->setValue(null, null); + } + + private function makeMockRetrieverReturning(?CloudObject $object) : ObjectRetriever { + $mock = $this->createMock(ObjectRetriever::class); + $mock->method('getCloudObject')->willReturn($object); + return $mock; + } + + public function testAcceptsIRI(): void { + $coid = new IRI('coid://cloudobjects.io'); + $mockObject = $this->createMock(CloudObject::class); + + $mockRetriever = $this->createMock(ObjectRetriever::class); + $mockRetriever->expects($this->once()) + ->method('getCloudObject') + ->with($coid) + ->willReturn($mockObject); + + ObjectRetrieverFacade::setObjectRetriever($mockRetriever); + + $this->assertSame($mockObject, cloudobjects_get_object($coid)); + } + + public function testConvertsStringToIRI(): void { + $mockObject = $this->createMock(CloudObject::class); + + $mockRetriever = $this->createMock(ObjectRetriever::class); + $mockRetriever->expects($this->once()) + ->method('getCloudObject') + ->with($this->callback(fn($arg) => $arg instanceof IRI && (string)$arg === 'coid://cloudobjects.io')) + ->willReturn($mockObject); + + ObjectRetrieverFacade::setObjectRetriever($mockRetriever); + + $this->assertSame($mockObject, cloudobjects_get_object('coid://cloudobjects.io')); + } + + public function testThrowsOnInvalidArgumentType(): void { + ObjectRetrieverFacade::setObjectRetriever($this->makeMockRetrieverReturning(null)); + + $this->expectException(InvalidArgumentException::class); + cloudobjects_get_object(42); + } + +}