Skip to content

Commit 85e0d53

Browse files
authored
Merge pull request #42 from ConvertKit/get-form-subscriptions
Add `get_form_subscriptions()` API functions
2 parents c60e6bf + 21e85fe commit 85e0d53

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

src/ConvertKit_API.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,35 @@ public function get_account()
150150
);
151151
}
152152

153+
/**
154+
* List subscriptions to a form
155+
*
156+
* @param integer $form_id Form ID.
157+
* @param string $sort_order Sort Order (asc|desc).
158+
* @param string $subscriber_state Subscriber State (active,cancelled).
159+
* @param integer $page Page.
160+
*
161+
* @see https://developers.convertkit.com/#list-subscriptions-to-a-form
162+
*
163+
* @return false|mixed
164+
*/
165+
public function get_form_subscriptions(
166+
int $form_id,
167+
string $sort_order = 'asc',
168+
string $subscriber_state = 'active',
169+
int $page = 1
170+
) {
171+
return $this->get(
172+
sprintf('forms/%s/subscriptions', $form_id),
173+
[
174+
'api_secret' => $this->api_secret,
175+
'sort_order' => $sort_order,
176+
'subscriber_state' => $subscriber_state,
177+
'page' => $page,
178+
]
179+
);
180+
}
181+
153182
/**
154183
* Gets all sequences
155184
*

tests/ConvertKitAPITest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,119 @@ public function testGetAccount()
6565
$this->assertArrayHasKey('primary_email_address', $result);
6666
}
6767

68+
/**
69+
* Test that get_form_subscriptions() returns the expected data
70+
* when a valid Form ID is specified.
71+
*
72+
* @since 1.0.0
73+
*
74+
* @return void
75+
*/
76+
public function testGetFormSubscriptions()
77+
{
78+
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID']);
79+
80+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
81+
$result = get_object_vars($result);
82+
$this->assertArrayHasKey('total_subscriptions', $result);
83+
$this->assertArrayHasKey('page', $result);
84+
$this->assertArrayHasKey('total_pages', $result);
85+
$this->assertArrayHasKey('subscriptions', $result);
86+
$this->assertIsArray($result['subscriptions']);
87+
88+
// Assert sort order is ascending.
89+
$this->assertGreaterThanOrEqual(
90+
$result['subscriptions'][0]->created_at,
91+
$result['subscriptions'][1]->created_at
92+
);
93+
}
94+
95+
/**
96+
* Test that get_form_subscriptions() returns the expected data
97+
* when a valid Form ID is specified and the sort order is descending.
98+
*
99+
* @since 1.0.0
100+
*
101+
* @return void
102+
*/
103+
public function testGetFormSubscriptionsWithDescSortOrder()
104+
{
105+
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID'], 'desc');
106+
107+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
108+
$result = get_object_vars($result);
109+
$this->assertArrayHasKey('total_subscriptions', $result);
110+
$this->assertArrayHasKey('page', $result);
111+
$this->assertArrayHasKey('total_pages', $result);
112+
$this->assertArrayHasKey('subscriptions', $result);
113+
$this->assertIsArray($result['subscriptions']);
114+
115+
// Assert sort order.
116+
$this->assertLessThanOrEqual(
117+
$result['subscriptions'][0]->created_at,
118+
$result['subscriptions'][1]->created_at
119+
);
120+
}
121+
122+
/**
123+
* Test that get_form_subscriptions() returns the expected data
124+
* when a valid Form ID is specified and the subscription status
125+
* is cancelled.
126+
*
127+
* @since 1.0.0
128+
*
129+
* @return void
130+
*/
131+
public function testGetFormSubscriptionsWithCancelledSubscriberState()
132+
{
133+
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID'], 'asc', 'cancelled');
134+
135+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
136+
$result = get_object_vars($result);
137+
$this->assertArrayHasKey('total_subscriptions', $result);
138+
$this->assertEquals($result['total_subscriptions'], 0);
139+
$this->assertArrayHasKey('page', $result);
140+
$this->assertArrayHasKey('total_pages', $result);
141+
$this->assertArrayHasKey('subscriptions', $result);
142+
$this->assertIsArray($result['subscriptions']);
143+
}
144+
145+
/**
146+
* Test that get_form_subscriptions() returns the expected data
147+
* when a valid Form ID is specified and the page is set to 2.
148+
*
149+
* @since 1.0.0
150+
*
151+
* @return void
152+
*/
153+
public function testGetFormSubscriptionsWithPage()
154+
{
155+
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID'], 'asc', 'active', 2);
156+
157+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
158+
$result = get_object_vars($result);
159+
$this->assertArrayHasKey('total_subscriptions', $result);
160+
$this->assertArrayHasKey('page', $result);
161+
$this->assertEquals($result['page'], 2);
162+
$this->assertArrayHasKey('total_pages', $result);
163+
$this->assertArrayHasKey('subscriptions', $result);
164+
$this->assertIsArray($result['subscriptions']);
165+
}
166+
167+
/**
168+
* Test that get_form_subscriptions() returns the expected data
169+
* when a valid Form ID is specified.
170+
*
171+
* @since 1.0.0
172+
*
173+
* @return void
174+
*/
175+
public function testGetFormSubscriptionsWithInvalidFormID()
176+
{
177+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
178+
$result = $this->api->get_form_subscriptions(12345);
179+
}
180+
68181
/**
69182
* Test that get_sequences() returns the expected data.
70183
*

0 commit comments

Comments
 (0)