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 376
/
TestCase.php
105 lines (88 loc) · 2.99 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Osiset\ShopifyApp\Test;
use Closure;
use Illuminate\Contracts\Http\Kernel as HttpKernelContract;
use Illuminate\Support\Facades\App;
use Orchestra\Database\ConsoleServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;
use Osiset\BasicShopifyAPI\Options;
use Osiset\ShopifyApp\ShopifyAppProvider;
use Osiset\ShopifyApp\Test\Stubs\Api as ApiStub;
use Osiset\ShopifyApp\Test\Stubs\Kernel as StubKernel;
use Osiset\ShopifyApp\Test\Stubs\User as UserStub;
abstract class TestCase extends OrchestraTestCase
{
protected $model;
public function setUp(): void
{
parent::setUp();
// Setup database
$this->setupDatabase($this->app);
$this->withFactories(__DIR__.'/../src/ShopifyApp/resources/database/factories');
// Assign the user model
$this->model = $this->app['config']->get('auth.providers.users.model');
}
protected function getPackageProviders($app): array
{
// ConsoleServiceProvider required to make migrations work
return [
ShopifyAppProvider::class,
ConsoleServiceProvider::class,
];
}
protected function resolveApplicationHttpKernel($app): void
{
// For adding custom the shop middleware
$app->singleton(HttpKernelContract::class, StubKernel::class);
}
protected function getEnvironmentSetUp($app): void
{
// Use memory SQLite, cleans it self up
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
$app['config']->set('auth.providers.users.model', UserStub::class);
}
protected function setupDatabase($app): void
{
// Run Laravel migrations
$this->loadLaravelMigrations();
// Run package migration
$this->artisan('migrate')->run();
}
protected function swapEnvironment(string $env, Closure $fn): void
{
// Get the current environemnt
$currentEnv = App::environment();
// Set the environment
App::detectEnvironment(function () use ($env) {
return $env;
});
// Run the closure
$fn();
// Reset
App::detectEnvironment(function () use ($currentEnv) {
return $currentEnv;
});
}
protected function setApiStub(): void
{
$this->app['config']->set(
'shopify-app.api_init',
function (Options $opts): ApiStub {
$ts = $this->app['config']->get('shopify-app.api_time_store');
$ls = $this->app['config']->get('shopify-app.api_limit_store');
$sd = $this->app['config']->get('shopify-app.api_deferrer');
return new ApiStub(
$opts,
new $ts(),
new $ls(),
new $sd()
);
}
);
}
}