Skip to content

Commit 5024c2a

Browse files
authored
Add Zone Subscription Creation (#196)
Consuming the tenant API requires this as a part of the provisioning
1 parent 12ac2bb commit 5024c2a

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Cloudflare\API\Endpoints;
4+
5+
use Cloudflare\API\Adapter\Adapter;
6+
use Cloudflare\API\Traits\BodyAccessorTrait;
7+
use stdClass;
8+
9+
class ZoneSubscriptions implements API
10+
{
11+
use BodyAccessorTrait;
12+
13+
/**
14+
* @var Adapter
15+
*/
16+
private $adapter;
17+
18+
public function __construct(Adapter $adapter)
19+
{
20+
$this->adapter = $adapter;
21+
}
22+
23+
public function listZoneSubscriptions(string $zoneId): \stdClass
24+
{
25+
$user = $this->adapter->get('zones/' . $zoneId . '/subscriptions');
26+
$this->body = json_decode($user->getBody());
27+
28+
return (object)[
29+
'result' => $this->body->result,
30+
];
31+
}
32+
33+
public function addZoneSubscription(string $zoneId, string $ratePlanId = ''): stdClass
34+
{
35+
$options = [];
36+
37+
if (empty($ratePlanId) === false) {
38+
$options['rate_plan'] = [
39+
'id' => $ratePlanId,
40+
];
41+
}
42+
43+
$existingSubscription = $this->listZoneSubscriptions($zoneId);
44+
$method = empty($existingSubscription->result) ? 'post' : 'put';
45+
46+
$subscription = $this->adapter->{$method}('zones/' . $zoneId . '/subscription', $options);
47+
$this->body = json_decode($subscription->getBody());
48+
49+
return $this->body->result;
50+
}
51+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
use Cloudflare\API\Adapter\Adapter;
4+
use Cloudflare\API\Endpoints\ZoneSubscriptions;
5+
6+
class ZoneSubscriptionsTest extends TestCase
7+
{
8+
public function testListZoneSubscriptions()
9+
{
10+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/listZoneSubscriptions.json');
11+
12+
$mock = $this->getMockBuilder(Adapter::class)->getMock();
13+
$mock->method('get')->willReturn($response);
14+
15+
$mock->expects($this->once())
16+
->method('get')
17+
->with(
18+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscriptions')
19+
);
20+
21+
$zoneSubscriptions = new ZoneSubscriptions($mock);
22+
$zoneSubscriptions->listZoneSubscriptions('023e105f4ecef8ad9ca31a8372d0c353');
23+
24+
$this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result[0]->id);
25+
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result[0]->zone->id);
26+
}
27+
28+
public function testAddZoneSubscriptionIfMissing()
29+
{
30+
$postResponse = $this->getPsr7JsonResponseForFixture('Endpoints/createZoneSubscription.json');
31+
$getResponse = $this->getPsr7JsonResponseForFixture('Endpoints/listEmptyZoneSubscriptions.json');
32+
33+
$mock = $this->getMockBuilder(Adapter::class)->getMock();
34+
$mock->method('post')->willReturn($postResponse);
35+
$mock->method('get')->willReturn($getResponse);
36+
37+
$mock->expects($this->once())
38+
->method('post')
39+
->with(
40+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscription'),
41+
$this->equalTo([
42+
'rate_plan' => [
43+
'id' => 'PARTNER_PRO',
44+
],
45+
])
46+
);
47+
48+
$zoneSubscriptions = new ZoneSubscriptions($mock);
49+
$zoneSubscriptions->addZoneSubscription('023e105f4ecef8ad9ca31a8372d0c353', 'PARTNER_PRO');
50+
51+
$this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result->id);
52+
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result->zone->id);
53+
}
54+
55+
public function testAddZoneSubscriptionIfExisting()
56+
{
57+
$postResponse = $this->getPsr7JsonResponseForFixture('Endpoints/createZoneSubscription.json');
58+
$getResponse = $this->getPsr7JsonResponseForFixture('Endpoints/listZoneSubscriptions.json');
59+
60+
$mock = $this->getMockBuilder(Adapter::class)->getMock();
61+
$mock->method('put')->willReturn($postResponse);
62+
$mock->method('get')->willReturn($getResponse);
63+
64+
$mock->expects($this->once())
65+
->method('put')
66+
->with(
67+
$this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/subscription'),
68+
$this->equalTo([
69+
'rate_plan' => [
70+
'id' => 'PARTNER_PRO',
71+
],
72+
])
73+
);
74+
75+
$zoneSubscriptions = new ZoneSubscriptions($mock);
76+
$zoneSubscriptions->addZoneSubscription('023e105f4ecef8ad9ca31a8372d0c353', 'PARTNER_PRO');
77+
78+
$this->assertEquals('506e3185e9c882d175a2d0cb0093d9f2', $zoneSubscriptions->getBody()->result->id);
79+
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zoneSubscriptions->getBody()->result->zone->id);
80+
}
81+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"success": true,
3+
"errors": [],
4+
"messages": [],
5+
"result": {
6+
"app": {
7+
"install_id": null
8+
},
9+
"id": "506e3185e9c882d175a2d0cb0093d9f2",
10+
"state": "Paid",
11+
"price": 20,
12+
"currency": "USD",
13+
"component_values": [
14+
{
15+
"name": "page_rules",
16+
"value": 20,
17+
"default": 5,
18+
"price": 5
19+
}
20+
],
21+
"zone": {
22+
"id": "023e105f4ecef8ad9ca31a8372d0c353",
23+
"name": "example.com"
24+
},
25+
"frequency": "monthly",
26+
"rate_plan": {
27+
"id": "free",
28+
"public_name": "Business Plan",
29+
"currency": "USD",
30+
"scope": "zone",
31+
"sets": [
32+
{}
33+
],
34+
"is_contract": false,
35+
"externally_managed": false
36+
},
37+
"current_period_end": "2014-03-31T12:20:00Z",
38+
"current_period_start": "2014-05-11T12:20:00Z"
39+
}
40+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"success": true,
3+
"errors": [],
4+
"messages": [],
5+
"result": []
6+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"success": true,
3+
"errors": [],
4+
"messages": [],
5+
"result": [
6+
{
7+
"app": {
8+
"install_id": null
9+
},
10+
"id": "506e3185e9c882d175a2d0cb0093d9f2",
11+
"state": "Paid",
12+
"price": 20,
13+
"currency": "USD",
14+
"component_values": [
15+
{
16+
"name": "page_rules",
17+
"value": 20,
18+
"default": 5,
19+
"price": 5
20+
}
21+
],
22+
"zone": {
23+
"id": "023e105f4ecef8ad9ca31a8372d0c353",
24+
"name": "example.com"
25+
},
26+
"frequency": "monthly",
27+
"rate_plan": {
28+
"id": "free",
29+
"public_name": "Business Plan",
30+
"currency": "USD",
31+
"scope": "zone",
32+
"sets": [
33+
{}
34+
],
35+
"is_contract": false,
36+
"externally_managed": false
37+
},
38+
"current_period_end": "2014-03-31T12:20:00Z",
39+
"current_period_start": "2014-05-11T12:20:00Z"
40+
}
41+
]
42+
}

0 commit comments

Comments
 (0)