Skip to content

Commit 7bdbfb9

Browse files
authored
Merge pull request #85 from ConvertKit/v4-api-segments
v4 API: Segments
2 parents af25143 + 4ca113b commit 7bdbfb9

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

src/ConvertKit_API.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,6 +1760,44 @@ public function create_purchase(
17601760
return $this->post('purchases', $options);
17611761
}
17621762

1763+
/**
1764+
* List segments.
1765+
*
1766+
* @param boolean $include_total_count To include the total count of records in the response, use true.
1767+
* @param string $after_cursor Return results after the given pagination cursor.
1768+
* @param string $before_cursor Return results before the given pagination cursor.
1769+
* @param integer $per_page Number of results to return.
1770+
*
1771+
* @since 2.0.0
1772+
*
1773+
* @see https://developers.convertkit.com/v4.html#convertkit-api-segments
1774+
*
1775+
* @return false|mixed
1776+
*/
1777+
public function get_segments(
1778+
bool $include_total_count = false,
1779+
string $after_cursor = '',
1780+
string $before_cursor = '',
1781+
int $per_page = 100
1782+
) {
1783+
// Build parameters.
1784+
$options = ['include_total_count' => $include_total_count];
1785+
1786+
// Build pagination parameters.
1787+
$options = $this->build_pagination_params(
1788+
params: $options,
1789+
after_cursor: $after_cursor,
1790+
before_cursor: $before_cursor,
1791+
per_page: $per_page
1792+
);
1793+
1794+
// Send request.
1795+
return $this->get(
1796+
endpoint: 'segments',
1797+
args: $options
1798+
);
1799+
}
1800+
17631801
/**
17641802
* Get markup from ConvertKit for the provided $url.
17651803
*

tests/ConvertKitAPITest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4012,6 +4012,105 @@ public function testCreatePurchaseWithInvalidData()
40124012
);
40134013
}
40144014

4015+
/**
4016+
* Test that get_segments() returns the expected data.
4017+
*
4018+
* @since 2.0.0
4019+
*
4020+
* @return void
4021+
*/
4022+
public function testGetSegments()
4023+
{
4024+
$result = $this->api->get_segments();
4025+
4026+
// Assert segments and pagination exist.
4027+
$this->assertDataExists($result, 'segments');
4028+
$this->assertPaginationExists($result);
4029+
}
4030+
4031+
/**
4032+
* Test that get_segments() returns the expected data
4033+
* when the total count is included.
4034+
*
4035+
* @since 1.0.0
4036+
*
4037+
* @return void
4038+
*/
4039+
public function testGetSegmentsWithTotalCount()
4040+
{
4041+
$result = $this->api->get_segments(
4042+
include_total_count: true
4043+
);
4044+
4045+
// Assert segments and pagination exist.
4046+
$this->assertDataExists($result, 'segments');
4047+
$this->assertPaginationExists($result);
4048+
4049+
// Assert total count is included.
4050+
$this->assertArrayHasKey('total_count', get_object_vars($result->pagination));
4051+
$this->assertGreaterThan(0, $result->pagination->total_count);
4052+
}
4053+
4054+
/**
4055+
* Test that get_segments() returns the expected data
4056+
* when pagination parameters and per_page limits are specified.
4057+
*
4058+
* @since 2.0.0
4059+
*
4060+
* @return void
4061+
*/
4062+
public function testGetSegmentsPagination()
4063+
{
4064+
$result = $this->api->get_segments(
4065+
per_page: 1
4066+
);
4067+
4068+
// Assert segments and pagination exist.
4069+
$this->assertDataExists($result, 'segments');
4070+
$this->assertPaginationExists($result);
4071+
4072+
// Assert a single segment was returned.
4073+
$this->assertCount(1, $result->segments);
4074+
4075+
// Assert has_previous_page and has_next_page are correct.
4076+
$this->assertFalse($result->pagination->has_previous_page);
4077+
$this->assertTrue($result->pagination->has_next_page);
4078+
4079+
// Use pagination to fetch next page.
4080+
$result = $this->api->get_segments(
4081+
per_page: 1,
4082+
after_cursor: $result->pagination->end_cursor
4083+
);
4084+
4085+
// Assert segments and pagination exist.
4086+
$this->assertDataExists($result, 'segments');
4087+
$this->assertPaginationExists($result);
4088+
4089+
// Assert a single segment was returned.
4090+
$this->assertCount(1, $result->segments);
4091+
4092+
// Assert has_previous_page and has_next_page are correct.
4093+
$this->assertTrue($result->pagination->has_previous_page);
4094+
$this->assertTrue($result->pagination->has_next_page);
4095+
4096+
// Use pagination to fetch previous page.
4097+
$result = $this->api->get_segments(
4098+
per_page: 1,
4099+
before_cursor: $result->pagination->start_cursor
4100+
);
4101+
4102+
// Assert segments and pagination exist.
4103+
$this->assertDataExists($result, 'segments');
4104+
$this->assertPaginationExists($result);
4105+
4106+
// Assert a single segment was returned.
4107+
$this->assertCount(1, $result->segments);
4108+
4109+
// Assert has_previous_page and has_next_page are correct.
4110+
$this->assertFalse($result->pagination->has_previous_page);
4111+
$this->assertTrue($result->pagination->has_next_page);
4112+
}
4113+
40154114
/**
40164115
* Test that fetching a legacy form's markup works.
40174116
*

0 commit comments

Comments
 (0)