Files
openstack-sdk-php/test/Tests/RemoteObjectTest.php
Matt Farina e74180781b Fixing a bug where PHPUnit is not included in the require-dev to run
the test suite. PHPUnit 4 added and the test suite passes.

Change-Id: I57f9860127d0a0ac037e31776e5d8a6f233a350e
Closes-Bug: 1295358
2014-04-04 12:37:19 -04:00

347 lines
9.1 KiB
PHP

<?php
/* ============================================================================
(c) Copyright 2012-2014 Hewlett-Packard Development Company, L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============================================================================ */
/**
* @file
*
* Unit tests for ObjectStorage RemoteObject.
*/
namespace OpenStack\Tests\Storage\ObjectStorage;
require_once 'test/TestCase.php';
use \OpenStack\Storage\ObjectStorage\RemoteObject;
use \OpenStack\Storage\ObjectStorage\Object;
use \OpenStack\Storage\ObjectStorage\Container;
class RemoteObjectTest extends \OpenStack\Tests\TestCase {
const FNAME = 'RemoteObjectTest';
//const FTYPE = 'text/plain; charset=UTF-8';
const FTYPE = 'application/octet-stream; charset=UTF-8';
const FCONTENT = 'Rah rah ah ah ah. Roma roma ma. Gaga oh la la.';
const FMETA_NAME = 'Foo';
const FMETA_VALUE = 'Bar';
const FDISPOSITION = 'attachment; roma.gaga';
const FENCODING = 'gzip';
const FCORS_NAME = 'Access-Control-Max-Age';
const FCORS_VALUE = '2000';
protected function createAnObject() {
$container = $this->containerFixture();
$object = new Object(self::FNAME, self::FCONTENT, self::FTYPE);
$object->setMetadata(array(self::FMETA_NAME => self::FMETA_VALUE));
$object->setDisposition(self::FDISPOSITION);
$object->setEncoding(self::FENCODING);
$object->setAdditionalHeaders(array(
'Access-Control-Allow-Origin' => 'http://example.com',
'Access-control-allow-origin' => 'http://example.com',
));
// Need some headers that Swift actually stores and returns. This
// one does not seem to be returned ever.
//$object->setAdditionalHeaders(array(self::FCORS_NAME => self::FCORS_VALUE));
$container->save($object);
}
public function testNewFromHeaders() {
// This is tested via the container.
$this->destroyContainerFixture();
$container = $this->containerFixture();
$this->createAnObject();
$obj = $container->remoteObject(self::FNAME);
$this->assertInstanceOf('\OpenStack\Storage\ObjectStorage\RemoteObject', $obj);
return $obj;
}
/**
* @depends testNewFromHeaders
*/
public function testContentLength($obj) {
$len = strlen(self::FCONTENT);
$this->assertEquals($len, $obj->contentLength());
return $obj;
}
/**
* @depends testContentLength
*/
public function testContentType($obj) {
$this->assertEquals(self::FTYPE, $obj->contentType());
return $obj;
}
/**
* @depends testContentType
*/
public function testEtag($obj) {
$hash = md5(self::FCONTENT);
$this->assertEquals($hash, $obj->eTag());
return $obj;
}
/**
* @depends testContentType
*/
public function testLastModified($obj) {
$date = $obj->lastModified();
$this->assertTrue(is_int($date));
$this->assertTrue($date > 0);
}
/**
* @depends testNewFromHeaders
*/
public function testMetadata($obj) {
$md = $obj->metadata();
$this->assertArrayHasKey(self::FMETA_NAME, $md);
$this->assertEquals(self::FMETA_VALUE, $md[self::FMETA_NAME]);
}
/**
* @depends testNewFromHeaders
*/
public function testDisposition($obj) {
$this->assertEquals(self::FDISPOSITION, $obj->disposition());
}
/**
* @depends testNewFromHeaders
*/
public function testEncoding($obj) {
$this->assertEquals(self::FENCODING, $obj->encoding());
}
/**
* @depends testNewFromHeaders
*/
public function testHeaders($obj) {
$headers = $obj->headers();
$this->assertTrue(count($headers) > 1);
//fwrite(STDOUT, print_r($headers, TRUE));
$this->assertNotEmpty($headers['Date']);
$obj->removeHeaders(array('Date'));
$headers = $obj->headers();
$this->assertFalse(isset($headers['Date']));
// Swift doesn't return CORS headers even though it is supposed to.
//$this->assertEquals(self::FCORS_VALUE, $headers[self::FCORS_NAME]);
}
/**
* @depends testNewFromHeaders
*/
public function testUrl($obj) {
$url = $obj->url();
$this->assertTrue(strpos($obj->url(), $obj->name())> 0);
}
/**
* @depends testNewFromHeaders
*/
public function testStream($obj) {
$res = $obj->stream();
$this->assertTrue(is_resource($res));
$res_md = stream_get_meta_data($res);
// This will be HTTP if we are using the PHP stream
// wrapper, but for CURL this will be PHP.
$klass = \OpenStack\Bootstrap::config('transport', NULL);
if ($klass == '\OpenStack\Transport\PHPStreamTransport') {
$expect = 'http';
}
else {
$expect = 'PHP';
}
$this->assertEquals($expect, $res_md['wrapper_type']);
$content = fread($res, $obj->contentLength());
fclose($res);
$this->assertEquals(self::FCONTENT, $content);
// Now repeat the tests, only with a local copy of the data.
// This allows us to test the local tempfile buffering.
$obj->setContent($content);
$res2 = $obj->stream();
$res_md = stream_get_meta_data($res2);
$this->assertEquals('PHP', $res_md['wrapper_type']);
$content = fread($res2, $obj->contentLength());
fclose($res2);
$this->assertEquals(self::FCONTENT, $content);
// Finally, we redo the first part of the test to make sure that
// refreshing gets us a new copy:
$res3 = $obj->stream(TRUE);
$res_md = stream_get_meta_data($res3);
$this->assertEquals($expect, $res_md['wrapper_type']);
fclose($res3);
return $obj;
}
// To avoid test tainting from testStream(), we start over.
public function testContent() {
$container = $this->containerFixture();
$obj = $container->object(self::FNAME);
$content = $obj->content();
$this->assertEquals(self::FCONTENT, $content);
// Make sure remoteObject retrieves the same content.
$obj = $container->remoteObject(self::FNAME);
$content = $obj->content();
$this->assertEquals(self::FCONTENT, $content);
}
/**
* @depends testStream
*/
public function testCaching() {
$container = $this->containerFixture();
$obj = $container->remoteObject(self::FNAME);
$this->assertFalse($obj->isCaching());
// This is a roundabout way of testing. We know that if caching is
// turned on, then we can get a local copy of the file instead of a
// remote, and the best way to find this out is by grabbing a
// stream. The local copy will be in a php://temp stream.
//
// The CURL, though, backs its up with a temp file wrapped in a PHP
// stream. Other backends are likely to do the same. So this test
// is weakened for CURL backends.
$transport = \OpenStack\Bootstrap::config('transport');
if ($transport == '\OpenStack\Transport\PHPStreamTransport') {
$expect = 'http';
}
else {
$expect = 'PHP';
}
$content = $obj->content();
$res1 = $obj->stream();
$md = stream_get_meta_data($res1);
$this->assertEquals($expect, $md['wrapper_type']);
fclose($res1);
// Enable caching and retest.
$obj->setCaching(TRUE);
$this->assertTrue($obj->isCaching());
// This will cache the content.
$content = $obj->content();
$res2 = $obj->stream();
$md = stream_get_meta_data($res2);
// If this is using the PHP version, it built content from the
// cached version.
$this->assertEquals('PHP', $md['wrapper_type']);
fclose($res2);
}
/**
* @depends testNewFromHeaders
*/
public function testContentVerification($obj) {
$this->assertTrue($obj->isVerifyingContent());
$obj->setContentVerification(FALSE);
$this->assertFALSE($obj->isVerifyingContent());
$obj->setContentVerification(TRUE);
}
/**
* @depends testCaching
*/
public function testIsDirty() {
$container = $this->containerFixture();
$obj = $container->remoteObject(self::FNAME);
// THere is no content. Assert false.
$this->assertFalse($obj->isDirty());
$obj->setCaching(TRUE);
$obj->content();
// THere is content, but it is unchanged.
$this->assertFalse($obj->isDirty());
// Change content and retest.
$obj->setContent('foo');
$this->assertTrue($obj->isDirty());
}
/**
* @depends testIsDirty
*/
public function testRefresh() {
$container = $this->containerFixture();
$obj = $container->remoteObject(self::FNAME);
$obj->setContent('foo');
$this->assertTrue($obj->isDirty());
$obj->refresh(FALSE);
$this->assertFalse($obj->isDirty());
$this->assertEquals(self::FCONTENT, $obj->content());
$obj->setContent('foo');
$this->assertTrue($obj->isDirty());
$obj->refresh(TRUE);
$this->assertFalse($obj->isDirty());
$this->assertEquals(self::FCONTENT, $obj->content());
$this->destroyContainerFixture();
}
}