Skip to content

Commit

Permalink
Modifications to recent PRs.
Browse files Browse the repository at this point in the history
- Added test cases for new doGraphQL for ApiHelper service
- Modified how errors are pulled in for doGraphQL
  • Loading branch information
gnikyt committed Jul 10, 2020
1 parent b8f1fe4 commit c63f95a
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 37 deletions.
2 changes: 0 additions & 2 deletions src/ShopifyApp/Actions/GetPlanUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public function __invoke(ShopId $shopId, NullablePlanId $planId): string

// Confirmation URL
$confirmation_url = null;

switch ($plan->getInterval()->toNative()) {
case ChargeInterval::ANNUAL()->toNative():
$api = $shop->apiHelper()->createChargeGraphQL(
Expand All @@ -80,7 +79,6 @@ public function __invoke(ShopId $shopId, NullablePlanId $planId): string

$confirmation_url = $api['confirmationUrl'];
break;

default:
$api = $shop->apiHelper()->createCharge(
ChargeType::fromNative($plan->getType()->toNative()),
Expand Down
3 changes: 1 addition & 2 deletions src/ShopifyApp/Objects/Enums/ChargeInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
use Funeralzone\ValueObjects\ValueObject;

/**
* Class ChargeInterval
* @package Osiset\ShopifyApp\Objects\Enums
* Charge interval with annual support.
*
* @method static ChargeInterval EVERY_30_DAYS()
* @method static ChargeInterval ANNUAL()
Expand Down
3 changes: 1 addition & 2 deletions src/ShopifyApp/Objects/Enums/PlanInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
use Funeralzone\ValueObjects\ValueObject;

/**
* Class PlanInterval
* @package Osiset\ShopifyApp\Objects\Enums
* Plan interval with annual support.
*
* @method static PlanInterval EVERY_30_DAYS()
* @method static PlanInterval ANNUAL()
Expand Down
40 changes: 24 additions & 16 deletions src/ShopifyApp/Services/ApiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public function getAccessData(string $code): ResponseAccess

/**
* {@inheritdoc}
* TODO: Convert to GraphQL.
*/
public function getScriptTags(array $params = []): ResponseAccess
{
Expand All @@ -173,6 +174,7 @@ public function getScriptTags(array $params = []): ResponseAccess

/**
* {@inheritdoc}
* TODO: Convert to GraphQL.
*/
public function createScriptTag(array $payload): ResponseAccess
{
Expand All @@ -188,6 +190,7 @@ public function createScriptTag(array $payload): ResponseAccess

/**
* {@inheritdoc}
* TODO: Convert to GraphQL.
*/
public function getCharge(ChargeType $chargeType, ChargeReference $chargeRef): ResponseAccess
{
Expand All @@ -205,6 +208,7 @@ public function getCharge(ChargeType $chargeType, ChargeReference $chargeRef): R

/**
* {@inheritdoc}
* TODO: Convert to GraphQL.
*/
public function activateCharge(ChargeType $chargeType, ChargeReference $chargeRef): ResponseAccess
{
Expand All @@ -222,6 +226,7 @@ public function activateCharge(ChargeType $chargeType, ChargeReference $chargeRe

/**
* {@inheritdoc}
* TODO: Convert to GraphQL (merge createChargeGraphQL).
*/
public function createCharge(ChargeType $chargeType, PlanDetailsTransfer $payload): ResponseAccess
{
Expand Down Expand Up @@ -271,19 +276,19 @@ public function createChargeGraphQL(PlanDetailsTransfer $payload): ResponseAcces
}
';
$variables = [
"name" => $payload->name,
"returnUrl" => $payload->returnUrl,
"trialDays" => $payload->trialDays,
"test" => $payload->test,
"lineItems" => [
'name' => $payload->name,
'returnUrl' => $payload->returnUrl,
'trialDays' => $payload->trialDays,
'test' => $payload->test,
'lineItems' => [
[
"plan" => [
"appRecurringPricingDetails" => [
"price" => [
"amount" => $payload->price,
"currencyCode" => "USD",
'plan' => [
'appRecurringPricingDetails' => [
'price' => [
'amount' => $payload->price,
'currencyCode' => 'USD',
],
"interval" => $payload->interval
'interval' => $payload->interval,
],
]
]
Expand All @@ -297,6 +302,7 @@ public function createChargeGraphQL(PlanDetailsTransfer $payload): ResponseAcces

/**
* {@inheritdoc}
* TODO: Convert to GraphQL.
*/
public function getWebhooks(array $params = []): ResponseAccess
{
Expand All @@ -321,6 +327,7 @@ public function getWebhooks(array $params = []): ResponseAccess

/**
* {@inheritdoc}
* TODO: Convert to GraphQL.
*/
public function createWebhook(array $payload): ResponseAccess
{
Expand All @@ -336,6 +343,7 @@ public function createWebhook(array $payload): ResponseAccess

/**
* {@inheritdoc}
* TODO: Convert to GraphQL.
*/
public function deleteWebhook(int $webhookId): ResponseAccess
{
Expand All @@ -350,6 +358,7 @@ public function deleteWebhook(int $webhookId): ResponseAccess

/**
* {@inheritdoc}
* TODO: Convert to GraphQL.
*/
public function createUsageCharge(UsageChargeDetailsTransfer $payload)
{
Expand Down Expand Up @@ -419,8 +428,8 @@ protected function doRequest(ApiMethod $method, string $path, array $payload = n
/**
* Fire the request using the GraphQL API Instance.
*
* @param string $query The query of GraphQL
* @param array $payload The option payload to using on the query
* @param string $query The query of GraphQL
* @param array $payload The option payload to using on the query
*
* @throws Exception
*
Expand All @@ -429,10 +438,9 @@ protected function doRequest(ApiMethod $method, string $path, array $payload = n
protected function doRequestGraphQL(string $query, array $payload = null)
{
$response = $this->api->graph($query, $payload);

if ($response['errors'] !== false) {
$message = isset($response['body']['errors']) && is_array($response['body']['errors'])
? $response['body']['errors'][0]['message'] : 'Unknown error';
$message = is_array($response['errors'])
? $response['errors'][0]['message'] : $response['errors'];

// Request error somewhere, throw the exception
throw new Exception($message);
Expand Down
4 changes: 4 additions & 0 deletions src/ShopifyApp/resources/database/factories/PlanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@
$factory->state(Plan::class, 'type_onetime', [
'type' => PlanType::ONETIME()->toNative(),
]);

$factory->state(Plan::class, 'interval_annual', [
'interval' => PlanInterval::ANNUAL()->toNative(),
]);
23 changes: 22 additions & 1 deletion tests/Actions/GetPlanUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function setUp(): void
$this->action = $this->app->make(GetPlanUrl::class);
}

public function testRun(): void
public function testRun30Days(): void
{
// Create a plan
factory(Plan::class)->states(['installable', 'type_recurring'])->create();
Expand All @@ -37,4 +37,25 @@ public function testRun(): void

$this->assertNotEmpty($result);
}

public function testRunAnnual(): void
{
// Create a plan
factory(Plan::class)->states(['installable', 'type_recurring', 'interval_annual'])->create();

// Create the shop with no plan
$shop = factory($this->model)->create();

// Setup API stub
$this->setApiStub();
ApiStub::stubResponses(['graphql_app_subscription_create']);

$result = call_user_func(
$this->action,
$shop->getId(),
NullablePlanId::fromNative(null)
);

$this->assertNotEmpty($result);
}
}
51 changes: 37 additions & 14 deletions tests/Services/ApiHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Osiset\ShopifyApp\Test\Services;

use Exception;
use Osiset\ShopifyApp\Test\TestCase;
use Osiset\BasicShopifyAPI\ResponseAccess;
use Osiset\BasicShopifyAPI\BasicShopifyAPI;
use Osiset\ShopifyApp\Objects\Enums\AuthMode;
use Osiset\ShopifyApp\Exceptions\ApiException;
use Osiset\ShopifyApp\Objects\Enums\ChargeType;
use Osiset\ShopifyApp\Test\Stubs\Api as ApiStub;
use Osiset\ShopifyApp\Objects\Enums\PlanInterval;
use Osiset\ShopifyApp\Objects\Values\ChargeReference;
use Osiset\ShopifyApp\Contracts\ApiHelper as IApiHelper;
use Osiset\ShopifyApp\Objects\Transfers\PlanDetails as PlanDetailsTransfer;
Expand Down Expand Up @@ -137,17 +139,17 @@ public function testCreateCharge(): void
$this->setApiStub();
ApiStub::stubResponses(['post_recurring_application_charges']);

// Build the details object
$transfer = new PlanDetailsTransfer();
$transfer->name = 'Test';
$transfer->price = 12.00;
$transfer->interval = PlanInterval::EVERY_30_DAYS()->toNative();
$transfer->test = true;
$transfer->trialDays = 7;

$data = $shop->apiHelper()->createCharge(
ChargeType::RECURRING(),
new PlanDetailsTransfer(
'Test',
12.00,
true,
7,
null,
null,
null
)
$transfer
);
$this->assertInstanceOf(ResponseAccess::class, $data);
$this->assertEquals('Basic Plan', $data['name']);
Expand Down Expand Up @@ -205,12 +207,12 @@ public function testCreateUsageCharge(): void
$this->setApiStub();
ApiStub::stubResponses(['post_recurring_application_charges_usage_charges']);

$tranfer = new UsageChargeDetailsTransfer();
$tranfer->chargeReference = new ChargeReference(1);
$tranfer->price = 12.00;
$tranfer->description = 'Hello!';
$transfer = new UsageChargeDetailsTransfer();
$transfer->chargeReference = new ChargeReference(1);
$transfer->price = 12.00;
$transfer->description = 'Hello!';

$data = $shop->apiHelper()->createUsageCharge($tranfer);
$data = $shop->apiHelper()->createUsageCharge($transfer);
$this->assertInstanceOf(ResponseAccess::class, $data);
}

Expand All @@ -227,4 +229,25 @@ public function testErrors(): void

$shop->apiHelper()->deleteWebhook(1);
}

public function testErrorsGraphQL(): void
{
$this->expectException(Exception::class);

// Create a shop
$shop = factory($this->model)->create();

// Response stubbing
$this->setApiStub();
ApiStub::stubResponses(['empty_with_error_graphql']);

$transfer = new PlanDetailsTransfer();
$transfer->name = 'Test';
$transfer->price = 12.00;
$transfer->interval = PlanInterval::ANNUAL()->toNative();
$transfer->test = true;
$transfer->trialDays = 7;

$shop->apiHelper()->createChargeGraphQL($transfer);
}
}
25 changes: 25 additions & 0 deletions tests/Stubs/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ public function rest(string $method, string $path, array $params = null, array $
];
}

public function graph(string $query, array $variables = [], bool $sync = true): array
{
try {
$filename = array_shift(self::$stubFiles);
$response = json_decode(file_get_contents(__DIR__."/../fixtures/{$filename}.json"), true);
} catch (ErrorException $error) {
throw new Exception('Missing fixture for GraphQL call');
}

$errors = false;
$exception = null;
if (isset($response['errors'])) {
$errors = $response['errors'];
$exception = new Exception();
}

return [
'errors' => $errors,
'exception' => $exception,
'response' => $response,
'status' => 200,
'body' => new ResponseAccess($response),
];
}

public function requestAccess(string $code): ResponseAccess
{
return new ResponseAccess(
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/empty_with_error_graphql.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"errors": [{
"message": "Error!"
}]
}
11 changes: 11 additions & 0 deletions tests/fixtures/graphql_app_subscription_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"data": {
"appSubscriptionCreate": {
"userErrors": [],
"confirmationUrl": "https://domain.myshopify.com/admin/charges/4019552312/confirm_recurring_application_charge?signature=BAh7BzoHaWRsKwc4gJXvOhJhdXRvX2FjdGl2YXRlVA%3D%3D--74e39487ff00313ca4409dea7ab00081001c45d5",
"appSubscription": {
"id": "gid://shopify/AppSubscription/4019552312"
}
}
}
}

0 comments on commit c63f95a

Please sign in to comment.