From 4adc505a898d7c24ac4b39002d737bd59414da48 Mon Sep 17 00:00:00 2001 From: Ha Phan Date: Fri, 9 Feb 2018 18:30:45 +0800 Subject: [PATCH] Object Storage retrieval: containerName and name missing (#210) * Repopulate instance from input array * Added unit tests * Fixed authentication v2 bug * Updated unit tests --- samples/identity/v2/authentication.php | 13 ++++--- src/ObjectStore/v1/Models/StorageObject.php | 18 ++++++++- .../v1/Fixtures/GET_Container.resp | 4 +- .../ObjectStore/v1/Models/ContainerTest.php | 39 +++++++++++++++++-- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/samples/identity/v2/authentication.php b/samples/identity/v2/authentication.php index 05220366..89cd14d1 100644 --- a/samples/identity/v2/authentication.php +++ b/samples/identity/v2/authentication.php @@ -5,22 +5,23 @@ use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use OpenStack\Common\Transport\Utils as TransportUtils; +use OpenStack\Identity\v2\Service; use OpenStack\OpenStack; $authUrl = 'https://keystone.example.com:5000/v2.0'; +$httpClient = new Client([ + 'base_uri' => TransportUtils::normalizeUrl($authUrl), + 'handler' => HandlerStack::create(), +]); + $options = [ 'authUrl' => $authUrl, 'region' => 'RegionOne', 'username' => 'foo', 'password' => 'bar', 'tenantName' => 'baz', - 'identityService' => new Client( - [ - 'base_uri' => TransportUtils::normalizeUrl($authUrl), - 'handler' => HandlerStack::create(), - ] - ), + 'identityService' => Service::factory($httpClient), ]; /** @var OpenStack $openstack */ diff --git a/src/ObjectStore/v1/Models/StorageObject.php b/src/ObjectStore/v1/Models/StorageObject.php index 09cbd104..6b8e60d8 100644 --- a/src/ObjectStore/v1/Models/StorageObject.php +++ b/src/ObjectStore/v1/Models/StorageObject.php @@ -104,8 +104,22 @@ public function getPublicUri(): Uri */ public function create(array $data): Creatable { - $response = $this->execute($this->api->putObject(), $data + ['containerName' => $this->containerName]); - return $this->populateFromResponse($response); + // Override containerName from input params only if local instance contains containerName attr + if ($this->containerName) { + $data['containerName'] = $this->containerName; + } + + $response = $this->execute($this->api->putObject(), $data); + $storageObject = $this->populateFromResponse($response); + + // Repopulate data for this newly created object instance + // due to the response from API does not contain object name and containerName + $storageObject = $storageObject->populateFromArray([ + 'name' => $data['name'], + 'containerName' => $data['containerName'] + ]); + + return $storageObject; } /** diff --git a/tests/unit/ObjectStore/v1/Fixtures/GET_Container.resp b/tests/unit/ObjectStore/v1/Fixtures/GET_Container.resp index ae021339..856af9c3 100644 --- a/tests/unit/ObjectStore/v1/Fixtures/GET_Container.resp +++ b/tests/unit/ObjectStore/v1/Fixtures/GET_Container.resp @@ -21,7 +21,7 @@ Date: Wed, 15 Jan 2014 16:57:35 GMT "hash": "ed076287532e86365e841e92bfc50d8c", "last_modified": "2014-01-15T16:37:43.427570", "bytes": 12, - "name": "helloworld", - "content_type": "application/octet-stream" + "name": "helloworld.json", + "content_type": "application/json" } ] \ No newline at end of file diff --git a/tests/unit/ObjectStore/v1/Models/ContainerTest.php b/tests/unit/ObjectStore/v1/Models/ContainerTest.php index 78fe4c65..4b76a28b 100644 --- a/tests/unit/ObjectStore/v1/Models/ContainerTest.php +++ b/tests/unit/ObjectStore/v1/Models/ContainerTest.php @@ -116,7 +116,8 @@ public function test_It_Create_Objects() $this->setupMock('PUT', self::NAME . '/' . $objectName, $content, $headers, 'Created'); - $this->container->createObject([ + /** @var StorageObject $storageObject */ + $storageObject = $this->container->createObject([ 'name' => $objectName, 'content' => $content, 'contentType' => $headers['Content-Type'], @@ -125,6 +126,9 @@ public function test_It_Create_Objects() 'deleteAfter' => $headers['X-Delete-After'], 'metadata' => ['Author' => 'foo', 'genre' => 'bar'], ]); + + $this->assertEquals('foo.txt', $storageObject->name); + $this->assertEquals(self::NAME, $storageObject->containerName); } public function test_it_lists_objects() @@ -134,8 +138,37 @@ public function test_it_lists_objects() ->shouldBeCalled() ->willReturn($this->getFixture('GET_Container')); - foreach ($this->container->listObjects(['limit' => 2]) as $object) { - $this->assertInstanceOf(StorageObject::class, $object); + $objects = iterator_to_array($this->container->listObjects(['limit' => 2])); + + $this->assertEquals(2, count($objects)); + + $expected = [ + [ + 'name' => 'goodbye', + 'contentLength' => '14', + 'lastModified' => new \DateTimeImmutable('2014-01-15T16:41:49.390270'), + 'contentType' => 'application/octet-stream', + 'hash' => '451e372e48e0f6b1114fa0724aa79fa1' + ], + [ + 'name' => 'helloworld.json', + 'contentLength' => '12', + 'lastModified' => new \DateTimeImmutable('2014-01-15T16:37:43.427570'), + 'contentType' => 'application/json', + 'hash' => 'ed076287532e86365e841e92bfc50d8c' + ], + ]; + + for ($i = 0; $i < count($objects); $i++) + { + $exp = $expected[$i]; + /** @var StorageObject $obj */ + $obj = $objects[$i]; + + foreach ($exp as $attr => $attrVal) + { + $this->assertEquals($attrVal, $obj->{$attr}); + } } }