Finished Object class.
This commit is contained in:
@@ -34,6 +34,8 @@ namespace HPCloud\Storage\ObjectStorage;
|
||||
*/
|
||||
class Object {
|
||||
|
||||
const DEFAULT_CONTENT_TYPE = 'application/octet-stream';
|
||||
|
||||
/**
|
||||
* The name of the object.
|
||||
*
|
||||
@@ -55,7 +57,7 @@ class Object {
|
||||
* The default type is 'application/octet-stream', which marks this as
|
||||
* a generic byte stream.
|
||||
*/
|
||||
protected $contentType = 'application/octet-stream';
|
||||
protected $contentType = self::DEFAULT_CONTENT_TYPE;
|
||||
/**
|
||||
* Associative array of stored metadata.
|
||||
*/
|
||||
@@ -251,7 +253,7 @@ class Object {
|
||||
* The length of the content, in bytes.
|
||||
*/
|
||||
public function contentLength() {
|
||||
// XXX: Need to verify that this ALWAYS returns byte count.
|
||||
// strlen() is binary safe (or at least it seems to be).
|
||||
return strlen($this->content);
|
||||
}
|
||||
|
||||
@@ -263,7 +265,11 @@ class Object {
|
||||
*
|
||||
* When extending this class, generate an ETag by creating an MD5 of
|
||||
* the entire object's content (but not the metadata or name).
|
||||
*
|
||||
* @return string
|
||||
* An MD5 value as a string of 32 hex digits (0-9a-f).
|
||||
*/
|
||||
public function etag() {
|
||||
|
||||
return md5($this->content);
|
||||
}
|
||||
}
|
||||
|
||||
103
test/Tests/ObjectTest.php
Normal file
103
test/Tests/ObjectTest.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Unit tests for ObjectStorage Object.
|
||||
*/
|
||||
namespace HPCloud\Tests\Storage;
|
||||
|
||||
require_once 'src/HPCloud/Bootstrap.php';
|
||||
require_once 'test/TestCase.php';
|
||||
|
||||
use \HPCloud\Storage\ObjectStorage\Object;
|
||||
|
||||
class ObjectTest extends \HPCloud\Tests\TestCase {
|
||||
|
||||
const FNAME = 'descartes.txt';
|
||||
const FCONTENT = 'Cogito ergo sum.';
|
||||
const FTYPE = 'text/plain; charset=ISO-8859-1';
|
||||
|
||||
/**
|
||||
* Set up a basic object fixture.
|
||||
*
|
||||
* This provides an Object initialized with the main constants defined
|
||||
* for this class. Use this as a fixture to avoid repetition.
|
||||
*
|
||||
* @return Object
|
||||
* An initialized object.
|
||||
*/
|
||||
public function basicObjectFixture() {
|
||||
|
||||
$o = new Object(self::FNAME);
|
||||
$o->setContent(self::FCONTENT, self::FTYPE);
|
||||
|
||||
return $o;
|
||||
}
|
||||
|
||||
public function testConstructor() {
|
||||
$o = $this->basicObjectFixture();
|
||||
|
||||
$this->assertEquals(self::FNAME, $o->name());
|
||||
}
|
||||
|
||||
public function testContentType() {
|
||||
// Don't use the fixture, we want to test content
|
||||
// type in its raw state.
|
||||
$o = new Object('foo.txt');
|
||||
|
||||
$this->assertEquals('application/octet-stream', $o->contentType());
|
||||
|
||||
$o->setContentType('text/plain; charset=UTF-8');
|
||||
$this->assertEquals('text/plain; charset=UTF-8', $o->contentType());
|
||||
}
|
||||
|
||||
public function testContent() {
|
||||
$o = $this->basicObjectFixture();
|
||||
|
||||
$this->assertEquals(self::FCONTENT, $o->content());
|
||||
|
||||
// Test binary data.
|
||||
$bin = sha1(self::FCONTENT, TRUE);
|
||||
$o->setContent($bin, 'application/octet-stream');
|
||||
|
||||
$this->assertEquals($bin, $o->content());
|
||||
}
|
||||
|
||||
public function testEtag() {
|
||||
$o = $this->basicObjectFixture();
|
||||
$md5 = md5(self::FCONTENT);
|
||||
|
||||
$this->assertEquals($md5, $o->etag());
|
||||
}
|
||||
|
||||
public function testContentLength() {
|
||||
$o = $this->basicObjectFixture();
|
||||
$this->assertEquals(strlen(self::FCONTENT), $o->contentLength());
|
||||
|
||||
// Test on binary data.
|
||||
$bin = sha1(self::FCONTENT, TRUE);
|
||||
|
||||
$o->setContent($bin);
|
||||
$this->assertFalse($o->contentLength() == 0);
|
||||
$this->assertEquals(strlen($bin), $o->contentLength());
|
||||
}
|
||||
|
||||
public function testMetadata() {
|
||||
$md = array(
|
||||
'Immanuel' => 'Kant',
|
||||
'David' => 'Hume',
|
||||
'Gottfried' => 'Leibniz',
|
||||
'Jean-Jaques' => 'Rousseau',
|
||||
);
|
||||
|
||||
$o = $this->basicObjectFixture();
|
||||
$o->setMetadata($md);
|
||||
|
||||
$got = $o->metadata();
|
||||
|
||||
$this->assertEquals(4, count($got));
|
||||
$this->assertArrayHasKey('Immanuel', $got);
|
||||
$this->assertEquals('Leibniz', $got['Gottfried']);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* Unit tests for ObjectStorage Object.
|
||||
*/
|
||||
namespace HPCloud\Tests\Storage;
|
||||
|
||||
require_once 'src/HPCloud/Bootstrap.php';
|
||||
require_once 'test/TestCase.php';
|
||||
|
||||
use \HPCloud\Storage\ObjectStorage\Object;
|
||||
|
||||
class ObjectTest extends \HPCloud\Tests\TestCase {
|
||||
|
||||
public function testConstructor() {
|
||||
$o = new Object('foo.txt');
|
||||
|
||||
$this->assertEquals('foo.txt', $o->name());
|
||||
}
|
||||
|
||||
public function testContentType() {
|
||||
$o = new Object('foo.txt');
|
||||
|
||||
$this->assertEquals('application/octet-stream', $o->contentType());
|
||||
|
||||
$o->setContentType('text/plain; charset=UTF-8');
|
||||
$this->assertEquals('text/plain; charset=UTF-8', $o->contentType());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user