Skip to content

Commit 0abfdc3

Browse files
committed
Merge remote-tracking branch 'origin/v4-api-oauth' into v4-api-improve-subscriber-tests
2 parents 3803eb8 + dba401b commit 0abfdc3

File tree

2 files changed

+175
-37
lines changed

2 files changed

+175
-37
lines changed

src/ConvertKit_API.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,38 @@ public function delete_broadcast(int $id)
15221522
return $this->delete(sprintf('broadcasts/%s', $id));
15231523
}
15241524

1525+
/**
1526+
* List webhooks.
1527+
*
1528+
* @param boolean $include_total_count To include the total count of records in the response, use true.
1529+
* @param string $after_cursor Return results after the given pagination cursor.
1530+
* @param string $before_cursor Return results before the given pagination cursor.
1531+
* @param integer $per_page Number of results to return.
1532+
*
1533+
* @since 2.0.0
1534+
*
1535+
* @see https://developers.convertkit.com/v4.html#list-webhooks
1536+
*
1537+
* @return false|mixed
1538+
*/
1539+
public function get_webhooks(
1540+
bool $include_total_count = false,
1541+
string $after_cursor = '',
1542+
string $before_cursor = '',
1543+
int $per_page = 100
1544+
) {
1545+
// Send request.
1546+
return $this->get(
1547+
endpoint: 'webhooks',
1548+
args: $this->build_total_count_and_pagination_params(
1549+
include_total_count: $include_total_count,
1550+
after_cursor: $after_cursor,
1551+
before_cursor: $before_cursor,
1552+
per_page: $per_page
1553+
)
1554+
);
1555+
}
1556+
15251557
/**
15261558
* Creates a webhook that will be called based on the chosen event types.
15271559
*
@@ -1531,7 +1563,7 @@ public function delete_broadcast(int $id)
15311563
*
15321564
* @since 1.0.0
15331565
*
1534-
* @see https://developers.convertkit.com/#create-a-webhook
1566+
* @see https://developers.convertkit.com/v4.html#create-a-webhook
15351567
*
15361568
* @throws \InvalidArgumentException If the event is not supported.
15371569
*
@@ -1543,6 +1575,8 @@ public function create_webhook(string $url, string $event, string $parameter = '
15431575
switch ($event) {
15441576
case 'subscriber.subscriber_activate':
15451577
case 'subscriber.subscriber_unsubscribe':
1578+
case 'subscriber.subscriber_bounce':
1579+
case 'subscriber.subscriber_complain':
15461580
case 'purchase.purchase_create':
15471581
$eventData = ['name' => $event];
15481582
break;
@@ -1590,7 +1624,7 @@ public function create_webhook(string $url, string $event, string $parameter = '
15901624

15911625
// Send request.
15921626
return $this->post(
1593-
'automations/hooks',
1627+
'webhooks',
15941628
[
15951629
'target_url' => $url,
15961630
'event' => $eventData,
@@ -1601,17 +1635,17 @@ public function create_webhook(string $url, string $event, string $parameter = '
16011635
/**
16021636
* Deletes an existing webhook.
16031637
*
1604-
* @param integer $rule_id Rule ID.
1638+
* @param integer $id Webhook ID.
16051639
*
16061640
* @since 1.0.0
16071641
*
1608-
* @see https://developers.convertkit.com/#destroy-webhook
1642+
* @see https://developers.convertkit.com/v4.html#delete-a-webhook
16091643
*
16101644
* @return false|object
16111645
*/
1612-
public function destroy_webhook(int $rule_id)
1646+
public function delete_webhook(int $id)
16131647
{
1614-
return $this->delete(sprintf('automations/hooks/%s', $rule_id));
1648+
return $this->delete(sprintf('webhooks/%s', $id));
16151649
}
16161650

16171651
/**

tests/ConvertKitAPITest.php

Lines changed: 135 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ class ConvertKitAPITest extends TestCase
5151
*/
5252
protected $subscriber_ids = [];
5353

54+
/**
55+
* Webhook IDs to delete on teardown of a test.
56+
*
57+
* @since 2.0.0
58+
*
59+
* @var array<int, int>
60+
*/
61+
protected $webhook_ids = [];
62+
5463
/**
5564
* Broadcast IDs to delete on teardown of a test.
5665
*
@@ -107,6 +116,11 @@ protected function tearDown(): void
107116
$this->api->unsubscribe($id);
108117
}
109118

119+
// Delete any Webhooks.
120+
foreach ($this->webhook_ids as $id) {
121+
$this->api->delete_webhook($id);
122+
}
123+
110124
// Delete any Broadcasts.
111125
foreach ($this->broadcast_ids as $id) {
112126
$this->api->delete_broadcast($id);
@@ -3886,7 +3900,82 @@ public function testDeleteBroadcastWithInvalidBroadcastID()
38863900
}
38873901

38883902
/**
3889-
* Test that create_webhook() and destroy_webhook() works.
3903+
* Test that get_webhooks() returns the expected data
3904+
* when pagination parameters and per_page limits are specified.
3905+
*
3906+
* @since 2.0.0
3907+
*
3908+
* @return void
3909+
*/
3910+
public function testGetWebhooksPagination()
3911+
{
3912+
// Create webhooks first.
3913+
$results = [
3914+
$this->api->create_webhook(
3915+
url: 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds'),
3916+
event: 'subscriber.subscriber_activate',
3917+
),
3918+
$this->api->create_webhook(
3919+
url: 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds'),
3920+
event: 'subscriber.subscriber_activate',
3921+
),
3922+
];
3923+
3924+
// Set webhook_ids to ensure webhooks are deleted after test.
3925+
$this->webhook_ids = [
3926+
$results[0]->webhook->id,
3927+
$results[1]->webhook->id,
3928+
];
3929+
3930+
// Get webhooks.
3931+
$result = $this->api->get_webhooks(
3932+
per_page: 1
3933+
);
3934+
3935+
// Assert webhooks and pagination exist.
3936+
$this->assertDataExists($result, 'webhooks');
3937+
$this->assertPaginationExists($result);
3938+
3939+
// Assert a single webhook was returned.
3940+
$this->assertCount(1, $result->webhooks);
3941+
3942+
// Assert has_previous_page and has_next_page are correct.
3943+
$this->assertFalse($result->pagination->has_previous_page);
3944+
$this->assertTrue($result->pagination->has_next_page);
3945+
3946+
// Use pagination to fetch next page.
3947+
$result = $this->api->get_webhooks(
3948+
per_page: 1,
3949+
after_cursor: $result->pagination->end_cursor
3950+
);
3951+
3952+
// Assert webhooks and pagination exist.
3953+
$this->assertDataExists($result, 'webhooks');
3954+
$this->assertPaginationExists($result);
3955+
3956+
// Assert a single webhook was returned.
3957+
$this->assertCount(1, $result->webhooks);
3958+
3959+
// Assert has_previous_page and has_next_page are correct.
3960+
$this->assertTrue($result->pagination->has_previous_page);
3961+
$this->assertFalse($result->pagination->has_next_page);
3962+
3963+
// Use pagination to fetch previous page.
3964+
$result = $this->api->get_webhooks(
3965+
per_page: 1,
3966+
before_cursor: $result->pagination->start_cursor
3967+
);
3968+
3969+
// Assert webhooks and pagination exist.
3970+
$this->assertDataExists($result, 'webhooks');
3971+
$this->assertPaginationExists($result);
3972+
3973+
// Assert a single webhook was returned.
3974+
$this->assertCount(1, $result->webhooks);
3975+
}
3976+
3977+
/**
3978+
* Test that create_webhook(), get_webhooks() and delete_webhook() works.
38903979
*
38913980
* We do both, so we don't end up with unnecessary webhooks remaining
38923981
* on the ConvertKit account when running tests.
@@ -3895,47 +3984,66 @@ public function testDeleteBroadcastWithInvalidBroadcastID()
38953984
*
38963985
* @return void
38973986
*/
3898-
public function testCreateAndDestroyWebhook()
3987+
public function testCreateGetAndDeleteWebhook()
38993988
{
3900-
$this->markTestIncomplete();
3901-
39023989
// Create a webhook first.
39033990
$result = $this->api->create_webhook(
3904-
url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb',
3991+
url: 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds'),
39053992
event: 'subscriber.subscriber_activate',
39063993
);
3907-
$ruleID = $result->rule->id;
3994+
$id = $result->webhook->id;
3995+
3996+
// Get webhooks.
3997+
$result = $this->api->get_webhooks();
39083998

3909-
// Destroy the webhook.
3910-
$result = $this->api->destroy_webhook($ruleID);
3911-
$this->assertEquals($result->success, true);
3999+
// Assert webhooks and pagination exist.
4000+
$this->assertDataExists($result, 'webhooks');
4001+
$this->assertPaginationExists($result);
4002+
4003+
// Get webhooks including total count.
4004+
$result = $this->api->get_webhooks(
4005+
include_total_count: true
4006+
);
4007+
4008+
// Assert webhooks and pagination exist.
4009+
$this->assertDataExists($result, 'webhooks');
4010+
$this->assertPaginationExists($result);
4011+
4012+
// Assert total count is included.
4013+
$this->assertArrayHasKey('total_count', get_object_vars($result->pagination));
4014+
$this->assertGreaterThan(0, $result->pagination->total_count);
4015+
4016+
// Delete the webhook.
4017+
$result = $this->api->delete_webhook($id);
39124018
}
39134019

39144020
/**
3915-
* Test that create_webhook() and destroy_webhook() works with an event parameter.
3916-
*
3917-
* We do both, so we don't end up with unnecessary webhooks remaining
3918-
* on the ConvertKit account when running tests.
4021+
* Test that create_webhook() works with an event parameter.
39194022
*
39204023
* @since 1.0.0
39214024
*
39224025
* @return void
39234026
*/
3924-
public function testCreateAndDestroyWebhookWithEventParameter()
4027+
public function testCreateWebhookWithEventParameter()
39254028
{
3926-
$this->markTestIncomplete();
3927-
3928-
// Create a webhook first.
4029+
// Create a webhook.
4030+
$url = 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds');
39294031
$result = $this->api->create_webhook(
3930-
url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb',
4032+
url: $url,
39314033
event: 'subscriber.form_subscribe',
39324034
parameter: $_ENV['CONVERTKIT_API_FORM_ID']
39334035
);
3934-
$ruleID = $result->rule->id;
39354036

3936-
// Destroy the webhook.
3937-
$result = $this->api->destroy_webhook($ruleID);
3938-
$this->assertEquals($result->success, true);
4037+
// Confirm webhook created with correct data.
4038+
$this->assertArrayHasKey('webhook', get_object_vars($result));
4039+
$this->assertArrayHasKey('id', get_object_vars($result->webhook));
4040+
$this->assertArrayHasKey('target_url', get_object_vars($result->webhook));
4041+
$this->assertEquals($result->webhook->target_url, $url);
4042+
$this->assertEquals($result->webhook->event->name, 'form_subscribe');
4043+
$this->assertEquals($result->webhook->event->form_id, $_ENV['CONVERTKIT_API_FORM_ID']);
4044+
4045+
// Delete the webhook.
4046+
$result = $this->api->delete_webhook($result->webhook->id);
39394047
}
39404048

39414049
/**
@@ -3948,29 +4056,25 @@ public function testCreateAndDestroyWebhookWithEventParameter()
39484056
*/
39494057
public function testCreateWebhookWithInvalidEvent()
39504058
{
3951-
$this->markTestIncomplete();
3952-
39534059
$this->expectException(InvalidArgumentException::class);
39544060
$this->api->create_webhook(
3955-
url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb',
4061+
url: 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds'),
39564062
event: 'invalid.event'
39574063
);
39584064
}
39594065

39604066
/**
3961-
* Test that destroy_webhook() throws a ClientException when an invalid
3962-
* rule ID is specified.
4067+
* Test that delete_webhook() throws a ClientException when an invalid
4068+
* ID is specified.
39634069
*
39644070
* @since 1.0.0
39654071
*
39664072
* @return void
39674073
*/
3968-
public function testDestroyWebhookWithInvalidRuleID()
4074+
public function testDeleteWebhookWithInvalidID()
39694075
{
3970-
$this->markTestIncomplete();
3971-
39724076
$this->expectException(ClientException::class);
3973-
$this->api->destroy_webhook(12345);
4077+
$this->api->delete_webhook(12345);
39744078
}
39754079

39764080
/**

0 commit comments

Comments
 (0)