From 2e76f985a0b2dea594ee1c0f866a843264005b57 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 10 Mar 2023 15:31:51 +0000 Subject: [PATCH 1/2] Added missing sequence API calls --- src/ConvertKit_API.php | 69 ++++++++++---- tests/ConvertKitAPITest.php | 178 ++++++++++++++++++++++++++---------- 2 files changed, 179 insertions(+), 68 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 9c7e861..46cb041 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -152,6 +152,8 @@ public function get_account() /** * Gets all sequences + * + * @see https://developers.convertkit.com/#list-sequences * * @return false|mixed */ @@ -166,39 +168,68 @@ public function get_sequences() } /** - * Gets subscribers to a sequence + * Adds a subscriber to a sequence by email address * - * @param integer $sequence_id Sequence ID. - * @param string $sort_order Sort Order (asc|desc). + * @param integer $sequence_id Sequence ID. + * @param string $email Email Address. + * @param string $first_name First Name. + * @param array $fields Custom Fields. + * @param array $tag_ids Tag ID(s) to subscribe to. + * + * @see https://developers.convertkit.com/#add-subscriber-to-a-sequence * * @return false|mixed */ - public function get_sequence_subscriptions(int $sequence_id, string $sort_order = 'asc') + public function add_subscriber_to_sequence(int $sequence_id, string $email, string $first_name = '', array $fields = [], array $tag_ids = []) { - return $this->get( - sprintf('sequences/%s/subscriptions', $sequence_id), - [ - 'api_secret' => $this->api_secret, - 'sort_order' => $sort_order, - ] + // Build parameters. + $options = [ + 'api_key' => $this->api_key, + 'email' => $email, + ]; + + if (!empty($first_name)) { + $options['first_name'] = $first_name; + } + if (!empty($fields)) { + $options['fields'] = $fields; + } + if (!empty($tags)) { + $options['tags'] = $tag_ids; + } + + // Send request. + return $this->post( + sprintf('sequences/%s/subscribe', $sequence_id), + $options ); } /** - * Adds a subscriber to a sequence by email address + * Gets subscribers to a sequence * - * @param integer $sequence_id Sequence ID. - * @param string $email Email Address. + * @param integer $sequence_id Sequence ID. + * @param string $sort_order Sort Order (asc|desc). + * @param string $subscriber_state Subscriber State (active,cancelled). + * @param integer $page Page. + * + * @see https://developers.convertkit.com/#list-subscriptions-to-a-sequence * * @return false|mixed */ - public function add_subscriber_to_sequence(int $sequence_id, string $email) - { - return $this->post( - sprintf('courses/%s/subscribe', $sequence_id), + public function get_sequence_subscriptions( + int $sequence_id, + string $sort_order = 'asc', + string $subscriber_state = 'active', + int $page = 1 + ){ + return $this->get( + sprintf('sequences/%s/subscriptions', $sequence_id), [ - 'api_key' => $this->api_key, - 'email' => $email, + 'api_secret' => $this->api_secret, + 'sort_order' => $sort_order, + 'subscriber_state' => $subscriber_state, + 'page' => $page, ] ); } diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 398bb42..c6cf0c6 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -86,6 +86,131 @@ public function testGetSequences() $this->assertArrayHasKey('created_at', $sequence); } + /** + * Test that add_subscriber_to_sequence() returns the expected data. + * + * @since 1.0.0 + * + * @return void + */ + public function testAddSubscriberToSequence() + { + $result = $this->api->add_subscriber_to_sequence( + $_ENV['CONVERTKIT_API_SEQUENCE_ID'], + $this->generateEmailAddress() + ); + $this->assertInstanceOf('stdClass', $result); + $this->assertArrayHasKey('subscription', get_object_vars($result)); + } + + /** + * Test that add_subscriber_to_sequence() throws a ClientException when an invalid + * sequence is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testAddSubscriberToSequenceWithInvalidSequenceID() + { + $this->expectException(GuzzleHttp\Exception\ClientException::class); + $result = $this->api->add_subscriber_to_sequence(12345, $this->generateEmailAddress()); + } + + /** + * Test that add_subscriber_to_sequence() throws a ClientException when an invalid + * email address is specified. + * + * @since 1.0.0 + * + * @return void + */ + public function testAddSubscriberToSequenceWithInvalidEmailAddress() + { + $this->expectException(GuzzleHttp\Exception\ClientException::class); + $result = $this->api->add_subscriber_to_sequence($_ENV['CONVERTKIT_API_SEQUENCE_ID'], 'not-an-email-address'); + } + + /** + * Test that add_subscriber_to_sequence() returns the expected data + * when a first_name parameter is included. + * + * @since 1.0.0 + * + * @return void + */ + public function testAddSubscriberToSequenceWithFirstName() + { + $emailAddress = $this->generateEmailAddress(); + $firstName = 'First Name'; + $result = $this->api->add_subscriber_to_sequence($_ENV['CONVERTKIT_API_SEQUENCE_ID'], $emailAddress, $firstName); + + $this->assertInstanceOf('stdClass', $result); + $this->assertArrayHasKey('subscription', get_object_vars($result)); + + // Fetch subscriber from API to confirm the first name was saved. + $subscriber = $this->api->get_subscriber($result->subscription->subscriber->id); + $this->assertEquals($subscriber->subscriber->email_address, $emailAddress); + $this->assertEquals($subscriber->subscriber->first_name, $firstName); + } + + /** + * Test that add_subscriber_to_sequence() returns the expected data + * when custom field data is included. + * + * @since 1.0.0 + * + * @return void + */ + public function testAddSubscriberToSequenceWithCustomFields() + { + $result = $this->api->add_subscriber_to_sequence( + $_ENV['CONVERTKIT_API_SEQUENCE_ID'], + $this->generateEmailAddress(), + 'First Name', + [ + 'last_name' => 'Last Name', + ] + ); + + // Check subscription object returned. + $this->assertInstanceOf('stdClass', $result); + $this->assertArrayHasKey('subscription', get_object_vars($result)); + + // Fetch subscriber from API to confirm the custom fields were saved. + $subscriber = $this->api->get_subscriber($result->subscription->subscriber->id); + $this->assertEquals($subscriber->subscriber->fields->last_name, 'Last Name'); + } + + /** + * Test that add_subscriber_to_sequence() returns the expected data + * when custom field data is included. + * + * @since 1.0.0 + * + * @return void + */ + public function testAddSubscriberToSequenceWithTagID() + { + $result = $this->api->add_subscriber_to_sequence( + $_ENV['CONVERTKIT_API_SEQUENCE_ID'], + $this->generateEmailAddress(), + 'First Name', + [], + [ + (int) $_ENV['CONVERTKIT_API_TAG_ID'] + ] + ); + + // Check subscription object returned. + $this->assertInstanceOf('stdClass', $result); + $this->assertArrayHasKey('subscription', get_object_vars($result)); + + // Fetch subscriber tags from API to confirm the tag saved. + $subscriberTags = $this->api->get_subscriber_tags($result->subscription->subscriber->id); + // @TODO. + } + /** * Test that get_sequence_subscriptions() returns the expected data. * @@ -143,20 +268,6 @@ public function testGetSequenceSubscriptionsWithDescSortOrder() ); } - /** - * Test that get_sequence_subscriptions() throws a ClientException when an invalid - * sequence ID is specified. - * - * @since 1.0.0 - * - * @return void - */ - public function testGetSequenceSubscriptionsWithInvalidSequenceID() - { - $this->expectException(GuzzleHttp\Exception\ClientException::class); - $result = $this->api->get_sequence_subscriptions(12345); - } - /** * Test that get_sequence_subscriptions() throws a ClientException when an invalid * sort order is specified. @@ -172,48 +283,17 @@ public function testGetSequenceSubscriptionsWithInvalidSortOrder() } /** - * Test that add_subscriber_to_sequence() returns the expected data. - * - * @since 1.0.0 - * - * @return void - */ - public function testAddSubscriberToSequence() - { - $result = $this->api->add_subscriber_to_sequence( - $_ENV['CONVERTKIT_API_SEQUENCE_ID'], - $this->generateEmailAddress() - ); - $this->assertInstanceOf('stdClass', $result); - $this->assertArrayHasKey('subscription', get_object_vars($result)); - } - - /** - * Test that add_subscriber_to_sequence() throws a ClientException when an invalid - * sequence is specified. - * - * @since 1.0.0 - * - * @return void - */ - public function testAddSubscriberToSequenceWithInvalidSequenceID() - { - $this->expectException(GuzzleHttp\Exception\ClientException::class); - $result = $this->api->add_subscriber_to_sequence(12345, $this->generateEmailAddress()); - } - - /** - * Test that add_subscriber_to_sequence() throws a ClientException when an invalid - * email address is specified. + * Test that get_sequence_subscriptions() throws a ClientException when an invalid + * sequence ID is specified. * * @since 1.0.0 * * @return void */ - public function testAddSubscriberToSequenceWithInvalidEmailAddress() + public function testGetSequenceSubscriptionsWithInvalidSequenceID() { $this->expectException(GuzzleHttp\Exception\ClientException::class); - $result = $this->api->add_subscriber_to_sequence($_ENV['CONVERTKIT_API_SEQUENCE_ID'], 'not-an-email-address'); + $result = $this->api->get_sequence_subscriptions(12345); } /** From 3f5eff7e377622d18b295bff5a5cbc3d68373bd2 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 10 Mar 2023 15:45:20 +0000 Subject: [PATCH 2/2] Coding standards; assert tag saves in test --- src/ConvertKit_API.php | 49 ++++++++++++++++++++----------------- tests/ConvertKitAPITest.php | 8 ++++-- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 46cb041..d8ff13c 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -152,7 +152,7 @@ public function get_account() /** * Gets all sequences - * + * * @see https://developers.convertkit.com/#list-sequences * * @return false|mixed @@ -175,13 +175,18 @@ public function get_sequences() * @param string $first_name First Name. * @param array $fields Custom Fields. * @param array $tag_ids Tag ID(s) to subscribe to. - * + * * @see https://developers.convertkit.com/#add-subscriber-to-a-sequence * * @return false|mixed */ - public function add_subscriber_to_sequence(int $sequence_id, string $email, string $first_name = '', array $fields = [], array $tag_ids = []) - { + public function add_subscriber_to_sequence( + int $sequence_id, + string $email, + string $first_name = '', + array $fields = [], + array $tag_ids = [] + ) { // Build parameters. $options = [ 'api_key' => $this->api_key, @@ -194,7 +199,7 @@ public function add_subscriber_to_sequence(int $sequence_id, string $email, stri if (!empty($fields)) { $options['fields'] = $fields; } - if (!empty($tags)) { + if (!empty($tag_ids)) { $options['tags'] = $tag_ids; } @@ -212,7 +217,7 @@ public function add_subscriber_to_sequence(int $sequence_id, string $email, stri * @param string $sort_order Sort Order (asc|desc). * @param string $subscriber_state Subscriber State (active,cancelled). * @param integer $page Page. - * + * * @see https://developers.convertkit.com/#list-subscriptions-to-a-sequence * * @return false|mixed @@ -222,14 +227,14 @@ public function get_sequence_subscriptions( string $sort_order = 'asc', string $subscriber_state = 'active', int $page = 1 - ){ + ) { return $this->get( sprintf('sequences/%s/subscriptions', $sequence_id), [ - 'api_secret' => $this->api_secret, - 'sort_order' => $sort_order, - 'subscriber_state' => $subscriber_state, - 'page' => $page, + 'api_secret' => $this->api_secret, + 'sort_order' => $sort_order, + 'subscriber_state' => $subscriber_state, + 'page' => $page, ] ); } @@ -720,8 +725,8 @@ private function strip_html_head_body_tags(string $markup) /** * Performs a GET request to the API. * - * @param string $endpoint API Endpoint. - * @param array $args Request arguments. + * @param string $endpoint API Endpoint. + * @param array|string> $args Request arguments. * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * @@ -739,8 +744,8 @@ public function get(string $endpoint, array $args = []) /** * Performs a POST request to the API. * - * @param string $endpoint API Endpoint. - * @param array $args Request arguments. + * @param string $endpoint API Endpoint. + * @param array|string> $args Request arguments. * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * @@ -758,8 +763,8 @@ public function post(string $endpoint, array $args = []) /** * Performs a PUT request to the API. * - * @param string $endpoint API Endpoint. - * @param array $args Request arguments. + * @param string $endpoint API Endpoint. + * @param array|string> $args Request arguments. * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * @@ -777,8 +782,8 @@ public function put(string $endpoint, array $args = []) /** * Performs a DELETE request to the API. * - * @param string $endpoint API Endpoint. - * @param array $args Request arguments. + * @param string $endpoint API Endpoint. + * @param array|string> $args Request arguments. * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * @@ -796,9 +801,9 @@ public function delete(string $endpoint, array $args = []) /** * Performs an API request using Guzzle. * - * @param string $endpoint API Endpoint. - * @param string $method Request method (POST, GET, PUT, PATCH, DELETE). - * @param array $args Request arguments. + * @param string $endpoint API Endpoint. + * @param string $method Request method. + * @param array|string> $args Request arguments. * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * @throws \Exception If JSON encoding arguments failed. diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index c6cf0c6..0603943 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -143,7 +143,11 @@ public function testAddSubscriberToSequenceWithFirstName() { $emailAddress = $this->generateEmailAddress(); $firstName = 'First Name'; - $result = $this->api->add_subscriber_to_sequence($_ENV['CONVERTKIT_API_SEQUENCE_ID'], $emailAddress, $firstName); + $result = $this->api->add_subscriber_to_sequence( + $_ENV['CONVERTKIT_API_SEQUENCE_ID'], + $emailAddress, + $firstName + ); $this->assertInstanceOf('stdClass', $result); $this->assertArrayHasKey('subscription', get_object_vars($result)); @@ -208,7 +212,7 @@ public function testAddSubscriberToSequenceWithTagID() // Fetch subscriber tags from API to confirm the tag saved. $subscriberTags = $this->api->get_subscriber_tags($result->subscription->subscriber->id); - // @TODO. + $this->assertEquals($subscriberTags->tags[0]->id, $_ENV['CONVERTKIT_API_TAG_ID']); } /**