Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests to ensure post key is correct on write ops #20

Merged
merged 2 commits into from
Oct 3, 2019
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
2 changes: 1 addition & 1 deletion src/Rest/Concerns/CreatesResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait CreatesResource
*/
public function create($data): Model
{
$json = $this->prepareJson($data, $this->singularResourceKey());
$json = $this->prepareJson($data, $this->postKey());

$response = $this->client->post(
$this->uri(), $json
Expand Down
2 changes: 1 addition & 1 deletion src/Rest/Concerns/UpdatesResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait UpdatesResource
*/
public function update(int $id, array $data): Model
{
$json = $this->prepareJson($data, $this->singularResourceKey());
$json = $this->prepareJson($data, $this->postKey());

$response = $this->client->put(
$this->uri((string) $id),
Expand Down
23 changes: 23 additions & 0 deletions src/Rest/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,49 @@ public function getChild(string $key, $id): ChildResource
return new $resource($this->client, $this, $id);
}

/**
* The key primarily used when a single resource is returned from the API.
*/
public function singularResourceKey(): string
{
return $this->guessResourceKey();
}

/**
* The key primarily used when a collection of resources are returned from
* the API.
*/
public function pluralResourceKey(): string
{
return Str::plural($this->singularResourceKey());
}

/**
* This tries to guess the resource key for the resource, based on the
* name of the model.
*/
protected function guessResourceKey(): string
{
return Str::snake(class_basename($this->model));
}

/**
* The key used in the URI when accessing the section of the API related
* to this resource.
*/
public function routeKey(): string
{
return $this->pluralResourceKey();
}

/**
* The key used when making a write operation to the API.
*/
public function postKey(): string
{
return $this->singularResourceKey();
}

/**
* Build the URI for a request to the Shopify resource.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Rest/Resources/Customers/AddressResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ public function pluralResourceKey(): string
{
return 'addresses';
}

public function postKey(): string
{
return 'address';
}
}
5 changes: 5 additions & 0 deletions src/Rest/Resources/Discounts/BatchResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public function routeKey(): string
{
return 'batch';
}

public function postKey(): string
{
return 'discount_codes';
}
}
15 changes: 15 additions & 0 deletions src/Rest/Resources/Plus/GiftCardResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@ public function search(string $query, array $options = []): Collection

return $this->toCollection($response);
}

/**
* Searches for gift card that match a supplied query.
*/
public function disable(int $id): GiftCard
{
$json = $this->prepareJson(['id' => $id], 'gift_card');

$response = $this->client->post(
$this->uri("{$id}/disable"),
$json
);

return $this->toModel($response);
}
}
5 changes: 5 additions & 0 deletions src/Rest/Resources/Shipping/FulfillmentEventResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public function routeKey(): string
{
return 'events';
}

public function postKey(): string
{
return 'event';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function testCreate(): void

$response = $this->resource->create($this->request('create'));

$this->assertPostKey('storefront_access_token');
$this->assertRequest('POST', 'storefront_access_tokens.json');
$this->assertModel($response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,6 @@ public function testGet(): void
$this->assertCollection($response, 2);
}

public function testGetWithOptions(): void
{
$this->queue(200, [], $this->response('get_with_options'));

$response = $this->resource->get([
'ids' => '517154478'
]);

$this->assertRequest('GET', 'reports.json?ids=517154478');
$this->assertCollection($response, 1);
}

public function testFind(): void
{
Expand All @@ -49,18 +38,6 @@ public function testFind(): void
$this->assertModel($response);
}

public function testFindWithOptions(): void
{
$this->queue(200, [], $this->response('find_with_options'));

$response = $this->resource->find(517154478, [
'fields' => 'id,shopify_ql'
]);

$this->assertRequest('GET', 'reports/517154478.json?fields=id,shopify_ql');
$this->assertModel($response);
}

public function testCreate(): void
{
$this->queue(201, [], $this->response('create'));
Expand All @@ -69,6 +46,7 @@ public function testCreate(): void
$this->request('create')
);

$this->assertPostKey('report');
$this->assertRequest('POST', 'reports.json');
$this->assertModel($response);
}
Expand All @@ -82,6 +60,7 @@ public function testUpdate(): void
$this->request('update')
);

$this->assertPostKey('report');
$this->assertRequest('PUT', 'reports/517154478.json');
$this->assertModel($response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function testCreate(): void
$this->request('create')
);

$this->assertPostKey('application_charge');
$this->assertRequest('POST', 'application_charges.json');
$this->assertModel($response);
}
Expand All @@ -39,18 +40,6 @@ public function testFind(): void
$this->assertModel($response);
}

public function testFindWithOptions(): void
{
$this->queue(200, [], $this->response('find'));

$response = $this->resource->find(675931192, [
'fields' => 'id,name',
]);

$this->assertRequest('GET', 'application_charges/675931192.json?fields=id,name');
$this->assertModel($response);
}

public function testGet(): void
{
$this->queue(200, [], $this->response('get'));
Expand All @@ -61,16 +50,6 @@ public function testGet(): void
$this->assertCollection($response, 3);
}

public function testGetWithOptions(): void
{
$this->queue(200, [], $this->response('get_with_options'));

$response = $this->resource->get(['since_id' => 556467234]);

$this->assertRequest('GET', 'application_charges.json?since_id=556467234');
$this->assertCollection($response, 2);
}

public function testActivate(): void
{
$this->queue(200, [], $this->response('activate'));
Expand All @@ -80,6 +59,7 @@ public function testActivate(): void
$this->request('activate')
);

$this->assertPostKey('application_charge');
$this->assertModel($response);
$this->assertRequest('POST', 'application_charges/675931192/activate.json');
}
Expand Down
25 changes: 1 addition & 24 deletions tests/Unit/Rest/Resources/Billing/ApplicationCreditResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function testCreate(): void
$this->request('create')
);

$this->assertPostKey('application_credit');
$this->assertRequest('POST', 'application_credits.json');
$this->assertModel($response);
}
Expand All @@ -39,18 +40,6 @@ public function testFind(): void
$this->assertModel($response);
}

public function testFindWithOptions(): void
{
$this->queue(200, [], $this->response('find'));

$response = $this->resource->find(445365009, [
'fields' => 'id,amount'
]);

$this->assertRequest('GET', 'application_credits/445365009.json?fields=id,amount');
$this->assertModel($response);
}

public function testGet(): void
{
$this->queue(200, [], $this->response('get'));
Expand All @@ -60,16 +49,4 @@ public function testGet(): void
$this->assertRequest('GET', 'application_credits.json');
$this->assertCollection($response);
}

public function testGetWithOptions(): void
{
$this->queue(200, [], $this->response('get'));

$response = $this->resource->get([
'fields' => 'id,amount'
]);

$this->assertRequest('GET', 'application_credits.json?fields=id,amount');
$this->assertCollection($response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testCreate(): void
$this->request('create')
);

$this->assertPostKey('recurring_application_charge');
$this->assertRequest('POST', 'recurring_application_charges.json');
$this->assertModel($response);
}
Expand All @@ -46,18 +47,6 @@ public function testFind(): void
$this->assertModel($response);
}

public function testFindWithOptions(): void
{
$this->queue(200, [], $this->response('find'));

$response = $this->resource->find(455696195, [
'fields' => 'id,name'
]);

$this->assertRequest('GET', 'recurring_application_charges/455696195.json?fields=id,name');
$this->assertModel($response);
}

public function testGet(): void
{
$this->queue(200, [], $this->response('get'));
Expand All @@ -68,18 +57,6 @@ public function testGet(): void
$this->assertCollection($response, 2);
}

public function testGetWithOptions(): void
{
$this->queue(200, [], $this->response('get_with_options'));

$response = $this->resource->get([
'since_id' => 455696195
]);

$this->assertRequest('GET', 'recurring_application_charges.json?since_id=455696195');
$this->assertCollection($response);
}

public function testActivate(): void
{
$this->queue(200, [], $this->response('activate'));
Expand All @@ -89,6 +66,7 @@ public function testActivate(): void
$this->request('activate')
);

$this->assertPostKey('recurring_application_charge');
$this->assertRequest('POST', 'recurring_application_charges/455696195/activate.json');
$this->assertModel($response);
}
Expand Down
25 changes: 1 addition & 24 deletions tests/Unit/Rest/Resources/Billing/UsageChargeResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testCreate(): void
$this->request('create')
);

$this->assertPostKey('usage_charge');
$this->assertRequest('POST', 'recurring_application_charges/1034618216/usage_charges.json');
$this->assertModel($response);
}
Expand All @@ -46,18 +47,6 @@ public function testFind(): void
$this->assertModel($response);
}

public function testFindWithOptions(): void
{
$this->queue(200, [], $this->response('find'));

$response = $this->resource->find(1034618216, [
'fields' => 'id,description'
]);

$this->assertRequest('GET', 'recurring_application_charges/1034618216/usage_charges/1034618216.json?fields=id,description');
$this->assertModel($response);
}

public function testGet(): void
{
$this->queue(200, [], $this->response('get'));
Expand All @@ -67,16 +56,4 @@ public function testGet(): void
$this->assertRequest('GET', 'recurring_application_charges/1034618216/usage_charges.json');
$this->assertCollection($response);
}

public function testGetWithOptions(): void
{
$this->queue(200, [], $this->response('get'));

$response = $this->resource->get([
'fields' => 'id,description'
]);

$this->assertRequest('GET', 'recurring_application_charges/1034618216/usage_charges.json?fields=id,description');
$this->assertCollection($response);
}
}
Loading