@@ -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 );
@@ -3886,7 +3900,82 @@ public function testDeleteBroadcastWithInvalidBroadcastID()
3886
3900
}
3887
3901
3888
3902
/**
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.
3890
3979
*
3891
3980
* We do both, so we don't end up with unnecessary webhooks remaining
3892
3981
* on the ConvertKit account when running tests.
@@ -3895,47 +3984,66 @@ public function testDeleteBroadcastWithInvalidBroadcastID()
3895
3984
*
3896
3985
* @return void
3897
3986
*/
3898
- public function testCreateAndDestroyWebhook ()
3987
+ public function testCreateGetAndDeleteWebhook ()
3899
3988
{
3900
- $ this ->markTestIncomplete ();
3901
-
3902
3989
// Create a webhook first.
3903
3990
$ result = $ this ->api ->create_webhook (
3904
- url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb ' ,
3991
+ url: 'https://webhook.site/ ' . str_shuffle ( ' wfervdrtgsdewrafvwefds ' ) ,
3905
3992
event: 'subscriber.subscriber_activate ' ,
3906
3993
);
3907
- $ ruleID = $ result ->rule ->id ;
3994
+ $ id = $ result ->webhook ->id ;
3995
+
3996
+ // Get webhooks.
3997
+ $ result = $ this ->api ->get_webhooks ();
3908
3998
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 );
3912
4018
}
3913
4019
3914
4020
/**
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.
3919
4022
*
3920
4023
* @since 1.0.0
3921
4024
*
3922
4025
* @return void
3923
4026
*/
3924
- public function testCreateAndDestroyWebhookWithEventParameter ()
4027
+ public function testCreateWebhookWithEventParameter ()
3925
4028
{
3926
- $ this ->markTestIncomplete ();
3927
-
3928
- // Create a webhook first.
4029
+ // Create a webhook.
4030
+ $ url = 'https://webhook.site/ ' . str_shuffle ('wfervdrtgsdewrafvwefds ' );
3929
4031
$ result = $ this ->api ->create_webhook (
3930
- url: ' https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb ' ,
4032
+ url: $ url ,
3931
4033
event: 'subscriber.form_subscribe ' ,
3932
4034
parameter: $ _ENV ['CONVERTKIT_API_FORM_ID ' ]
3933
4035
);
3934
- $ ruleID = $ result ->rule ->id ;
3935
4036
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 );
3939
4047
}
3940
4048
3941
4049
/**
@@ -3948,29 +4056,25 @@ public function testCreateAndDestroyWebhookWithEventParameter()
3948
4056
*/
3949
4057
public function testCreateWebhookWithInvalidEvent ()
3950
4058
{
3951
- $ this ->markTestIncomplete ();
3952
-
3953
4059
$ this ->expectException (InvalidArgumentException::class);
3954
4060
$ this ->api ->create_webhook (
3955
- url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb ' ,
4061
+ url: 'https://webhook.site/ ' . str_shuffle ( ' wfervdrtgsdewrafvwefds ' ) ,
3956
4062
event: 'invalid.event '
3957
4063
);
3958
4064
}
3959
4065
3960
4066
/**
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.
3963
4069
*
3964
4070
* @since 1.0.0
3965
4071
*
3966
4072
* @return void
3967
4073
*/
3968
- public function testDestroyWebhookWithInvalidRuleID ()
4074
+ public function testDeleteWebhookWithInvalidID ()
3969
4075
{
3970
- $ this ->markTestIncomplete ();
3971
-
3972
4076
$ this ->expectException (ClientException::class);
3973
- $ this ->api ->destroy_webhook (12345 );
4077
+ $ this ->api ->delete_webhook (12345 );
3974
4078
}
3975
4079
3976
4080
/**
0 commit comments