Skip to content

Commit

Permalink
Theme, Asset, and Shop
Browse files Browse the repository at this point in the history
  • Loading branch information
gnikyt committed Oct 22, 2018
1 parent 8202c9f commit 65ee504
Show file tree
Hide file tree
Showing 14 changed files with 661 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -53,13 +53,15 @@ $products = $collect->map(function ($c) { return $c->product; });
- [x] CustomCollection
- [x] Collect
- [x] Product
- [x] Shop
- [x] Asset
- [x] Theme
- [ ] AccessScope
- [ ] StorefrontAccessToken
- [ ] Policy
- [ ] ShippingZone
- [ ] Province
- [ ] Country
- [ ] Shop
- [ ] AbandonedCheckout
- [ ] DraftOrder
- [ ] Order
Expand Down Expand Up @@ -94,10 +96,8 @@ $products = $collect->map(function ($c) { return $c->product; });
- [ ] FulfillmentEvent
- [ ] Fulfillment
- [ ] FulfillmentService
- [ ] Theme
- [ ] Page
- [ ] Comment
- [ ] Asset
- [ ] Blog
- [ ] Redirect
- [ ] ScriptTag
Expand Down
53 changes: 53 additions & 0 deletions src/OhMyBrew/BasicShopifyResource/Models/Asset.php
@@ -0,0 +1,53 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Models;

use OhMyBrew\BasicShopifyResource\Resource;
use OhMyBrew\BasicShopifyResource\Relationships\HasOne;
use OhMyBrew\BasicShopifyResource\Models\Theme;

/**
* Asset API.
*/
class Asset extends Resource
{
/**
* The resource path part.
*
* @var string
*/
protected $resourcePath = 'assets';

/**
* The resource name.
*
* @var string
*/
protected $resourceName = 'asset';

/**
* The resource name (plural).
*
* @var string
*/
protected $resourceNamePlural = 'assets';

/**
* The constructor.
*
* @return $this
*/
public function __construct()
{
$this->relationships = [
'theme' => (new HasOne(Theme::class))->setParams(
/**
* @codeCoverageIgnore
*/
function () {
return ['theme_id' => $this->theme_id];
}
),
];
}
}
25 changes: 25 additions & 0 deletions src/OhMyBrew/BasicShopifyResource/Models/Shop.php
@@ -0,0 +1,25 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Models;

use OhMyBrew\BasicShopifyResource\Resource;

/**
* Shop API.
*/
class Shop extends Resource
{
/**
* The resource path.
*
* @var string
*/
protected $resourcePath = 'shop';

/**
* The resource name.
*
* @var string
*/
protected $resourceName = 'shop';
}
52 changes: 52 additions & 0 deletions src/OhMyBrew/BasicShopifyResource/Models/Theme.php
@@ -0,0 +1,52 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Models;

use OhMyBrew\BasicShopifyResource\Resource;
use OhMyBrew\BasicShopifyResource\Relationships\HasManyThrough;

/**
* Theme API.
*/
class Theme extends Resource
{
/**
* The resource path part.
*
* @var string
*/
protected $resourcePath = 'themes';

/**
* The resource name.
*
* @var string
*/
protected $resourceName = 'theme';

/**
* The resource name (plural).
*
* @var string
*/
protected $resourceNamePlural = 'themes';


/**
* The constructor.
*
* @return $this
*/
public function __construct()
{
$this->relationships = [
'assets' => (new HasManyThrough(Asset::class))
/**
* @codeCoverageIgnore
*/
->setThrough(function () {
return $this;
})
];
}
}
39 changes: 39 additions & 0 deletions src/OhMyBrew/BasicShopifyResource/Relationships/HasManyThrough.php
@@ -0,0 +1,39 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Relationships;

use Closure;

class HasManyThrough extends Relationship
{
/**
* The through resource.
*
* @var string
*/
protected $through;

/**
* Sets the through resource.
*
* @param Closure $through The through resource.
*
* @return $this
*/
public function setThrough(Closure $through)
{
$this->through = $through;

return $this;
}

/**
* Gets the through resource class.
*
* @return Closure
*/
public function getThrough()
{
return $this->through;
}
}
27 changes: 24 additions & 3 deletions src/OhMyBrew/BasicShopifyResource/Resource.php
Expand Up @@ -6,6 +6,7 @@
use OhMyBrew\BasicShopifyResource\Relationships\HasMany;
use OhMyBrew\BasicShopifyResource\Relationships\HasOne;
use OhMyBrew\BasicShopifyResource\Relationships\HasOneThrough;
use OhMyBrew\BasicShopifyResource\Relationships\HasManyThrough;
use OhMyBrew\BasicShopifyResource\Relationships\IncludesMany;
use OhMyBrew\BasicShopifyResource\Relationships\IncludesOne;
use Tightenco\Collect\Support\Collection;
Expand Down Expand Up @@ -122,7 +123,7 @@ protected static function request(string $type, $resourceId = null, array $param
}
$path[] = $resourcePath;

if ($resourceId) {
if ($resourceId && $resourceId > 0) {
// Add the targeted resource ID
$path[] = $resourceId;
}
Expand All @@ -132,10 +133,10 @@ protected static function request(string $type, $resourceId = null, array $param
$response = self::getConnection()
->rest($type, $path, $params)
->body
->{$resourceId ? $resourceName : $resourceNamePlural};
->{($resourceId || $resourceId === 0) ? $resourceName : $resourceNamePlural};

if ($type !== 'DELETE') {
if ($resourceId) {
if ($resourceId || $resourceId === 0) {
// If singular, build a single model
return self::buildResource($resource, $response);
}
Expand Down Expand Up @@ -337,6 +338,23 @@ public function hasOneThrough($resource, array $params, $throughResource, $throu
return $instance::findThrough($id, $throughInstance::find($throughId), $params);
}

/**
* Relationship of hasManyThrough.
* This resource has many resources through another resource.
*
* @param string $resource The class name of the resource.
* @param array $params Additional param to pass with the request.
* @param Closure $throughResource The through resource.
*
* @return resource
*/
public function hasManyThrough($resource, array $params, $throughResource)
{
$instance = new $resource();

return $instance::allThrough($throughResource(), $params);
}

/**
* Saves (or creates) a record.
*
Expand Down Expand Up @@ -542,6 +560,9 @@ protected function getRelationship($property)
} elseif ($relationship instanceof HasOneThrough) {
// Has a single resource through
$this->properties[$property] = $this->hasOneThrough($resource, $params, $relationship->getThrough(), $relationship->getThroughId());
} elseif ($relationship instanceof HasManyThrough) {
// Has a single resource through
$this->properties[$property] = $this->hasManyThrough($resource, $params, $relationship->getThrough());
}

return $this->properties[$property];
Expand Down
31 changes: 31 additions & 0 deletions test/Models/AssetTest.php
@@ -0,0 +1,31 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Test\Models;

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

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

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

public function testRelationships()
{
$connection = $this->createConnection(['models/theme', 'models/assets', 'models/themes']);
$theme = $this->invokeMethod(Theme::class, 'find', [828155753]);

// Assets (API call)
$theme2 = $theme->assets->first()->theme;
$this->assertInstanceOf(Theme::class, $theme2);
}
}
29 changes: 29 additions & 0 deletions test/Models/ShopTest.php
@@ -0,0 +1,29 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Test\Models;

use OhMyBrew\BasicShopifyResource\Test\TestCase;
use OhMyBrew\BasicShopifyResource\Models\Shop;

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

$this->assertEquals('shop', $props->resourceName);
$this->assertEquals('id', $props->resourcePk);
}

public function testFinders()
{
$connection = $this->createConnection('models/shop');
$shop = $this->invokeMethod(Shop::class, 'find', [0]);

$this->assertEquals(
'/admin/shop.json',
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(SHop::class, $shop);
}
}
52 changes: 52 additions & 0 deletions test/Models/ThemeTest.php
@@ -0,0 +1,52 @@
<?php

namespace OhMyBrew\BasicShopifyResource\Test\Models;

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

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

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

public function testFinders()
{
$connection = $this->createConnection(['models/theme', 'models/themes']);
$theme = $this->invokeMethod(Theme::class, 'find', [828155753]);

$this->assertEquals(
'/admin/themes/828155753.json',
$this->getLastPathCalled($connection)
);
$this->assertInstanceOf(Theme::class, $theme);

$themes = $this->invokeMethod(Theme::class, 'all');

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

public function testRelationships()
{
$connection = $this->createConnection(['models/theme', 'models/assets']);
$theme = $this->invokeMethod(Theme::class, 'find', [828155753]);

// Assets (API call)
$assets = $theme->assets;
$this->assertInstanceOf(Collection::class, $assets);
$this->assertTrue($assets->count() > 0);
}
}
Empty file added test/fixtures/models/asset.json
Empty file.

0 comments on commit 65ee504

Please sign in to comment.