Skip to content

Commit

Permalink
First model test
Browse files Browse the repository at this point in the history
  • Loading branch information
gnikyt committed Oct 15, 2018
1 parent 7c0c1e5 commit 87e8331
Show file tree
Hide file tree
Showing 12 changed files with 670 additions and 10 deletions.
66 changes: 66 additions & 0 deletions test/Models/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Test\Models;

use OhMyBrew\BasicShopifyResource\Models\Product;
use OhMyBrew\BasicShopifyResource\Test\TestCase;
use Tightenco\Collect\Support\Collection;

class ProducteTest extends TestCase
{
public function testSetup()
{
$props = $this->getResourceProperties(new Product());

$this->assertEquals('products', $props->resourcePath);
$this->assertEquals('product', $props->resourceName);
$this->assertEquals('products', $props->resourceNamePlural);
$this->assertEquals('id', $props->resourcePk);
}

public function testFinders()
{
$connection = $this->createConnection(['models/product', 'models/products']);
$product = $this->invokeMethod(Product::class, 'find', [632910392]);

$this->assertEquals(
'/admin/products/632910392.json',
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(Product::class, $product);

$products = $this->invokeMethod(Product::class, 'all');

$this->assertEquals(
'/admin/products.json',
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(Collection::class, $products);
}

public function testRelationships()
{
$connection = $this->createConnection(['models/product', 'models/custom_collections', 'models/collects']);
$product = $this->invokeMethod(Product::class, 'find', [632910392]);

// Variants (no API call)
$variants = $product->variants;
$this->assertInstanceOf(Collection::class, $variants);
$this->assertTrue($variants->count() > 0);

// Images (no API call)
$images = $product->images;
$this->assertInstanceOf(Collection::class, $images);
$this->assertTrue($images->count() > 0);

// Custom collections (API call)
$collections = $product->collections;
$this->assertInstanceOf(Collection::class, $collections);
$this->assertTrue($collections->count() > 0);

// Collect (API call)
$collects = $product->collects;
$this->assertInstanceOf(Collection::class, $collects);
$this->assertTrue($collects->count() > 0);
}
}
10 changes: 5 additions & 5 deletions test/Resource/ResourceFindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testFind()

$this->assertEquals(
'/admin/products/632910392.json',
parse_url($connection['mock']->getLastRequest()->getUri(), PHP_URL_PATH)
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(Product::class, $product);
}
Expand All @@ -33,7 +33,7 @@ public function testFindThrough()

$this->assertEquals(
'/admin/products/632910392/variants/808950810.json',
parse_url($connection['mock']->getLastRequest()->getUri(), PHP_URL_PATH)
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(Variant::class, $variant);

Expand All @@ -42,7 +42,7 @@ public function testFindThrough()

$this->assertEquals(
'/admin/products/632910392/variants/808950810.json',
parse_url($connection['mock']->getLastRequest()->getUri(), PHP_URL_PATH)
$this->getLastPathCalled($connection)
);
}

Expand All @@ -53,7 +53,7 @@ public function testAll()

$this->assertEquals(
'/admin/products.json',
parse_url($connection['mock']->getLastRequest()->getUri(), PHP_URL_PATH)
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(Collection::class, $products);
}
Expand All @@ -66,7 +66,7 @@ public function testAllThrough()

$this->assertEquals(
'/admin/products/632910392/variants.json',
parse_url($connection['mock']->getLastRequest()->getUri(), PHP_URL_PATH)
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(Collection::class, $variants);
}
Expand Down
6 changes: 3 additions & 3 deletions test/Resource/ResourceRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testIncludesManyRelationship()
$this->assertInstanceOf(Collection::class, $product->variants);
$this->assertEquals(
'/admin/products/632910392.json',
parse_url($connection['mock']->getLastRequest()->getUri(), PHP_URL_PATH)
$this->getLastPathCalled($connection)
);

// Product with no variants in response, should make an API call to get them
Expand All @@ -46,7 +46,7 @@ public function testIncludesManyRelationship()
$this->assertInstanceOf(Collection::class, $product->variants);
$this->assertEquals(
'/admin/products/632910392/variants.json',
parse_url($connection['mock']->getLastRequest()->getUri(), PHP_URL_PATH)
$this->getLastPathCalled($connection)
);
}

Expand All @@ -59,7 +59,7 @@ public function testHasOneRelationship()
$this->assertInstanceOf(Product::class, $variant->product);
$this->assertEquals(
'/admin/products.json',
parse_url($connection['mock']->getLastRequest()->getUri(), PHP_URL_PATH)
$this->getLastPathCalled($connection)
);

// Second call should not fire an API call since we now have the data
Expand Down
26 changes: 24 additions & 2 deletions test/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace OhMyBrew\BasicShopifyResource\Test;

use ReflectionClass;
use ReflectionProperty;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use OhMyBrew\BasicShopifyResource\Connection;
use ReflectionClass;

abstract class TestCase extends \PHPUnit\Framework\TestCase
{
public function createResponse($fixture)
protected function createResponse($fixture)
{
return new Response(
200,
Expand Down Expand Up @@ -49,6 +50,11 @@ protected function createConnection($fixtures = null)
return $fixtures ? ['client' => $client, 'mock' => $mock] : null;
}

protected function getLastPathCalled($connection)
{
return parse_url($connection['mock']->getLastRequest()->getUri(), PHP_URL_PATH);
}

protected function invokeMethod($className, $methodName, array $args = [])
{
$class = new ReflectionClass($className);
Expand All @@ -57,4 +63,20 @@ protected function invokeMethod($className, $methodName, array $args = [])

return count($args) === 0 ? $method->invoke(new $className()) : $method->invokeArgs(new $className(), $args);
}

protected function getResourceProperties($resource)
{
$ref = new ReflectionClass($resource);
$propList = $ref->getProperties(ReflectionProperty::IS_PROTECTED);

$returnProps = [];
foreach ($propList as $prop) {
$property = $ref->getProperty($prop->getName());
$property->setAccessible(true);

$returnProps[$prop->getName()] = $property->getValue($resource);
}

return (object) $returnProps;
}
}
12 changes: 12 additions & 0 deletions test/fixtures/models/collect.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"collect": {
"id": 841564295,
"collection_id": 841564295,
"product_id": 632910392,
"featured": false,
"created_at": null,
"updated_at": null,
"position": 1,
"sort_value": "0000000001"
}
}
24 changes: 24 additions & 0 deletions test/fixtures/models/collects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"collects": [
{
"id": 395646240,
"collection_id": 395646240,
"product_id": 632910392,
"featured": false,
"created_at": null,
"updated_at": null,
"position": 1,
"sort_value": "0000000001"
},
{
"id": 841564295,
"collection_id": 841564295,
"product_id": 632910392,
"featured": false,
"created_at": null,
"updated_at": null,
"position": 1,
"sort_value": "0000000001"
}
]
}
17 changes: 17 additions & 0 deletions test/fixtures/models/custom_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"custom_collection": {
"id": 1063001463,
"handle": "macbooks",
"title": "Macbooks",
"updated_at": "2016-03-17T16:58:37-04:00",
"body_html": null,
"published_at": "2016-03-17T16:58:37-04:00",
"sort_order": "alpha-asc",
"template_suffix": null,
"published_scope": "global",
"image": {
"created_at": "2016-03-17T16:58:37-04:00",
"src": "https:\/\/cdn.shopify.com\/s\/files\/1\/0006\/9093\/3842\/collections\/fd43f2c8883f6e9b680e3295fd990d2c.gif?v=1458248317"
}
}
}
47 changes: 47 additions & 0 deletions test/fixtures/models/custom_collections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"custom_collections": [
{
"id": 841564295,
"handle": "ipods",
"title": "IPods",
"updated_at": "2008-02-01T19:00:00-05:00",
"body_html": "<p>The best selling ipod ever</p>",
"published_at": "2008-02-01T19:00:00-05:00",
"sort_order": "manual",
"template_suffix": null,
"published_scope": "web",
"admin_graphql_api_id": "gid://shopify/Collection/841564295",
"image": {
"created_at": "2018-09-25T15:15:37-04:00",
"alt": "iPod Nano 8gb",
"width": 123,
"height": 456,
"src": "https://cdn.shopify.com/s/files/1/0006/9093/3842/collections/ipod_nano_8gb.jpg?v=1537902937"
}
},
{
"id": 395646240,
"handle": "ipods_two",
"title": "IPods Two",
"updated_at": "2008-02-01T19:00:00-05:00",
"body_html": "<p>The best selling ipod ever. Again</p>",
"published_at": "2008-02-01T19:00:00-05:00",
"sort_order": "manual",
"template_suffix": null,
"published_scope": "web",
"admin_graphql_api_id": "gid://shopify/Collection/395646240"
},
{
"id": 691652237,
"handle": "non-ipods",
"title": "Non Ipods",
"updated_at": "2013-02-01T19:00:00-05:00",
"body_html": "<p>No ipods here</p>",
"published_at": "2013-02-01T19:00:00-05:00",
"sort_order": "manual",
"template_suffix": null,
"published_scope": "web",
"admin_graphql_api_id": "gid://shopify/Collection/691652237"
}
]
}
20 changes: 20 additions & 0 deletions test/fixtures/models/images.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"images": [
{
"created_at": "2014-01-17T16:11:52-05:00",
"id": 850703190,
"position": 1,
"product_id": 632910392,
"updated_at": "2014-01-17T16:11:52-05:00",
"src": "http://cdn.shopify.com/s/files/1/0006/9093/3842/products/ipod-nano.png?v=1389993112"
},
{
"created_at": "2014-01-17T16:11:52-05:00",
"id": 562641783,
"position": 2,
"product_id": 632910392,
"updated_at": "2014-01-17T16:11:52-05:00",
"src": "http://cdn.shopify.com/s/files/1/0006/9093/3842/products/ipod-nano-2.png?v=1389993112"
}
]
}
Loading

0 comments on commit 87e8331

Please sign in to comment.