Skip to content

Commit

Permalink
Continue of code and finalized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gnikyt committed Oct 19, 2018
1 parent 06bb3e5 commit f094144
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 23 deletions.
19 changes: 14 additions & 5 deletions README.md
Expand Up @@ -7,7 +7,7 @@

This library is a simple wrapper for the Basic Shopify API to interact with the Shopify resources in a more friendly manner.

**THIS IS IN DEVELOPMENT** do not use, unit tests are on-going.
**Currently in the works, many models are missing**

## Examples:

Expand All @@ -26,10 +26,10 @@ echo "Product: {$product->title}";
$product->title = 'New Title';
$product->save();

echo $p->variants->first()->id;
print_r($p->varianrs->first()->image->src); // Gets the variant image (lazy loaded)
print_r($p->variants->first()->product); // Gets product for variant (lazy loaded)
print_r($p->collections->first()->collects); // Gets collects for the collection (lazy loaded)
echo $pproduct->variants->first()->id;
print_r($pproduct->variants->first()->image->src); // Gets the variant image (lazy loaded)
print_r($products->variants->first()->product); // Gets product for variant (lazy loaded)
print_r($pproducts->collections->first()->collects); // Gets collects for the collection (lazy loaded)

$count = Product::all()->count();
echo "There are {$count} products";
Expand All @@ -38,4 +38,13 @@ $variant = Variant::findThrough(12999209309, $product);
echo $variant->id;

$collection = CustomCollection::find(29889201111);
echo $collection->handle;

$collect = Collect::all(['collection_id' => $collection->id]);
$products = $collect->map(function ($c) { return $c->product; });
```

## Testing

Run `bin/phpunit --no-coverage` for tests.
Run `bin/phpunit` for full coverage.
16 changes: 16 additions & 0 deletions src/OhMyBrew/BasicShopifyResource/Models/Collect.php
Expand Up @@ -3,6 +3,9 @@
namespace OhMyBrew\BasicShopifyResource\Models;

use OhMyBrew\BasicShopifyResource\Resource;
use OhMyBrew\BasicShopifyResource\Relationships\HasOne;
use OhMyBrew\BasicShopifyResource\Models\Product;
use OhMyBrew\BasicShopifyResource\Models\CustomCollection;

/**
* Custom Collection API.
Expand All @@ -29,4 +32,17 @@ class Collect extends Resource
* @var string
*/
protected $resourceNamePlural = 'collects';

/**
* The constructor.
*
* @return $this
*/
public function __construct()
{
$this->relationships = [
'product' => new HasOne(Product::class),
'collection' => new HasOne(CustomCollection::class),
];
}
}
11 changes: 8 additions & 3 deletions src/OhMyBrew/BasicShopifyResource/Models/CustomCollection.php
Expand Up @@ -39,9 +39,14 @@ class CustomCollection extends Resource
public function __construct()
{
$this->relationships = [
'collects' => (new HasMany(Collect::class))->setParams(function () {
return ['collection_id' => $this->id];
}),
'collects' => (new HasMany(Collect::class))->setParams(
/**
* @codeCoverageIgnore
*/
function () {
return ['collection_id' => $this->id];
}
),
];
}
}
33 changes: 24 additions & 9 deletions src/OhMyBrew/BasicShopifyResource/Models/Product.php
Expand Up @@ -41,15 +41,30 @@ public function __construct()
{
$this->relationships = [
'variants' => new IncludesMany(Variant::class),
'images' => (new IncludesMany(Image::class))->setParams(function () {
return ['product_id' => $this->id];
}),
'collections' => (new HasMany(CustomCollection::class))->setParams(function () {
return ['product_id' => $this->id];
}),
'collects' => (new HasMany(Collect::class))->setParams(function () {
return ['product_id' => $this->id];
}),
'images' => (new IncludesMany(Image::class))->setParams(
/**
* @codeCoverageIgnore
*/
function () {
return ['product_id' => $this->id];
}
),
'collections' => (new HasMany(CustomCollection::class))->setParams(
/**
* @codeCoverageIgnore
*/
function () {
return ['product_id' => $this->id];
}
),
'collects' => (new HasMany(Collect::class))->setParams(
/**
* @codeCoverageIgnore
*/
function () {
return ['product_id' => $this->id];
}
),
];
}
}
17 changes: 13 additions & 4 deletions src/OhMyBrew/BasicShopifyResource/Models/Variant.php
Expand Up @@ -39,12 +39,21 @@ class Variant extends Resource
public function __construct()
{
$this->relationships = [
'product' => (new HasOne(Product::class))->setParams(function () {
return ['product_id' => $this->product_id];
}),
'product' => (new HasOne(Product::class))->setParams(
/**
* @codeCoverageIgnore
*/
function () {
return ['product_id' => $this->product_id];
}
),
'image' => (new HasOneThrough(Image::class))
->setThrough(Product::class)
->setThroughId(function () {
->setThroughId(
/**
* @codeCoverageIgnore
*/
function () {
return $this->product_id;
})
];
Expand Down
15 changes: 14 additions & 1 deletion src/OhMyBrew/BasicShopifyResource/Resource.php
Expand Up @@ -420,7 +420,20 @@ public function __get($property)
*/
public function __set($property, $value)
{
$this->mutatedProperties[$property] = $value;
$relationship = $this->getRelationalProperty($property);
if ($relationship) {
// Is relational property, magically work out
if (!$value instanceof self) {
// Bad value
throw new Exception('Setting relational property must be instance of a resource');
}

// Example, product_id => product[id] ... product_id => 1
$this->mutatedProperties["{$value->resourceName}_{$value->resourcePk}"] = $value->{$value->resourcePk};
} else {
// Standard setter
$this->mutatedProperties[$property] = $value;
}
}

/**
Expand Down
56 changes: 56 additions & 0 deletions test/Models/CollectTest.php
@@ -0,0 +1,56 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Test\Models;

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

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

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

public function testFinders()
{
$connection = $this->createConnection(['models/collect', 'models/collects']);
$collect = $this->invokeMethod(Collect::class, 'find', [841564295]);

$this->assertEquals(
'/admin/collects/841564295.json',
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(Collect::class, $collect);

$collects = $this->invokeMethod(Collect::class, 'all');

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

public function testRelationships()
{
$connection = $this->createConnection(['models/collect', 'models/products', 'models/custom_collections']);
$collect = $this->invokeMethod(Collect::class, 'find', [841564295]);

// Product (API call)
$product = $collect->product;
$this->assertInstanceOf(Product::class, $product);

// Collection (API call)
$collection = $collect->collection;
$this->assertInstanceOf(CustomCollection::class, $collection);
}
}
51 changes: 51 additions & 0 deletions test/Models/CustomCollection.php
@@ -0,0 +1,51 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Test\Models;

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

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

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

public function testFinders()
{
$connection = $this->createConnection(['models/custom_collection', 'models/custom_collections']);
$collection = $this->invokeMethod(CustomCollection::class, 'find', [1063001463]);

$this->assertEquals(
'/admin/custom_collections/1063001463.json',
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(CustomCollection::class, $collection);

$collections = $this->invokeMethod(Collection::class, 'all');

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

public function testRelationships()
{
$connection = $this->createConnection(['models/custom_collection', 'models/collects']);
$collection = $this->invokeMethod(CustomCollection::class, 'find', [1063001463]);

// Collects (API call)
$collects = $collection->collects;
$this->assertInstanceOf(Collection::class, $collects);
}
}
51 changes: 51 additions & 0 deletions test/Models/ImageTest.php
@@ -0,0 +1,51 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Test\Models;

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

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

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

public function testFinders()
{
$connection = $this->createConnection(['models/image', 'models/images']);
$image = $this->invokeMethod(Image::class, 'find', [850703190]);

$this->assertEquals(
'/admin/images/850703190.json',
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(Image::class, $image);

$images = $this->invokeMethod(Image::class, 'all');

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

public function testRelationships()
{
$connection = $this->createConnection(['models/image', 'models/products']);
$image = $this->invokeMethod(Image::class, 'find', [850703190]);

// Product (API call)
$product = $image->product;
$this->assertInstanceOf(Product::class, $product);
}
}
2 changes: 1 addition & 1 deletion test/Models/ProductTest.php
Expand Up @@ -6,7 +6,7 @@
use OhMyBrew\BasicShopifyResource\Test\TestCase;
use Tightenco\Collect\Support\Collection;

class ProducteTest extends TestCase
class ProductTest extends TestCase
{
public function testSetup()
{
Expand Down
17 changes: 17 additions & 0 deletions test/Resource/ResourceTest.php
Expand Up @@ -4,6 +4,7 @@

use OhMyBrew\BasicShopifyAPI;
use OhMyBrew\BasicShopifyResource\Models\Product;
use OhMyBrew\BasicShopifyResource\Models\Variant;
use OhMyBrew\BasicShopifyResource\Test\TestCase;
use ReflectionClass;

Expand Down Expand Up @@ -58,6 +59,22 @@ public function testProperties()
// Test refresh of properties
$this->invokeMethod($product, 'refreshProperties', [['title' => 'IPod Nano - 8GB']]);
$this->assertEquals('IPod Nano - 8GB', $product->title);

// Test setting a relational property
$variant = new Variant();

$this->assertNull($variant->product_id);
$variant->product = $product;
$this->assertEquals(632910392, $variant->product_id);
}

/**
* @expectedException Exception
*/
public function testThrowsErrorTryingToSetInvalidRelational()
{
$variant = new Variant();
$variant->product = 1;
}

public function testIsNewAndIsExisting()
Expand Down

0 comments on commit f094144

Please sign in to comment.