forked from rackspace/php-opencloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionTest.php
More file actions
123 lines (118 loc) · 2.9 KB
/
CollectionTest.php
File metadata and controls
123 lines (118 loc) · 2.9 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
<?php
/**
* Unit Tests
*
* @copyright 2012-2013 Rackspace Hosting, Inc.
* See COPYING for licensing information
*
* @version 1.0.0
* @author Glen Campbell <glen.campbell@rackspace.com>
*/
require_once('collection.inc');
class Gertrude {
public function foobar($item) {
$obj = new stdClass();
if (is_array($item) || is_object($item)) {
foreach($item as $k => $v)
$obj->$k = $v;
}
else
$obj->id = $item;
return $obj;
}
}
class CollectionTest extends PHPUnit_Framework_TestCase
{
private $my;
/**
* create our private collection
*/
public function __construct() {
$x = new Gertrude;
$this->my = new OpenCloud\Collection(
$x,
'foobar',
//array('one', 'two', 'three', 'four'));
array(
(object)array('id'=>'one', 'val'=>5),
(object)array('id'=>'two', 'val'=>5),
(object)array('id'=>'three','val'=>9),
(object)array('id'=>'four', 'val'=>0),
));
}
/**
* Tests
*/
/**
* @expectedException OpenCloud\CollectionError
*/
public function test___construct() {
$this->assertEquals('one', $this->my->First()->id);
// this causes the expected exception
$coll = new OpenCloud\Collection(
new Gertrude,
'foobar',
"This string is not an array");
}
public function testService() {
$this->assertEquals('Gertrude', get_class($this->my->Service()));
}
public function test_first_and_next() {
$this->assertEquals($this->my->First()->id, 'one');
$this->assertEquals($this->my->Next()->id, 'two');
$this->assertEquals($this->my->Next()->id, 'three');
$this->assertEquals($this->my->Next()->id, 'four');
$this->assertEquals($this->my->Next(), FALSE);
}
public function testReset() {
$first = $this->my->First();
$this->my->Reset();
$this->assertEquals(
$first,
$this->my->Next());
}
public function testSize() {
$this->assertEquals(4, $this->my->Size());
}
public function testSort() {
$this->my->Sort();
$this->assertEquals(
'four',
$this->my->First()->id);
// test non-string items
$this->my->Sort('val');
}
/**
* @expectedException OpenCloud\DomainError
*/
public function testSelect() {
$coll = $this->my; // don't modify the global collection
$this->assertEquals(
4,
$coll->Size());
$coll->Select(function($item) { return strpos($item->id,'o')!==FALSE;});
$this->assertEquals(
3,
$coll->Size());
// this should cause an error
$coll->Select(function(){ return 5;});
}
public function testNextPage() {
$coll = $this->my;
$coll->SetNextPageCallback(
array($this,'_callback'),
'http://something');
$this->assertEquals(
'CollectionTest',
get_class($coll->NextPage()));
$this->assertEquals(
FALSE,
$coll->NextPage());
}
public function _callback($class, $url) {
// stub function used by SetNextPageCallback
}
public function testSetNextPageCallback() {
// not needed; exercised by testNextPage(), above
}
}