diff --git a/.env.dist.testing b/.env.dist.testing index f7cfb50..6e6f963 100644 --- a/.env.dist.testing +++ b/.env.dist.testing @@ -4,6 +4,7 @@ CONVERTKIT_API_FORM_ID="2765139" CONVERTKIT_API_FORM_ID_2="2780977" CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099" CONVERTKIT_API_LANDING_PAGE_URL="https://cheerful-architect-3237.ck.page/cc5eb21744" +CONVERTKIT_API_LANDING_PAGE_CHARACTER_ENCODING_URL="https://cheerful-architect-3237.ck.page/cc5eb21744" CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages/470103" CONVERTKIT_API_SEQUENCE_ID="1030824" CONVERTKIT_API_SEQUENCE_EMAIL_ID="4533458" diff --git a/.env.example b/.env.example index 7cc34b4..09e8a97 100644 --- a/.env.example +++ b/.env.example @@ -16,6 +16,7 @@ CONVERTKIT_API_FORM_ID="2765139" CONVERTKIT_API_FORM_ID_2="2780977" CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099" CONVERTKIT_API_LANDING_PAGE_URL="https://cheerful-architect-3237.ck.page/cc5eb21744" +CONVERTKIT_API_LANDING_PAGE_CHARACTER_ENCODING_URL="https://cheerful-architect-3237.ck.page/cc5eb21744" CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages/470103" CONVERTKIT_API_SEQUENCE_ID="1030824" CONVERTKIT_API_SEQUENCE_EMAIL_ID="4533458" diff --git a/phpunit.xml b/phpunit.xml index dd54dd6..0d14407 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,7 +3,6 @@ tests - tests/ConvertKitAPITest.php diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index 93a4b13..c7d7c3f 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -14,8 +14,11 @@ /** * ConvertKit API class tests. + * + * Extended by ConvertKitAPIKeyTest and ConvertKitAPIOAuthTest, which define + * the authentication method to use when running these tests. */ -class ConvertKitAPITest extends TestCase +abstract class ConvertKitAPITest extends TestCase { use TestsTrait; @@ -102,18 +105,29 @@ protected function tearDown(): void } /** - * Assert that the given callable throws a ClientException, ServerException, or InvalidArgumentException. + * Assert that the given callable throws an exception. + * + * Any Throwable is accepted by default, as the API may return a ClientException + * or a ServerException depending on the error. Where the SDK validates arguments + * before performing an API request, specify $expected, to assert that the SDK's + * validation produced the error, and not the API. * * @since 2.7.0 * - * @param callable $fn Callable that should fail. + * @param callable $fn Callable that should fail. + * @param string|null $expected Expected exception class name. * @return void */ - protected function assertApiError(callable $fn): void + protected function assertApiError(callable $fn, string|null $expected = null): void { try { $fn(); } catch (\Throwable $e) { + if (!is_null($expected)) { + $this->assertInstanceOf($expected, $e); + return; + } + $this->assertTrue(true, 'Callable threw an exception as expected.'); return; } diff --git a/tests/TestsTrait.php b/tests/TestsTrait.php index 1bc0cfb..0f14ad0 100644 --- a/tests/TestsTrait.php +++ b/tests/TestsTrait.php @@ -6161,12 +6161,15 @@ public function testCreateWebhookWithEventParameter() */ public function testCreateWebhookWithInvalidEvent() { - $this->assertApiError(function () { - return $this->api->create_webhook( - url: 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds'), - event: 'invalid.event' - ); - }); + $this->assertApiError( + function () { + return $this->api->create_webhook( + url: 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds'), + event: 'invalid.event' + ); + }, + \InvalidArgumentException::class + ); } /** @@ -6577,7 +6580,7 @@ public function testGetPurchasesPagination() } /** - * Test that get_purchases() returns the expected data. + * Test that get_purchase() returns the expected data. * * @since 1.0.0 * @@ -6863,7 +6866,7 @@ public function testGetResourceLegacyForm() */ public function testGetResourceLandingPage() { - $markup = $this->api->get_resource($_ENV['CONVERTKIT_API_LANDING_PAGE_URL']); + $markup = $this->api->get_resource($_ENV['CONVERTKIT_API_LANDING_PAGE_CHARACTER_ENCODING_URL']); // Assert that the markup is HTML. $this->assertTrue($this->isHtml($markup)); @@ -6900,9 +6903,12 @@ public function testGetResourceLegacyLandingPage() */ public function testGetResourceInvalidURL() { - $this->assertApiError(function () { - return $this->api->get_resource('not-a-url'); - }); + $this->assertApiError( + function () { + return $this->api->get_resource('not-a-url'); + }, + \InvalidArgumentException::class + ); } /** @@ -6954,14 +6960,18 @@ public function isHtml($string) /** * Helper method to assert the given key exists as an array in the API response. * + * Accepts either a stdClass object (PHP SDK, Guzzle-decoded) or an + * associative array (WP Libs, wp_remote_retrieve_body -> json_decode true), + * so the same trait file works verbatim in both repos. + * * @since 2.0.0 * - * @param object $result API Result. - * @param string $key Key. + * @param object|array $result API Result. + * @param string $key Key. */ public function assertDataExists($result, $key) { - $result = get_object_vars($result); + $result = is_object($result) ? get_object_vars($result) : $result; $this->assertArrayHasKey($key, $result); $this->assertIsArray($result[$key]); } @@ -6969,15 +6979,18 @@ public function assertDataExists($result, $key) /** * Helper method to assert pagination object exists in response. * + * Accepts either a stdClass object (PHP SDK) or an associative array + * (WP Libs), so the same trait file works verbatim in both repos. + * * @since 2.0.0 * - * @param object $result API Result. + * @param object|array $result API Result. */ public function assertPaginationExists($result) { - $result = get_object_vars($result); + $result = is_object($result) ? get_object_vars($result) : $result; $this->assertArrayHasKey('pagination', $result); - $pagination = get_object_vars($result['pagination']); + $pagination = is_object($result['pagination']) ? get_object_vars($result['pagination']) : $result['pagination']; $this->assertArrayHasKey('has_previous_page', $pagination); $this->assertArrayHasKey('has_next_page', $pagination); $this->assertArrayHasKey('start_cursor', $pagination);