From 5024c2ab53f7384703d23605f8a00fe9908bea8e Mon Sep 17 00:00:00 2001 From: Phil Young Date: Wed, 13 Oct 2021 04:39:47 +0100 Subject: [PATCH] Add Zone Subscription Creation (#196) Consuming the tenant API requires this as a part of the provisioning --- src/Endpoints/ZoneSubscriptions.php | 51 ++++++++++++ tests/Endpoints/ZoneSubscriptionsTest.php | 81 +++++++++++++++++++ .../Endpoints/createZoneSubscription.json | 40 +++++++++ .../Endpoints/listEmptyZoneSubscriptions.json | 6 ++ .../Endpoints/listZoneSubscriptions.json | 42 ++++++++++ 5 files changed, 220 insertions(+) create mode 100644 src/Endpoints/ZoneSubscriptions.php create mode 100644 tests/Endpoints/ZoneSubscriptionsTest.php create mode 100644 tests/Fixtures/Endpoints/createZoneSubscription.json create mode 100644 tests/Fixtures/Endpoints/listEmptyZoneSubscriptions.json create mode 100644 tests/Fixtures/Endpoints/listZoneSubscriptions.json diff --git a/src/Endpoints/ZoneSubscriptions.php b/src/Endpoints/ZoneSubscriptions.php new file mode 100644 index 0000000..6149dd9 --- /dev/null +++ b/src/Endpoints/ZoneSubscriptions.php @@ -0,0 +1,51 @@ +adapter = $adapter; + } + + public function listZoneSubscriptions(string $zoneId): \stdClass + { + $user = $this->adapter->get('zones/' . $zoneId . '/subscriptions'); + $this->body = json_decode($user->getBody()); + + return (object)[ + 'result' => $this->body->result, + ]; + } + + public function addZoneSubscription(string $zoneId, string $ratePlanId = ''): stdClass + { + $options = []; + + if (empty($ratePlanId) === false) { + $options['rate_plan'] = [ + 'id' => $ratePlanId, + ]; + } + + $existingSubscription = $this->listZoneSubscriptions($zoneId); + $method = empty($existingSubscription->result) ? 'post' : 'put'; + + $subscription = $this->adapter->{$method}('zones/' . $zoneId . '/subscription', $options); + $this->body = json_decode($subscription->getBody()); + + return $this->body->result; + } +} diff --git a/tests/Endpoints/ZoneSubscriptionsTest.php b/tests/Endpoints/ZoneSubscriptionsTest.php new file mode 100644 index 0000000..bd4b7c4 --- /dev/null +++ b/tests/Endpoints/ZoneSubscriptionsTest.php @@ -0,0 +1,81 @@ +getPsr7JsonResponseForFixture('Endpoints/listZoneSubscriptions.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscriptions') + ); + + $zoneSubscriptions = new ZoneSubscriptions($mock); + $zoneSubscriptions->listZoneSubscriptions('023e105f4ecef8ad9ca31a8372d0c353'); + + $this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result[0]->id); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result[0]->zone->id); + } + + public function testAddZoneSubscriptionIfMissing() + { + $postResponse = $this->getPsr7JsonResponseForFixture('Endpoints/createZoneSubscription.json'); + $getResponse = $this->getPsr7JsonResponseForFixture('Endpoints/listEmptyZoneSubscriptions.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('post')->willReturn($postResponse); + $mock->method('get')->willReturn($getResponse); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscription'), + $this->equalTo([ + 'rate_plan' => [ + 'id' => 'PARTNER_PRO', + ], + ]) + ); + + $zoneSubscriptions = new ZoneSubscriptions($mock); + $zoneSubscriptions->addZoneSubscription('023e105f4ecef8ad9ca31a8372d0c353', 'PARTNER_PRO'); + + $this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result->id); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result->zone->id); + } + + public function testAddZoneSubscriptionIfExisting() + { + $postResponse = $this->getPsr7JsonResponseForFixture('Endpoints/createZoneSubscription.json'); + $getResponse = $this->getPsr7JsonResponseForFixture('Endpoints/listZoneSubscriptions.json'); + + $mock = $this->getMockBuilder(Adapter::class)->getMock(); + $mock->method('put')->willReturn($postResponse); + $mock->method('get')->willReturn($getResponse); + + $mock->expects($this->once()) + ->method('put') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscription'), + $this->equalTo([ + 'rate_plan' => [ + 'id' => 'PARTNER_PRO', + ], + ]) + ); + + $zoneSubscriptions = new ZoneSubscriptions($mock); + $zoneSubscriptions->addZoneSubscription('023e105f4ecef8ad9ca31a8372d0c353', 'PARTNER_PRO'); + + $this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result->id); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result->zone->id); + } +} diff --git a/tests/Fixtures/Endpoints/createZoneSubscription.json b/tests/Fixtures/Endpoints/createZoneSubscription.json new file mode 100644 index 0000000..b16f022 --- /dev/null +++ b/tests/Fixtures/Endpoints/createZoneSubscription.json @@ -0,0 +1,40 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "app": { + "install_id": null + }, + "id": "506e3185e9c882d175a2d0cb0093d9f2", + "state": "Paid", + "price": 20, + "currency": "USD", + "component_values": [ + { + "name": "page_rules", + "value": 20, + "default": 5, + "price": 5 + } + ], + "zone": { + "id": "023e105f4ecef8ad9ca31a8372d0c353", + "name": "example.com" + }, + "frequency": "monthly", + "rate_plan": { + "id": "free", + "public_name": "Business Plan", + "currency": "USD", + "scope": "zone", + "sets": [ + {} + ], + "is_contract": false, + "externally_managed": false + }, + "current_period_end": "2014-03-31T12:20:00Z", + "current_period_start": "2014-05-11T12:20:00Z" + } +} diff --git a/tests/Fixtures/Endpoints/listEmptyZoneSubscriptions.json b/tests/Fixtures/Endpoints/listEmptyZoneSubscriptions.json new file mode 100644 index 0000000..8e0c15e --- /dev/null +++ b/tests/Fixtures/Endpoints/listEmptyZoneSubscriptions.json @@ -0,0 +1,6 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/listZoneSubscriptions.json b/tests/Fixtures/Endpoints/listZoneSubscriptions.json new file mode 100644 index 0000000..080f4d7 --- /dev/null +++ b/tests/Fixtures/Endpoints/listZoneSubscriptions.json @@ -0,0 +1,42 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "app": { + "install_id": null + }, + "id": "506e3185e9c882d175a2d0cb0093d9f2", + "state": "Paid", + "price": 20, + "currency": "USD", + "component_values": [ + { + "name": "page_rules", + "value": 20, + "default": 5, + "price": 5 + } + ], + "zone": { + "id": "023e105f4ecef8ad9ca31a8372d0c353", + "name": "example.com" + }, + "frequency": "monthly", + "rate_plan": { + "id": "free", + "public_name": "Business Plan", + "currency": "USD", + "scope": "zone", + "sets": [ + {} + ], + "is_contract": false, + "externally_managed": false + }, + "current_period_end": "2014-03-31T12:20:00Z", + "current_period_start": "2014-05-11T12:20:00Z" + } + ] +} \ No newline at end of file