Skip to content

Commit

Permalink
Object Storage retrieval: containerName and name missing (#210)
Browse files Browse the repository at this point in the history
* Repopulate instance from input array

* Added unit tests

* Fixed authentication v2 bug

* Updated unit tests
  • Loading branch information
haphan committed Feb 9, 2018
1 parent 1033431 commit 4adc505
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
13 changes: 7 additions & 6 deletions samples/identity/v2/authentication.php
Expand Up @@ -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 */
Expand Down
18 changes: 16 additions & 2 deletions src/ObjectStore/v1/Models/StorageObject.php
Expand Up @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/ObjectStore/v1/Fixtures/GET_Container.resp
Expand Up @@ -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"
}
]
39 changes: 36 additions & 3 deletions tests/unit/ObjectStore/v1/Models/ContainerTest.php
Expand Up @@ -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'],
Expand All @@ -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()
Expand All @@ -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});
}
}
}

Expand Down

0 comments on commit 4adc505

Please sign in to comment.