Skip to content

Commit

Permalink
refactor unit tests: use mockRequest for all requests (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
k0ka authored Feb 1, 2024
1 parent 9f817df commit ba44841
Show file tree
Hide file tree
Showing 57 changed files with 470 additions and 562 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"justinrainbow/json-schema": "^5.2"
},
"require-dev": {
"ext-json": "*",
"ergebnis/composer-normalize": "^2.0",
"friendsofphp/php-cs-fixer": "^3",
"php-coveralls/php-coveralls": "^2.0",
Expand All @@ -49,7 +50,6 @@
},
"autoload-dev": {
"psr-4": {
"OpenStack\\Integration\\": "tests/integration/",
"OpenStack\\Sample\\": "tests/sample/",
"OpenStack\\Test\\": "tests/unit/"
}
Expand Down
7 changes: 6 additions & 1 deletion tests/sample/Compute/v2/VolumeAttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testAttach(): VolumeAttachment
// let's wait for the server to be completely up
// https://bugs.launchpad.net/nova/+bug/1998148
// https://bugs.launchpad.net/nova/+bug/1960346
sleep(10);
sleep(15);

$volume = $this->getCachedService(Service::class)->createVolume(
[
Expand Down Expand Up @@ -67,6 +67,11 @@ public function testList(VolumeAttachment $createdVolumeAttachment)
*/
public function testDetach(VolumeAttachment $createdVolumeAttachment)
{
// let's wait for the server to be completely up
// https://bugs.launchpad.net/nova/+bug/1998148
// https://bugs.launchpad.net/nova/+bug/1960346
sleep(15);

require_once $this->sampleFile(
'servers/detach_volume_attachment.php',
[
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/BlockStorage/v2/Models/QuotaSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function setUp(): void

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

$this->quotaSet->retrieve();
self::assertEquals(1, $this->quotaSet->gigabytes);
Expand All @@ -41,7 +41,7 @@ public function test_it_updates()
],
];

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

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

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

$this->quotaSet->delete();
}
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/BlockStorage/v2/Models/SnapshotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ public function test_it_updates()
$this->snapshot->description = 'bar';

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

$this->snapshot->update();
}

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

$this->snapshot->delete();
}

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

$expected = [
'foo' => '1',
Expand All @@ -54,22 +54,22 @@ public function test_it_gets_metadata()

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

$this->snapshot->retrieve();
}

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

$expectedJson = ['metadata' => [
'foo' => 'newFoo',
'bar' => '2',
'baz' => 'bazVal',
]];

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

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

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

$this->snapshot->resetMetadata(['key1' => 'val1']);
}
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/BlockStorage/v2/Models/VolumeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ public function test_it_updates()
$this->volume->description = 'bar';

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

$this->volume->update();
}

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

$this->volume->delete();
}

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

$this->volume->retrieve();

Expand All @@ -67,15 +67,15 @@ public function test_it_retrieves()

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

$expectedJson = ['metadata' => [
'foo' => 'newFoo',
'bar' => '2',
'baz' => 'bazVal',
]];

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

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

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

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

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

$this->volume->setBootable(true);
}
Expand All @@ -107,7 +107,7 @@ public function test_it_sets_image_meta_data()
],
];

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

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

$this->volume->resetStatus(
[
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/BlockStorage/v2/Models/VolumeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public function test_it_updates()
{
$expectedJson = ['volume_type' => ['name' => 'foo']];

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

$this->volumeType->name = 'foo';
$this->volumeType->update();
}

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

$this->volumeType->delete();
}
Expand Down
50 changes: 21 additions & 29 deletions tests/unit/BlockStorage/v2/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,21 @@ public function test_it_creates_volumes()
],
];

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

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

public function test_it_lists_volumes()
{
$this->client
->request('GET', 'volumes', ['headers' => []])
->shouldBeCalled()
->willReturn($this->getFixture('GET_volumes'));

$this->client
->request('GET', 'volumes', ['query' => ['marker' => '5aa119a8-d25b-45a7-8d1b-88e127885635'], 'headers' => []])
->shouldBeCalled()
->willReturn(new Response(204));
$this->mockRequest('GET', 'volumes', 'GET_volumes', null, []);
$this->mockRequest(
'GET',
['path' => 'volumes', 'query' => ['marker' => '5aa119a8-d25b-45a7-8d1b-88e127885635']],
new Response(204),
null,
[]
);

$count = 0;

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

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

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

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

public function test_it_lists_volume_types()
{
$this->client
->request('GET', 'types', ['headers' => []])
->shouldBeCalled()
->willReturn($this->getFixture('GET_types'));
$this->mockRequest('GET', 'types', 'GET_types', null, []);

$count = 0;

Expand Down Expand Up @@ -145,22 +141,21 @@ public function test_it_creates_snapshots()
'force' => $opts['force'],
]];

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

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

public function test_it_lists_snapshots()
{
$this->client
->request('GET', 'snapshots', ['headers' => []])
->shouldBeCalled()
->willReturn($this->getFixture('GET_snapshots'));

$this->client
->request('GET', 'snapshots', ['query' => ['marker' => 'e820db06-58b5-439d-bac6-c01faa3f6499'], 'headers' => []])
->shouldBeCalled()
->willReturn(new Response(204));
$this->mockRequest('GET', 'snapshots', 'GET_snapshots', null, []);
$this->mockRequest(
'GET',
['path' => 'snapshots', 'query' => ['marker' => 'e820db06-58b5-439d-bac6-c01faa3f6499']],
new Response(204),
null,
[]
);

$count = 0;

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

public function test_it_gets_quota_set()
{
$this->client
->request('GET', 'os-quota-sets/tenant-id-1234', ['headers' => []])
->shouldBeCalled()
->willReturn($this->getFixture('GET_quota_set'));
$this->mockRequest('GET', 'os-quota-sets/tenant-id-1234', 'GET_quota_set', null, []);

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

Expand Down
Loading

0 comments on commit ba44841

Please sign in to comment.