Skip to content

Commit

Permalink
remove support for page query params (#115)
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Vieira <bruno@chartmogul.com>
  • Loading branch information
kmossco committed Oct 31, 2023
1 parent f40743f commit e44fc93
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 267 deletions.
32 changes: 32 additions & 0 deletions 6.0-Upgrade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Upgrading to chartmogul-php 6.0.0

This new version replaces the existing pagination for the `.all()` endpoints that used a combination of `page` and `per_page` parameters, and instead uses a `cursor` based pagination. So to list (as an example) Plans you now can:

```php
// Getting the first page
$plans = ChartMogul\Plan::all([
"per_page" => 1
]);

// This will return an array of plans (if available), and a cursor + has_more fields
{
"plans": [
{
"uuid": "some_uuid",
"data_source_uuid": "some_uuid",
"name": "Master Plan"
}
],
"has_more": true,
"cursor": "MjAyMy0wNy0yOFQwODowOToyMi4xNTQyMDMwMDBaJjk0NDQ0Mg=="
}

if ($plans->has_more) {
$more_plans = ChartMogul\Plan::all([
"per_page" => 1,
"cursor" => $plans->cursor
])
}
```

If you have existing code that relies on the `page` parameter, those requests will throw an error now alerting you of their deprecation.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning].
[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html

## [6.0.0] - 2023-10-30

### Removed
- Support for old pagination using `page` query params.

## [5.1.3] - 2023-10-03

### Added
Expand Down
12 changes: 12 additions & 0 deletions src/Exceptions/DeprecatedParameterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace ChartMogul\Exceptions;

/**
* DeprecatedParameterException Interface
*
* @codeCoverageIgnore
*/
class DeprecatedParameterException extends ChartMogulException
{
}
8 changes: 7 additions & 1 deletion src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Client implements ClientInterface
/**
* @var string
*/
private $apiVersion = '5.1.3';
private $apiVersion = '6.0.0';

/**
* @var string
Expand Down Expand Up @@ -163,6 +163,12 @@ function () use ($request) {

public function send($path = '', $method = 'GET', $data = [])
{
if (isset($data['page'])) {
throw new \ChartMogul\Exceptions\DeprecatedParameterException(
"The 'page' query parameter is deprecated"
);
}

$query = '';
if ($method === 'GET') {
$query = http_build_query($data);
Expand Down
60 changes: 3 additions & 57 deletions tests/Unit/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,36 +128,6 @@ class CustomerTest extends TestCase
}';

const SEARCH_CUSTOMER_JSON = '{
"entries":[
{
"id": 25647,
"uuid": "cus_de305d54-75b4-431b-adb2-eb6b9e546012",
"external_id": "34916129",
"email": "bob@examplecompany.com",
"name": "Example Company",
"address": {
"address_zip": "0185128",
"city": "Nowhereville",
"country": "US",
"state": "Alaska"
},
"mrr": 3000,
"arr": 36000,
"status": "Active",
"customer-since": "2015-06-09T13:16:00-04:00",
"billing-system-url": "https:\/\/dashboard.stripe.com\/customers\/cus_4Z2ZpyJFuQ0XMb",
"chartmogul-url": "https:\/\/app.chartmogul.com\/#customers\/25647-Example_Company",
"billing-system-type": "Stripe",
"currency": "USD",
"currency-sign": "$"
}
],
"has_more":false,
"per_page":200,
"page":1
}';

const SEARCH_CUSTOMER_NEW_PAGINATION_JSON = '{
"entries":[
{
"id": 25647,
Expand Down Expand Up @@ -250,6 +220,7 @@ public function testRetrieveCustomer()
$this->assertTrue($result instanceof Customer);
$this->assertEquals($uuid, $result->uuid);
}

public function testCreateCustomer()
{
$stream = Psr7\stream_for(CustomerTest::CREATE_CUSTOMER_JSON);
Expand All @@ -276,35 +247,12 @@ public function testCreateCustomer()

$this->assertTrue($result instanceof Customer);
}

public function testSearchCustomer()
{

$stream = Psr7\stream_for(CustomerTest::SEARCH_CUSTOMER_JSON);
list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream);


$email = "bob@examplecompany.com";
$result = Customer::search($email, $cmClient);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
$this->assertEquals("email=".urlencode($email), $uri->getQuery());
$this->assertEquals("/v1/customers/search", $uri->getPath());

$this->assertTrue($result instanceof Collection);
$this->assertTrue($result[0] instanceof Customer);
$this->assertEquals($result->has_more, false);
$this->assertEquals($result->page, 1);
$this->assertEquals($result->per_page, 200);
}
public function testSearchCustomerNewPagination()
{

$stream = Psr7\stream_for(CustomerTest::SEARCH_CUSTOMER_NEW_PAGINATION_JSON);
list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream);


$email = "bob@examplecompany.com";
$result = Customer::search($email, $cmClient);
$request = $mockClient->getRequests()[0];
Expand All @@ -319,6 +267,7 @@ public function testSearchCustomerNewPagination()
$this->assertEquals($result->has_more, false);
$this->assertEquals($result->cursor, "cursor==");
}

public function testConnectSubscriptions()
{
$stream = Psr7\stream_for('{}');
Expand Down Expand Up @@ -350,19 +299,16 @@ public function testConnectSubscriptions()

public function testFindByExternalId()
{

$stream = Psr7\stream_for(CustomerTest::SEARCH_CUSTOMER_JSON);
list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream);


$result = Customer::findByExternalId("34916129", $cmClient);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
$this->assertEquals("external_id=34916129", $uri->getQuery());
$this->assertEquals("/v1/customers", $uri->getPath());

$this->assertTrue($result instanceof Customer);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testGetUserAgent()
->onlyMethods([])
->getMock();

$this->assertEquals("chartmogul-php/5.1.3/PHP-".PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION, $mock->getUserAgent());
$this->assertEquals("chartmogul-php/6.0.0/PHP-".PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION, $mock->getUserAgent());
}

public function testGetBasicAuthHeader()
Expand Down
91 changes: 12 additions & 79 deletions tests/Unit/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ class InvoiceTest extends TestCase
]
}
],
"current_page": 2,
"total_pages": 3
"has_more": false,
"cursor": "cursor=="
}';

const RETRIEVE_INVOICE_JSON = '{
Expand Down Expand Up @@ -120,111 +120,44 @@ class InvoiceTest extends TestCase
]
}';

const ALL_INVOICES_NEW_PAGINATION_JSON = '{
"invoices": [
{
"uuid": "inv_565c73b2-85b9-49c9-a25e-2b7df6a677c9",
"customer_uuid": "cus_f466e33d-ff2b-4a11-8f85-417eb02157a7",
"external_id": "INV0001",
"date": "2015-11-01T00:00:00.000Z",
"due_date": "2015-11-15T00:00:00.000Z",
"currency": "USD",
"line_items": [
{
"uuid": "li_d72e6843-5793-41d0-bfdf-0269514c9c56",
"external_id": null,
"type": "subscription",
"subscription_uuid": "sub_e6bc5407-e258-4de0-bb43-61faaf062035",
"plan_uuid": "pl_eed05d54-75b4-431b-adb2-eb6b9e543206",
"prorated": false,
"service_period_start": "2015-11-01T00:00:00.000Z",
"service_period_end": "2015-12-01T00:00:00.000Z",
"amount_in_cents": 5000,
"quantity": 1,
"discount_code": "PSO86",
"discount_amount_in_cents": 1000,
"tax_amount_in_cents": 900,
"transaction_fees_currency": "EUR",
"discount_description": "5 EUR",
"account_code": null
},
{
"uuid": "li_0cc8c112-beac-416d-af11-f35744ca4e83",
"external_id": null,
"type": "one_time",
"description": "Setup Fees",
"amount_in_cents": 2500,
"quantity": 1,
"discount_code": "PSO86",
"discount_amount_in_cents": 500,
"tax_amount_in_cents": 450,
"transaction_fees_currency": "EUR",
"discount_description": "2 EUR",
"account_code": null
}
],
"transactions": [
{
"uuid": "tr_879d560a-1bec-41bb-986e-665e38a2f7bc",
"external_id": null,
"type": "payment",
"date": "2015-11-05T00:14:23.000Z",
"result": "successful"
}
]
}
],
"has_more": false,
"cursor": "cursor=="
}';

public function testCreateInvoiceFailsOnValidation()
{
$stream = Psr7\stream_for('{invoices: [{errors: {"plan_id": "doesn\'t exist"}}]}');
list($cmClient, $mockClient) = $this->getMockClient(0, [422], $stream);

$this->expectException(\ChartMogul\Exceptions\SchemaInvalidException::class);
$restult = CustomerInvoices::create(
$result = CustomerInvoices::create(
[
'customer_uuid' => 'some_id',
'invoices' => [['mock' => 'invoice']]
], $cmClient
);
}

public function testAllInvoices()
public function testAllInvoicesDeprecatedPagination()
{
$stream = Psr7\stream_for(InvoiceTest::ALL_INVOICES_JSON);
list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream);
list($cmClient, $mockClient) = $this->getMockClientException(
0, [200], $stream, [\ChartMogul\Exceptions\DeprecatedParameterException::class]
);

// $this->expectException(\ChartMogul\Exceptions\DeprecatedParameterException::class);
$query = ["page" => 2, "external_id" => "INV0001"];
$result = Invoice::all($query, $cmClient);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
$this->assertEquals("page=2&external_id=INV0001", $uri->getQuery());
$this->assertEquals("/v1/invoices", $uri->getPath());

$this->assertEquals(1, sizeof($result));
$this->assertTrue($result[0] instanceof Invoice);
$this->assertEquals("cus_f466e33d-ff2b-4a11-8f85-417eb02157a7", $result[0]->customer_uuid);
$this->assertEquals(2, $result->current_page);
$this->assertEquals(3, $result->total_pages);
}

public function testAllInvoicesNewPagination()
public function testAllInvoices()
{
$stream = Psr7\stream_for(InvoiceTest::ALL_INVOICES_NEW_PAGINATION_JSON);
$stream = Psr7\stream_for(InvoiceTest::ALL_INVOICES_JSON);
list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream);

$query = ["page" => 2, "external_id" => "INV0001"];
$query = ["per_page" => 2, "external_id" => "INV0001"];
$result = Invoice::all($query, $cmClient);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
$this->assertEquals("page=2&external_id=INV0001", $uri->getQuery());
$this->assertEquals("per_page=2&external_id=INV0001", $uri->getQuery());
$this->assertEquals("/v1/invoices", $uri->getPath());

$this->assertEquals(1, sizeof($result));
Expand Down
Loading

0 comments on commit e44fc93

Please sign in to comment.