Skip to content
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
15 changes: 2 additions & 13 deletions src/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use ChartMogul\Service\CreateTrait;
use ChartMogul\Service\AllTrait;
use ChartMogul\Service\DestroyTrait;
use ChartMogul\Service\RequestService;
use ChartMogul\Service\GetTrait;

/**
* @codeCoverageIgnore
Expand All @@ -24,6 +24,7 @@ class DataSource extends AbstractResource
use CreateTrait;
use AllTrait;
use DestroyTrait;
use GetTrait;

/**
* @ignore
Expand All @@ -49,16 +50,4 @@ class DataSource extends AbstractResource
protected $invoice_handling_setting;

public $name;

public static function retrieve($uuid, $query = [], ?ClientInterface $client = null)
{
return (new RequestService($client))
->setResourceClass(static::class)
->getWithQuery($uuid, $query);
}

public static function get($uuid, $query = [], ?ClientInterface $client = null)
{
return static::retrieve($uuid, $query, $client);
}
}
20 changes: 20 additions & 0 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@

/**
* @property-read string $uuid
* @property-read string $customer_uuid
* @property-read string $external_id
* @property-read string $data_source_uuid
* @property-read string $customer_external_id
* @property-read string $currency
* @property-read string $date
* @property-read string $due_date
* @property-read bool $disabled
* @property-read string $disabled_at
* @property-read string $disabled_by
* @property-read array $edit_history_summary
* @property-read array $errors
* @property-read ArrayCollection $line_items
* @property-read ArrayCollection $transactions
*/
class Invoice extends AbstractResource
{
Expand Down Expand Up @@ -48,6 +62,12 @@ class Invoice extends AbstractResource
public $date;
public $due_date;

public $disabled;
public $disabled_at;
public $disabled_by;
public $edit_history_summary;
public $errors;

public $line_items = [];
public $transactions = [];

Expand Down
17 changes: 11 additions & 6 deletions src/Service/GetTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ trait GetTrait
*
* @return resource
*/
public static function retrieve($uuid, ?ClientInterface $client = null)
public static function retrieve($uuid, ?ClientInterface $client = null, array $query = [])
{
return (new RequestService($client))
->setResourceClass(static::class)
->get($uuid);
$requestService = (new RequestService($client))
->setResourceClass(static::class);

if (empty($query)) {
return $requestService->get($uuid);
}

return $requestService->getWithQuery($uuid, $query);
}

public static function get($uuid, ?ClientInterface $client = null)
public static function get($uuid, ?ClientInterface $client = null, array $query = [])
{
return static::retrieve($uuid, $client);
return static::retrieve($uuid, $client, $query);
}
}
70 changes: 67 additions & 3 deletions tests/Unit/DataSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ public function testRetrieveDataSource()

$uuid = 'ds_fef05d54-47b4-431b-aed2-eb6b9e545430';

$result = DataSource::retrieve($uuid, [
$result = DataSource::retrieve($uuid, $cmClient, [
'with_processing_status' => true,
'with_auto_churn_subscription_setting' => true,
'with_invoice_handling_setting' => true
], $cmClient);
]);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
Expand Down Expand Up @@ -174,7 +174,7 @@ public function testGetDataSource()
$uuid = 'ds_fef05d54-47b4-431b-aed2-eb6b9e545430';

// Test the alias method get()
$result = DataSource::get($uuid, [], $cmClient);
$result = DataSource::get($uuid, $cmClient);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
Expand All @@ -186,6 +186,70 @@ public function testGetDataSource()
$this->assertEquals($uuid, $result->uuid);
}

public function testRetrieveDataSourceWithProcessingStatus()
{
$stream = Psr7\stream_for(DataSourceTest::RETRIEVE_DATA_SOURCE_JSON);
list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream);

$uuid = 'ds_fef05d54-47b4-431b-aed2-eb6b9e545430';

$result = DataSource::retrieve($uuid, $cmClient, ['with_processing_status' => true]);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
$this->assertEquals("with_processing_status=1", $uri->getQuery());
$this->assertEquals("/v1/data_sources/".$uuid, $uri->getPath());
}

public function testRetrieveDataSourceWithAutoChurnSubscriptionSetting()
{
$stream = Psr7\stream_for(DataSourceTest::RETRIEVE_DATA_SOURCE_JSON);
list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream);

$uuid = 'ds_fef05d54-47b4-431b-aed2-eb6b9e545430';

$result = DataSource::retrieve($uuid, $cmClient, ['with_auto_churn_subscription_setting' => true]);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
$this->assertEquals("with_auto_churn_subscription_setting=1", $uri->getQuery());
$this->assertEquals("/v1/data_sources/".$uuid, $uri->getPath());
}

public function testRetrieveDataSourceWithInvoiceHandlingSetting()
{
$stream = Psr7\stream_for(DataSourceTest::RETRIEVE_DATA_SOURCE_JSON);
list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream);

$uuid = 'ds_fef05d54-47b4-431b-aed2-eb6b9e545430';

$result = DataSource::retrieve($uuid, $cmClient, ['with_invoice_handling_setting' => true]);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
$this->assertEquals("with_invoice_handling_setting=1", $uri->getQuery());
$this->assertEquals("/v1/data_sources/".$uuid, $uri->getPath());
}

public function testRetrieveDataSourceWithProcessingStatusFalse()
{
$stream = Psr7\stream_for(DataSourceTest::RETRIEVE_DATA_SOURCE_JSON);
list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream);

$uuid = 'ds_fef05d54-47b4-431b-aed2-eb6b9e545430';

$result = DataSource::retrieve($uuid, $cmClient, ['with_processing_status' => false]);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
$this->assertEquals("with_processing_status=0", $uri->getQuery());
$this->assertEquals("/v1/data_sources/".$uuid, $uri->getPath());
}

public function testListDataSources()
{
$stream = Psr7\stream_for(DataSourceTest::LIST_DATA_SOURCES_JSON);
Expand Down
139 changes: 139 additions & 0 deletions tests/Unit/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ class InvoiceTest extends TestCase
"date": "2015-11-01T00:00:00.000Z",
"due_date": "2015-11-15T00:00:00.000Z",
"currency": "USD",
"disabled": false,
"disabled_at": null,
"disabled_by": null,
"edit_history_summary": {
"values_changed": {
"amount_in_cents": {
"original_value": 4500,
"edited_value": 5000
}
},
"latest_edit_author": "admin@example.com",
"latest_edit_performed_at": "2024-01-10T12:00:00.000Z"
},
"errors": null,
"line_items": [
{
"uuid": "li_d72e6843-5793-41d0-bfdf-0269514c9c56",
Expand Down Expand Up @@ -75,6 +89,27 @@ class InvoiceTest extends TestCase
"date": "2015-11-01T00:00:00.000Z",
"due_date": "2015-11-15T00:00:00.000Z",
"currency": "USD",
"disabled": true,
"disabled_at": "2024-01-15T10:30:00.000Z",
"disabled_by": "user@example.com",
"edit_history_summary": {
"values_changed": {
"currency": {
"original_value": "EUR",
"edited_value": "USD"
},
"date": {
"original_value": "2024-01-01T00:00:00.000Z",
"edited_value": "2024-01-02T00:00:00.000Z"
}
},
"latest_edit_author": "editor@example.com",
"latest_edit_performed_at": "2024-01-20T15:45:00.000Z"
},
"errors": {
"currency": ["Currency is invalid", "Currency must be supported"],
"date": ["Date is in the future"]
},
"line_items": [
{
"uuid": "li_d72e6843-5793-41d0-bfdf-0269514c9c56",
Expand Down Expand Up @@ -206,4 +241,108 @@ public function testRetrieveInvoice()
$this->assertTrue($result instanceof Invoice);
$this->assertEquals($uuid, $result->uuid);
}

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

$uuid = 'inv_565c73b2-85b9-49c9-a25e-2b7df6a677c9';

$result = Invoice::retrieve($uuid, $cmClient, ['validation_type' => 'all']);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
$this->assertEquals("validation_type=all", $uri->getQuery());
$this->assertEquals("/v1/invoices/".$uuid, $uri->getPath());

$this->assertTrue($result instanceof Invoice);
$this->assertEquals($uuid, $result->uuid);
}

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

$uuid = 'inv_565c73b2-85b9-49c9-a25e-2b7df6a677c9';

$result = Invoice::retrieve($uuid, $cmClient, [
'validation_type' => 'invalid',
'include_edit_histories' => true,
'with_disabled' => true
]);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
parse_str($uri->getQuery(), $queryParams);
$this->assertEquals('invalid', $queryParams['validation_type']);
$this->assertEquals('1', $queryParams['include_edit_histories']);
$this->assertEquals('1', $queryParams['with_disabled']);
$this->assertEquals("/v1/invoices/".$uuid, $uri->getPath());

$this->assertTrue($result instanceof Invoice);
$this->assertEquals($uuid, $result->uuid);

// Verify new fields are present
$this->assertTrue($result->disabled);
$this->assertEquals('2024-01-15T10:30:00.000Z', $result->disabled_at);
$this->assertEquals('user@example.com', $result->disabled_by);
$this->assertNotNull($result->edit_history_summary);
$this->assertIsArray($result->edit_history_summary);
$this->assertArrayHasKey('values_changed', $result->edit_history_summary);
$this->assertArrayHasKey('currency', $result->edit_history_summary['values_changed']);
$this->assertEquals('EUR', $result->edit_history_summary['values_changed']['currency']['original_value']);
$this->assertEquals('USD', $result->edit_history_summary['values_changed']['currency']['edited_value']);
$this->assertEquals('editor@example.com', $result->edit_history_summary['latest_edit_author']);
$this->assertEquals('2024-01-20T15:45:00.000Z', $result->edit_history_summary['latest_edit_performed_at']);
$this->assertNotNull($result->errors);
$this->assertIsArray($result->errors);
$this->assertArrayHasKey('currency', $result->errors);
$this->assertIsArray($result->errors['currency']);
$this->assertCount(2, $result->errors['currency']);
$this->assertEquals('Currency is invalid', $result->errors['currency'][0]);
$this->assertEquals('Currency must be supported', $result->errors['currency'][1]);
$this->assertArrayHasKey('date', $result->errors);
$this->assertIsArray($result->errors['date']);
$this->assertCount(1, $result->errors['date']);
$this->assertEquals('Date is in the future', $result->errors['date'][0]);
}

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

$result = Invoice::all(['validation_type' => 'all'], $cmClient);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
$this->assertEquals("validation_type=all", $uri->getQuery());
$this->assertEquals("/v1/invoices", $uri->getPath());
}

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

$result = Invoice::all([
'validation_type' => 'valid',
'include_edit_histories' => false,
'with_disabled' => true
], $cmClient);
$request = $mockClient->getRequests()[0];

$this->assertEquals("GET", $request->getMethod());
$uri = $request->getUri();
parse_str($uri->getQuery(), $queryParams);
$this->assertEquals('valid', $queryParams['validation_type']);
$this->assertEquals('0', $queryParams['include_edit_histories']);
$this->assertEquals('1', $queryParams['with_disabled']);
$this->assertEquals("/v1/invoices", $uri->getPath());
}
}
Loading