Skip to content

Commit 703941c

Browse files
committed
Refactoring PATCH headers to Base.
1 parent a0671b8 commit 703941c

File tree

6 files changed

+44
-15
lines changed

6 files changed

+44
-15
lines changed

lib/OpenCloud/Common/Base.php

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*/
3333
abstract class Base
3434
{
35+
const PATCH_CONTENT_TYPE = MimeConst::JSON_PATCH;
36+
3537
/**
3638
* Holds all the properties added by overloading.
3739
*
@@ -419,4 +421,9 @@ protected static function getJsonHeader()
419421
{
420422
return array(HeaderConst::CONTENT_TYPE => MimeConst::JSON);
421423
}
424+
425+
protected static function getPatchHeaders()
426+
{
427+
return array(HeaderConst::CONTENT_TYPE => static::PATCH_CONTENT_TYPE);
428+
}
422429
}

lib/OpenCloud/Common/Constants/Mime.php

+1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ class Mime
2121
{
2222
const JSON = 'application/json';
2323
const TEXT = 'text/plain';
24+
const JSON_PATCH = 'application/json-patch+json';
2425
}

lib/OpenCloud/Image/Resource/Image.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Image extends AbstractSchemaResource implements ImageInterface
3535
protected static $json_name = '';
3636
protected static $json_collection_name = 'images';
3737

38+
const PATCH_CONTENT_TYPE = 'application/openstack-images-v2.1-json-patch';
39+
3840
/**
3941
* Update this resource
4042
*
@@ -85,7 +87,7 @@ public function update(array $params, Schema $schema = null)
8587
$body = $document->toString();
8688

8789
return $this->getClient()
88-
->patch($this->getUrl(), $this->getService()->getPatchHeaders(), $body)
90+
->patch($this->getUrl(), $this->getPatchHeaders(), $body)
8991
->send();
9092
}
9193

lib/OpenCloud/Image/Service.php

-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace OpenCloud\Image;
1919

20-
use OpenCloud\Common\Constants\Header;
2120
use OpenCloud\Common\Service\CatalogService;
2221
use OpenCloud\Image\Resource\Image;
2322
use OpenCloud\Image\Resource\Schema\Schema;
@@ -32,18 +31,6 @@ class Service extends CatalogService
3231
const DEFAULT_TYPE = 'image';
3332
const DEFAULT_NAME = 'cloudImages';
3433

35-
const PATCH_CONTENT_TYPE = 'application/openstack-images-v2.1-json-patch';
36-
37-
/**
38-
* Get the default headers to send for PATCH requests
39-
*
40-
* @return array
41-
*/
42-
public function getPatchHeaders()
43-
{
44-
return array(Header::CONTENT_TYPE => self::PATCH_CONTENT_TYPE);
45-
}
46-
4734
/**
4835
* This operation returns images you created, shared images that you accepted, and standard images.
4936
*

tests/OpenCloud/Tests/Common/BaseTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function getBar()
3636
{
3737
return $this->bar;
3838
}
39+
40+
public static function getPatchHeaders()
41+
{
42+
return parent::getPatchHeaders();
43+
}
3944
}
4045

4146
class BaseTest extends \OpenCloud\Tests\OpenCloudTestCase
@@ -100,4 +105,14 @@ public function testSetProperty()
100105
$this->my->setBaz('goodbye');
101106
$this->assertEquals('goodbye', $this->my->getBaz());
102107
}
108+
109+
public function testGetPatchHeaders()
110+
{
111+
$expectedHeaders = array(
112+
'Content-Type' => 'application/json-patch+json'
113+
);
114+
115+
$my = $this->my;
116+
$this->assertEquals($expectedHeaders, $my::getPatchHeaders());
117+
}
103118
}

tests/OpenCloud/Tests/Image/Resource/ImageTest.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,19 @@
2222
use OpenCloud\Image\Resource\Schema\Schema;
2323
use OpenCloud\Tests\OpenCloudTestCase;
2424

25+
class PublicImage extends Image
26+
{
27+
public static function getPatchHeaders()
28+
{
29+
return parent::getPatchHeaders();
30+
}
31+
}
32+
2533
class ImageTest extends OpenCloudTestCase
2634
{
2735
public function setupObjects()
2836
{
29-
$this->image = new Image($this->getClient()->imageService('cloudImages', 'IAD'));
37+
$this->image = new PublicImage($this->getClient()->imageService('cloudImages', 'IAD'));
3038
}
3139

3240
protected function getSchemaData()
@@ -139,4 +147,13 @@ public function test_Delete_Tag()
139147

140148
$this->assertInstanceOf('Guzzle\Http\Message\Response', $this->image->deleteTag(12345));
141149
}
150+
151+
public function testGetPatchHeaders()
152+
{
153+
$expectedHeaders = array(
154+
'Content-Type' => 'application/openstack-images-v2.1-json-patch'
155+
);
156+
157+
$this->assertEquals($expectedHeaders, $this->image->getPatchHeaders());
158+
}
142159
}

0 commit comments

Comments
 (0)