Skip to content

Commit 0261903

Browse files
committed
Added tests
1 parent 98b010a commit 0261903

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/ConvertKit_API.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ public function create_webhook(string $url, string $event, string|int $parameter
661661
[
662662
'api_secret' => $this->api_secret,
663663
'target_url' => $url,
664-
'event' => json_encode($eventData),
664+
'event' => $eventData,
665665
]
666666
);
667667
}

tests/ConvertKitAPITest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,83 @@ public function testGetSubscriberTagsWithInvalidSubscriberID()
693693
$subscriber = $this->api->get_subscriber_tags(12345);
694694
}
695695

696+
/**
697+
* Test that creating and destroying a webhook works.
698+
*
699+
* We do both, so we don't end up with unnecessary webhooks remaining
700+
* on the ConvertKit account when running tests.
701+
*
702+
* @since 1.0.0
703+
*
704+
* @return void
705+
*/
706+
public function testCreateAndDestroyWebhook()
707+
{
708+
// Create a webhook first.
709+
$result = $this->api->create_webhook(
710+
'https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d',
711+
'subscriber.subscriber_activate',
712+
);
713+
$ruleID = $result->rule->id;
714+
715+
// Destroy the webhook.
716+
$result = $this->api->destroy_webhook($ruleID);
717+
$this->assertEquals($result->success, true);
718+
}
719+
720+
/**
721+
* Test that creating and destroying a webhook works with an event parameter.
722+
*
723+
* We do both, so we don't end up with unnecessary webhooks remaining
724+
* on the ConvertKit account when running tests.
725+
*
726+
* @since 1.0.0
727+
*
728+
* @return void
729+
*/
730+
public function testCreateAndDestroyWebhookWithEventParameter()
731+
{
732+
// Create a webhook first.
733+
$result = $this->api->create_webhook(
734+
'https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d',
735+
'subscriber.form_subscribe',
736+
$_ENV['CONVERTKIT_API_FORM_ID']
737+
);
738+
$ruleID = $result->rule->id;
739+
740+
// Destroy the webhook.
741+
$result = $this->api->destroy_webhook($ruleID);
742+
$this->assertEquals($result->success, true);
743+
}
744+
745+
/**
746+
* Test that create_webook() throws an InvalidArgumentException when an invalid
747+
* event is specified.
748+
*
749+
* @since 1.0.0
750+
*
751+
* @return void
752+
*/
753+
public function testCreateWebhookWithInvalidEvent()
754+
{
755+
$this->expectException(\InvalidArgumentException::class);
756+
$this->api->create_webhook('https://webhook.site/2705fef6-34ef-4252-9c78-d511c540b58d', 'invalid.event');
757+
}
758+
759+
/**
760+
* Test that destroy_webhook() throws a ClientException when an invalid
761+
* rule ID is specified.
762+
*
763+
* @since 1.0.0
764+
*
765+
* @return void
766+
*/
767+
public function testDestroyWebhookWithInvalidRuleID()
768+
{
769+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
770+
$this->api->destroy_webhook(12345);
771+
}
772+
696773
/**
697774
* Test that list_purchases() returns the expected data.
698775
*

0 commit comments

Comments
 (0)