Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<testsuites>
<testsuite name="ConvertKit API Tests">
<directory>tests</directory>
<exclude>tests/ConvertKitAPITest.php</exclude>
</testsuite>
</testsuites>
</phpunit>
22 changes: 18 additions & 4 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
47 changes: 30 additions & 17 deletions tests/TestsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

/**
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
);
}

/**
Expand Down Expand Up @@ -6954,30 +6960,37 @@ 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<string, mixed> $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]);
}

/**
* 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<string, mixed> $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);
Expand Down