-
Notifications
You must be signed in to change notification settings - Fork 18
/
ObjectTest.php
executable file
·114 lines (88 loc) · 4.32 KB
/
ObjectTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
require_once(dirname(__FILE__) . '/BaseTest.php');
class ObjectTest extends BaseTest
{
protected static $newContainerName = null;
protected static $newObjectNames = array();
protected static $newObjectBody = null;
protected static $newObjectMeta = null;
protected static $metaKey = null;
protected static $wackyObjectName = null;
protected static $copiedFileName = null;
protected static $localFileName = null;
public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
// Removes cached authentication data if any
self::$objectStorage->reloadAuthenticationData();
// This container will be used to test creation/update and delete
self::$newContainerName = 'phpUnit_' . substr(md5(time()), 0, 7) . time();
foreach (range(0, 5) as $idx) {
self::$newObjectNames[] = self::$newContainerName . '/' . $idx . '.txt';
}
self::$metaKey = 'Description';
self::$newObjectMeta = 'META DATA TO TEST';
self::$newObjectBody = 'This is a test file created by PHPUnit test. It should be removed shortly. If not, something\'s gone wrong.';
self::$copiedFileName = self::$newContainerName . '/copied-file.dummy';
self::$localFileName = '/tmp/object-storage-' . time() . '.dummy.file';
}
public function testCreate()
{
$newContainer = self::$objectStorage->with(self::$newContainerName)->create();
$this->assertInstanceOf('ObjectStorage_Abstract', $newContainer, 'Failed to create new container: ' . self::$newContainerName);
foreach (self::$newObjectNames as $name) {
$object = self::$objectStorage->with($name)
->setMeta(self::$metaKey, self::$newObjectMeta)
->setBody(self::$newObjectBody)
->create();
$this->assertInstanceOf('ObjectStorage_Abstract', $object, 'Failed to create new object: ' . $name);
}
sleep(1);
}
public function testGet()
{
$container = self::$objectStorage->with(self::$newContainerName)->get();
$this->assertEquals($container->getObjectCount(), count(self::$newObjectNames), 'Object count must be ' . count(self::$newObjectNames));
if ($container->getObjectCount() > 0) {
foreach ($container->objects as $shallowObject) {
$object = $shallowObject->get();
$this->assertEquals(self::$newObjectMeta, $object->getHeader('X-object-meta-' . self::$metaKey));
$this->assertEquals(self::$newObjectBody, $object->getBody());
}
}
}
public function testCreateFromFile()
{
$result = @file_put_contents(self::$localFileName, str_repeat(rand(0,9), 1024*1024));
sleep(1);
if ($result == false) {
echo 'WARN: failed to create a temporary file for upload test. Path: ' . self::$localFileName . "\n";
} else {
$object = self::$objectStorage->with(self::$copiedFileName)
->setLocalFile(self::$localFileName)
->create();
$this->assertInstanceOf('ObjectStorage_Abstract', $object, 'Failed to create new object: ' . $name);
}
$copiedObject = self::$objectStorage->with(self::$copiedFileName)->getInfo();
$fileSize = filesize(self::$localFileName);
$this->assertEquals($copiedObject->getHeader('content-length'), $fileSize, 'Copied object size (' . $copiedObject->getHeader('content-length') . ') and the local file size (' . $fileSize. ') do not match.');
}
public function testDelete()
{
foreach (self::$newObjectNames as $name) {
$result = self::$objectStorage->with($name)->delete();
$this->assertTrue($result, 'Failed to delete ' . $name);
}
self::$objectStorage->with(self::$copiedFileName)->delete();
$result = self::$objectStorage->with(self::$newContainerName)->delete();
$this->assertTrue($result, 'Failed to delete the test container ' . self::$newContainerName);
@unlink(self::$localFileName);
}
/**
* @expectedException ObjectStorage_Exception_Http_NotFound
*/
public function testDeleteNonExistingObject()
{
$result = self::$objectStorage->with('i-do-not-exist-' . time())->delete();
}
}