Skip to content

Add get_form_subscriptions() API functions #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2023
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
29 changes: 29 additions & 0 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,35 @@ public function get_account()
);
}

/**
* List subscriptions to a form
*
* @param integer $form_id Form 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-form
*
* @return false|mixed
*/
public function get_form_subscriptions(
int $form_id,
string $sort_order = 'asc',
string $subscriber_state = 'active',
int $page = 1
) {
return $this->get(
sprintf('forms/%s/subscriptions', $form_id),
[
'api_secret' => $this->api_secret,
'sort_order' => $sort_order,
'subscriber_state' => $subscriber_state,
'page' => $page,
]
);
}

/**
* Gets all sequences
*
Expand Down
113 changes: 113 additions & 0 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,119 @@ public function testGetAccount()
$this->assertArrayHasKey('primary_email_address', $result);
}

/**
* Test that get_form_subscriptions() returns the expected data
* when a valid Form ID is specified.
*
* @since 1.0.0
*
* @return void
*/
public function testGetFormSubscriptions()
{
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID']);

// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
$result = get_object_vars($result);
$this->assertArrayHasKey('total_subscriptions', $result);
$this->assertArrayHasKey('page', $result);
$this->assertArrayHasKey('total_pages', $result);
$this->assertArrayHasKey('subscriptions', $result);
$this->assertIsArray($result['subscriptions']);

// Assert sort order is ascending.
$this->assertGreaterThanOrEqual(
$result['subscriptions'][0]->created_at,
$result['subscriptions'][1]->created_at
);
}

/**
* Test that get_form_subscriptions() returns the expected data
* when a valid Form ID is specified and the sort order is descending.
*
* @since 1.0.0
*
* @return void
*/
public function testGetFormSubscriptionsWithDescSortOrder()
{
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID'], 'desc');

// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
$result = get_object_vars($result);
$this->assertArrayHasKey('total_subscriptions', $result);
$this->assertArrayHasKey('page', $result);
$this->assertArrayHasKey('total_pages', $result);
$this->assertArrayHasKey('subscriptions', $result);
$this->assertIsArray($result['subscriptions']);

// Assert sort order.
$this->assertLessThanOrEqual(
$result['subscriptions'][0]->created_at,
$result['subscriptions'][1]->created_at
);
}

/**
* Test that get_form_subscriptions() returns the expected data
* when a valid Form ID is specified and the subscription status
* is cancelled.
*
* @since 1.0.0
*
* @return void
*/
public function testGetFormSubscriptionsWithCancelledSubscriberState()
{
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID'], 'asc', 'cancelled');

// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
$result = get_object_vars($result);
$this->assertArrayHasKey('total_subscriptions', $result);
$this->assertEquals($result['total_subscriptions'], 0);
$this->assertArrayHasKey('page', $result);
$this->assertArrayHasKey('total_pages', $result);
$this->assertArrayHasKey('subscriptions', $result);
$this->assertIsArray($result['subscriptions']);
}

/**
* Test that get_form_subscriptions() returns the expected data
* when a valid Form ID is specified and the page is set to 2.
*
* @since 1.0.0
*
* @return void
*/
public function testGetFormSubscriptionsWithPage()
{
$result = $this->api->get_form_subscriptions((int) $_ENV['CONVERTKIT_API_FORM_ID'], 'asc', 'active', 2);

// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
$result = get_object_vars($result);
$this->assertArrayHasKey('total_subscriptions', $result);
$this->assertArrayHasKey('page', $result);
$this->assertEquals($result['page'], 2);
$this->assertArrayHasKey('total_pages', $result);
$this->assertArrayHasKey('subscriptions', $result);
$this->assertIsArray($result['subscriptions']);
}

/**
* Test that get_form_subscriptions() returns the expected data
* when a valid Form ID is specified.
*
* @since 1.0.0
*
* @return void
*/
public function testGetFormSubscriptionsWithInvalidFormID()
{
$this->expectException(GuzzleHttp\Exception\ClientException::class);
$result = $this->api->get_form_subscriptions(12345);
}

/**
* Test that get_sequences() returns the expected data.
*
Expand Down