Skip to content
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

v4 API: Accounts #75

Merged
merged 13 commits into from
Mar 22, 2024
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
74 changes: 74 additions & 0 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,80 @@ public function get_account()
return $this->get('account');
}

/**
* Gets the account's colors
*
* @see https://developers.convertkit.com/v4.html#list-colors
*
* @return false|mixed
*/
public function get_account_colors()
{
return $this->get('account/colors');
}

/**
* Gets the account's colors
*
* @param array<string, string> $colors Hex colors.
*
* @see https://developers.convertkit.com/v4.html#list-colors
*
* @return false|mixed
*/
public function update_account_colors(array $colors)
{
return $this->put(
endpoint: 'account/colors',
args: ['colors' => $colors]
);
}

/**
* Gets the Creator Profile
*
* @see https://developers.convertkit.com/v4.html#get-creator-profile
*
* @return false|mixed
*/
public function get_creator_profile()
{
return $this->get('account/creator_profile');
}

/**
* Gets email stats
*
* @see https://developers.convertkit.com/v4.html#get-email-stats
*
* @return false|mixed
*/
public function get_email_stats()
{
return $this->get('account/email_stats');
}

/**
* Gets growth stats
*
* @param \DateTime $starting Gets stats for time period beginning on this date. Defaults to 90 days ago.
* @param \DateTime $ending Gets stats for time period ending on this date. Defaults to today.
*
* @see https://developers.convertkit.com/v4.html#get-growth-stats
*
* @return false|mixed
*/
public function get_growth_stats(\DateTime $starting = null, \DateTime $ending = null)
{
return $this->get(
'account/growth_stats',
[
'starting' => (!is_null($starting) ? $starting->format('Y-m-d') : ''),
'ending' => (!is_null($ending) ? $ending->format('Y-m-d') : ''),
]
);
}

/**
* Gets all forms.
*
Expand Down
182 changes: 179 additions & 3 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,17 +433,193 @@ public function testGetAccount()
$result = $this->api->get_account();
$this->assertInstanceOf('stdClass', $result);

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

$account = get_object_vars($result['account']);
$this->assertArrayHasKey('name', $account);
$this->assertArrayHasKey('plan_type', $account);
$this->assertArrayHasKey('primary_email_address', $account);
}

/**
* Test that get_account_colors() returns the expected data.
*
* @since 2.0.0
*
* @return void
*/
public function testGetAccountColors()
{
$result = $this->api->get_account_colors();
$this->assertInstanceOf('stdClass', $result);

$result = get_object_vars($result);
$this->assertArrayHasKey('colors', $result);
$this->assertIsArray($result['colors']);
}

/**
* Test that update_account_colors() updates the account's colors.
*
* @since 2.0.0
*
* @return void
*/
public function testUpdateAccountColors()
{
$result = $this->api->update_account_colors([
'#111111',
]);
$this->assertInstanceOf('stdClass', $result);

$result = get_object_vars($result);
$this->assertArrayHasKey('colors', $result);
$this->assertIsArray($result['colors']);
$this->assertEquals($result['colors'][0], '#111111');
}

/**
* Test that get_creator_profile() returns the expected data.
*
* @since 2.0.0
*
* @return void
*/
public function testGetCreatorProfile()
{
$result = $this->api->get_creator_profile();
$this->assertInstanceOf('stdClass', $result);

$result = get_object_vars($result);
$profile = get_object_vars($result['profile']);
$this->assertArrayHasKey('name', $profile);
$this->assertArrayHasKey('byline', $profile);
$this->assertArrayHasKey('bio', $profile);
$this->assertArrayHasKey('image_url', $profile);
$this->assertArrayHasKey('profile_url', $profile);
}

/**
* Test that get_email_stats() returns the expected data.
*
* @since 2.0.0
*
* @return void
*/
public function testGetEmailStats()
{
$result = $this->api->get_email_stats();
$this->assertInstanceOf('stdClass', $result);

$result = get_object_vars($result);
$stats = get_object_vars($result['stats']);
$this->assertArrayHasKey('sent', $stats);
$this->assertArrayHasKey('clicked', $stats);
$this->assertArrayHasKey('opened', $stats);
$this->assertArrayHasKey('email_stats_mode', $stats);
$this->assertArrayHasKey('open_tracking_enabled', $stats);
$this->assertArrayHasKey('click_tracking_enabled', $stats);
$this->assertArrayHasKey('starting', $stats);
$this->assertArrayHasKey('ending', $stats);
}

/**
* Test that get_growth_stats() returns the expected data.
*
* @since 2.0.0
*
* @return void
*/
public function testGetGrowthStats()
{
$result = $this->api->get_growth_stats();
$this->assertInstanceOf('stdClass', $result);

$result = get_object_vars($result);
$stats = get_object_vars($result['stats']);
$this->assertArrayHasKey('cancellations', $stats);
$this->assertArrayHasKey('net_new_subscribers', $stats);
$this->assertArrayHasKey('new_subscribers', $stats);
$this->assertArrayHasKey('subscribers', $stats);
$this->assertArrayHasKey('starting', $stats);
$this->assertArrayHasKey('ending', $stats);
}

/**
* Test that get_growth_stats() returns the expected data
* when a start date is specified.
*
* @since 2.0.0
*
* @return void
*/
public function testGetGrowthStatsWithStartDate()
{
// Define start and end dates.
$starting = new DateTime('now');
$starting->modify('-7 days');
$ending = new DateTime('now');

// Send request.
$result = $this->api->get_growth_stats(
starting: $starting
);
$this->assertInstanceOf('stdClass', $result);

// Confirm response object contains expected keys.
$result = get_object_vars($result);
$stats = get_object_vars($result['stats']);
$this->assertArrayHasKey('cancellations', $stats);
$this->assertArrayHasKey('net_new_subscribers', $stats);
$this->assertArrayHasKey('new_subscribers', $stats);
$this->assertArrayHasKey('subscribers', $stats);
$this->assertArrayHasKey('starting', $stats);
$this->assertArrayHasKey('ending', $stats);

// Assert start and end dates were honored.
$this->assertEquals($stats['starting'], $starting->format('Y-m-d') . 'T00:00:00-04:00');
$this->assertEquals($stats['ending'], $ending->format('Y-m-d') . 'T23:59:59-04:00');
}

/**
* Test that get_growth_stats() returns the expected data
* when an end date is specified.
*
* @since 2.0.0
*
* @return void
*/
public function testGetGrowthStatsWithEndDate()
{
// Define start and end dates.
$starting = new DateTime('now');
$starting->modify('-90 days');
$ending = new DateTime('now');
$ending->modify('-7 days');

// Send request.
$result = $this->api->get_growth_stats(
ending: $ending
);
$this->assertInstanceOf('stdClass', $result);

// Confirm response object contains expected keys.
$result = get_object_vars($result);
$stats = get_object_vars($result['stats']);
$this->assertArrayHasKey('cancellations', $stats);
$this->assertArrayHasKey('net_new_subscribers', $stats);
$this->assertArrayHasKey('new_subscribers', $stats);
$this->assertArrayHasKey('subscribers', $stats);
$this->assertArrayHasKey('starting', $stats);
$this->assertArrayHasKey('ending', $stats);

// Assert start and end dates were honored.
$this->assertEquals($stats['starting'], $starting->format('Y-m-d') . 'T00:00:00-04:00');
$this->assertEquals($stats['ending'], $ending->format('Y-m-d') . 'T23:59:59-04:00');
}

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