Skip to content

Commit

Permalink
Refactoring & implement Products API.
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Sep 20, 2020
1 parent a569828 commit 6b40da1
Show file tree
Hide file tree
Showing 13 changed files with 516 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/Traits/ChargifyAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ trait ChargifyAPI
use ChargifyAPI\Adjustments;
use ChargifyAPI\BillingPortal;
use ChargifyAPI\Customers;
use ChargifyAPI\ProductFamilies;
use ChargifyAPI\Products;
use ChargifyAPI\ProductsFamilies;
use ChargifyAPI\Sites;
use ChargifyAPI\Stats;
use ChargifyAPI\Transactions;
Expand Down
96 changes: 96 additions & 0 deletions src/Traits/ChargifyAPI/Products.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Srmklive\Chargify\Traits\ChargifyAPI;

trait Products
{
/**
* Create a new product.
*
* @param string $product_family_id
* @param array $data
*
* @return array
*/
public function product_create(string $product_family_id, array $data): array
{
$this->apiEndPoint = "/product_families/{$product_family_id}/products.json";

$this->verb = 'post';

$this->options['json'] = [
'product' => $data,
];

return $this->doChargifyRequest();
}

/**
* Get product details.
*
* @param string $product_id
*
* @return array
*/
public function product_details(string $product_id): array
{
$this->apiEndPoint = "/products/{$product_id}.json";

$this->verb = 'get';

return $this->doChargifyRequest();
}

/**
* Get product details via API Handle.
*
* @param string $handle
*
* @return array
*/
public function product_details_by_handle(string $handle): array
{
$this->apiEndPoint = "/products/handle/{$handle}.json";

$this->verb = 'get';

return $this->doChargifyRequest();
}

/**
* Update product details.
*
* @param string $product_id
* @param array $data
*
* @return array
*/
public function product_update(string $product_id, array $data): array
{
$this->apiEndPoint = "/products/{$product_id}.json";

$this->verb = 'put';

$this->options['json'] = [
'product' => $data,
];

return $this->doChargifyRequest();
}

/**
* Archive a product.
*
* @param string $product_id
*
* @return array
*/
public function product_archive(string $product_id): array
{
$this->apiEndPoint = "/products/{$product_id}.json";

$this->verb = 'delete';

return $this->doChargifyRequest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Srmklive\Chargify\Traits\ChargifyAPI;

trait ProductFamilies
trait ProductsFamilies
{
/**
* Create a product family.
Expand Down Expand Up @@ -53,4 +53,20 @@ public function product_families_list(): array

return $this->doChargifyRequest();
}

/**
* Get products list for a product family.
*
* @param int $family_id
*
* @return array
*/
public function product_family_products(int $family_id): array
{
$this->apiEndPoint = "/product_families/{$family_id}/products.json";

$this->verb = 'get';

return $this->doChargifyRequest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Srmklive\Chargify\Tests\MockRequestPayloads;
use Srmklive\Chargify\Tests\MockResponsePayloads;

class ProductFamiliesTest extends TestCase
class ProductsFamiliesTest extends TestCase
{
use MockClientClasses;
use MockRequestPayloads;
Expand Down Expand Up @@ -70,4 +70,19 @@ public function it_can_list_all_product_families()
$this->assertNotEmpty($response);
$this->assertArrayHasKey('product_family', $response[0]);
}

/** @test */
public function it_can_get_products_for_a_product_family()
{
$expectedResponse = $this->mockProductsListResponse();

$this->client->setClient(
$this->mock_http_client($expectedResponse)
);

$response = $this->client->product_family_products(933860);

$this->assertNotEmpty($response);
$this->assertArrayHasKey('product', $response[0]);
}
}
105 changes: 105 additions & 0 deletions tests/Feature/ProductsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Srmklive\Chargify\Tests\Feature;

use PHPUnit\Framework\TestCase;
use Srmklive\Chargify\Services\ChargifyClient;
use Srmklive\Chargify\Tests\MockClientClasses;
use Srmklive\Chargify\Tests\MockRequestPayloads;
use Srmklive\Chargify\Tests\MockResponsePayloads;

class ProductsTest extends TestCase
{
use MockClientClasses;
use MockRequestPayloads;
use MockResponsePayloads;

/** @var \Srmklive\Chargify\Services\ChargifyClient */
protected $client;

protected function setUp(): void
{
$this->client = new ChargifyClient($this->getMockApiCredentials());
parent::setUp();
}


/** @test */
public function it_can_create_a_product()
{
$expectedResponse = $this->mockProductDetailsResponse();

$expectedParams = $this->mockCreateProductParams();

$this->client->setClient(
$this->mock_http_client($expectedResponse)
);

$response = $this->client->product_create('933860', $expectedParams);

$this->assertNotEmpty($response);
$this->assertArrayHasKey('product', $response);
}

/** @test */
public function it_can_get_details_for_a_product()
{
$expectedResponse = $this->mockProductDetailsResponse();

$this->client->setClient(
$this->mock_http_client($expectedResponse)
);

$response = $this->client->product_details('4364984');

$this->assertNotEmpty($response);
$this->assertArrayHasKey('product', $response);
}

/** @test */
public function it_can_get_details_for_a_product_by_product_handle()
{
$expectedResponse = $this->mockProductDetailsResponse();

$this->client->setClient(
$this->mock_http_client($expectedResponse)
);

$response = $this->client->product_details_by_handle('gold');

$this->assertNotEmpty($response);
$this->assertArrayHasKey('product', $response);
}

/** @test */
public function it_can_update_details_for_a_product()
{
$expectedResponse = $this->mockProductDetailsResponse();

$expectedParams = $this->mockCreateProductParams();

$this->client->setClient(
$this->mock_http_client($expectedResponse)
);

$response = $this->client->product_update('4364984', $expectedParams);

$this->assertNotEmpty($response);
$this->assertArrayHasKey('product', $response);
}

/** @test */
public function it_can_archive_a_product()
{
$expectedResponse = $this->mockProductDetailsResponse();

$this->client->setClient(
$this->mock_http_client($expectedResponse)
);

$response = $this->client->product_archive('4364984');

$this->assertNotEmpty($response);
$this->assertArrayHasKey('product', $response);
}
}
3 changes: 2 additions & 1 deletion tests/MockRequestPayloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
trait MockRequestPayloads
{
use Mocks\Requests\Customers;
use Mocks\Requests\ProductFamilies;
use Mocks\Requests\Products;
use Mocks\Requests\ProductsFamilies;
}
3 changes: 2 additions & 1 deletion tests/MockResponsePayloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ trait MockResponsePayloads
use Mocks\Responses\Adjustments;
use Mocks\Responses\BillingPortal;
use Mocks\Responses\Customers;
use Mocks\Responses\ProductFamilies;
use Mocks\Responses\Products;
use Mocks\Responses\ProductsFamilies;
use Mocks\Responses\Sites;
use Mocks\Responses\Stats;
use Mocks\Responses\Transactions;
Expand Down
25 changes: 25 additions & 0 deletions tests/Mocks/Requests/Products.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Srmklive\Chargify\Tests\Mocks\Requests;

trait Products
{
/**
* @return array
*/
protected function mockCreateProductParams()
{
return \GuzzleHttp\json_decode('{
"name": "Gold Plan",
"handle": "gold",
"description": "This is our gold plan.",
"accounting_code": "123",
"request_credit_card": true,
"price_in_cents": 1000,
"interval": 1,
"interval_unit": "month",
"auto_create_signup_page": true,
"tax_code": "D0000000"
}', true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Srmklive\Chargify\Tests\Mocks\Requests;

trait ProductFamilies
trait ProductsFamilies
{
/**
* @return array
Expand Down

0 comments on commit 6b40da1

Please sign in to comment.