Skip to content

Commit

Permalink
List invoices with unit test (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkopac committed May 3, 2017
1 parent 4d1389a commit c7c6414
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,16 @@ $ci = ChartMogul\CustomerInvoices::all([
]);
```

**List Invoices**

```php
$invoices = ChartMogul\Invoice::all([
'external_id' => 'my_invoice',
'page' => 1,
'per_page' => 200
]);
```

### Transactions

**Create a Transaction**
Expand Down
18 changes: 17 additions & 1 deletion src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use ChartMogul\LineItems\AbstractLineItem;
use ChartMogul\LineItems\OneTime;
use ChartMogul\LineItems\Subscription as SubsItem;
use ChartMogul\Service\AllTrait;
use ChartMogul\Resource\AbstractResource;
use ChartMogul\Transactions\AbstractTransaction;
use ChartMogul\Transactions\Payment;
use ChartMogul\Transactions\Refund;
Expand All @@ -13,11 +15,25 @@
/**
* @property-read string $uuid
*/
class Invoice extends \ChartMogul\Resource\AbstractModel
class Invoice extends AbstractResource
{
use AllTrait;
/**
* @ignore
*/
const RESOURCE_PATH = '/v1/invoices';
/**
* @ignore
*/
const ENTRY_CLASS = Invoice::class;
/**
* @ignore
*/
const ROOT_KEY = 'invoices';

protected $uuid;

public $customer_uuid;
public $date;
public $currency;
public $line_items = [];
Expand Down
88 changes: 88 additions & 0 deletions tests/Unit/InvoiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

use ChartMogul\Http\Client;
use ChartMogul\Invoice;
use ChartMogul\Exceptions\ChartMogulException;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Response;

class InvoiceTest extends PHPUnit_Framework_TestCase
{
const ALL_INVOICE_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,
"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,
"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"
}
]
}
],
"current_page": 2,
"total_pages": 3
}';

public function testAllInvoices()
{
$stream = Psr7\stream_for(InvoiceTest::ALL_INVOICE_JSON);
$response = new Response(200, ['Content-Type' => 'application/json'], $stream);
$mockClient = new \Http\Mock\Client();
$mockClient->addResponse($response);

$cmClient = new Client(null, $mockClient);
$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);
}
}

0 comments on commit c7c6414

Please sign in to comment.