Skip to content

Commit ba44841

Browse files
authored
refactor unit tests: use mockRequest for all requests (#397)
1 parent 9f817df commit ba44841

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+470
-562
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"justinrainbow/json-schema": "^5.2"
3535
},
3636
"require-dev": {
37+
"ext-json": "*",
3738
"ergebnis/composer-normalize": "^2.0",
3839
"friendsofphp/php-cs-fixer": "^3",
3940
"php-coveralls/php-coveralls": "^2.0",
@@ -49,7 +50,6 @@
4950
},
5051
"autoload-dev": {
5152
"psr-4": {
52-
"OpenStack\\Integration\\": "tests/integration/",
5353
"OpenStack\\Sample\\": "tests/sample/",
5454
"OpenStack\\Test\\": "tests/unit/"
5555
}

tests/sample/Compute/v2/VolumeAttachmentTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testAttach(): VolumeAttachment
1414
// let's wait for the server to be completely up
1515
// https://bugs.launchpad.net/nova/+bug/1998148
1616
// https://bugs.launchpad.net/nova/+bug/1960346
17-
sleep(10);
17+
sleep(15);
1818

1919
$volume = $this->getCachedService(Service::class)->createVolume(
2020
[
@@ -67,6 +67,11 @@ public function testList(VolumeAttachment $createdVolumeAttachment)
6767
*/
6868
public function testDetach(VolumeAttachment $createdVolumeAttachment)
6969
{
70+
// let's wait for the server to be completely up
71+
// https://bugs.launchpad.net/nova/+bug/1998148
72+
// https://bugs.launchpad.net/nova/+bug/1960346
73+
sleep(15);
74+
7075
require_once $this->sampleFile(
7176
'servers/detach_volume_attachment.php',
7277
[

tests/unit/BlockStorage/v2/Models/QuotaSetTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function setUp(): void
2424

2525
public function test_it_retrieves()
2626
{
27-
$this->setupMock('GET', 'os-quota-sets/tenant-foo', [], [], 'GET_quota_set');
27+
$this->mockRequest('GET', 'os-quota-sets/tenant-foo', 'GET_quota_set', [], []);
2828

2929
$this->quotaSet->retrieve();
3030
self::assertEquals(1, $this->quotaSet->gigabytes);
@@ -41,7 +41,7 @@ public function test_it_updates()
4141
],
4242
];
4343

44-
$this->setupMock('PUT', 'os-quota-sets/tenant-foo', $expectedJson, [], 'GET_type');
44+
$this->mockRequest('PUT', 'os-quota-sets/tenant-foo', 'GET_type', $expectedJson, []);
4545

4646
$this->quotaSet->volumes = 1111;
4747
$this->quotaSet->snapshots = 2222;
@@ -50,7 +50,7 @@ public function test_it_updates()
5050

5151
public function test_it_deletes()
5252
{
53-
$this->setupMock('DELETE', 'os-quota-sets/tenant-foo', null, [], new Response(204));
53+
$this->mockRequest('DELETE', 'os-quota-sets/tenant-foo', new Response(204), null, []);
5454

5555
$this->quotaSet->delete();
5656
}

tests/unit/BlockStorage/v2/Models/SnapshotTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ public function test_it_updates()
2828
$this->snapshot->description = 'bar';
2929

3030
$expectedJson = ['snapshot' => ['name' => 'foo', 'description' => 'bar']];
31-
$this->setupMock('PUT', 'snapshots/1', $expectedJson, [], 'GET_snapshot');
31+
$this->mockRequest('PUT', 'snapshots/1', 'GET_snapshot', $expectedJson, []);
3232

3333
$this->snapshot->update();
3434
}
3535

3636
public function test_it_deletes()
3737
{
38-
$this->setupMock('DELETE', 'snapshots/1', null, [], new Response(204));
38+
$this->mockRequest('DELETE', 'snapshots/1', new Response(204), null, []);
3939

4040
$this->snapshot->delete();
4141
}
4242

4343
public function test_it_gets_metadata()
4444
{
45-
$this->setupMock('GET', 'snapshots/1/metadata', null, [], 'GET_metadata');
45+
$this->mockRequest('GET', 'snapshots/1/metadata', 'GET_metadata', null, []);
4646

4747
$expected = [
4848
'foo' => '1',
@@ -54,22 +54,22 @@ public function test_it_gets_metadata()
5454

5555
public function test_it_retrieves()
5656
{
57-
$this->setupMock('GET', 'snapshots/1', null, [], 'GET_snapshot');
57+
$this->mockRequest('GET', 'snapshots/1', 'GET_snapshot', null, []);
5858

5959
$this->snapshot->retrieve();
6060
}
6161

6262
public function test_it_merges_metadata()
6363
{
64-
$this->setupMock('GET', 'snapshots/1/metadata', null, [], 'GET_metadata');
64+
$this->mockRequest('GET', 'snapshots/1/metadata', 'GET_metadata', null, []);
6565

6666
$expectedJson = ['metadata' => [
6767
'foo' => 'newFoo',
6868
'bar' => '2',
6969
'baz' => 'bazVal',
7070
]];
7171

72-
$this->setupMock('PUT', 'snapshots/1/metadata', $expectedJson, [], 'GET_metadata');
72+
$this->mockRequest('PUT', 'snapshots/1/metadata', 'GET_metadata', $expectedJson, []);
7373

7474
$this->snapshot->mergeMetadata(['foo' => 'newFoo', 'baz' => 'bazVal']);
7575
}
@@ -78,7 +78,7 @@ public function test_it_resets_metadata()
7878
{
7979
$expectedJson = ['metadata' => ['key1' => 'val1']];
8080

81-
$this->setupMock('PUT', 'snapshots/1/metadata', $expectedJson, [], 'GET_metadata');
81+
$this->mockRequest('PUT', 'snapshots/1/metadata', 'GET_metadata', $expectedJson, []);
8282

8383
$this->snapshot->resetMetadata(['key1' => 'val1']);
8484
}

tests/unit/BlockStorage/v2/Models/VolumeTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ public function test_it_updates()
2828
$this->volume->description = 'bar';
2929

3030
$expectedJson = ['volume' => ['name' => 'foo', 'description' => 'bar']];
31-
$this->setupMock('PUT', 'volumes/1', $expectedJson, [], 'GET_volume');
31+
$this->mockRequest('PUT', 'volumes/1', 'GET_volume', $expectedJson, []);
3232

3333
$this->volume->update();
3434
}
3535

3636
public function test_it_deletes()
3737
{
38-
$this->setupMock('DELETE', 'volumes/1', null, [], new Response(204));
38+
$this->mockRequest('DELETE', 'volumes/1', new Response(204), null, []);
3939

4040
$this->volume->delete();
4141
}
4242

4343
public function test_it_retrieves()
4444
{
45-
$this->setupMock('GET', 'volumes/1', null, [], 'GET_volume');
45+
$this->mockRequest('GET', 'volumes/1', 'GET_volume', null, []);
4646

4747
$this->volume->retrieve();
4848

@@ -67,15 +67,15 @@ public function test_it_retrieves()
6767

6868
public function test_it_merges_metadata()
6969
{
70-
$this->setupMock('GET', 'volumes/1/metadata', null, [], 'GET_metadata');
70+
$this->mockRequest('GET', 'volumes/1/metadata', 'GET_metadata', null, []);
7171

7272
$expectedJson = ['metadata' => [
7373
'foo' => 'newFoo',
7474
'bar' => '2',
7575
'baz' => 'bazVal',
7676
]];
7777

78-
$this->setupMock('PUT', 'volumes/1/metadata', $expectedJson, [], 'GET_metadata');
78+
$this->mockRequest('PUT', 'volumes/1/metadata', 'GET_metadata', $expectedJson, []);
7979

8080
$this->volume->mergeMetadata(['foo' => 'newFoo', 'baz' => 'bazVal']);
8181
}
@@ -84,14 +84,14 @@ public function test_it_resets_metadata()
8484
{
8585
$expectedJson = ['metadata' => ['key1' => 'val1']];
8686

87-
$this->setupMock('PUT', 'volumes/1/metadata', $expectedJson, [], 'GET_metadata');
87+
$this->mockRequest('PUT', 'volumes/1/metadata', 'GET_metadata', $expectedJson, []);
8888

8989
$this->volume->resetMetadata(['key1' => 'val1']);
9090
}
9191

9292
public function test_it_sets_volume_bootable()
9393
{
94-
$this->setupMock('POST', 'volumes/1/action', ['os-set_bootable' => ['bootable' => 'True']], [], new Response(200));
94+
$this->mockRequest('POST', 'volumes/1/action', new Response(200), ['os-set_bootable' => ['bootable' => 'True']], []);
9595

9696
$this->volume->setBootable(true);
9797
}
@@ -107,7 +107,7 @@ public function test_it_sets_image_meta_data()
107107
],
108108
];
109109

110-
$this->setupMock('POST', 'volumes/1/action', $expectedJson, [], new Response(200));
110+
$this->mockRequest('POST', 'volumes/1/action', new Response(200), $expectedJson, []);
111111
$this->volume->setImageMetadata([
112112
'attr_foo' => 'foofoo',
113113
'attr_bar' => 'barbar',
@@ -118,7 +118,7 @@ public function test_it_resets_status()
118118
{
119119
$expectedJson = ['os-reset_status' => ['status' => 'available', 'attach_status' => 'detached', 'migration_status' => 'migrating']];
120120

121-
$this->setupMock('POST', 'volumes/1/action', $expectedJson, [], new Response(202));
121+
$this->mockRequest('POST', 'volumes/1/action', new Response(202), $expectedJson, []);
122122

123123
$this->volume->resetStatus(
124124
[

tests/unit/BlockStorage/v2/Models/VolumeTypeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public function test_it_updates()
2626
{
2727
$expectedJson = ['volume_type' => ['name' => 'foo']];
2828

29-
$this->setupMock('PUT', 'types/1', $expectedJson, [], 'GET_type');
29+
$this->mockRequest('PUT', 'types/1', 'GET_type', $expectedJson, []);
3030

3131
$this->volumeType->name = 'foo';
3232
$this->volumeType->update();
3333
}
3434

3535
public function test_it_deletes()
3636
{
37-
$this->setupMock('DELETE', 'types/1', null, [], new Response(204));
37+
$this->mockRequest('DELETE', 'types/1', new Response(204), null, []);
3838

3939
$this->volumeType->delete();
4040
}

tests/unit/BlockStorage/v2/ServiceTest.php

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,21 @@ public function test_it_creates_volumes()
5858
],
5959
];
6060

61-
$this->setupMock('POST', 'volumes', $expectedJson, [], 'GET_volume');
61+
$this->mockRequest('POST', 'volumes', 'GET_volume', $expectedJson, []);
6262

6363
self::assertInstanceOf(Volume::class, $this->service->createVolume($opts));
6464
}
6565

6666
public function test_it_lists_volumes()
6767
{
68-
$this->client
69-
->request('GET', 'volumes', ['headers' => []])
70-
->shouldBeCalled()
71-
->willReturn($this->getFixture('GET_volumes'));
72-
73-
$this->client
74-
->request('GET', 'volumes', ['query' => ['marker' => '5aa119a8-d25b-45a7-8d1b-88e127885635'], 'headers' => []])
75-
->shouldBeCalled()
76-
->willReturn(new Response(204));
68+
$this->mockRequest('GET', 'volumes', 'GET_volumes', null, []);
69+
$this->mockRequest(
70+
'GET',
71+
['path' => 'volumes', 'query' => ['marker' => '5aa119a8-d25b-45a7-8d1b-88e127885635']],
72+
new Response(204),
73+
null,
74+
[]
75+
);
7776

7877
$count = 0;
7978

@@ -99,17 +98,14 @@ public function test_it_creates_volume_types()
9998

10099
$expectedJson = ['volume_type' => $opts];
101100

102-
$this->setupMock('POST', 'types', $expectedJson, [], 'GET_type');
101+
$this->mockRequest('POST', 'types', 'GET_type', $expectedJson, []);
103102

104103
self::assertInstanceOf(VolumeType::class, $this->service->createVolumeType($opts));
105104
}
106105

107106
public function test_it_lists_volume_types()
108107
{
109-
$this->client
110-
->request('GET', 'types', ['headers' => []])
111-
->shouldBeCalled()
112-
->willReturn($this->getFixture('GET_types'));
108+
$this->mockRequest('GET', 'types', 'GET_types', null, []);
113109

114110
$count = 0;
115111

@@ -145,22 +141,21 @@ public function test_it_creates_snapshots()
145141
'force' => $opts['force'],
146142
]];
147143

148-
$this->setupMock('POST', 'snapshots', $expectedJson, [], 'GET_snapshot');
144+
$this->mockRequest('POST', 'snapshots', 'GET_snapshot', $expectedJson, []);
149145

150146
self::assertInstanceOf(Snapshot::class, $this->service->createSnapshot($opts));
151147
}
152148

153149
public function test_it_lists_snapshots()
154150
{
155-
$this->client
156-
->request('GET', 'snapshots', ['headers' => []])
157-
->shouldBeCalled()
158-
->willReturn($this->getFixture('GET_snapshots'));
159-
160-
$this->client
161-
->request('GET', 'snapshots', ['query' => ['marker' => 'e820db06-58b5-439d-bac6-c01faa3f6499'], 'headers' => []])
162-
->shouldBeCalled()
163-
->willReturn(new Response(204));
151+
$this->mockRequest('GET', 'snapshots', 'GET_snapshots', null, []);
152+
$this->mockRequest(
153+
'GET',
154+
['path' => 'snapshots', 'query' => ['marker' => 'e820db06-58b5-439d-bac6-c01faa3f6499']],
155+
new Response(204),
156+
null,
157+
[]
158+
);
164159

165160
$count = 0;
166161

@@ -182,10 +177,7 @@ public function test_it_gets_a_snapshot()
182177

183178
public function test_it_gets_quota_set()
184179
{
185-
$this->client
186-
->request('GET', 'os-quota-sets/tenant-id-1234', ['headers' => []])
187-
->shouldBeCalled()
188-
->willReturn($this->getFixture('GET_quota_set'));
180+
$this->mockRequest('GET', 'os-quota-sets/tenant-id-1234', 'GET_quota_set', null, []);
189181

190182
$quotaSet = $this->service->getQuotaSet('tenant-id-1234');
191183

0 commit comments

Comments
 (0)