Skip to content

Commit bf449bf

Browse files
committed
Completed tests
1 parent b04f628 commit bf449bf

File tree

1 file changed

+76
-59
lines changed

1 file changed

+76
-59
lines changed

tests/ConvertKitAPITest.php

+76-59
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
* Load .env configuration into $_ENV superglobal, and initialize the API
5665
* class before each test.
@@ -97,6 +106,11 @@ protected function tearDown(): void
97106
foreach ($this->subscriber_ids as $id) {
98107
$this->api->unsubscribe($id);
99108
}
109+
110+
// Delete any Webhooks.
111+
foreach ($this->webhook_ids as $id) {
112+
$this->api->delete_webhook($id);
113+
}
100114
}
101115

102116
/**
@@ -3790,45 +3804,6 @@ public function testDestroyBroadcastWithInvalidBroadcastID()
37903804
$this->api->destroy_broadcast(12345);
37913805
}
37923806

3793-
/**
3794-
* Test that get_webhooks() returns the expected data.
3795-
*
3796-
* @since 2.0.0
3797-
*
3798-
* @return void
3799-
*/
3800-
public function testGetWebhooks()
3801-
{
3802-
$result = $this->api->get_webhooks();
3803-
3804-
// Assert webhooks and pagination exist.
3805-
$this->assertDataExists($result, 'webhooks');
3806-
$this->assertPaginationExists($result);
3807-
}
3808-
3809-
/**
3810-
* Test that get_webhooks() returns the expected data
3811-
* when the total count is included.
3812-
*
3813-
* @since 2.0.0
3814-
*
3815-
* @return void
3816-
*/
3817-
public function testGetWebhooksWithTotalCount()
3818-
{
3819-
$result = $this->api->get_webhooks(
3820-
include_total_count: true
3821-
);
3822-
3823-
// Assert webhooks and pagination exist.
3824-
$this->assertDataExists($result, 'webhooks');
3825-
$this->assertPaginationExists($result);
3826-
3827-
// Assert total count is included.
3828-
$this->assertArrayHasKey('total_count', get_object_vars($result->pagination));
3829-
$this->assertGreaterThan(0, $result->pagination->total_count);
3830-
}
3831-
38323807
/**
38333808
* Test that get_webhooks() returns the expected data
38343809
* when pagination parameters and per_page limits are specified.
@@ -3839,6 +3814,25 @@ public function testGetWebhooksWithTotalCount()
38393814
*/
38403815
public function testGetWebhooksPagination()
38413816
{
3817+
// Create webhooks first.
3818+
$results = [
3819+
$this->api->create_webhook(
3820+
url: 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds'),
3821+
event: 'subscriber.subscriber_activate',
3822+
),
3823+
$this->api->create_webhook(
3824+
url: 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds'),
3825+
event: 'subscriber.subscriber_activate',
3826+
),
3827+
];
3828+
3829+
// Set webhook_ids to ensure webhooks are deleted after test.
3830+
$this->webhook_ids = [
3831+
$results[0]->webhook->id,
3832+
$results[1]->webhook->id,
3833+
];
3834+
3835+
// Get webhooks.
38423836
$result = $this->api->get_webhooks(
38433837
per_page: 1
38443838
);
@@ -3869,7 +3863,7 @@ public function testGetWebhooksPagination()
38693863

38703864
// Assert has_previous_page and has_next_page are correct.
38713865
$this->assertTrue($result->pagination->has_previous_page);
3872-
$this->assertTrue($result->pagination->has_next_page);
3866+
$this->assertFalse($result->pagination->has_next_page);
38733867

38743868
// Use pagination to fetch previous page.
38753869
$result = $this->api->get_webhooks(
@@ -3886,7 +3880,7 @@ public function testGetWebhooksPagination()
38863880
}
38873881

38883882
/**
3889-
* Test that create_webhook() and delete_webhook() works.
3883+
* Test that create_webhook(), get_webhooks() and delete_webhook() works.
38903884
*
38913885
* We do both, so we don't end up with unnecessary webhooks remaining
38923886
* on the ConvertKit account when running tests.
@@ -3895,43 +3889,66 @@ public function testGetWebhooksPagination()
38953889
*
38963890
* @return void
38973891
*/
3898-
public function testCreateAndDeleteWebhook()
3892+
public function testCreateGetAndDeleteWebhook()
38993893
{
39003894
// Create a webhook first.
39013895
$result = $this->api->create_webhook(
3902-
url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb',
3896+
url: 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds'),
39033897
event: 'subscriber.subscriber_activate',
39043898
);
3905-
$ruleID = $result->rule->id;
3899+
$id = $result->webhook->id;
3900+
3901+
// Get webhooks.
3902+
$result = $this->api->get_webhooks();
3903+
3904+
// Assert webhooks and pagination exist.
3905+
$this->assertDataExists($result, 'webhooks');
3906+
$this->assertPaginationExists($result);
3907+
3908+
// Get webhooks including total count.
3909+
$result = $this->api->get_webhooks(
3910+
include_total_count: true
3911+
);
3912+
3913+
// Assert webhooks and pagination exist.
3914+
$this->assertDataExists($result, 'webhooks');
3915+
$this->assertPaginationExists($result);
3916+
3917+
// Assert total count is included.
3918+
$this->assertArrayHasKey('total_count', get_object_vars($result->pagination));
3919+
$this->assertGreaterThan(0, $result->pagination->total_count);
39063920

39073921
// Delete the webhook.
3908-
$result = $this->api->delete_webhook($ruleID);
3909-
$this->assertEquals($result->success, true);
3922+
$result = $this->api->delete_webhook($id);
39103923
}
39113924

39123925
/**
3913-
* Test that create_webhook() and delete_webhook() works with an event parameter.
3914-
*
3915-
* We do both, so we don't end up with unnecessary webhooks remaining
3916-
* on the ConvertKit account when running tests.
3926+
* Test that create_webhook() works with an event parameter.
39173927
*
39183928
* @since 1.0.0
39193929
*
39203930
* @return void
39213931
*/
3922-
public function testCreateAndDeleteWebhookWithEventParameter()
3932+
public function testCreateWebhookWithEventParameter()
39233933
{
3924-
// Create a webhook first.
3934+
// Create a webhook.
3935+
$url = 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds');
39253936
$result = $this->api->create_webhook(
3926-
url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb',
3937+
url: $url,
39273938
event: 'subscriber.form_subscribe',
39283939
parameter: $_ENV['CONVERTKIT_API_FORM_ID']
39293940
);
3930-
$ruleID = $result->rule->id;
3941+
3942+
// Confirm webhook created with correct data.
3943+
$this->assertArrayHasKey('webhook', get_object_vars($result));
3944+
$this->assertArrayHasKey('id', get_object_vars($result->webhook));
3945+
$this->assertArrayHasKey('target_url', get_object_vars($result->webhook));
3946+
$this->assertEquals($result->webhook->target_url, $url);
3947+
$this->assertEquals($result->webhook->event->name, 'form_subscribe');
3948+
$this->assertEquals($result->webhook->event->form_id, $_ENV['CONVERTKIT_API_FORM_ID']);
39313949

39323950
// Delete the webhook.
3933-
$result = $this->api->delete_webhook($ruleID);
3934-
$this->assertEquals($result->success, true);
3951+
$result = $this->api->delete_webhook($result->webhook->id);
39353952
}
39363953

39373954
/**
@@ -3946,20 +3963,20 @@ public function testCreateWebhookWithInvalidEvent()
39463963
{
39473964
$this->expectException(InvalidArgumentException::class);
39483965
$this->api->create_webhook(
3949-
url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb',
3966+
url: 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds'),
39503967
event: 'invalid.event'
39513968
);
39523969
}
39533970

39543971
/**
39553972
* Test that delete_webhook() throws a ClientException when an invalid
3956-
* rule ID is specified.
3973+
* ID is specified.
39573974
*
39583975
* @since 1.0.0
39593976
*
39603977
* @return void
39613978
*/
3962-
public function testsDeleteWebhookWithInvalidRuleID()
3979+
public function testDeleteWebhookWithInvalidID()
39633980
{
39643981
$this->expectException(ClientException::class);
39653982
$this->api->delete_webhook(12345);

0 commit comments

Comments
 (0)