diff --git a/src/ConvertKitAPI.php b/src/ConvertKitAPI.php index 918a2d9..0949d1b 100644 --- a/src/ConvertKitAPI.php +++ b/src/ConvertKitAPI.php @@ -2,14 +2,13 @@ namespace ConvertKit_API; -use Monolog\Logger; -use Monolog\Handler\StreamHandler; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; +use Monolog\Handler\StreamHandler; +use Monolog\Logger; - -class ConvertKit_API { - +class ConvertKit_API +{ /** * ConvertKit API Key * @@ -79,22 +78,25 @@ class ConvertKit_API { * @param string $api_key ConvertKit API Key. * @param string $api_secret ConvertKit API Secret. * @param boolean $debug if log debug info. + * @throws \Exception */ - public function __construct( $api_key, $api_secret, $debug = false ) { - - $this->api_key = $api_key; + public function __construct($api_key, $api_secret, $debug = false, $logPath = null) + { + $this->api_key = $api_key; $this->api_secret = $api_secret; - $this->debug = $debug; - $this->client = new Client(); + $this->debug = $debug; + $this->client = new Client(); - if( $debug ) { + if ($debug) { + $logPath = !empty($logPath) ? $logPath : __DIR__ . '/logs/debug.log'; $this->debug_logger = new Logger('ck-debug'); - $this->debug_logger->pushHandler(new StreamHandler(__DIR__.'/logs/debug.log', Logger::DEBUG)); + $this->debug_logger->pushHandler(new StreamHandler($logPath, Logger::DEBUG)); } } - private function create_log($message) { - if($this->debug) { + private function create_log($message) + { + if ($this->debug) { $this->debug_logger->info($message); } } @@ -114,7 +116,7 @@ public function get_account() $this->create_log(sprintf("GET account: %s, %s", $request, json_encode($options))); - return $this->make_request( $request, 'GET', $options ); + return $this->make_request($request, 'GET', $options); } /** @@ -132,7 +134,7 @@ public function get_sequences() $this->create_log(sprintf("GET sequences: %s, %s", $request, json_encode($options))); - return $this->make_request( $request, 'GET', $options ); + return $this->make_request($request, 'GET', $options); } /** @@ -149,12 +151,12 @@ public function get_sequence_subscriptions($sequence_id, $sort_order = 'asc') $options = array( 'api_secret' => $this->api_secret, - 'sort_order' => $sort_order + 'sort_order' => $sort_order, ); $this->create_log(sprintf("GET sequence subscriptions: %s, %s, %s", $request, json_encode($options), $sequence_id)); - return $this->make_request( $request, 'GET', $options ); + return $this->make_request($request, 'GET', $options); } /** @@ -171,12 +173,12 @@ public function add_subscriber_to_sequence($sequence_id, $email) $options = array( 'api_key' => $this->api_key, - 'email' => $email + 'email' => $email, ); $this->create_log(sprintf("POST add subscriber to sequence: %s, %s, %s, %s", $request, json_encode($options), $sequence_id, $email)); - return $this->make_request( $request, 'POST', $options ); + return $this->make_request($request, 'POST', $options); } /** @@ -186,19 +188,19 @@ public function add_subscriber_to_sequence($sequence_id, $email) * @param array $options Array of user data * @return false|object */ - public function add_tag( $tag, $options ) { - - if( !is_int($tag) || !is_array($options) ) { + public function add_tag($tag, $options) + { + if (!is_int($tag) || !is_array($options)) { throw new \InvalidArgumentException; } - $request = $this->api_version . sprintf( '/tags/%s/subscribe', $tag ); + $request = $this->api_version . sprintf('/tags/%s/subscribe', $tag); $options['api_key'] = $this->api_key; $this->create_log(sprintf("POST add tag: %s, %s, %s", $request, json_encode($options), $tag)); - return $this->make_request( $request, 'POST', $options ); + return $this->make_request($request, 'POST', $options); } /** @@ -210,17 +212,17 @@ public function add_tag( $tag, $options ) { * @param string $resource Resource type. * @return object API response */ - public function get_resources( $resource ) { - - if( !is_string($resource) ) { + public function get_resources($resource) + { + if (!is_string($resource)) { throw new \InvalidArgumentException; } - if ( ! array_key_exists( $resource, $this->resources ) ) { + if (!array_key_exists($resource, $this->resources)) { $options = array( - 'api_key' => $this->api_key, - 'timeout' => 10, + 'api_key' => $this->api_key, + 'timeout' => 10, 'Accept-Encoding' => 'gzip', ); @@ -228,84 +230,84 @@ public function get_resources( $resource ) { $this->create_log(sprintf("GET request %s, %s", $request, json_encode($options))); - $resources = $this->make_request( $request, 'GET', $options ); + $resources = $this->make_request($request, 'GET', $options); - if(!$resources) { + if (!$resources) { $this->create_log("No resources"); - $this->resources[ $resource ] = array( + $this->resources[$resource] = array( array( - 'id' => '-2', + 'id' => '-2', 'name' => 'Error contacting API', ), ); } else { $_resource = array(); - if ( 'forms' === $resource ) { - $response = isset( $resources->forms ) ? $resources->forms : array(); + if ('forms' === $resource) { + $response = isset($resources->forms) ? $resources->forms : array(); $this->create_log(sprintf("forms response %s", json_encode($response))); - foreach ( $response as $form ) { - if ( isset( $form->archived ) && $form->archived ) { + foreach ($response as $form) { + if (isset($form->archived) && $form->archived) { continue; } $_resource[] = $form; } - } elseif ( 'landing_pages' === $resource ) { - $response = isset($resources->forms ) ? $resources->forms : array(); + } elseif ('landing_pages' === $resource) { + $response = isset($resources->forms) ? $resources->forms : array(); $this->create_log(sprintf("landing_pages response %s", json_encode($response))); - foreach ( $response as $landing_page ) { - if ( 'hosted' === $landing_page->type ) { - if ( isset( $landing_page->archived ) && $landing_page->archived ) { + foreach ($response as $landing_page) { + if ('hosted' === $landing_page->type) { + if (isset($landing_page->archived) && $landing_page->archived) { continue; } $_resource[] = $landing_page; } } - } elseif ( 'subscription_forms' === $resource ) { + } elseif ('subscription_forms' === $resource) { $this->create_log("subscription_forms"); - foreach ( $resources as $mapping ) { - if ( isset( $mapping->archived ) && $mapping->archived ) { + foreach ($resources as $mapping) { + if (isset($mapping->archived) && $mapping->archived) { continue; } - $_resource[ $mapping->id ] = $mapping->form_id; + $_resource[$mapping->id] = $mapping->form_id; } - } elseif ( 'tags' === $resource ) { - $response = isset( $resources->tags ) ? $resources->tags : array(); + } elseif ('tags' === $resource) { + $response = isset($resources->tags) ? $resources->tags : array(); $this->create_log(sprintf("tags response %s", json_encode($response))); - foreach ( $response as $tag ) { + foreach ($response as $tag) { $_resource[] = $tag; } } - $this->resources[ $resource ] = $_resource; + $this->resources[$resource] = $_resource; } } - return $this->resources[ $resource ]; + return $this->resources[$resource]; } /** * Adds a subscriber to a form. * * @param string $form_id Form ID. - * @param array $options Array of user data (email, name). + * @param array $options Array of user data (email, name). * * @return false|object */ - public function form_subscribe( $form_id, $options ) { - - if( !is_int($form_id) || !is_array($options) ) { + public function form_subscribe($form_id, $options) + { + if (!is_int($form_id) || !is_array($options)) { throw new \InvalidArgumentException; } - $request = $this->api_version . sprintf( '/forms/%s/subscribe', $form_id ); + $request = $this->api_version . sprintf('/forms/%s/subscribe', $form_id); $options['api_key'] = $this->api_key; $this->create_log(sprintf("POST form subscribe: %s, %s, %s", $request, json_encode($options), $form_id)); - return $this->make_request( $request, 'POST', $options ); + return $this->make_request($request, 'POST', $options); } @@ -316,9 +318,9 @@ public function form_subscribe( $form_id, $options ) { * * @return false|object */ - public function form_unsubscribe( $options ) { - - if( !is_array($options) ) { + public function form_unsubscribe($options) + { + if (!is_array($options)) { throw new \InvalidArgumentException; } @@ -328,7 +330,7 @@ public function form_unsubscribe( $options ) { $this->create_log(sprintf("PUT form unsubscribe: %s, %s", $request, json_encode($options))); - return $this->make_request( $request, 'PUT', $options ); + return $this->make_request($request, 'PUT', $options); } /** @@ -338,9 +340,9 @@ public function form_unsubscribe( $options ) { * @param $email_address string * @return false|int $subscriber_id */ - public function get_subscriber_id( $email_address ) { - - if( !is_string($email_address) || !filter_var($email_address, FILTER_VALIDATE_EMAIL)) { + public function get_subscriber_id($email_address) + { + if (!is_string($email_address) || !filter_var($email_address, FILTER_VALIDATE_EMAIL)) { throw new \InvalidArgumentException; } @@ -348,21 +350,21 @@ public function get_subscriber_id( $email_address ) { $options = array( 'api_secret' => $this->api_secret, - 'status' => 'all', + 'status' => 'all', ); $this->create_log(sprintf("GET subscriber id from all subscribers: %s, %s, %s", $request, json_encode($options), $email_address)); - $subscribers = $this->make_request( $request, 'GET', $options ); + $subscribers = $this->make_request($request, 'GET', $options); - if( !$subscribers ) { + if (!$subscribers) { $this->create_log("No subscribers"); return false; } - $subscriber_id = $this::check_if_subscriber_in_array($email_address, $subscribers->subscribers); + $subscriber_id = self::check_if_subscriber_in_array($email_address, $subscribers->subscribers); - if($subscriber_id) { + if ($subscriber_id) { return $subscriber_id; } @@ -370,18 +372,18 @@ public function get_subscriber_id( $email_address ) { $this->create_log(sprintf("Total number of pages is %s", $total_pages)); - for ( $i = 2; $i <= $total_pages; $i++ ) { + for ($i = 2; $i <= $total_pages; $i++) { $options['page'] = $i; $this->create_log(sprintf("Go to page %s", $i)); - $subscribers = $this->make_request( $request, 'GET', $options ); + $subscribers = $this->make_request($request, 'GET', $options); - if( !$subscribers ) { + if (!$subscribers) { return false; } - $subscriber_id = $this::check_if_subscriber_in_array($email_address, $subscribers->subscribers); + $subscriber_id = self::check_if_subscriber_in_array($email_address, $subscribers->subscribers); - if($subscriber_id) { + if ($subscriber_id) { return $subscriber_id; } } @@ -399,13 +401,13 @@ public function get_subscriber_id( $email_address ) { * * @return false|int */ - public function get_subscriber( $subscriber_id ) { - - if( !is_int($subscriber_id) || $subscriber_id < 1 ) { + public function get_subscriber($subscriber_id) + { + if (!is_int($subscriber_id) || $subscriber_id < 1) { throw new \InvalidArgumentException; } - $request = $this->api_version . sprintf( '/subscribers/%s', $subscriber_id ); + $request = $this->api_version . sprintf('/subscribers/%s', $subscriber_id); $options = array( 'api_secret' => $this->api_secret, @@ -413,7 +415,7 @@ public function get_subscriber( $subscriber_id ) { $this->create_log(sprintf("GET subscriber tags: %s, %s, %s", $request, json_encode($options), $subscriber_id)); - return $this->make_request( $request, 'GET', $options ); + return $this->make_request($request, 'GET', $options); } @@ -423,13 +425,13 @@ public function get_subscriber( $subscriber_id ) { * @param $subscriber_id * @return false|array $subscriber_tags Array of tags for customer with key of tag_id */ - public function get_subscriber_tags( $subscriber_id ) { - - if( !is_int($subscriber_id) || $subscriber_id < 1 ) { + public function get_subscriber_tags($subscriber_id) + { + if (!is_int($subscriber_id) || $subscriber_id < 1) { throw new \InvalidArgumentException; } - $request = $this->api_version . sprintf( '/subscribers/%s/tags', $subscriber_id ); + $request = $this->api_version . sprintf('/subscribers/%s/tags', $subscriber_id); $options = array( 'api_key' => $this->api_key, @@ -437,7 +439,7 @@ public function get_subscriber_tags( $subscriber_id ) { $this->create_log(sprintf("GET subscriber tags: %s, %s, %s", $request, json_encode($options), $subscriber_id)); - return $this->make_request( $request, 'GET', $options ); + return $this->make_request($request, 'GET', $options); } @@ -446,9 +448,9 @@ public function get_subscriber_tags( $subscriber_id ) { * * @return false|object */ - public function list_purchases($options) { - - if( !is_array($options) ) { + public function list_purchases($options) + { + if (!is_array($options)) { throw new \InvalidArgumentException; } @@ -458,19 +460,19 @@ public function list_purchases($options) { $this->create_log(sprintf("GET list purchases: %s, %s", $request, json_encode($options))); - return $this->make_request( $request, 'GET', $options ); + return $this->make_request($request, 'GET', $options); } /** * Creates a purchase. * - * @param array $options + * @param array $options * * @return false|object */ - public function create_purchase($options) { - - if( !is_array($options) ) { + public function create_purchase($options) + { + if (!is_array($options)) { throw new \InvalidArgumentException; } @@ -480,7 +482,7 @@ public function create_purchase($options) { $this->create_log(sprintf("POST create purchase: %s, %s", $request, json_encode($options))); - return $this->make_request( $request, 'POST', $options ); + return $this->make_request($request, 'POST', $options); } /** @@ -489,13 +491,13 @@ public function create_purchase($options) { * @param string $url URL of API action. * @return false|string */ - public function get_resource( $url ) { - - if( !is_string($url) ) { + public function get_resource($url) + { + if (!is_string($url)) { throw new \InvalidArgumentException; } - if (strpos( $url, 'api_key' ) === false) { + if (strpos($url, 'api_key') === false) { $url .= '?api_key=' . $this->api_key; } @@ -503,48 +505,48 @@ public function get_resource( $url ) { $this->create_log(sprintf("Getting resource %s", $url)); - if ( ! empty( $url ) && isset( $this->markup[ $url ] ) ) { + if (!empty($url) && isset($this->markup[$url])) { $this->create_log("Resource already set"); - $resource = $this->markup[ $url ]; - } elseif ( ! empty( $url ) ) { + $resource = $this->markup[$url]; + } elseif (!empty($url)) { - if ( ! function_exists( 'str_get_html' ) ) { - require_once( dirname( __FILE__ ) . '/lib/simple-html-dom.php' ); + if (!function_exists('str_get_html')) { + require_once dirname(__FILE__) . '/lib/simple-html-dom.php'; } - if ( ! function_exists( 'url_to_absolute' ) ) { - require_once( dirname( __FILE__ ) . '/lib/url-to-absolute.php' ); + if (!function_exists('url_to_absolute')) { + require_once dirname(__FILE__) . '/lib/url-to-absolute.php'; } $this->create_log("Getting html from url"); $html = file_get_html($url); - foreach ( $html->find( 'a, link' ) as $element ) { - if ( isset( $element->href ) ) { + foreach ($html->find('a, link') as $element) { + if (isset($element->href)) { $this->create_log(sprintf("To absolute url: %s", $element->href)); - echo url_to_absolute( $url, $element->href ); - $element->href = url_to_absolute( $url, $element->href ); + echo url_to_absolute($url, $element->href); + $element->href = url_to_absolute($url, $element->href); } } - foreach ( $html->find( 'img, script' ) as $element ) { - if ( isset( $element->src ) ) { + foreach ($html->find('img, script') as $element) { + if (isset($element->src)) { $this->create_log(sprintf("To absolute src: %s", $element->src)); - $element->src = url_to_absolute( $url, $element->src ); + $element->src = url_to_absolute($url, $element->src); } } - foreach ( $html->find( 'form' ) as $element ) { - if ( isset( $element->action ) ) { + foreach ($html->find('form') as $element) { + if (isset($element->action)) { $this->create_log(sprintf("To absolute form: %s", $element->action)); - $element->action = url_to_absolute( $url, $element->action ); + $element->action = url_to_absolute($url, $element->action); } else { $element->action = $url; } } - $resource = $html->save(); - $this->markup[ $url ] = $resource; + $resource = $html->save(); + $this->markup[$url] = $resource; } @@ -554,13 +556,13 @@ public function get_resource( $url ) { /** * @param $endpoint string, endpoint for request * @param $method string, POST, GET, PUT, PATCH, DELETE - * @param array $args array, additional arguments for request + * @param array $args array, additional arguments for request * * @return false|mixed */ - private function make_request($endpoint, $method, $args = array()) { - - if( !is_string($endpoint) || !is_string($method) || !is_array($args) ) { + private function make_request($endpoint, $method, $args = array()) + { + if (!is_string($endpoint) || !is_string($method) || !is_array($args)) { throw new \InvalidArgumentException; } @@ -572,20 +574,20 @@ private function make_request($endpoint, $method, $args = array()) { $this->create_log(sprintf("%s, Request body: %s", $method, $request_body)); - if( $method === "GET" ){ - if($args) { + if ($method === "GET") { + if ($args) { $url .= '?' . http_build_query($args); } $request = new Request($method, $url); } else { $request = new Request($method, $url, array( 'Content-Type' => 'application/json', - 'Content-Length' => strlen($request_body) + 'Content-Length' => strlen($request_body), ), $request_body); } $response = $this->client->send($request, [ - 'exceptions' => false + 'exceptions' => false, ]); $status_code = $response->getStatusCode(); @@ -598,7 +600,7 @@ private function make_request($endpoint, $method, $args = array()) { $response_body = json_decode($response->getBody()->getContents()); - if($response_body) { + if ($response_body) { $this->create_log("Finish request successfully."); return $response_body; } @@ -616,8 +618,8 @@ private function make_request($endpoint, $method, $args = array()) { * * @return false|int false if not found, else subscriber object */ - private function check_if_subscriber_in_array($email_address, $subscribers) { - + private static function check_if_subscriber_in_array($email_address, $subscribers) + { foreach ($subscribers as $subscriber) { if ($subscriber->email_address === $email_address) { $this->create_log("Subscriber found!"); @@ -627,7 +629,61 @@ private function check_if_subscriber_in_array($email_address, $subscribers) { $this->create_log("Subscriber not found on current page."); return false; + } + + /** + * Gets all subscribers from a specific form + * + * @param $form_id + * @param int $page + * @param null $subscriber_state + * @param string $sort_order + * @return false|mixed + */ + public function get_form_subscriptions($form_id, $page = 1, $subscriber_state = null, $sort_order = 'asc') + { + $request = $this->api_version . sprintf('/forms/%s/subscriptions', (string) $form_id); + + $options = array( + 'api_secret' => $this->api_secret, + 'page' => $page, + 'sort_order' => $sort_order, + ); + + if (!empty($subscriber_state) && in_array($subscriber_state, ['active', 'cancelled'])) { + $options['subscriber_state'] = $subscriber_state; + } + + $this->create_log(sprintf("GET form subscriptions: %s, %s, %s", $request, json_encode($options), $form_id)); + return $this->make_request($request, 'GET', $options); } -} \ No newline at end of file + /** + * Gets all subscribers from a specific tag + * + * @param $tag_id + * @param int $page + * @param null $subscriber_state + * @param string $sort_order + * @return false|mixed + */ + public function get_tag_subscriptions($tag_id, $page = 1, $subscriber_state = null, $sort_order = 'asc') + { + $request = $this->api_version . sprintf('/tags/%s/subscriptions', (string) $tag_id); + + $options = array( + 'api_secret' => $this->api_secret, + 'page' => $page, + 'sort_order' => $sort_order, + ); + + if (!empty($subscriber_state) && in_array($subscriber_state, ['active', 'cancelled'])) { + $options['subscriber_state'] = $subscriber_state; + } + + $this->create_log(sprintf("GET tag subscriptions: %s, %s, %s", $request, json_encode($options), $tag_id)); + + return $this->make_request($request, 'GET', $options); + } +} diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 5a41ea3..5a09c87 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -2,341 +2,416 @@ use PHPUnit\Framework\TestCase; -class ConvertKitAPITest extends TestCase { - - /** - * ConvertKit Class Object - * - * @var object - */ - protected $api; - - /** - * Test subscribed user email - * - * @var string - */ - protected $test_email; - - /** - * Test subscribed user id - * - * @var string - */ - protected $test_user_id; - - /** - * Test tag id - * - * @var int - */ - protected $test_tag_id; - - /** - * Form url - * - * @var int - */ - protected $test_form_url; - - /** - * Form id - * - * @var int - */ - protected $test_form_id; - - protected function setUp() { - - include_once( dirname(__FILE__) . "/config.php" ); - - $api_key = CONVERTKIT_PUBLIC_KEY; - $api_secret = CONVERTKIT_SECRET_KEY; - $this->test_email = CONVERTKIT_TESTING_EMAIL; - $this->test_user_id = CONVERTKIT_TESTING_USER_ID; - $this->test_form_id = CONVERTKIT_TESTING_FORM_ID; - $this->test_tag_id = CONVERTKIT_TESTING_TAG_ID; - $this->test_form_url = CONVERTKIT_TESTING_FORM_URL; - - $this->api = new \ConvertKit_API\ConvertKit_API($api_key, $api_secret); - } - - /** - * @dataProvider inputGetResourcesArguments - * @expectedException InvalidArgumentException - * - * @param $input - */ - public function testGetResourcesArguments($input) { - $this->api->get_resources($input); - } - - /** - * Data provider for @testGetResourcesArguments - * - * @return array - */ - public function inputGetResourcesArguments() { - return [ - [2], - [['2', '1']], - [new stdClass()] - ]; - } - - /** - * @dataProvider inputGetSubscriberId - * @expectedException InvalidArgumentException - * - * @param $input - */ - public function testArgumentsGetSubscriberId($input) { - $this->api->get_subscriber_id($input); - } - - /** - * Data provider for @testGetSubscriberId - * - * @return array - */ - public function inputGetSubscriberId() { - return [ - [2], - [['2', '1']], - [new stdClass()], - ['teststring'], - ['teststring@'] - ]; - } - - /** - * @dataProvider inputGetSubscriber - * @expectedException InvalidArgumentException - * - * @param $input - */ - public function testArgumentsGetSubscriber($input) { - $this->api->get_subscriber($input); - } - - /** - * Data provider for @testGetSubscriber - * - * @return array - */ - public function inputGetSubscriber() { - return [ - [['2', '1']], - [new stdClass()], - ['teststring'], - [1.2], - [-10], - ]; - } - - /** - * @dataProvider inputAddTag - * @expectedException InvalidArgumentException - * - * @param $tag - * @param $options - */ - public function testArgumentsAddTag($tag, $options) { - $this->api->add_tag($tag, $options); - } - - /** - * Data provider for @testAddTag - * - * @return array - */ - public function inputAddTag() { - return [ - [['2', '1'], 1], - [new stdClass(), 2], - ['teststring', 3], - [3, 3], - ]; - } - - public function testIncorrectApiData() { - $api_key = 'test'; - $api_secret = 'test'; - - $test_client = new \ConvertKit_API\ConvertKit_API($api_key, $api_secret); - $this->assertFalse($test_client->get_subscriber_id($this->test_email)); - $this->assertFalse($test_client->get_subscriber($this->test_user_id)); - $this->assertFalse($test_client->get_subscriber_tags($this->test_user_id)); - } - - /** - * Get subscriber id by email - */ - public function testGetSubscriberId() { - $subscriber_id = $this->api->get_subscriber_id($this->test_email); - $this->assertInternalType("int", $subscriber_id); - } - - /** - * Get subscriber by id - */ - public function testGetSubscriber() { - $subscriber = $this->api->get_subscriber($this->test_user_id); - $this->assertInstanceOf('stdClass', $subscriber); - $this->assertArrayHasKey('subscriber', get_object_vars($subscriber)); - $this->assertArrayHasKey('id', get_object_vars($subscriber->subscriber)); - $this->assertEquals(get_object_vars($subscriber->subscriber)['id'], $this->test_user_id); - } - - /** - * Get subscriber tags - */ - public function testGetSubscriberTags() { - $subscriber = $this->api->get_subscriber_tags($this->test_user_id); - $this->assertInstanceOf('stdClass', $subscriber); - $this->assertArrayHasKey('tags', get_object_vars($subscriber)); - } - - /** - * Subscribe and unsubscribe from form - */ - public function testUserActions() { - - $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; - - /* - * Subscribe - */ - $options = [ - 'email' => $random_email, - 'name' => 'Full Name', - 'first_name' => 'First Name', - 'tags' => $this->test_tag_id, - 'fields' => [ - 'phone' => 134567891243, - 'shirt_size' => 'M', - 'website_url' => 'testurl.com' - ] - ]; - - $subscribed = $this->api->form_subscribe($this->test_form_id, $options); - $this->assertInstanceOf('stdClass', $subscribed); - $this->assertArrayHasKey('subscription', get_object_vars($subscribed)); - $this->assertArrayHasKey('id', get_object_vars($subscribed->subscription)); - $this->assertEquals(get_object_vars($subscribed->subscription)['subscribable_id'], $this->test_form_id); - - /* - * Add tag - */ - $added_tag = $this->api->add_tag($this->test_tag_id, [ - 'email' => $random_email - ]); - $this->assertInstanceOf('stdClass', $added_tag); - $this->assertArrayHasKey('subscription', get_object_vars($added_tag)); - $this->assertArrayHasKey('id', get_object_vars($added_tag->subscription)); - $this->assertEquals(get_object_vars($added_tag->subscription)['subscribable_id'], $this->test_tag_id); - $this->assertEquals(get_object_vars($added_tag->subscription)['subscribable_type'], 'tag'); - - /* - * Purchase - */ - $purchase_options = [ - 'purchase' => [ - 'email_address' => $random_email, - 'transaction_id' => str_shuffle('wfervdrtgsdewrafvwefds'), - 'subtotal' => 20.00, - 'tax' => 2.00, - 'shipping' => 2.00, - 'discount' => 3.00, - 'total' => 21.00, - 'status' => 'paid', - 'products' => array( - 0 => array( - 'name' => 'Floppy Disk (512k)', - 'sku' => '7890-ijkl', - 'unit_price' => 5.00, - 'quantity' => 2 - ) - ) - ] - ]; - $purchase = $this->api->create_purchase($purchase_options); - $this->assertInstanceOf('stdClass', $purchase); - $this->assertArrayHasKey('transaction_id', get_object_vars($purchase)); - - /* - * Unsubscribe - */ - $unsubscribed = $this->api->form_unsubscribe([ - 'email' => $random_email - ]); - - $this->assertInstanceOf('stdClass', $unsubscribed); - $this->assertArrayHasKey('subscriber', get_object_vars($unsubscribed)); - $this->assertArrayHasKey('email_address', get_object_vars($unsubscribed->subscriber)); - $this->assertEquals(get_object_vars($unsubscribed->subscriber)['email_address'], $random_email); - - } - - /** - * List purchases - */ - public function testListPurchases() { - - $list_purchases = $this->api->list_purchases(['page' => 1]); - $this->assertInstanceOf('stdClass', $list_purchases); - $this->assertArrayHasKey('total_purchases', get_object_vars($list_purchases)); - $this->assertArrayHasKey('page', get_object_vars($list_purchases)); - $this->assertArrayHasKey('total_pages', get_object_vars($list_purchases)); - $this->assertArrayHasKey('purchases', get_object_vars($list_purchases)); - - } - - /** - * Get resources - */ - public function testGetResources() { - - $resources = ['forms', 'landing_pages', 'tags']; - - foreach ($resources as $resource) { - $get_resources = $this->api->get_resources($resource); - $this->assertTrue(is_array($get_resources) || empty($get_resources)); - if(count($get_resources) > 0) { - $get_resource = $get_resources[0]; - $this->assertInstanceOf('stdClass', $get_resource); - $this->assertArrayHasKey('id', get_object_vars($get_resource)); - $this->assertArrayHasKey('name', get_object_vars($get_resource)); - } - } - - } - - /** - * Get subscription forms - */ - public function testGetLandingPages() { - $landing_pages = $this->api->get_resources('subscription_forms'); - $this->assertTrue(is_array($landing_pages) || empty($landing_pages)); - } - - /** - * Get resource by url - */ - public function testGetResource() { - $markup = $this->api->get_resource($this->test_form_url); - $this->assertTrue($this->isHtml($markup)); - } - - /** - * Checks if string is html - * - * @param $string - * - * @return bool - */ - protected static function isHtml($string) { - return preg_match("/<[^<]+>/",$string,$m) != 0; - } - -} \ No newline at end of file +class ConvertKitAPITest extends TestCase +{ + /** + * ConvertKit Class Object + * + * @var object + */ + protected $api; + + /** + * Test subscribed user email + * + * @var string + */ + protected $test_email; + + /** + * Test subscribed user id + * + * @var string + */ + protected $test_user_id; + + /** + * Test tag id + * + * @var int + */ + protected $test_tag_id; + + /** + * Form url + * + * @var int + */ + protected $test_form_url; + + /** + * Form id + * + * @var int + */ + protected $test_form_id; + + protected function setUp() + { + include_once dirname(__FILE__) . "/config.php"; + + $api_key = CONVERTKIT_PUBLIC_KEY; + $api_secret = CONVERTKIT_SECRET_KEY; + $this->test_email = CONVERTKIT_TESTING_EMAIL; + $this->test_user_id = CONVERTKIT_TESTING_USER_ID; + $this->test_form_id = CONVERTKIT_TESTING_FORM_ID; + $this->test_tag_id = CONVERTKIT_TESTING_TAG_ID; + $this->test_form_url = CONVERTKIT_TESTING_FORM_URL; + + $this->api = new \ConvertKit_API\ConvertKit_API($api_key, $api_secret); + } + + /** + * @dataProvider inputGetResourcesArguments + * @expectedException InvalidArgumentException + * + * @param $input + */ + public function testGetResourcesArguments($input) + { + $this->api->get_resources($input); + } + + /** + * Data provider for @testGetResourcesArguments + * + * @return array + */ + public function inputGetResourcesArguments() + { + return [ + [2], + [['2', '1']], + [new stdClass()], + ]; + } + + /** + * @dataProvider inputGetSubscriberId + * @expectedException InvalidArgumentException + * + * @param $input + */ + public function testArgumentsGetSubscriberId($input) + { + $this->api->get_subscriber_id($input); + } + + /** + * Data provider for @testGetSubscriberId + * + * @return array + */ + public function inputGetSubscriberId() + { + return [ + [2], + [['2', '1']], + [new stdClass()], + ['teststring'], + ['teststring@'], + ]; + } + + /** + * @dataProvider inputGetSubscriber + * @expectedException InvalidArgumentException + * + * @param $input + */ + public function testArgumentsGetSubscriber($input) + { + $this->api->get_subscriber($input); + } + + /** + * Data provider for @testGetSubscriber + * + * @return array + */ + public function inputGetSubscriber() + { + return [ + [['2', '1']], + [new stdClass()], + ['teststring'], + [1.2], + [-10], + ]; + } + + /** + * @dataProvider inputAddTag + * @expectedException InvalidArgumentException + * + * @param $tag + * @param $options + */ + public function testArgumentsAddTag($tag, $options) + { + $this->api->add_tag($tag, $options); + } + + /** + * Data provider for @testAddTag + * + * @return array + */ + public function inputAddTag() + { + return [ + [['2', '1'], 1], + [new stdClass(), 2], + ['teststring', 3], + [3, 3], + ]; + } + + public function testIncorrectApiData() + { + $api_key = 'test'; + $api_secret = 'test'; + + $test_client = new \ConvertKit_API\ConvertKit_API($api_key, $api_secret); + $this->assertFalse($test_client->get_subscriber_id($this->test_email)); + $this->assertFalse($test_client->get_subscriber($this->test_user_id)); + $this->assertFalse($test_client->get_subscriber_tags($this->test_user_id)); + } + + /** + * Get subscriber id by email + */ + public function testGetSubscriberId() + { + $subscriber_id = $this->api->get_subscriber_id($this->test_email); + $this->assertInternalType("int", $subscriber_id); + } + + /** + * Get subscriber by id + */ + public function testGetSubscriber() + { + $subscriber = $this->api->get_subscriber($this->test_user_id); + $this->assertInstanceOf('stdClass', $subscriber); + $this->assertArrayHasKey('subscriber', get_object_vars($subscriber)); + $this->assertArrayHasKey('id', get_object_vars($subscriber->subscriber)); + $this->assertEquals(get_object_vars($subscriber->subscriber)['id'], $this->test_user_id); + } + + /** + * Get subscriber tags + */ + public function testGetSubscriberTags() + { + $subscriber = $this->api->get_subscriber_tags($this->test_user_id); + $this->assertInstanceOf('stdClass', $subscriber); + $this->assertArrayHasKey('tags', get_object_vars($subscriber)); + } + + /** + * Subscribe and unsubscribe from form + */ + public function testUserActions() + { + $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; + + /* + * Subscribe + */ + $options = [ + 'email' => $random_email, + 'name' => 'Full Name', + 'first_name' => 'First Name', + 'tags' => $this->test_tag_id, + 'fields' => [ + 'phone' => 134567891243, + 'shirt_size' => 'M', + 'website_url' => 'testurl.com', + ], + ]; + + $subscribed = $this->api->form_subscribe($this->test_form_id, $options); + $this->assertInstanceOf('stdClass', $subscribed); + $this->assertArrayHasKey('subscription', get_object_vars($subscribed)); + $this->assertArrayHasKey('id', get_object_vars($subscribed->subscription)); + $this->assertEquals(get_object_vars($subscribed->subscription)['subscribable_id'], $this->test_form_id); + + /* + * Add tag + */ + $added_tag = $this->api->add_tag($this->test_tag_id, [ + 'email' => $random_email, + ]); + $this->assertInstanceOf('stdClass', $added_tag); + $this->assertArrayHasKey('subscription', get_object_vars($added_tag)); + $this->assertArrayHasKey('id', get_object_vars($added_tag->subscription)); + $this->assertEquals(get_object_vars($added_tag->subscription)['subscribable_id'], $this->test_tag_id); + $this->assertEquals(get_object_vars($added_tag->subscription)['subscribable_type'], 'tag'); + + /* + * Purchase + */ + $purchase_options = [ + 'purchase' => [ + 'email_address' => $random_email, + 'transaction_id' => str_shuffle('wfervdrtgsdewrafvwefds'), + 'subtotal' => 20.00, + 'tax' => 2.00, + 'shipping' => 2.00, + 'discount' => 3.00, + 'total' => 21.00, + 'status' => 'paid', + 'products' => array( + 0 => array( + 'name' => 'Floppy Disk (512k)', + 'sku' => '7890-ijkl', + 'unit_price' => 5.00, + 'quantity' => 2, + ), + ), + ], + ]; + $purchase = $this->api->create_purchase($purchase_options); + $this->assertInstanceOf('stdClass', $purchase); + $this->assertArrayHasKey('transaction_id', get_object_vars($purchase)); + + /* + * Unsubscribe + */ + $unsubscribed = $this->api->form_unsubscribe([ + 'email' => $random_email, + ]); + + $this->assertInstanceOf('stdClass', $unsubscribed); + $this->assertArrayHasKey('subscriber', get_object_vars($unsubscribed)); + $this->assertArrayHasKey('email_address', get_object_vars($unsubscribed->subscriber)); + $this->assertEquals(get_object_vars($unsubscribed->subscriber)['email_address'], $random_email); + } + + /** + * List purchases + */ + public function testListPurchases() + { + $list_purchases = $this->api->list_purchases(['page' => 1]); + $this->assertInstanceOf('stdClass', $list_purchases); + $this->assertArrayHasKey('total_purchases', get_object_vars($list_purchases)); + $this->assertArrayHasKey('page', get_object_vars($list_purchases)); + $this->assertArrayHasKey('total_pages', get_object_vars($list_purchases)); + $this->assertArrayHasKey('purchases', get_object_vars($list_purchases)); + } + + /** + * Get resources + */ + public function testGetResources() + { + $resources = ['forms', 'landing_pages', 'tags']; + + foreach ($resources as $resource) { + $get_resources = $this->api->get_resources($resource); + $this->assertTrue(is_array($get_resources) || empty($get_resources)); + if (count($get_resources) > 0) { + $get_resource = $get_resources[0]; + $this->assertInstanceOf('stdClass', $get_resource); + $this->assertArrayHasKey('id', get_object_vars($get_resource)); + $this->assertArrayHasKey('name', get_object_vars($get_resource)); + } + } + } + + /** + * Get subscription forms + */ + public function testGetLandingPages() + { + $landing_pages = $this->api->get_resources('subscription_forms'); + $this->assertTrue(is_array($landing_pages) || empty($landing_pages)); + } + + /** + * Get resource by url + */ + public function testGetResource() + { + $markup = $this->api->get_resource($this->test_form_url); + $this->assertTrue($this->isHtml($markup)); + } + + /** + * Checks if string is html + * + * @param $string + * + * @return bool + */ + protected static function isHtml($string) + { + return preg_match("/<[^<]+>/", $string, $m) != 0; + } + + /** + * List purchases + */ + public function testFormSubscriptions() + { + $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; + + /* + * Subscribe + */ + $options = [ + 'email' => $random_email, + 'name' => 'Full Name', + 'first_name' => 'First Name', + 'tags' => $this->test_tag_id, + 'fields' => [ + 'phone' => 134567891243, + 'shirt_size' => 'M', + 'website_url' => 'testurl.com', + ], + ]; + + $subscribed = $this->api->form_subscribe($this->test_form_id, $options); + + $subscriptions = $this->api->get_form_subscriptions($this->test_form_id); + $this->assertInstanceOf('stdClass', $subscriptions); + $this->assertArrayHasKey('total_subscriptions', get_object_vars($subscriptions)); + $this->assertArrayHasKey('page', get_object_vars($subscriptions)); + $this->assertArrayHasKey('total_pages', get_object_vars($subscriptions)); + $this->assertArrayHasKey('subscriptions', get_object_vars($subscriptions)); + } + + /** + * List purchases + */ + public function testTagSubscriptions() + { + $random_email = str_shuffle('1234567890') . 'test@growdevelopment.com'; + + /* + * Subscribe + */ + $options = [ + 'email' => $random_email, + 'name' => 'Full Name', + 'first_name' => 'First Name', + 'tags' => $this->test_tag_id, + 'fields' => [ + 'phone' => 134567891243, + 'shirt_size' => 'M', + 'website_url' => 'testurl.com', + ], + ]; + + $subscribed = $this->api->form_subscribe($this->test_form_id, $options); + + $subscriptions = $this->api->get_tag_subscriptions($this->test_tag_id); + $this->assertInstanceOf('stdClass', $subscriptions); + $this->assertArrayHasKey('total_subscriptions', get_object_vars($subscriptions)); + $this->assertArrayHasKey('page', get_object_vars($subscriptions)); + $this->assertArrayHasKey('total_pages', get_object_vars($subscriptions)); + $this->assertArrayHasKey('subscriptions', get_object_vars($subscriptions)); + } +}