Skip to content

Commit 33130c8

Browse files
author
Jamie Hannaford
committed
add more tests
1 parent 435a2d9 commit 33130c8

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

src/Common/Error/Builder.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ private function str(MessageInterface $message)
8686
$msg = 'HTTP/' . $message->getProtocolVersion() . ' '
8787
. $message->getStatusCode() . ' '
8888
. $message->getReasonPhrase();
89-
} else {
90-
throw new \InvalidArgumentException('Unknown message type');
9189
}
9290

9391
foreach ($message->getHeaders() as $name => $values) {

src/ObjectStore/v1/Service.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,19 @@ public function createContainer(array $data)
6161
return $this->getContainer()->create($data);
6262
}
6363

64+
/**
65+
* Checks the existence of a container.
66+
*
67+
* @param string $name The name of the container
68+
*
69+
* @return bool TRUE if exists, FALSE if it doesn't
70+
* @throws BadResponseError Thrown for any non 404 status error
71+
*/
6472
public function containerExists($name)
6573
{
6674
try {
67-
$this->getContainer($name);
75+
$this->execute($this->api->headContainer(), ['name' => $name]);
76+
return true;
6877
} catch (BadResponseError $e) {
6978
if ($e->getResponse()->getStatusCode() === 404) {
7079
return false;

tests/unit/Common/Error/BuilderTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use OpenStack\Common\Error\BadResponseError;
1313
use OpenStack\Common\Error\Builder;
1414
use OpenStack\Common\Error\UserInputError;
15+
use Psr\Http\Message\MessageInterface;
16+
use Psr\Http\Message\StreamInterface;
1517

1618
class BuilderTest extends \PHPUnit_Framework_TestCase
1719
{
@@ -118,4 +120,4 @@ public function test_dead_links_are_ignored()
118120

119121
$this->assertEquals($e, $this->builder->userInputError($expected, $value, 'sdffsda'));
120122
}
121-
}
123+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ public function test_it_chunks_according_to_provided_segment_size()
190190
'segmentContainer' => 'segments',
191191
];
192192

193+
// check container creation
194+
$e = new BadResponseError();
195+
$e->setRequest(new Request('HEAD', 'segments'));
196+
$e->setResponse(new Response(404));
197+
198+
$this->client
199+
->request('HEAD', 'segments', ['headers' => []])
200+
->shouldBeCalled()
201+
->willThrow($e);
202+
203+
$this->setupMock('PUT', 'segments', null, [], new Response(201));
204+
193205
$this->setupMock('PUT', 'segments/objectPrefix/1', $stream->read(10), [], new Response(201));
194206
$this->setupMock('PUT', 'segments/objectPrefix/2', $stream->read(10), [], new Response(201));
195207
$this->setupMock('PUT', 'segments/objectPrefix/3', $stream->read(10), [], new Response(201));

tests/unit/ObjectStore/v1/ServiceTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace OpenStack\Test\ObjectStore\v1;
44

5+
use GuzzleHttp\Psr7\Request;
56
use GuzzleHttp\Psr7\Response;
7+
use OpenStack\Common\Error\BadResponseError;
68
use OpenStack\ObjectStore\v1\Api;
79
use OpenStack\ObjectStore\v1\Models\Account;
810
use OpenStack\ObjectStore\v1\Models\Container;
@@ -44,4 +46,42 @@ public function test_It_Create_Containers()
4446
$this->setupMock('PUT', 'foo', null, [], 'Created');
4547
$this->service->createContainer(['name' => 'foo']);
4648
}
49+
50+
public function test_it_returns_true_for_existing_containers()
51+
{
52+
$this->setupMock('HEAD', 'foo', null, [], new Response(200));
53+
54+
$this->assertTrue($this->service->containerExists('foo'));
55+
}
56+
57+
public function test_it_returns_false_if_container_does_not_exist()
58+
{
59+
$e = new BadResponseError();
60+
$e->setRequest(new Request('HEAD', 'foo'));
61+
$e->setResponse(new Response(404));
62+
63+
$this->client
64+
->request('HEAD', 'foo', ['headers' => []])
65+
->shouldBeCalled()
66+
->willThrow($e);
67+
68+
$this->assertFalse($this->service->containerExists('foo'));
69+
}
70+
71+
/**
72+
* @expectedException \OpenStack\Common\Error\BadResponseError
73+
*/
74+
public function test_it_throws_exception_when_error()
75+
{
76+
$e = new BadResponseError();
77+
$e->setRequest(new Request('HEAD', 'foo'));
78+
$e->setResponse(new Response(500));
79+
80+
$this->client
81+
->request('HEAD', 'foo', ['headers' => []])
82+
->shouldBeCalled()
83+
->willThrow($e);
84+
85+
$this->assertFalse($this->service->containerExists('foo'));
86+
}
4787
}

0 commit comments

Comments
 (0)