@@ -51,6 +51,15 @@ class ConvertKitAPITest extends TestCase
51
51
*/
52
52
protected $ subscriber_ids = [];
53
53
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
+
54
63
/**
55
64
* Broadcast IDs to delete on teardown of a test.
56
65
*
@@ -107,6 +116,11 @@ protected function tearDown(): void
107
116
$ this ->api ->unsubscribe ($ id );
108
117
}
109
118
119
+ // Delete any Webhooks.
120
+ foreach ($ this ->webhook_ids as $ id ) {
121
+ $ this ->api ->delete_webhook ($ id );
122
+ }
123
+
110
124
// Delete any Broadcasts.
111
125
foreach ($ this ->broadcast_ids as $ id ) {
112
126
$ this ->api ->delete_broadcast ($ id );
@@ -3846,7 +3860,82 @@ public function testDeleteBroadcastWithInvalidBroadcastID()
3846
3860
}
3847
3861
3848
3862
/**
3849
- * Test that create_webhook() and destroy_webhook() works.
3863
+ * Test that get_webhooks() returns the expected data
3864
+ * when pagination parameters and per_page limits are specified.
3865
+ *
3866
+ * @since 2.0.0
3867
+ *
3868
+ * @return void
3869
+ */
3870
+ public function testGetWebhooksPagination ()
3871
+ {
3872
+ // Create webhooks first.
3873
+ $ results = [
3874
+ $ this ->api ->create_webhook (
3875
+ url: 'https://webhook.site/ ' . str_shuffle ('wfervdrtgsdewrafvwefds ' ),
3876
+ event: 'subscriber.subscriber_activate ' ,
3877
+ ),
3878
+ $ this ->api ->create_webhook (
3879
+ url: 'https://webhook.site/ ' . str_shuffle ('wfervdrtgsdewrafvwefds ' ),
3880
+ event: 'subscriber.subscriber_activate ' ,
3881
+ ),
3882
+ ];
3883
+
3884
+ // Set webhook_ids to ensure webhooks are deleted after test.
3885
+ $ this ->webhook_ids = [
3886
+ $ results [0 ]->webhook ->id ,
3887
+ $ results [1 ]->webhook ->id ,
3888
+ ];
3889
+
3890
+ // Get webhooks.
3891
+ $ result = $ this ->api ->get_webhooks (
3892
+ per_page: 1
3893
+ );
3894
+
3895
+ // Assert webhooks and pagination exist.
3896
+ $ this ->assertDataExists ($ result , 'webhooks ' );
3897
+ $ this ->assertPaginationExists ($ result );
3898
+
3899
+ // Assert a single webhook was returned.
3900
+ $ this ->assertCount (1 , $ result ->webhooks );
3901
+
3902
+ // Assert has_previous_page and has_next_page are correct.
3903
+ $ this ->assertFalse ($ result ->pagination ->has_previous_page );
3904
+ $ this ->assertTrue ($ result ->pagination ->has_next_page );
3905
+
3906
+ // Use pagination to fetch next page.
3907
+ $ result = $ this ->api ->get_webhooks (
3908
+ per_page: 1 ,
3909
+ after_cursor: $ result ->pagination ->end_cursor
3910
+ );
3911
+
3912
+ // Assert webhooks and pagination exist.
3913
+ $ this ->assertDataExists ($ result , 'webhooks ' );
3914
+ $ this ->assertPaginationExists ($ result );
3915
+
3916
+ // Assert a single webhook was returned.
3917
+ $ this ->assertCount (1 , $ result ->webhooks );
3918
+
3919
+ // Assert has_previous_page and has_next_page are correct.
3920
+ $ this ->assertTrue ($ result ->pagination ->has_previous_page );
3921
+ $ this ->assertFalse ($ result ->pagination ->has_next_page );
3922
+
3923
+ // Use pagination to fetch previous page.
3924
+ $ result = $ this ->api ->get_webhooks (
3925
+ per_page: 1 ,
3926
+ before_cursor: $ result ->pagination ->start_cursor
3927
+ );
3928
+
3929
+ // Assert webhooks and pagination exist.
3930
+ $ this ->assertDataExists ($ result , 'webhooks ' );
3931
+ $ this ->assertPaginationExists ($ result );
3932
+
3933
+ // Assert a single webhook was returned.
3934
+ $ this ->assertCount (1 , $ result ->webhooks );
3935
+ }
3936
+
3937
+ /**
3938
+ * Test that create_webhook(), get_webhooks() and delete_webhook() works.
3850
3939
*
3851
3940
* We do both, so we don't end up with unnecessary webhooks remaining
3852
3941
* on the ConvertKit account when running tests.
@@ -3855,47 +3944,66 @@ public function testDeleteBroadcastWithInvalidBroadcastID()
3855
3944
*
3856
3945
* @return void
3857
3946
*/
3858
- public function testCreateAndDestroyWebhook ()
3947
+ public function testCreateGetAndDeleteWebhook ()
3859
3948
{
3860
- $ this ->markTestIncomplete ();
3861
-
3862
3949
// Create a webhook first.
3863
3950
$ result = $ this ->api ->create_webhook (
3864
- url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb ' ,
3951
+ url: 'https://webhook.site/ ' . str_shuffle ( ' wfervdrtgsdewrafvwefds ' ) ,
3865
3952
event: 'subscriber.subscriber_activate ' ,
3866
3953
);
3867
- $ ruleID = $ result ->rule ->id ;
3954
+ $ id = $ result ->webhook ->id ;
3955
+
3956
+ // Get webhooks.
3957
+ $ result = $ this ->api ->get_webhooks ();
3868
3958
3869
- // Destroy the webhook.
3870
- $ result = $ this ->api ->destroy_webhook ($ ruleID );
3871
- $ this ->assertEquals ($ result ->success , true );
3959
+ // Assert webhooks and pagination exist.
3960
+ $ this ->assertDataExists ($ result , 'webhooks ' );
3961
+ $ this ->assertPaginationExists ($ result );
3962
+
3963
+ // Get webhooks including total count.
3964
+ $ result = $ this ->api ->get_webhooks (
3965
+ include_total_count: true
3966
+ );
3967
+
3968
+ // Assert webhooks and pagination exist.
3969
+ $ this ->assertDataExists ($ result , 'webhooks ' );
3970
+ $ this ->assertPaginationExists ($ result );
3971
+
3972
+ // Assert total count is included.
3973
+ $ this ->assertArrayHasKey ('total_count ' , get_object_vars ($ result ->pagination ));
3974
+ $ this ->assertGreaterThan (0 , $ result ->pagination ->total_count );
3975
+
3976
+ // Delete the webhook.
3977
+ $ result = $ this ->api ->delete_webhook ($ id );
3872
3978
}
3873
3979
3874
3980
/**
3875
- * Test that create_webhook() and destroy_webhook() works with an event parameter.
3876
- *
3877
- * We do both, so we don't end up with unnecessary webhooks remaining
3878
- * on the ConvertKit account when running tests.
3981
+ * Test that create_webhook() works with an event parameter.
3879
3982
*
3880
3983
* @since 1.0.0
3881
3984
*
3882
3985
* @return void
3883
3986
*/
3884
- public function testCreateAndDestroyWebhookWithEventParameter ()
3987
+ public function testCreateWebhookWithEventParameter ()
3885
3988
{
3886
- $ this ->markTestIncomplete ();
3887
-
3888
- // Create a webhook first.
3989
+ // Create a webhook.
3990
+ $ url = 'https://webhook.site/ ' . str_shuffle ('wfervdrtgsdewrafvwefds ' );
3889
3991
$ result = $ this ->api ->create_webhook (
3890
- url: ' https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb ' ,
3992
+ url: $ url ,
3891
3993
event: 'subscriber.form_subscribe ' ,
3892
3994
parameter: $ _ENV ['CONVERTKIT_API_FORM_ID ' ]
3893
3995
);
3894
- $ ruleID = $ result ->rule ->id ;
3895
3996
3896
- // Destroy the webhook.
3897
- $ result = $ this ->api ->destroy_webhook ($ ruleID );
3898
- $ this ->assertEquals ($ result ->success , true );
3997
+ // Confirm webhook created with correct data.
3998
+ $ this ->assertArrayHasKey ('webhook ' , get_object_vars ($ result ));
3999
+ $ this ->assertArrayHasKey ('id ' , get_object_vars ($ result ->webhook ));
4000
+ $ this ->assertArrayHasKey ('target_url ' , get_object_vars ($ result ->webhook ));
4001
+ $ this ->assertEquals ($ result ->webhook ->target_url , $ url );
4002
+ $ this ->assertEquals ($ result ->webhook ->event ->name , 'form_subscribe ' );
4003
+ $ this ->assertEquals ($ result ->webhook ->event ->form_id , $ _ENV ['CONVERTKIT_API_FORM_ID ' ]);
4004
+
4005
+ // Delete the webhook.
4006
+ $ result = $ this ->api ->delete_webhook ($ result ->webhook ->id );
3899
4007
}
3900
4008
3901
4009
/**
@@ -3908,29 +4016,25 @@ public function testCreateAndDestroyWebhookWithEventParameter()
3908
4016
*/
3909
4017
public function testCreateWebhookWithInvalidEvent ()
3910
4018
{
3911
- $ this ->markTestIncomplete ();
3912
-
3913
4019
$ this ->expectException (InvalidArgumentException::class);
3914
4020
$ this ->api ->create_webhook (
3915
- url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb ' ,
4021
+ url: 'https://webhook.site/ ' . str_shuffle ( ' wfervdrtgsdewrafvwefds ' ) ,
3916
4022
event: 'invalid.event '
3917
4023
);
3918
4024
}
3919
4025
3920
4026
/**
3921
- * Test that destroy_webhook () throws a ClientException when an invalid
3922
- * rule ID is specified.
4027
+ * Test that delete_webhook () throws a ClientException when an invalid
4028
+ * ID is specified.
3923
4029
*
3924
4030
* @since 1.0.0
3925
4031
*
3926
4032
* @return void
3927
4033
*/
3928
- public function testDestroyWebhookWithInvalidRuleID ()
4034
+ public function testDeleteWebhookWithInvalidID ()
3929
4035
{
3930
- $ this ->markTestIncomplete ();
3931
-
3932
4036
$ this ->expectException (ClientException::class);
3933
- $ this ->api ->destroy_webhook (12345 );
4037
+ $ this ->api ->delete_webhook (12345 );
3934
4038
}
3935
4039
3936
4040
/**
0 commit comments