Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #92 from ohmybrew/namespacing
Browse files Browse the repository at this point in the history
Namespacing
  • Loading branch information
gnikyt authored Aug 31, 2018
2 parents 5aaa2d2 + 0b4e62a commit 2572975
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 23 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ php:
env:
- LARAVEL_VERSION=5.5.*
- LARAVEL_VERSION=5.6.*
- LARAVEL_VERSION=5.7.*

matrix:
fast_finish: true
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![StyleCI](https://styleci.io/repos/96462257/shield?branch=master)](https://styleci.io/repos/96462257)
[![License](https://poser.pugx.org/ohmybrew/laravel-shopify/license)](https://packagist.org/packages/ohmybrew/laravel-shopify)

A full-featured Laravel package for aiding in Shopify App development, similar to `shopify_app` for Rails. Works for Laravel 5.5-5.6.
A full-featured Laravel package for aiding in Shopify App development, similar to `shopify_app` for Rails. Works for Laravel 5.5-5.7.

![Screenshot](https://github.com/ohmybrew/laravel-shopify/raw/master/screenshot.png)
![Screenshot: Billable](https://github.com/ohmybrew/laravel-shopify/raw/master/screenshot-billable.png)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"orchestra/database": "~3.5",
"orchestra/testbench": "~3.5",
"phpunit/phpunit": "~6.0||~7.0",
"squizlabs/php_codesniffer": "^3.0"
"squizlabs/php_codesniffer": "^3.0",
"mockery/mockery": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/ShopifyApp/Console/WebhookJobMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function handle()
parent::$method();

// Remind user to enter job into config
$this->info("Don't forget to register the webhook in config/shopify-app.php. Example:");
$this->info("For non-GDPR webhooks, don't forget to register the webhook in config/shopify-app.php. Example:");
$this->info("
'webhooks' => [
[
Expand Down
14 changes: 14 additions & 0 deletions src/ShopifyApp/Models/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use OhMyBrew\ShopifyApp\Facades\ShopifyApp;
use OhMyBrew\ShopifyApp\Scopes\NamespaceScope;

class Shop extends Model
{
Expand All @@ -19,6 +20,7 @@ class Shop extends Model
'shopify_domain',
'shopify_token',
'grandfathered',
'namespace',
];

/**
Expand All @@ -35,6 +37,18 @@ class Shop extends Model
*/
protected $api;

/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();

static::addGlobalScope(new NamespaceScope());
}

/**
* Creates or returns an instance of API for the shop.
*
Expand Down
23 changes: 23 additions & 0 deletions src/ShopifyApp/Observers/ShopObserver.php
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');
}
}
}
23 changes: 23 additions & 0 deletions src/ShopifyApp/Scopes/NamespaceScope.php
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'));
}
}
5 changes: 5 additions & 0 deletions src/ShopifyApp/ShopifyAppProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace OhMyBrew\ShopifyApp;

use Illuminate\Support\ServiceProvider;
use OhMyBrew\ShopifyApp\Models\Shop;
use OhMyBrew\ShopifyApp\Observers\ShopObserver;

class ShopifyAppProvider extends ServiceProvider
{
Expand Down Expand Up @@ -33,6 +35,9 @@ public function boot()
$this->publishes([
__DIR__.'/resources/jobs/AppUninstalledJob.php' => app_path().'/Jobs/AppUninstalledJob.php',
], 'jobs');

// Shop observer
Shop::observe(ShopObserver::class);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/ShopifyApp/resources/config/shopify-app.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Namespace
|--------------------------------------------------------------------------
|
| This option allows you to set a namespace.
| Useful for multiple apps using the same database instance.
| Meaning, one shop can be part of many apps on the same database.
|
*/

'namespace' => env('SHOPIFY_APP_NAMESPACE', null),

/*
|--------------------------------------------------------------------------
| Shop Model
Expand Down
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']);
});
}
}
57 changes: 37 additions & 20 deletions tests/Console/WebhookJobMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,43 @@ class WebhookJobMakeCommandTest extends TestCase
{
public function testItShouldRun()
{
$application = new ConsoleApplication();

$testedCommand = $this->app->make(WebhookJobMakeCommand::class);
$testedCommand->setLaravel($this->app);
$application->add($testedCommand);

$command = $application->find('shopify-app:make:webhook');
$commandTester = new CommandTester($command);

$commandTester->execute([
'command' => $command->getName(),
'name' => 'OrdersCreateJob',
'topic' => 'orders/create',
]);

$output = $commandTester->getDisplay();

$this->assertContains("Don't forget to register the webhook in config/shopify-app.php", $output);
$this->assertContains("'address' => 'https://your-domain.com/webhook/orders-create'", $output);
$this->assertContains("'topic' => 'orders/create',", $output);
$version = $this->app::VERSION;
$versionParts = explode('.', explode('-', $version)[0]);

if (intval($versionParts[0]) === 5 && intval($versionParts[1]) < 7) {
// For 5.5-5.6
$application = new ConsoleApplication();

$testedCommand = $this->app->make(WebhookJobMakeCommand::class);
$testedCommand->setLaravel($this->app);
$application->add($testedCommand);

$command = $application->find('shopify-app:make:webhook');
$commandTester = new CommandTester($command);

$commandTester->execute([
'command' => $command->getName(),
'name' => 'OrdersCreateJob',
'topic' => 'orders/create',
]);

$output = $commandTester->getDisplay();

$this->assertContains("For non-GDPR webhooks, don't forget to register the webhook in config/shopify-app.php", $output);
$this->assertContains("'address' => 'https://your-domain.com/webhook/orders-create'", $output);
$this->assertContains("'topic' => 'orders/create',", $output);
} else {
// For 5.7 up
$this->artisan(
'shopify-app:make:webhook',
[
'name' => 'OrdersCreateJob',
'topic' => 'orders/create',
]
)
->expectsOutput('For non-GDPR webhooks, don\'t forget to register the webhook in config/shopify-app.php. Example:')
->assertExitCode(0);
}
}

public function testShouldMakeUrlFromName()
Expand Down
22 changes: 22 additions & 0 deletions tests/Observers/ShopObserverTest.php
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);
}
}
53 changes: 53 additions & 0 deletions tests/Scopes/NamespaceScopeTest.php
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);
}
}

0 comments on commit 2572975

Please sign in to comment.