Added global function cloudobjects_get_object()
This commit is contained in:
63
tests/OfflineTests/FunctionsTest.php
Normal file
63
tests/OfflineTests/FunctionsTest.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
namespace CloudObjects\SDK;
|
||||
|
||||
use ML\IRI\IRI;
|
||||
use ReflectionProperty,
|
||||
InvalidArgumentException;
|
||||
|
||||
class FunctionsTest extends \PHPUnit\Framework\TestCase {
|
||||
|
||||
protected function setUp(): void {
|
||||
$prop = new ReflectionProperty(ObjectRetrieverFacade::class, 'instance');
|
||||
$prop->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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user