Skip to content

Commit 4adc505

Browse files
authored
Object Storage retrieval: containerName and name missing (#210)
* Repopulate instance from input array * Added unit tests * Fixed authentication v2 bug * Updated unit tests
1 parent 1033431 commit 4adc505

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

samples/identity/v2/authentication.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
use GuzzleHttp\Client;
66
use GuzzleHttp\HandlerStack;
77
use OpenStack\Common\Transport\Utils as TransportUtils;
8+
use OpenStack\Identity\v2\Service;
89
use OpenStack\OpenStack;
910

1011
$authUrl = 'https://keystone.example.com:5000/v2.0';
1112

13+
$httpClient = new Client([
14+
'base_uri' => TransportUtils::normalizeUrl($authUrl),
15+
'handler' => HandlerStack::create(),
16+
]);
17+
1218
$options = [
1319
'authUrl' => $authUrl,
1420
'region' => 'RegionOne',
1521
'username' => 'foo',
1622
'password' => 'bar',
1723
'tenantName' => 'baz',
18-
'identityService' => new Client(
19-
[
20-
'base_uri' => TransportUtils::normalizeUrl($authUrl),
21-
'handler' => HandlerStack::create(),
22-
]
23-
),
24+
'identityService' => Service::factory($httpClient),
2425
];
2526

2627
/** @var OpenStack $openstack */

src/ObjectStore/v1/Models/StorageObject.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,22 @@ public function getPublicUri(): Uri
104104
*/
105105
public function create(array $data): Creatable
106106
{
107-
$response = $this->execute($this->api->putObject(), $data + ['containerName' => $this->containerName]);
108-
return $this->populateFromResponse($response);
107+
// Override containerName from input params only if local instance contains containerName attr
108+
if ($this->containerName) {
109+
$data['containerName'] = $this->containerName;
110+
}
111+
112+
$response = $this->execute($this->api->putObject(), $data);
113+
$storageObject = $this->populateFromResponse($response);
114+
115+
// Repopulate data for this newly created object instance
116+
// due to the response from API does not contain object name and containerName
117+
$storageObject = $storageObject->populateFromArray([
118+
'name' => $data['name'],
119+
'containerName' => $data['containerName']
120+
]);
121+
122+
return $storageObject;
109123
}
110124

111125
/**

tests/unit/ObjectStore/v1/Fixtures/GET_Container.resp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Date: Wed, 15 Jan 2014 16:57:35 GMT
2121
"hash": "ed076287532e86365e841e92bfc50d8c",
2222
"last_modified": "2014-01-15T16:37:43.427570",
2323
"bytes": 12,
24-
"name": "helloworld",
25-
"content_type": "application/octet-stream"
24+
"name": "helloworld.json",
25+
"content_type": "application/json"
2626
}
2727
]

tests/unit/ObjectStore/v1/Models/ContainerTest.php

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public function test_It_Create_Objects()
116116

117117
$this->setupMock('PUT', self::NAME . '/' . $objectName, $content, $headers, 'Created');
118118

119-
$this->container->createObject([
119+
/** @var StorageObject $storageObject */
120+
$storageObject = $this->container->createObject([
120121
'name' => $objectName,
121122
'content' => $content,
122123
'contentType' => $headers['Content-Type'],
@@ -125,6 +126,9 @@ public function test_It_Create_Objects()
125126
'deleteAfter' => $headers['X-Delete-After'],
126127
'metadata' => ['Author' => 'foo', 'genre' => 'bar'],
127128
]);
129+
130+
$this->assertEquals('foo.txt', $storageObject->name);
131+
$this->assertEquals(self::NAME, $storageObject->containerName);
128132
}
129133

130134
public function test_it_lists_objects()
@@ -134,8 +138,37 @@ public function test_it_lists_objects()
134138
->shouldBeCalled()
135139
->willReturn($this->getFixture('GET_Container'));
136140

137-
foreach ($this->container->listObjects(['limit' => 2]) as $object) {
138-
$this->assertInstanceOf(StorageObject::class, $object);
141+
$objects = iterator_to_array($this->container->listObjects(['limit' => 2]));
142+
143+
$this->assertEquals(2, count($objects));
144+
145+
$expected = [
146+
[
147+
'name' => 'goodbye',
148+
'contentLength' => '14',
149+
'lastModified' => new \DateTimeImmutable('2014-01-15T16:41:49.390270'),
150+
'contentType' => 'application/octet-stream',
151+
'hash' => '451e372e48e0f6b1114fa0724aa79fa1'
152+
],
153+
[
154+
'name' => 'helloworld.json',
155+
'contentLength' => '12',
156+
'lastModified' => new \DateTimeImmutable('2014-01-15T16:37:43.427570'),
157+
'contentType' => 'application/json',
158+
'hash' => 'ed076287532e86365e841e92bfc50d8c'
159+
],
160+
];
161+
162+
for ($i = 0; $i < count($objects); $i++)
163+
{
164+
$exp = $expected[$i];
165+
/** @var StorageObject $obj */
166+
$obj = $objects[$i];
167+
168+
foreach ($exp as $attr => $attrVal)
169+
{
170+
$this->assertEquals($attrVal, $obj->{$attr});
171+
}
139172
}
140173
}
141174

0 commit comments

Comments
 (0)