@@ -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
* Load .env configuration into $_ENV superglobal, and initialize the API
56
65
* class before each test.
@@ -97,6 +106,11 @@ protected function tearDown(): void
97
106
foreach ($ this ->subscriber_ids as $ id ) {
98
107
$ this ->api ->unsubscribe ($ id );
99
108
}
109
+
110
+ // Delete any Webhooks.
111
+ foreach ($ this ->webhook_ids as $ id ) {
112
+ $ this ->api ->delete_webhook ($ id );
113
+ }
100
114
}
101
115
102
116
/**
@@ -3790,45 +3804,6 @@ public function testDestroyBroadcastWithInvalidBroadcastID()
3790
3804
$ this ->api ->destroy_broadcast (12345 );
3791
3805
}
3792
3806
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
-
3832
3807
/**
3833
3808
* Test that get_webhooks() returns the expected data
3834
3809
* when pagination parameters and per_page limits are specified.
@@ -3839,6 +3814,25 @@ public function testGetWebhooksWithTotalCount()
3839
3814
*/
3840
3815
public function testGetWebhooksPagination ()
3841
3816
{
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.
3842
3836
$ result = $ this ->api ->get_webhooks (
3843
3837
per_page: 1
3844
3838
);
@@ -3869,7 +3863,7 @@ public function testGetWebhooksPagination()
3869
3863
3870
3864
// Assert has_previous_page and has_next_page are correct.
3871
3865
$ this ->assertTrue ($ result ->pagination ->has_previous_page );
3872
- $ this ->assertTrue ($ result ->pagination ->has_next_page );
3866
+ $ this ->assertFalse ($ result ->pagination ->has_next_page );
3873
3867
3874
3868
// Use pagination to fetch previous page.
3875
3869
$ result = $ this ->api ->get_webhooks (
@@ -3886,7 +3880,7 @@ public function testGetWebhooksPagination()
3886
3880
}
3887
3881
3888
3882
/**
3889
- * Test that create_webhook() and delete_webhook() works.
3883
+ * Test that create_webhook(), get_webhooks() and delete_webhook() works.
3890
3884
*
3891
3885
* We do both, so we don't end up with unnecessary webhooks remaining
3892
3886
* on the ConvertKit account when running tests.
@@ -3895,43 +3889,66 @@ public function testGetWebhooksPagination()
3895
3889
*
3896
3890
* @return void
3897
3891
*/
3898
- public function testCreateAndDeleteWebhook ()
3892
+ public function testCreateGetAndDeleteWebhook ()
3899
3893
{
3900
3894
// Create a webhook first.
3901
3895
$ result = $ this ->api ->create_webhook (
3902
- url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb ' ,
3896
+ url: 'https://webhook.site/ ' . str_shuffle ( ' wfervdrtgsdewrafvwefds ' ) ,
3903
3897
event: 'subscriber.subscriber_activate ' ,
3904
3898
);
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 );
3906
3920
3907
3921
// Delete the webhook.
3908
- $ result = $ this ->api ->delete_webhook ($ ruleID );
3909
- $ this ->assertEquals ($ result ->success , true );
3922
+ $ result = $ this ->api ->delete_webhook ($ id );
3910
3923
}
3911
3924
3912
3925
/**
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.
3917
3927
*
3918
3928
* @since 1.0.0
3919
3929
*
3920
3930
* @return void
3921
3931
*/
3922
- public function testCreateAndDeleteWebhookWithEventParameter ()
3932
+ public function testCreateWebhookWithEventParameter ()
3923
3933
{
3924
- // Create a webhook first.
3934
+ // Create a webhook.
3935
+ $ url = 'https://webhook.site/ ' . str_shuffle ('wfervdrtgsdewrafvwefds ' );
3925
3936
$ result = $ this ->api ->create_webhook (
3926
- url: ' https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb ' ,
3937
+ url: $ url ,
3927
3938
event: 'subscriber.form_subscribe ' ,
3928
3939
parameter: $ _ENV ['CONVERTKIT_API_FORM_ID ' ]
3929
3940
);
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 ' ]);
3931
3949
3932
3950
// 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 );
3935
3952
}
3936
3953
3937
3954
/**
@@ -3946,20 +3963,20 @@ public function testCreateWebhookWithInvalidEvent()
3946
3963
{
3947
3964
$ this ->expectException (InvalidArgumentException::class);
3948
3965
$ this ->api ->create_webhook (
3949
- url: 'https://webhook.site/9c731823-7e61-44c8-af39-43b11f700ecb ' ,
3966
+ url: 'https://webhook.site/ ' . str_shuffle ( ' wfervdrtgsdewrafvwefds ' ) ,
3950
3967
event: 'invalid.event '
3951
3968
);
3952
3969
}
3953
3970
3954
3971
/**
3955
3972
* Test that delete_webhook() throws a ClientException when an invalid
3956
- * rule ID is specified.
3973
+ * ID is specified.
3957
3974
*
3958
3975
* @since 1.0.0
3959
3976
*
3960
3977
* @return void
3961
3978
*/
3962
- public function testsDeleteWebhookWithInvalidRuleID ()
3979
+ public function testDeleteWebhookWithInvalidID ()
3963
3980
{
3964
3981
$ this ->expectException (ClientException::class);
3965
3982
$ this ->api ->delete_webhook (12345 );
0 commit comments