This repository has been archived by the owner on Nov 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from ohmybrew/namespacing
Namespacing
- Loading branch information
Showing
13 changed files
with
227 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace OhMyBrew\ShopifyApp\Observers; | ||
|
||
use OhMyBrew\ShopifyApp\Models\Shop; | ||
|
||
class ShopObserver | ||
{ | ||
/** | ||
* Listen to the shop creating event. | ||
* | ||
* @param Shop $shop | ||
* | ||
* @return void | ||
*/ | ||
public function creating(Shop $shop) | ||
{ | ||
if (!isset($shop->namespace)) { | ||
// Automatically add the current namespace to new records | ||
$shop->namespace = config('shopify-app.namespace'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace OhMyBrew\ShopifyApp\Scopes; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Scope; | ||
|
||
class NamespaceScope implements Scope | ||
{ | ||
/** | ||
* Apply the scope to a given Eloquent query builder. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Builder $builder | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* | ||
* @return void | ||
*/ | ||
public function apply(Builder $builder, Model $model) | ||
{ | ||
$builder->where('namespace', config('shopify-app.namespace')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...opifyApp/resources/database/migrations/2018_08_30_114021_add_namespace_to_shops_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class AddNamespaceToShopsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::table('shops', function (Blueprint $table) { | ||
$table->string('namespace')->nullable(true)->default(null); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('shops', function (Blueprint $table) { | ||
$table->dropColumn(['namespace']); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace OhMyBrew\ShopifyApp\Test\Observers; | ||
|
||
use Illuminate\Support\Facades\Event; | ||
use OhMyBrew\ShopifyApp\Models\Shop; | ||
use OhMyBrew\ShopifyApp\Test\TestCase; | ||
|
||
class ShopObserverTest extends TestCase | ||
{ | ||
public function testObserverAddsNamespace() | ||
{ | ||
// Need a better way to test... event faking not working... | ||
config(['shopify-app.namespace' => 'shopify-test-namespace']); | ||
|
||
$shop = new Shop(); | ||
$shop->shopify_domain = 'observer.myshopify.com'; | ||
$shop->save(); | ||
|
||
$this->assertEquals('shopify-test-namespace', $shop->namespace); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace OhMyBrew\ShopifyApp\Test\Models; | ||
|
||
use OhMyBrew\ShopifyApp\Models\Shop; | ||
use OhMyBrew\ShopifyApp\Scopes\NamespaceScope; | ||
use OhMyBrew\ShopifyApp\Test\TestCase; | ||
|
||
class NamespaceScopeTest extends TestCase | ||
{ | ||
public function testScopeCanApply() | ||
{ | ||
// Test the default | ||
$builder = Shop::where('shopify_domain', 'example.myshopify.com'); | ||
$this->assertEquals('select * from "shops" where "shopify_domain" = ? and "shops"."deleted_at" is null and "namespace" is null', $builder->toSql()); | ||
$this->assertEquals(['example.myshopify.com'], $builder->getBindings()); | ||
|
||
// Test for a real namespace added | ||
config(['shopify-app.namespace' => 'shopify']); | ||
$builder = Shop::where('shopify_domain', 'example.myshopify.com'); | ||
$this->assertEquals('select * from "shops" where "shopify_domain" = ? and "shops"."deleted_at" is null and "namespace" = ?', $builder->toSql()); | ||
$this->assertEquals(['example.myshopify.com', 'shopify'], $builder->getBindings()); | ||
} | ||
|
||
public function testShopCanBeScopedToNamespaces() | ||
{ | ||
$shop = new Shop(); | ||
$shop->shopify_domain = 'namespace.myshopify.com'; | ||
$shop->namespace = 'shopify-test'; | ||
$shop->save(); | ||
|
||
$shop_2 = new Shop(); | ||
$shop_2->shopify_domain = 'namespace.myshopify.com'; | ||
$shop_2->namespace = 'shopify-test-2'; | ||
$shop_2->save(); | ||
|
||
// Test getting all entries for this shop | ||
$shopEntries = Shop::withoutGlobalScope(NamespaceScope::class) | ||
->select('shopify_domain', 'namespace') | ||
->where('shopify_domain', 'namespace.myshopify.com') | ||
->orderBy('id', 'asc') | ||
->get(); | ||
$this->assertEquals('shopify-test', $shopEntries[0]->namespace); | ||
$this->assertEquals('shopify-test-2', $shopEntries[1]->namespace); | ||
|
||
// Test namespacing config | ||
config(['shopify-app.namespace' => 'shopify-test']); | ||
$this->assertEquals('shopify-test', Shop::where('shopify_domain', 'namespace.myshopify.com')->first()->namespace); | ||
|
||
config(['shopify-app.namespace' => 'shopify-test-2']); | ||
$this->assertEquals('shopify-test-2', Shop::where('shopify_domain', 'namespace.myshopify.com')->first()->namespace); | ||
} | ||
} |