Skip to content

Commit 7d1409b

Browse files
committed
Added more unit tests
1 parent a37b032 commit 7d1409b

File tree

4 files changed

+87
-26
lines changed

4 files changed

+87
-26
lines changed

tests/unit/Common/Resource/AbstractResourceTest.php

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace OpenStack\Test\Common\Resource;
44

5-
use function GuzzleHttp\Psr7\stream_for;
65
use GuzzleHttp\Psr7\Response;
6+
use function GuzzleHttp\Psr7\stream_for;
77
use OpenStack\Common\Resource\AbstractResource;
88
use OpenStack\Common\Resource\Alias;
99
use OpenStack\Common\Resource\ResourceInterface;
1010
use OpenStack\Test\TestCase;
11-
use Prophecy\Argument;
1211

1312
class AbstractResourceTest extends TestCase
1413
{
@@ -37,11 +36,14 @@ public function test_it_populates_from_response()
3736

3837
public function test_it_populates_datetimes_from_arrays()
3938
{
40-
$dt = new \DateTimeImmutable('2015');
39+
$ca = new \DateTimeImmutable('2015');
40+
$ua = new \DateTimeImmutable('2016');
4141

42-
$this->resource->populateFromArray(['created' => '2015']);
42+
$this->resource->populateFromArray(['created_at' => '2015']);
43+
$this->resource->populateFromArray(['updated_at' => '2016']);
4344

44-
$this->assertEquals($this->resource->created, $dt);
45+
$this->assertEquals($this->resource->createdAt, $ca);
46+
$this->assertEquals($this->resource->updatedAt, $ua);
4547
}
4648

4749
public function test_it_populates_model_objects_from_arrays()
@@ -78,8 +80,36 @@ public function test_it_populates_models_from_response()
7880

7981
public function test_it_populates_models_from_arrays()
8082
{
81-
$data = ['flavor' => [], 'image' => []];
82-
$this->assertInstanceOf(ResourceInterface::class, $this->resource->model(TestResource::class, $data));
83+
$data = [
84+
'bar' => 'this-is-bar',
85+
'camel_attr' => 'this-is-camel-attr',
86+
'child' => ['bar' => 'child-bar', 'camel_attr' => 'child-camel'],
87+
'children' => [
88+
['bar' => 'child1-bar', 'camel_attr' => 'child1-camel'],
89+
['bar' => 'child2-bar', 'camel_attr' => 'child2-camel'],
90+
],
91+
];
92+
93+
/** @var TestResource $model */
94+
$model = $this->resource->model(TestResource::class, $data);
95+
96+
$this->assertInstanceOf(ResourceInterface::class, $model);
97+
98+
$this->assertEquals('this-is-bar', $model->bar);
99+
$this->assertEquals('this-is-camel-attr', $model->camelAttr);
100+
101+
$child = $model->child;
102+
$this->assertInstanceOf(TestResource::class, $child);
103+
$this->assertEquals('child-bar', $child->bar);
104+
$this->assertEquals('child-camel', $child->camelAttr);
105+
106+
$this->assertContainsOnlyInstancesOf(TestResource::class, $model->children);
107+
$this->assertCount(2, $model->children);
108+
$this->assertEquals('child1-bar', $model->children[0]->bar);
109+
$this->assertEquals('child1-camel', $model->children[0]->camelAttr);
110+
$this->assertEquals('child2-bar', $model->children[1]->bar);
111+
$this->assertEquals('child2-camel', $model->children[1]->camelAttr);
112+
83113
}
84114
}
85115

@@ -90,24 +120,35 @@ class TestResource extends AbstractResource
90120
/** @var string */
91121
public $bar;
92122

123+
/** @var string */
124+
public $camelAttr;
125+
93126
public $id;
94127

95128
/** @var \DateTimeImmutable */
96-
public $created;
129+
public $createdAt;
130+
131+
/** @var \DateTimeImmutable */
132+
public $updatedAt;
97133

98134
/** @var []TestResource */
99135
public $children;
100136

101137
/** @var TestResource */
102138
public $child;
103139

140+
protected $aliases = [
141+
'camel_attr' => 'camelAttr',
142+
];
143+
104144
protected function getAliases(): array
105145
{
106-
$aliases = parent::getAliases();
107-
$aliases['created'] = new Alias('created', \DateTimeImmutable::class);
108-
$aliases['child'] = new Alias('child', TestResource::class);
109-
$aliases['children'] = new Alias('children', TestResource::class, true);
110-
return $aliases;
146+
return parent::getAliases() + [
147+
'created_at' => new Alias('createdAt', \DateTimeImmutable::class),
148+
'updated_at' => new Alias('updatedAt', \DateTimeImmutable::class),
149+
'child' => new Alias('child', TestResource::class),
150+
'children' => new Alias('children', TestResource::class, true),
151+
];
111152
}
112153

113154
public function getAttrs(array $keys)

tests/unit/Common/Resource/OperatorResourceTest.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use OpenStack\Test\Common\Service\Fixtures\Api;
1010
use OpenStack\Test\Common\Service\Fixtures\Models\Foo;
1111
use OpenStack\Test\Common\Service\Fixtures\Service;
12-
use OpenStack\Test\TestCase;
1312
use OpenStack\Test\Fixtures\ComputeV2Api;
13+
use OpenStack\Test\TestCase;
1414

1515
class OperatorResourceTest extends TestCase
1616
{
@@ -39,7 +39,7 @@ public function test_it_retrieves_base_http_url()
3939

4040
public function test_it_executes_with_state()
4141
{
42-
$this->resource->id = 'foo';
42+
$this->resource->id = 'foo';
4343
$this->resource->bar = 'bar';
4444

4545
$expectedJson = ['id' => 'foo', 'bar' => 'bar'];
@@ -66,7 +66,7 @@ public function test_it_executes_operations_until_a_204_is_received()
6666
$api = new ComputeV2Api();
6767

6868
foreach ($this->resource->enumerate($api->getServers()) as $item) {
69-
$count++;
69+
++$count;
7070
$this->assertInstanceOf(TestOperatorResource::class, $item);
7171
}
7272

@@ -90,7 +90,7 @@ public function test_it_invokes_function_if_provided()
9090
$count = 0;
9191

9292
$fn = function () use (&$count) {
93-
$count++;
93+
++$count;
9494
};
9595

9696
foreach ($this->resource->enumerate($api->getServers(), [], $fn) as $item) {
@@ -111,7 +111,7 @@ public function test_it_halts_when_user_provided_limit_is_reached()
111111
$api = new ComputeV2Api();
112112

113113
foreach ($this->resource->enumerate($api->getServers(), ['limit' => 2]) as $item) {
114-
$count++;
114+
++$count;
115115
}
116116

117117
$this->assertEquals(2, $count);
@@ -126,11 +126,11 @@ public function test_it_predicts_resources_key_without_explicit_property()
126126

127127
$count = 0;
128128

129-
$api = new ComputeV2Api();
130-
$resource = new Server($this->client->reveal(), new $api);
129+
$api = new ComputeV2Api();
130+
$resource = new Server($this->client->reveal(), new $api());
131131

132132
foreach ($resource->enumerate($api->getServers(), ['limit' => 2]) as $item) {
133-
$count++;
133+
++$count;
134134
}
135135

136136
$this->assertEquals(2, $count);
@@ -166,16 +166,25 @@ public function test_it_populates_models_from_response()
166166

167167
public function test_it_populates_models_from_arrays()
168168
{
169-
$data = ['flavor' => [], 'image' => []];
170-
$this->assertInstanceOf(ResourceInterface::class, $this->resource->model(TestResource::class, $data));
169+
$data = [
170+
'id' => 123,
171+
'bar' => 'this-is-bar',
172+
];
173+
174+
/** @var TestOperatorResource $model */
175+
$model = $this->resource->model(TestOperatorResource::class, $data);
176+
177+
$this->assertInstanceOf(ResourceInterface::class, $model);
178+
$this->assertEquals(123, $model->id);
179+
$this->assertEquals('this-is-bar', $model->bar);
171180
}
172181
}
173182

174183
class TestOperatorResource extends OperatorResource
175184
{
176-
protected $resourceKey = 'foo';
185+
protected $resourceKey = 'foo';
177186
protected $resourcesKey = 'servers';
178-
protected $markerKey = 'id';
187+
protected $markerKey = 'id';
179188

180189
/** @var string */
181190
public $bar;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
HTTP/1.1 200 OK
2+
Content-Type: application/json
3+
4+
{
5+
"created_at": "2013-09-20T19:22:19Z",
6+
"image_id": "a96be11e-8536-4910-92cb-de50aa19dfe6",
7+
"member_id": "8989447062e04a818baf9e073fd04fa7",
8+
"schema": "/v2/schemas/member",
9+
"status": "pending",
10+
"updated_at": "2013-09-20T19:25:31Z"
11+
}

tests/unit/Images/v2/Models/ImageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function test_it_creates_member()
149149
$memberId = '8989447062e04a818baf9e073fd04fa7';
150150
$expectedJson = ['member' => $memberId];
151151

152-
$this->setupMock('POST', $this->path . '/members', $expectedJson, [], 'GET_member');
152+
$this->setupMock('POST', $this->path . '/members', $expectedJson, [], 'POST_members');
153153

154154
$member = $this->image->addMember('8989447062e04a818baf9e073fd04fa7');
155155
$this->assertInstanceOf(Member::class, $member);

0 commit comments

Comments
 (0)