Files
openstack-sdk-php/tests/Tests/ObjectStore/v1/Resource/StreamWrapperTest.php
Jamie Hannaford c91e2932aa Refactors ClientInterface and Guzzle adapter.
This patch achieves three interrelated changes

1. an update to the ClientInterface, making it more efficient and
consistent with other HTTP libraries. PSR-FIG messages have also been
added as separate classes, rather than lumped as one. Guzzle
functionality has also been moved to its own namespace;

2. the refactoring of `GuzzleClient` to `GuzzleAdapter`, including
relevant changes needed for the interface change (1). We now have
ADAPTERS that bridge our interfaces with Guzzle's - making that
difference much clearer, extensible, less tightly coupled and less
brittle;

3. moving "bad request" error handling into its own dedicated space,
based on how the new Adapter functionality (2). Previously the client
tried to do all the exception logic - but this is not strictly its
responsibility. This logic has been shipped out to a base
RequestException which instantiates one of its children exception based
on the HTTP status code.

Although I have attempted to keep the scope of this patch to a minimum,
as granular as possible, because the interface is a core internal API,
various other files need to be modified to reflect the change. In terms
of the other two changes, these are inextricably linked to the interface
change, so cannot be teased out into their own patches.

Change-Id: Ibc1b50cec125c11d32ee6e4f0dbb395fcaea864e
2014-06-05 10:09:03 +02:00

312 lines
8.8 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.
============================================================================ */
namespace OpenStack\Tests\ObjectStore\v1\Resource;
use OpenStack\Bootstrap;
use OpenStack\ObjectStore\v1\Resource\StreamWrapper;
use OpenStack\Tests\TestCase;
/**
* @group streamWrapper
*/
class StreamWrapperTest extends StreamWrapperTestCase
{
const SCHEME = StreamWrapper::DEFAULT_SCHEME;
public function testStreamContext()
{
$context = stream_context_get_options($this->context)['swift'];
$this->assertNotEmpty($context['token']);
$this->assertNotEmpty($context['swift_endpoint']);
$this->assertEquals(self::FTYPE, $context['content_type']);
}
public function testOpenFailureWithoutContext()
{
$url = $this->createNewUrl('non_existent_container/foo.txt');
$this->assertFalse(@fopen($url, 'r'));
}
public function testResourceType()
{
$this->assertInternalType('resource', $this->resource);
}
public function testCreatingResourceInWriteMode()
{
$resource = $this->createNewResource($this->createNewUrl(), 'w+');
$this->assertInternalType('resource', $resource);
fclose($resource);
}
public function testCreatingResourceInCreateMode()
{
$resource = $this->createNewResource($this->createNewUrl(), 'c+');
$this->assertInternalType('resource', $resource);
fclose($resource);
}
public function testTell()
{
// Sould be at the beginning of the buffer.
$this->assertEquals(0, ftell($this->resource));
}
public function testWrite()
{
$string = 'To be is to be the value of a bound variable. -- Quine';
fwrite($this->resource, $string);
$this->assertGreaterThan(0, ftell($this->resource));
}
public function testStat()
{
$this->assertEquals(0, fstat($this->resource)['size']);
fwrite($this->resource, 'foo');
fflush($this->resource);
$this->assertGreaterThan(0, fstat($this->resource)['size']);
}
public function testSeek()
{
$text = 'Foo bar';
fwrite($this->resource, $text);
fseek($this->resource, 0, SEEK_END);
$pointer = ftell($this->resource);
$this->assertGreaterThan(0, $pointer);
}
public function testEof()
{
$this->assertFalse(feof($this->resource));
fwrite($this->resource, 'foo');
rewind($this->resource);
stream_get_contents($this->resource);
$this->assertTrue(feof($this->resource));
}
public function testFlush()
{
$content = str_repeat('foo', 50);
fwrite($this->resource, $content);
fflush($this->resource);
rewind($this->resource);
$this->assertEquals($content, stream_get_contents($this->resource));
}
public function testStreamGetMetadata()
{
$object = stream_get_meta_data($this->resource)['wrapper_data']->object();
$this->assertInstanceOf('OpenStack\ObjectStore\v1\Resource\Object', $object);
$this->assertEquals(self::FTYPE, $object->contentType());
}
public function testClose()
{
fclose($this->resource);
$this->assertFalse(is_resource($this->resource));
}
public function testCast()
{
$read = [$this->resource];
$write = [];
$except = [];
$this->assertGreaterThan(0, stream_select($read, $write, $except, 0));
}
public function testUrlStat()
{
$stat = stat($this->url);
// Check that the array looks right.
$this->assertCount(26, $stat);
$this->assertEquals(0, $stat[3]);
$this->assertEquals($stat[2], $stat['mode']);
}
public function testFileExists()
{
$this->assertTrue(file_exists($this->url));
}
public function testFileIsReadable()
{
$this->assertTrue(is_readable($this->url));
}
public function testFileIsWritable()
{
$this->assertTrue(is_writeable($this->url));
}
public function testFileModifyTime()
{
$this->assertGreaterThan(0, filemtime($this->url));
}
public function testFileSize()
{
$url = $this->createNewUrl('file_size_test');
$resource = $this->createNewResource($url, 'w+');
fwrite($resource, '!');
fclose($resource);
$this->assertEquals(1, filesize($url));
unlink($url);
}
public function testPermissions()
{
$perm = fileperms($this->url);
// Assert that this is a file. Objects are *always* marked as files.
$this->assertEquals(0x8000, $perm & 0x8000);
// Assert writeable by owner.
$this->assertEquals(0x0080, $perm & 0x0080);
// Assert not world writable.
$this->assertEquals(0, $perm & 0x0002);
}
public function testFileGetContents()
{
$url = $this->createNewUrl('get_contents');
$resource = $this->createNewResource($url, 'w+');
fwrite($resource, '!');
fclose($resource);
$contents = file_get_contents($url, null, $this->context);
$this->assertEquals('!', $contents);
unlink($url);
}
public function testCopy()
{
$newUrl = '/tmp/new_file_from_swift.txt';
copy($this->url, $newUrl, $this->context);
$this->assertTrue(file_exists($newUrl));
unlink($newUrl);
}
public function testUnlink()
{
unlink($this->url, $this->context);
$this->assertFalse(file_exists($this->url));
}
public function testSetOption()
{
$this->assertTrue(stream_set_blocking($this->resource, 1));
// Returns 0 on success.
$this->assertEquals(0, stream_set_write_buffer($this->resource, 8192));
// Cannot set a timeout on a tmp storage:
$this->assertFalse(stream_set_timeout($this->resource, 10));
}
public function testRename()
{
$oldUrl = $this->createNewUrl('old');
$newUrl = $this->createNewUrl('new');
$original = $this->createNewResource($oldUrl, 'w+');
fwrite($original, 'fooooo');
fclose($original);
rename($oldUrl, $newUrl, $this->context);
$this->assertTrue(file_exists($newUrl));
$this->assertFalse(file_exists($this->url));
unlink($newUrl, $this->context);
}
public function testOpenDir()
{
$baseDirectory = opendir($this->createNewUrl(''), $this->context);
$this->assertInternalType('resource', $baseDirectory);
closedir($baseDirectory);
}
public function testReadDir()
{
$paths = ['test1.txt', 'foo/test2.txt', 'foo/test3.txt', 'bar/test4.txt'];
foreach ($paths as $path) {
$file = fopen($this->createNewUrl($path), 'c+', false, $this->context);
fwrite($file, 'Test.');
fclose($file);
}
$baseDirectory = opendir($this->createNewUrl(''), $this->context);
$expectedPaths = ['bar/', 'foo/', 'test1.txt'];
while (false !== ($currentEntry = readdir($baseDirectory))) {
$nextPath = array_shift($expectedPaths);
$this->assertEquals($nextPath, $currentEntry);
}
$this->assertFalse(readdir($baseDirectory));
closedir($baseDirectory);
}
public function testRewindDir()
{
$baseDirectory = opendir($this->createNewUrl(''), $this->context);
rewinddir($baseDirectory);
$this->assertEquals('bar/', readdir($baseDirectory));
closedir($baseDirectory);
}
public function testCloseDir()
{
$baseDirectory = opendir($this->createNewUrl(''), $this->context);
closedir($baseDirectory);
$this->assertFalse(is_resource($baseDirectory));
}
public function testOpenSubdir()
{
// Opening foo we should find test2.txt and test3.txt.
$url = $this->createNewUrl('foo/');
$dir = opendir($url, $this->context);
$this->assertEquals('test2.txt', readdir($dir));
$this->assertEquals('test3.txt', readdir($dir));
$array = scandir($url, -1, $this->context);
$this->assertEquals(2, count($array));
$this->assertEquals('test3.txt', $array[0]);
}
}