Skip to content

Commit 4cd3ba4

Browse files
committed
Merge branch '1.0-beta' into get-forms-landing-pages
# Conflicts: # src/ConvertKit_API.php # tests/ConvertKitAPITest.php
2 parents f211633 + 85e0d53 commit 4cd3ba4

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
@@ -174,6 +174,35 @@ public function get_landing_pages()
174174
return $this->get_resources('landing_pages');
175175
}
176176

177+
/**
178+
* List subscriptions to a form
179+
*
180+
* @param integer $form_id Form ID.
181+
* @param string $sort_order Sort Order (asc|desc).
182+
* @param string $subscriber_state Subscriber State (active,cancelled).
183+
* @param integer $page Page.
184+
*
185+
* @see https://developers.convertkit.com/#list-subscriptions-to-a-form
186+
*
187+
* @return false|mixed
188+
*/
189+
public function get_form_subscriptions(
190+
int $form_id,
191+
string $sort_order = 'asc',
192+
string $subscriber_state = 'active',
193+
int $page = 1
194+
) {
195+
return $this->get(
196+
sprintf('forms/%s/subscriptions', $form_id),
197+
[
198+
'api_secret' => $this->api_secret,
199+
'sort_order' => $sort_order,
200+
'subscriber_state' => $subscriber_state,
201+
'page' => $page,
202+
]
203+
);
204+
}
205+
177206
/**
178207
* Gets all sequences
179208
*

tests/ConvertKitAPITest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,119 @@ public function testGetLandingPages()
114114
$this->assertArrayHasKey('archived', $landingPage);
115115
}
116116

117+
/**
118+
* Test that get_form_subscriptions() returns the expected data
119+
* when a valid Form ID is specified.
120+
*
121+
* @since 1.0.0
122+
*
123+
* @return void
124+
*/
125+
public function testGetFormSubscriptions()
126+
{
127+
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID']);
128+
129+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
130+
$result = get_object_vars($result);
131+
$this->assertArrayHasKey('total_subscriptions', $result);
132+
$this->assertArrayHasKey('page', $result);
133+
$this->assertArrayHasKey('total_pages', $result);
134+
$this->assertArrayHasKey('subscriptions', $result);
135+
$this->assertIsArray($result['subscriptions']);
136+
137+
// Assert sort order is ascending.
138+
$this->assertGreaterThanOrEqual(
139+
$result['subscriptions'][0]->created_at,
140+
$result['subscriptions'][1]->created_at
141+
);
142+
}
143+
144+
/**
145+
* Test that get_form_subscriptions() returns the expected data
146+
* when a valid Form ID is specified and the sort order is descending.
147+
*
148+
* @since 1.0.0
149+
*
150+
* @return void
151+
*/
152+
public function testGetFormSubscriptionsWithDescSortOrder()
153+
{
154+
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID'], 'desc');
155+
156+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
157+
$result = get_object_vars($result);
158+
$this->assertArrayHasKey('total_subscriptions', $result);
159+
$this->assertArrayHasKey('page', $result);
160+
$this->assertArrayHasKey('total_pages', $result);
161+
$this->assertArrayHasKey('subscriptions', $result);
162+
$this->assertIsArray($result['subscriptions']);
163+
164+
// Assert sort order.
165+
$this->assertLessThanOrEqual(
166+
$result['subscriptions'][0]->created_at,
167+
$result['subscriptions'][1]->created_at
168+
);
169+
}
170+
171+
/**
172+
* Test that get_form_subscriptions() returns the expected data
173+
* when a valid Form ID is specified and the subscription status
174+
* is cancelled.
175+
*
176+
* @since 1.0.0
177+
*
178+
* @return void
179+
*/
180+
public function testGetFormSubscriptionsWithCancelledSubscriberState()
181+
{
182+
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID'], 'asc', 'cancelled');
183+
184+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
185+
$result = get_object_vars($result);
186+
$this->assertArrayHasKey('total_subscriptions', $result);
187+
$this->assertEquals($result['total_subscriptions'], 0);
188+
$this->assertArrayHasKey('page', $result);
189+
$this->assertArrayHasKey('total_pages', $result);
190+
$this->assertArrayHasKey('subscriptions', $result);
191+
$this->assertIsArray($result['subscriptions']);
192+
}
193+
194+
/**
195+
* Test that get_form_subscriptions() returns the expected data
196+
* when a valid Form ID is specified and the page is set to 2.
197+
*
198+
* @since 1.0.0
199+
*
200+
* @return void
201+
*/
202+
public function testGetFormSubscriptionsWithPage()
203+
{
204+
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID'], 'asc', 'active', 2);
205+
206+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
207+
$result = get_object_vars($result);
208+
$this->assertArrayHasKey('total_subscriptions', $result);
209+
$this->assertArrayHasKey('page', $result);
210+
$this->assertEquals($result['page'], 2);
211+
$this->assertArrayHasKey('total_pages', $result);
212+
$this->assertArrayHasKey('subscriptions', $result);
213+
$this->assertIsArray($result['subscriptions']);
214+
}
215+
216+
/**
217+
* Test that get_form_subscriptions() returns the expected data
218+
* when a valid Form ID is specified.
219+
*
220+
* @since 1.0.0
221+
*
222+
* @return void
223+
*/
224+
public function testGetFormSubscriptionsWithInvalidFormID()
225+
{
226+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
227+
$result = $this->api->get_form_subscriptions(12345);
228+
}
229+
117230
/**
118231
* Test that get_sequences() returns the expected data.
119232
*

0 commit comments

Comments
 (0)