Compare commits

..
5 Commits
Author SHA1 Message Date
lukas 153a33d5bd Test with multiple PHP versions 2026-10-26 11:59:07 +00:00
lukas 7887b0983e Updated dependencies 2026-10-26 11:58:52 +00:00
lukas fde9661c2c Improved attachment retrieval and added test cases 2026-10-22 09:33:19 +00:00
lukas 6f2fd40464 Mock test for attachment retrieval 2026-10-22 09:07:27 +00:00
lukas bcfeedc703 Minor refactoring to avoid deprecated function 2026-10-22 09:07:16 +00:00
6 changed files with 203 additions and 83 deletions

No files matched your search

+29 -8
View File
@@ -635,10 +635,10 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
*/ */
public function get($coid) { public function get($coid) {
if (is_string($coid)) if (is_string($coid))
return $this->getObject(new IRI($coid)); return $this->getObjectNode(new IRI($coid));
if (is_object($coid) && get_class($coid)=='ML\IRI\IRI') if (is_object($coid) && get_class($coid)=='ML\IRI\IRI')
return $this->getObject($coid); return $this->getObjectNode($coid);
throw new SDKException('COID must be passed as a string or an IRI object.'); throw new SDKException('COID must be passed as a string or an IRI object.');
} }
@@ -650,7 +650,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
* @param string $filename * @param string $filename
*/ */
public function getAttachment(IRI $coid, $filename) { public function getAttachment(IRI $coid, $filename) {
$object = $this->getObject($coid); $object = $this->getObjectNode($coid);
if (!$object) if (!$object)
// Cannot get attachment for non-existing object // Cannot get attachment for non-existing object
@@ -680,14 +680,35 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
$this->putIntoCache($cacheId, $fileData, 0); $this->putIntoCache($cacheId, $fileData, 0);
$this->logInfoWithTime('Fetched attachment <'.$filename.'> for <'.$object->getId().'> from Core API ['.$response->getStatusCode().'].', $ts); $this->logInfoWithTime('Fetched attachment <'.$filename.'> for <'.$object->getId().'> from Core API ['.$response->getStatusCode().'].', $ts);
} catch (ConnectException $e) {
// connection failed or timed out
$this->logInfoWithTime('Attachment <'.$filename.'> for <'.$object->getId().'> could not be retrieved from Core API (connection failed).', $ts);
throw new CoreAPIException($e->getMessage(), $e);
} catch (RequestException $e) {
if ($e->hasResponse() && $e->getResponse()->getStatusCode() == 404) {
// not found (404)
$this->logInfoWithTime('Attachment <'.$filename.'> for <'.$object->getId().'> not found in Core API.', $ts);
return null;
} else
if ($e->hasResponse()) {
// other API error (4xx, 5xx)
$statusCode = $e->getResponse()->getStatusCode();
$this->logInfoWithTime('Attachment <'.$filename.'> for <'.$object->getId().'> could not be retrieved from Core API ['.$statusCode.'].', $ts);
throw new CoreAPIException($e->getMessage(), $e);
} else {
// API error without response
$this->logInfoWithTime('Attachment <'.$filename.'> for <'.$object->getId().'> could not be retrieved from Core API (no status code).', $ts);
throw new CoreAPIException($e->getMessage(), $e);
}
} catch (Exception $e) { } catch (Exception $e) {
$this->logInfoWithTime('Attachment <'.$filename.'> for <'.$object->getId().'> not found in Core API ['.$e->getResponse()->getStatusCode().'].', $ts); // other exceptions
// ignore exception - treat as non-existing file $this->logInfoWithTime('Attachment <'.$filename.'> for <'.$object->getId().'> could not be retrieved from Core API (unexpected exception).', $ts);
throw new CoreAPIException($e->getMessage(), $e);
} }
} }
return $fileContent; return $fileContent ?? null;
} }
private function assertAuthenticatingNamespaceAndGetId() : IRI { private function assertAuthenticatingNamespaceAndGetId() : IRI {
@@ -709,7 +730,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
* @return Node * @return Node
*/ */
public function getAuthenticatingNamespaceObject() : ?Node { public function getAuthenticatingNamespaceObject() : ?Node {
return $this->getObject($this->assertAuthenticatingNamespaceAndGetId()); return $this->getObjectNode($this->assertAuthenticatingNamespaceAndGetId());
} }
/** /**
@@ -719,7 +740,7 @@ class ObjectRetriever implements CustomCacheAndLogInterface {
* @return Node * @return Node
*/ */
public function getAuthenticatingNamespaceObjectNode() : ?Node { public function getAuthenticatingNamespaceObjectNode() : ?Node {
return $this->getObject($this->assertAuthenticatingNamespaceAndGetId()); return $this->getObjectNode($this->assertAuthenticatingNamespaceAndGetId());
} }
/** /**
Generated
+73 -71
View File
@@ -8,22 +8,22 @@
"packages": [ "packages": [
{ {
"name": "guzzlehttp/guzzle", "name": "guzzlehttp/guzzle",
"version": "7.15.5", "version": "7.16.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/guzzle/guzzle.git", "url": "https://github.com/guzzle/guzzle.git",
"reference": "ee80339fd9177ba44c49cdb653ff02a4d1106b9a" "reference": "81755d989b3007ca7f88a069857553cd08e7dc2e"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/ee80339fd9177ba44c49cdb653ff02a4d1106b9a", "url": "https://api.github.com/repos/guzzle/guzzle/zipball/81755d989b3007ca7f88a069857553cd08e7dc2e",
"reference": "ee80339fd9177ba44c49cdb653ff02a4d1106b9a", "reference": "81755d989b3007ca7f88a069857553cd08e7dc2e",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-json": "*", "ext-json": "*",
"guzzlehttp/promises": "^2.5.3", "guzzlehttp/promises": "^2.6",
"guzzlehttp/psr7": "^2.13.1", "guzzlehttp/psr7": "^2.13.2",
"php": "^7.2.5 || ^8.0", "php": "^7.2.5 || ^8.0",
"psr/http-client": "^1.0", "psr/http-client": "^1.0",
"symfony/deprecation-contracts": "^2.5 || ^3.0", "symfony/deprecation-contracts": "^2.5 || ^3.0",
@@ -116,7 +116,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/guzzle/guzzle/issues", "issues": "https://github.com/guzzle/guzzle/issues",
"source": "https://github.com/guzzle/guzzle/tree/7.15.5" "source": "https://github.com/guzzle/guzzle/tree/7.16.1"
}, },
"funding": [ "funding": [
{ {
@@ -132,20 +132,20 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2026-08-24T09:21:06+00:00" "time": "2026-10-25T21:35:21+00:00"
}, },
{ {
"name": "guzzlehttp/promises", "name": "guzzlehttp/promises",
"version": "2.5.3", "version": "2.6.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/guzzle/promises.git", "url": "https://github.com/guzzle/promises.git",
"reference": "cde49999552d185d64715fe9c1f77a2aadd2f9f1" "reference": "3d88c829bd5f13d13847b39067dbf09a60af2154"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/cde49999552d185d64715fe9c1f77a2aadd2f9f1", "url": "https://api.github.com/repos/guzzle/promises/zipball/3d88c829bd5f13d13847b39067dbf09a60af2154",
"reference": "cde49999552d185d64715fe9c1f77a2aadd2f9f1", "reference": "3d88c829bd5f13d13847b39067dbf09a60af2154",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -200,7 +200,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/guzzle/promises/issues", "issues": "https://github.com/guzzle/promises/issues",
"source": "https://github.com/guzzle/promises/tree/2.5.3" "source": "https://github.com/guzzle/promises/tree/2.6.0"
}, },
"funding": [ "funding": [
{ {
@@ -216,20 +216,20 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2026-08-24T09:11:28+00:00" "time": "2026-10-18T12:46:59+00:00"
}, },
{ {
"name": "guzzlehttp/psr7", "name": "guzzlehttp/psr7",
"version": "2.13.1", "version": "2.13.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/guzzle/psr7.git", "url": "https://github.com/guzzle/psr7.git",
"reference": "95e7828100de18b4e269fb1703be530082d5166d" "reference": "adee536f95101929f262b8c59e8a61472f347d58"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/95e7828100de18b4e269fb1703be530082d5166d", "url": "https://api.github.com/repos/guzzle/psr7/zipball/adee536f95101929f262b8c59e8a61472f347d58",
"reference": "95e7828100de18b4e269fb1703be530082d5166d", "reference": "adee536f95101929f262b8c59e8a61472f347d58",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -319,7 +319,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/guzzle/psr7/issues", "issues": "https://github.com/guzzle/psr7/issues",
"source": "https://github.com/guzzle/psr7/tree/2.13.1" "source": "https://github.com/guzzle/psr7/tree/2.13.2"
}, },
"funding": [ "funding": [
{ {
@@ -335,7 +335,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2026-08-24T09:13:11+00:00" "time": "2026-10-17T23:49:46+00:00"
}, },
{ {
"name": "kevinrob/guzzle-cache-middleware", "name": "kevinrob/guzzle-cache-middleware",
@@ -970,16 +970,16 @@
}, },
{ {
"name": "symfony/polyfill-php80", "name": "symfony/polyfill-php80",
"version": "v1.37.0", "version": "v1.43.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/polyfill-php80.git", "url": "https://github.com/symfony/polyfill-php80.git",
"reference": "dfb55726c3a76ea3b6459fcfda1ec2d80a682411" "reference": "9c6a5d6b01ca51d486c813e9a9c0ed55f208bb74"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/dfb55726c3a76ea3b6459fcfda1ec2d80a682411", "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/9c6a5d6b01ca51d486c813e9a9c0ed55f208bb74",
"reference": "dfb55726c3a76ea3b6459fcfda1ec2d80a682411", "reference": "9c6a5d6b01ca51d486c813e9a9c0ed55f208bb74",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -1030,7 +1030,7 @@
"shim" "shim"
], ],
"support": { "support": {
"source": "https://github.com/symfony/polyfill-php80/tree/v1.37.0" "source": "https://github.com/symfony/polyfill-php80/tree/v1.43.0"
}, },
"funding": [ "funding": [
{ {
@@ -1050,7 +1050,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2026-04-10T16:19:22+00:00" "time": "2026-09-25T15:50:05+00:00"
}, },
{ {
"name": "webmozart/assert", "name": "webmozart/assert",
@@ -1865,16 +1865,16 @@
}, },
{ {
"name": "phpunit/phpunit", "name": "phpunit/phpunit",
"version": "10.5.64", "version": "10.5.66",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git", "url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "0e8c1d19cea35ad97d4887f363d07c78e30fbf06" "reference": "747c9889613d0b3ffa08b74272e50ed3b8fa6d72"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/0e8c1d19cea35ad97d4887f363d07c78e30fbf06", "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/747c9889613d0b3ffa08b74272e50ed3b8fa6d72",
"reference": "0e8c1d19cea35ad97d4887f363d07c78e30fbf06", "reference": "747c9889613d0b3ffa08b74272e50ed3b8fa6d72",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -1884,7 +1884,7 @@
"ext-libxml": "*", "ext-libxml": "*",
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-xmlwriter": "*", "ext-xmlwriter": "*",
"myclabs/deep-copy": "^1.13.4", "myclabs/deep-copy": "^1.14.0",
"phar-io/manifest": "^2.0.4", "phar-io/manifest": "^2.0.4",
"phar-io/version": "^3.2.1", "phar-io/version": "^3.2.1",
"php": ">=8.1", "php": ">=8.1",
@@ -1901,7 +1901,7 @@
"sebastian/exporter": "^5.1.4", "sebastian/exporter": "^5.1.4",
"sebastian/global-state": "^6.0.2", "sebastian/global-state": "^6.0.2",
"sebastian/object-enumerator": "^5.0.0", "sebastian/object-enumerator": "^5.0.0",
"sebastian/recursion-context": "^5.0.1", "sebastian/recursion-context": "^5.0.2",
"sebastian/type": "^4.0.0", "sebastian/type": "^4.0.0",
"sebastian/version": "^4.0.1" "sebastian/version": "^4.0.1"
}, },
@@ -1946,7 +1946,7 @@
"support": { "support": {
"issues": "https://github.com/sebastianbergmann/phpunit/issues", "issues": "https://github.com/sebastianbergmann/phpunit/issues",
"security": "https://github.com/sebastianbergmann/phpunit/security/policy", "security": "https://github.com/sebastianbergmann/phpunit/security/policy",
"source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.64" "source": "https://github.com/sebastianbergmann/phpunit/tree/10.5.66"
}, },
"funding": [ "funding": [
{ {
@@ -1954,7 +1954,7 @@
"type": "other" "type": "other"
} }
], ],
"time": "2026-07-06T14:50:35+00:00" "time": "2026-10-05T05:46:26+00:00"
}, },
{ {
"name": "sebastian/cli-parser", "name": "sebastian/cli-parser",
@@ -2911,36 +2911,37 @@
}, },
{ {
"name": "symfony/http-foundation", "name": "symfony/http-foundation",
"version": "v8.1.7", "version": "v7.4.21",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/http-foundation.git", "url": "https://github.com/symfony/http-foundation.git",
"reference": "d8fdc670ed510a69e3a9eb4f5eac89f5ed627e17" "reference": "58c89144c7a36dd89c3d01917e9fa3a72d8aaf31"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/http-foundation/zipball/d8fdc670ed510a69e3a9eb4f5eac89f5ed627e17", "url": "https://api.github.com/repos/symfony/http-foundation/zipball/58c89144c7a36dd89c3d01917e9fa3a72d8aaf31",
"reference": "d8fdc670ed510a69e3a9eb4f5eac89f5ed627e17", "reference": "58c89144c7a36dd89c3d01917e9fa3a72d8aaf31",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=8.4.1", "php": ">=8.2",
"symfony/deprecation-contracts": "^2.5|^3", "symfony/deprecation-contracts": "^2.5|^3",
"symfony/polyfill-mbstring": "^1.1" "symfony/polyfill-mbstring": "^1.1"
}, },
"conflict": { "conflict": {
"doctrine/dbal": "<4.3" "doctrine/dbal": "<3.6",
"symfony/cache": "<6.4.12|>=7.0,<7.1.5"
}, },
"require-dev": { "require-dev": {
"doctrine/dbal": "^4.3", "doctrine/dbal": "^3.6|^4",
"predis/predis": "^1.1|^2.0", "predis/predis": "^1.1|^2.0",
"symfony/cache": "^7.4|^8.0", "symfony/cache": "^6.4.12|^7.1.5|^8.0",
"symfony/clock": "^7.4|^8.0", "symfony/clock": "^6.4|^7.0|^8.0",
"symfony/dependency-injection": "^7.4|^8.0", "symfony/dependency-injection": "^6.4|^7.0|^8.0",
"symfony/expression-language": "^7.4|^8.0", "symfony/expression-language": "^6.4|^7.0|^8.0",
"symfony/http-kernel": "^7.4|^8.0", "symfony/http-kernel": "^6.4|^7.0|^8.0",
"symfony/mime": "^7.4|^8.0", "symfony/mime": "^6.4|^7.0|^8.0",
"symfony/rate-limiter": "^7.4|^8.0" "symfony/rate-limiter": "^6.4|^7.0|^8.0"
}, },
"type": "library", "type": "library",
"autoload": { "autoload": {
@@ -2968,7 +2969,7 @@
"description": "Defines an object-oriented layer for the HTTP specification", "description": "Defines an object-oriented layer for the HTTP specification",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"support": { "support": {
"source": "https://github.com/symfony/http-foundation/tree/v8.1.7" "source": "https://github.com/symfony/http-foundation/tree/v7.4.21"
}, },
"funding": [ "funding": [
{ {
@@ -2988,20 +2989,20 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2026-09-14T17:47:24+00:00" "time": "2026-10-08T16:56:12+00:00"
}, },
{ {
"name": "symfony/polyfill-mbstring", "name": "symfony/polyfill-mbstring",
"version": "v1.38.2", "version": "v1.43.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git", "url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6" "reference": "4d5d331bf731e0aedbc6b2f5e61135b7b7996a72"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6", "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/4d5d331bf731e0aedbc6b2f5e61135b7b7996a72",
"reference": "d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6", "reference": "4d5d331bf731e0aedbc6b2f5e61135b7b7996a72",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -3053,7 +3054,7 @@
"shim" "shim"
], ],
"support": { "support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.38.2" "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.43.0"
}, },
"funding": [ "funding": [
{ {
@@ -3073,40 +3074,41 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2026-05-27T06:59:30+00:00" "time": "2026-09-24T15:19:04+00:00"
}, },
{ {
"name": "symfony/psr-http-message-bridge", "name": "symfony/psr-http-message-bridge",
"version": "v8.1.0", "version": "v7.4.21",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/psr-http-message-bridge.git", "url": "https://github.com/symfony/psr-http-message-bridge.git",
"reference": "67fd34de15ded1763aa1e330fe345f080a94022c" "reference": "b05964aa9d788bc8ffcc31c59d5c4573e3d4d1fc"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/psr-http-message-bridge/zipball/67fd34de15ded1763aa1e330fe345f080a94022c", "url": "https://api.github.com/repos/symfony/psr-http-message-bridge/zipball/b05964aa9d788bc8ffcc31c59d5c4573e3d4d1fc",
"reference": "67fd34de15ded1763aa1e330fe345f080a94022c", "reference": "b05964aa9d788bc8ffcc31c59d5c4573e3d4d1fc",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=8.4.1", "php": ">=8.2",
"psr/http-message": "^1.0|^2.0", "psr/http-message": "^1.0|^2.0",
"symfony/http-foundation": "^7.4|^8.0" "symfony/http-foundation": "^6.4|^7.0|^8.0"
}, },
"conflict": { "conflict": {
"php-http/discovery": "<1.15" "php-http/discovery": "<1.15",
"symfony/http-kernel": "<6.4"
}, },
"require-dev": { "require-dev": {
"nyholm/psr7": "^1.1", "nyholm/psr7": "^1.1",
"php-http/discovery": "^1.15", "php-http/discovery": "^1.15",
"psr/log": "^1.1.4|^2|^3", "psr/log": "^1.1.4|^2|^3",
"symfony/browser-kit": "^7.4|^8.0", "symfony/browser-kit": "^6.4|^7.0|^8.0",
"symfony/config": "^7.4|^8.0", "symfony/config": "^6.4|^7.0|^8.0",
"symfony/event-dispatcher": "^7.4|^8.0", "symfony/event-dispatcher": "^6.4|^7.0|^8.0",
"symfony/framework-bundle": "^7.4|^8.0", "symfony/framework-bundle": "^6.4.13|^7.1.6|^8.0",
"symfony/http-kernel": "^7.4|^8.0", "symfony/http-kernel": "^6.4.13|^7.1.6|^8.0",
"symfony/runtime": "^7.4|^8.0" "symfony/runtime": "^6.4.13|^7.1.6|^8.0"
}, },
"type": "symfony-bridge", "type": "symfony-bridge",
"autoload": { "autoload": {
@@ -3140,7 +3142,7 @@
"psr-7" "psr-7"
], ],
"support": { "support": {
"source": "https://github.com/symfony/psr-http-message-bridge/tree/v8.1.0" "source": "https://github.com/symfony/psr-http-message-bridge/tree/v7.4.21"
}, },
"funding": [ "funding": [
{ {
@@ -3160,7 +3162,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2026-05-29T05:06:50+00:00" "time": "2026-10-07T08:20:48+00:00"
}, },
{ {
"name": "theseer/tokenizer", "name": "theseer/tokenizer",
+5 -1
View File
@@ -1,5 +1,9 @@
docker run -d -v .:/root --name cloudobjects-sdk-test cloudobjects/php-build-base:8.4 #!/bin/bash
for version in 8.3 8.4; do
docker run -d -v .:/root --name cloudobjects-sdk-test cloudobjects/php-build-base:$version
docker exec -t cloudobjects-sdk-test bash -c "cd /root && composer install" docker exec -t cloudobjects-sdk-test bash -c "cd /root && composer install"
docker exec -t cloudobjects-sdk-test bash -c "cd /root && vendor/bin/phpunit" docker exec -t cloudobjects-sdk-test bash -c "cd /root && vendor/bin/phpunit"
docker stop cloudobjects-sdk-test docker stop cloudobjects-sdk-test
docker rm cloudobjects-sdk-test docker rm cloudobjects-sdk-test
done
+57 -3
View File
@@ -17,12 +17,14 @@ class ObjectRetrieverMockTest extends \PHPUnit\Framework\TestCase {
private $retriever; 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) { private function setMockResponse(...$responses) : MockHandler {
$mock = new MockHandler([$response]); $mock = new MockHandler($responses);
$handler = HandlerStack::create($mock); $handler = HandlerStack::create($mock);
$this->retriever->setClient(new Client(['handler' => $handler])); $this->retriever->setClient(new Client(['handler' => $handler]));
return $mock;
} }
protected function setUp(): void { protected function setUp(): void {
@@ -90,6 +92,58 @@ 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);
}
public function testWhenAPIThrowsErrorForAttachment() {
$this->setMockResponse(
new Response(200, ['Content-Type' => 'application/ld+json'],
'{"@id":"coid://cloudobjects.io/Test","coid://cloudobjects.io/isAtRevision":"1-abc"}'),
new Response(500)
);
$this->expectException(CoreAPIException::class);
$this->retriever->getAttachment(new IRI('coid://cloudobjects.io/Test'), 'file.txt');
}
public function testWhenAPITimesOutForAttachment() {
$this->setMockResponse(
new Response(200, ['Content-Type' => 'application/ld+json'],
'{"@id":"coid://cloudobjects.io/Test","coid://cloudobjects.io/isAtRevision":"1-abc"}'),
new ConnectException(
'cURL error 28: Operation timed out',
new Request('GET', 'cloudobjects.io')
)
);
try {
$this->retriever->getAttachment(new IRI('coid://cloudobjects.io/Test'), 'file.txt');
$this->fail('Expected CoreAPIException was not thrown.');
} catch (CoreAPIException $e) {
$this->assertInstanceOf(ConnectException::class, $e->getPrevious());
}
}
private function createFallbackStoreDirectory() : string { private function createFallbackStoreDirectory() : string {
$directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'clobj-fallback-'.uniqid(); $directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'clobj-fallback-'.uniqid();
mkdir($directory); mkdir($directory);
@@ -51,4 +51,34 @@ class ObjectRetrieverCacheTest extends \PHPUnit\Framework\TestCase {
$this->assertEquals($object2->getRevision(), $object3->getRevision()); $this->assertEquals($object2->getRevision(), $object3->getRevision());
} }
public function testAttachmentCacheInRuntimeStorage() {
$cacheStorage = new VolatileRuntimeStorage;
$logger = new InMemoryLogger;
$coid = new IRI('coid://cloudobjects.io/Test');
$cacheId = (string)$coid.'#file.txt';
$retriever = new ObjectRetriever([
'cache_storage' => $cacheStorage,
'logger' => $logger
]);
$content1 = $retriever->getAttachment($coid, 'file.txt');
$this->assertEquals('Test file content', trim($content1));
$this->assertNotNull($cacheStorage->fetch($retriever->getCacheKey($cacheId)));
$this->assertStringContainsString('from Core API', $logger->getLastLogMessage());
// Reinitialize retriever with same cache storage to verify that cache is used
$retriever = new ObjectRetriever([
'cache_storage' => $cacheStorage,
'logger' => $logger
]);
$content2 = $retriever->getAttachment($coid, 'file.txt');
$this->assertEquals('Test file content', trim($content2));
$this->assertStringContainsString('from object cache', $logger->getLastLogMessage());
}
} }
@@ -33,6 +33,15 @@ class ObjectRetrieverPublicTest extends \PHPUnit\Framework\TestCase {
$this->assertEquals('CloudObjects', $object->getProperty('http://www.w3.org/2000/01/rdf-schema#label')->getValue()); $this->assertEquals('CloudObjects', $object->getProperty('http://www.w3.org/2000/01/rdf-schema#label')->getValue());
} }
public function testGetTestObject() {
$coid = new IRI('coid://cloudobjects.io/Test');
$object = $this->retriever->getObjectNode($coid);
$this->assertNotNull($object);
$this->assertEquals((string)$coid, $object->getId());
$this->assertNotNull($object->getProperty('http://www.w3.org/2000/01/rdf-schema#label'));
$this->assertEquals('Test Object', $object->getProperty('http://www.w3.org/2000/01/rdf-schema#label')->getValue());
}
public function testGetRelatedObject() { public function testGetRelatedObject() {
$coid = new IRI('coid://cloudobjects.io'); $coid = new IRI('coid://cloudobjects.io');
$object = $this->retriever->getCloudObject($coid); $object = $this->retriever->getCloudObject($coid);