Skip to content

Commit 663f84f

Browse files
committed
Merge branch '1.0-beta' into sequence-functions
2 parents 3f5eff7 + 3cc8333 commit 663f84f

File tree

2 files changed

+225
-5
lines changed

2 files changed

+225
-5
lines changed

src/ConvertKit_API.php

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

153+
/**
154+
* Gets all forms.
155+
*
156+
* @since 1.0.0
157+
*
158+
* @return false|mixed
159+
*/
160+
public function get_forms()
161+
{
162+
return $this->get_resources('forms');
163+
}
164+
165+
/**
166+
* Gets all landing pages.
167+
*
168+
* @since 1.0.0
169+
*
170+
* @return false|mixed
171+
*/
172+
public function get_landing_pages()
173+
{
174+
return $this->get_resources('landing_pages');
175+
}
176+
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+
153206
/**
154207
* Gets all sequences
155208
*
@@ -302,9 +355,7 @@ public function get_resources(string $resource)
302355
$resources = $this->get(
303356
$request,
304357
[
305-
'api_key' => $this->api_key,
306-
'timeout' => 10,
307-
'Accept-Encoding' => 'gzip',
358+
'api_key' => $this->api_key,
308359
]
309360
);
310361

@@ -327,12 +378,18 @@ public function get_resources(string $resource)
327378
return [];
328379
}
329380

330-
// Exclude archived forms.
381+
// Build array of forms.
331382
foreach ($resources->forms as $form) {
383+
// Exclude archived forms.
332384
if (isset($form->archived) && $form->archived) {
333385
continue;
334386
}
335387

388+
// Exclude hosted forms.
389+
if ($form->type === 'hosted') {
390+
continue;
391+
}
392+
336393
$_resource[] = $form;
337394
}
338395
break;
@@ -345,12 +402,13 @@ public function get_resources(string $resource)
345402
return [];
346403
}
347404

348-
// Exclude forms and archived forms/landing pages.
349405
foreach ($resources->forms as $form) {
406+
// Exclude archived landing pages.
350407
if (isset($form->archived) && $form->archived) {
351408
continue;
352409
}
353410

411+
// Exclude non-hosted (i.e. forms).
354412
if ($form->type !== 'hosted') {
355413
continue;
356414
}

tests/ConvertKitAPITest.php

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

68+
/**
69+
* Test that get_forms() returns the expected data.
70+
*
71+
* @since 1.0.0
72+
*
73+
* @return void
74+
*/
75+
public function testGetForms()
76+
{
77+
$result = $this->api->get_forms();
78+
$this->assertIsArray($result);
79+
80+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
81+
$form = get_object_vars($result[0]);
82+
$this->assertArrayHasKey('id', $form);
83+
$this->assertArrayHasKey('name', $form);
84+
$this->assertArrayHasKey('created_at', $form);
85+
$this->assertArrayHasKey('type', $form);
86+
$this->assertArrayHasKey('format', $form);
87+
$this->assertArrayHasKey('embed_js', $form);
88+
$this->assertArrayHasKey('embed_url', $form);
89+
$this->assertArrayHasKey('archived', $form);
90+
}
91+
92+
/**
93+
* Test that get_landing_pages() returns the expected data.
94+
*
95+
* @since 1.0.0
96+
*
97+
* @return void
98+
*/
99+
public function testGetLandingPages()
100+
{
101+
$result = $this->api->get_landing_pages();
102+
$this->assertIsArray($result);
103+
104+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
105+
$landingPage = get_object_vars($result[0]);
106+
$this->assertArrayHasKey('id', $landingPage);
107+
$this->assertArrayHasKey('name', $landingPage);
108+
$this->assertArrayHasKey('created_at', $landingPage);
109+
$this->assertArrayHasKey('type', $landingPage);
110+
$this->assertEquals('hosted', $landingPage['type']);
111+
$this->assertArrayHasKey('format', $landingPage);
112+
$this->assertArrayHasKey('embed_js', $landingPage);
113+
$this->assertArrayHasKey('embed_url', $landingPage);
114+
$this->assertArrayHasKey('archived', $landingPage);
115+
}
116+
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+
68230
/**
69231
* Test that get_sequences() returns the expected data.
70232
*

0 commit comments

Comments
 (0)