Skip to content

Commit ad34864

Browse files
committed
Model is in place and tests for database
1 parent 82d7989 commit ad34864

File tree

8 files changed

+113
-20
lines changed

8 files changed

+113
-20
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ Open `app/Http/Kernel.php` find `routeMiddleware` array. Add a new line with:
4242

4343
`'auth.shop' => \OhMyBrew\ShopifyApp\Middleware\AuthShop::class,`
4444

45+
### Migrations
46+
47+
Run `php artisan migrate`.
48+
4549
## Routes
4650

4751
Here are the defined routes and what they do.

src/ShopifyApp/Middleware/AuthShop.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AuthShop
1515
*/
1616
public function handle(Request $request, Closure $next)
1717
{
18-
if (ShopifyApp::shop() === false) {
18+
if (ShopifyApp::shop() === null) {
1919
// Shall not pass
2020
abort(403);
2121
}

src/ShopifyApp/Models/Shop.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php namespace OhMyBrew\ShopifyApp\Models;
2+
3+
use Illuminate\Database\Eloquent\Model;
4+
5+
class Shop extends Model
6+
{
7+
//
8+
}

src/ShopifyApp/ShopifyApp.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace OhMyBrew\ShopifyApp;
22

33
use OhMyBrew\BasicShopifyAPI as ShopifyAPI;
4+
use OhMyBrew\ShopifyApp\Models\Shop;
45
use Illuminate\Foundation\Application;
56

67
class ShopifyApp
@@ -13,10 +14,17 @@ class ShopifyApp
1314
public $app;
1415

1516
/**
16-
* The current shop
17+
* The current API instance
1718
*
1819
* @var \OhMyBrew\BasicShopifyAPI
1920
*/
21+
public $api;
22+
23+
/**
24+
* The current shop
25+
*
26+
* @var \OhMyBrew\ShopifyApp\Models\Shop
27+
*/
2028
public $shop;
2129

2230
/**
@@ -32,32 +40,38 @@ public function __construct(Application $app)
3240
}
3341

3442
/**
35-
* Gets the current shop.
43+
* Gets/sets the current shop.
3644
*
37-
* @return \OhMyBrew\BasicShopifyAPI
45+
* @return \OhMyBrew\Models\Shop
3846
*/
3947
public function shop() {
40-
if ($this->shop) {
41-
// Return the instance
42-
return $this->shop;
48+
$shopifyDomain = session('shopify_domain');
49+
if (!$this->shop && $shopifyDomain) {
50+
// Grab shop from database here
51+
$shop = Shop::where('shopify_domain', $shopifyDomain)->first();
52+
53+
// Update shop instance
54+
$this->shop = $shop;
4355
}
4456

45-
// New instance
57+
return $this->shop;
58+
}
59+
60+
/**
61+
* Gets/sets the current API instance.
62+
*
63+
* @return \OhMyBrew\BasicShopifyAPI
64+
*/
65+
public function api() {
4666
$shopifyDomain = session('shopify_domain');
47-
if ($shopifyDomain) {
67+
if (!$this->api && $shopifyDomain) {
4868
// Grab shop from database here
4969

50-
// Start the API
70+
// Update API instance
5171
$api = new ShopifyAPI;
52-
//$api->setSession($shopifyDomain);
53-
54-
// Update shop instance
55-
$this->shop = $api;
56-
57-
return $api;
72+
$this->api = $api;
5873
}
5974

60-
// No shop
61-
return false;
75+
return $this->api;
6276
}
6377
}

src/ShopifyApp/ShopifyAppProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public function boot()
2121
$this->publishes([
2222
__DIR__.'/resources/config/shopify-app.php' => config_path('shopify-app.php')
2323
]);
24+
25+
// Database migrations
26+
$this->loadMigrationsFrom(__DIR__.'/resources/database/migrations');
2427
}
2528

2629
/**
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateShopsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('shops', function(Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('shopify_domain');
19+
$table->string('shopify_token');
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::drop('shops');
32+
}
33+
}

tests/ShopifyAppTest.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,28 @@ public function testShopWithoutSession()
88
{
99
// No session, no API instance, thus no shop
1010
$shopifyApp = new ShopifyApp($this->app);
11-
$this->assertFalse($shopifyApp->shop());
11+
$this->assertNull($shopifyApp->shop());
12+
$this->assertNull($shopifyApp->api());
1213
}
1314

1415
public function testShopWithSession()
1516
{
1617
session(['shopify_domain' => 'example.myshopify.com']);
1718
$shopifyApp = new ShopifyApp($this->app);
1819

19-
// First run should store the API to shop var
20+
// First run should store the shop object to shop var
2021
$run1 = $shopifyApp->shop();
2122

2223
// Second run should retrive shop var
2324
$run2 = $shopifyApp->shop();
2425

26+
// First run should store the shop object to shop var
27+
$run3 = $shopifyApp->api();
28+
29+
// Second run should retrive shop var
30+
$run4 = $shopifyApp->api();
31+
2532
$this->assertEquals($run1, $run2);
33+
$this->assertEquals($run3, $run4);
2634
}
2735
}

tests/TestCase.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
11
<?php namespace OhMyBrew\ShopifyApp\Test;
22

33
use Orchestra\Testbench\TestCase as OrchestraTestCase;
4+
use Orchestra\Database\ConsoleServiceProvider;
45
use OhMyBrew\ShopifyApp\ShopifyAppProvider;
6+
use OhMyBrew\ShopifyApp\Models\Shop;
57

68
abstract class TestCase extends OrchestraTestCase
79
{
810
public function setUp()
911
{
1012
parent::setUp();
13+
$this->setupDatabase($this->app);
1114
}
1215

1316
protected function getPackageProviders($app)
1417
{
1518
return [
1619
ShopifyAppProvider::class,
20+
ConsoleServiceProvider::class
1721
];
1822
}
23+
24+
protected function getEnvironmentSetUp($app)
25+
{
26+
$app['config']->set('database.default', 'sqlite');
27+
$app['config']->set('database.connections.sqlite', [
28+
'driver' => 'sqlite',
29+
'database' => ':memory:',
30+
'prefix' => ''
31+
]);
32+
}
33+
34+
protected function setupDatabase($app) {
35+
$this->loadMigrationsFrom(realpath(__DIR__.'/../src/ShopifyApp/resources/database/migrations'));
36+
37+
$shop = new Shop;
38+
$shop->shopify_domain = 'example.myshopify.com';
39+
$shop->shopify_token = '1234';
40+
$shop->save();
41+
}
1942
}

0 commit comments

Comments
 (0)