PersistentObjectTest.php
8.49 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* Unit Tests
*
* @copyright 2012-2014 Rackspace Hosting, Inc.
* See COPYING for licensing information
*
* @version 1.0.0
* @author Glen Campbell <glen.campbell@rackspace.com>
*/
namespace OpenCloud\Tests\Common;
use OpenCloud\Common\PersistentObject;
use OpenCloud\Compute\Service as ComputeService;
// make a real class from the abstract one
class MyPersistentObject extends PersistentObject
{
public $status;
public $updated;
public $hostId;
public $addresses;
public $links;
public $image;
public $hostname;
public $flavor;
public $id;
public $user_id;
public $name;
public $created;
public $tenant_id;
public $accessIPv4;
public $accessIPv6;
public $volume;
public $progress;
public $adminPass;
public $metadata;
protected static $json_name = 'instance';
protected static $json_collection_name = 'instanceCollection';
protected static $url_resource = 'instances';
public function Refresh($id = NULL, $url = NULL)
{
return parent::Refresh($id, $url);
}
public function NoCreate()
{
return parent::NoCreate();
}
public function NoUpdate()
{
return parent::NoUpdate();
}
public function NoDelete()
{
return parent::NoDelete();
}
public function Action($object)
{
return parent::Action($object);
}
public function CreateUrl()
{
return parent::CreateUrl();
}
}
class NamelessObject extends PersistentObject
{
}
class PersistentObjectTest extends \OpenCloud\Tests\OpenCloudTestCase
{
private $service;
private $instance;
public function setupObjects()
{
$this->service = $this->getClient()->computeService();
$this->instance = new MyPersistentObject($this->service);
}
public function test__construct()
{
$inst = new MyPersistentObject($this->service);
$this->assertInstanceOf('OpenCloud\Tests\Common\MyPersistentObject', $inst);
$inst = new MyPersistentObject($this->service, array('id' => '42'));
$this->assertInstanceOf('OpenCloud\Tests\Common\MyPersistentObject', $inst);
}
/**
* @expectedException \OpenCloud\Common\Exceptions\InvalidArgumentError
*/
public function test__construct2()
{
$inst = new MyPersistentObject($this->service, FALSE);
}
public function testUrl()
{
$this->instance->id = '12';
$this->assertEquals(
'https://dfw.servers.api.rackspacecloud.com/v2/123456/instances/12',
(string) $this->instance->getUrl()
);
$this->assertEquals(
'https://dfw.servers.api.rackspacecloud.com/v2/123456/instances/12/foobar?foo=BAZ',
(string) $this->instance->getUrl('foobar', array('foo' => 'BAZ'))
);
}
public function testUrl2()
{
$this->instance->id = '12';
/* this tests for subresources and query strings */
$qstr = array('a' => 1, 'b' => 2);
$this->assertEquals(
'https://dfw.servers.api.rackspacecloud.com/v2/123456/instances/12/pogo?a=1&b=2',
(string) $this->instance->getUrl('pogo', $qstr)
);
}
/**
* @mockFile Server
* @mockPath Compute
*/
public function testRefresh()
{
$this->instance->refresh('ef08aa7a-b5e4-4bb8-86df-5ac56230f841');
$this->assertObjectHasAttribute('status', $this->instance);
}
/**
* @covers OpenCloud\Common\PersistentObject::waitFor()
*/
public function testWaitFor()
{
$this->instance->id = '11';
$this->instance->WaitFor('FOOBAR', -1, array($this, 'WaitForCallBack'));
$this->assertEquals('FOOBAR', $this->instance->status);
}
// this is called by the WaitFor function, above
public function WaitForCallBack($server)
{
$server->status = 'FOOBAR';
}
/**
* @expectedException OpenCloud\Common\Exceptions\RuntimeException
*/
public function testCreate()
{
$this->instance->create();
}
/**
* @expectedException OpenCloud\Common\Exceptions\UpdateError
*/
public function testUpdate()
{
$this->instance->Update();
}
public function testName()
{
$this->instance->name = '';
$this->assertEquals('', $this->instance->name());
}
public function testStatus()
{
$this->assertEquals('N/A', $this->instance->Status());
}
public function testId()
{
$this->assertNull($this->instance->Id());
}
public function testJsonName()
{
$this->assertEquals('instance', MyPersistentObject::JsonName());
}
public function testResourceName()
{
$this->assertEquals('instances', MyPersistentObject::ResourceName());
}
public function testJsonCollectionName()
{
$this->assertEquals(
'instanceCollection',
MyPersistentObject::JsonCollectionName()
);
}
/**
* @expectedException \OpenCloud\Common\Exceptions\CreateError
*/
public function testNoCreate()
{
$this->instance->NoCreate();
}
/**
* @expectedException \OpenCloud\Common\Exceptions\UpdateError
*/
public function testNoUpdate()
{
$this->instance->NoUpdate();
}
/**
* @expectedException \OpenCloud\Common\Exceptions\DeleteError
*/
public function testNoDelete()
{
$this->instance->NoDelete();
}
public function testService()
{
$this->assertInstanceOf('OpenCloud\Compute\Service', $this->instance->getService());
}
public function testParent()
{
$this->assertInstanceOf('OpenCloud\Compute\Service', $this->instance->getParent());
}
/**
* @expectedException \OpenCloud\Common\Exceptions\UnsupportedExtensionError
*/
public function testCheckExtension()
{
// this should work
$this->assertTrue($this->instance->CheckExtension('os-rescue'));
// this causes the exception
$this->instance->CheckExtension('foobar');
}
public function testAction()
{
$obj = new \stdClass;
$this->instance->id = 'foo';
$this->instance->Action($obj);
}
public function testCreateUrl()
{
$this->assertEquals(
'https://dfw.servers.api.rackspacecloud.com/v2/123456/instances',
(string) $this->instance->CreateUrl()
);
}
public function testRegion()
{
$this->assertEquals('DFW', $this->instance->Region());
}
/**
* @expectedException OpenCloud\Common\Exceptions\ServiceException
*/
public function testGettingServiceFailsIfNotSet()
{
$service = new MyPersistentObject;
$service->getService();
}
/**
* @expectedException OpenCloud\Common\Exceptions\IdRequiredError
*/
public function testRefreshFailsWithoutId()
{
$this->instance->id = null;
$this->instance->refresh();
}
/**
* @expectedException OpenCloud\Common\Exceptions\NameError
*/
public function testGetNameFailsWhenNoName()
{
$object = new NamelessObject($this->service);
$object->name();
}
/**
* @expectedException OpenCloud\Common\Exceptions\IdRequiredError
*/
public function testActionWhenNoId()
{
$this->instance->id = null;
$this->instance->action(null);
}
/**
* @expectedException OpenCloud\Common\Exceptions\ServerActionError
*/
public function testActionFailsWhenNotValidObject()
{
$this->instance->id = 123;
$this->instance->action(array());
}
public function testGettingSelfLinkFailsIfNotSet()
{
$server = $this->instance;
$server->links = array(
(object) array(
"href" => "https://dfw.servers.api.rackspacecloud.com/9999/servers/9bfd203a-0695-xxxx-yyyy-66c4194c967b",
"rel" => "bookmark"
)
);
$this->assertFalse($server->findLink());
}
/**
* @expectedException \OpenCloud\Common\Exceptions\DocumentError
*/
public function test_Json_Name_Fails_If_Not_Set()
{
$server = new NamelessObject($this->service);
$server->jsonName();
}
/**
* @expectedException OpenCloud\Common\Exceptions\UrlError
*/
public function testResourceNameFailsIfNotSet()
{
$server = new NamelessObject($this->service);
$server->resourceName();
}
}