Skip to content

Commit 6ebe131

Browse files
enmaboyaTyler King
andauthored
Add variables command (gnikyt#1172)
* update provider, add command * add variables command * variables command test * Minor change to trigger GH Action Co-authored-by: Tyler King <tyler@osiset.com>
1 parent 48a0b59 commit 6ebe131

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

src/Console/AddVariablesCommand.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Str;
7+
8+
class AddVariablesCommand extends Command
9+
{
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'shopify-app:add:variables';
16+
17+
/**
18+
* The console command signature.
19+
*
20+
* @var string
21+
*/
22+
protected $signature = 'shopify-app:add:variables
23+
{--always-no : Skip generating variable if it already exists.}
24+
{--force : Skip confirmation when overwriting an existing variable.}';
25+
26+
/**
27+
* The console command description.
28+
*
29+
* @var string
30+
*/
31+
protected $description = 'Add default variables to env';
32+
33+
/**
34+
* Execute the console command.
35+
*
36+
* @return void
37+
*/
38+
public function handle()
39+
{
40+
$env = $this->envPath();
41+
42+
if (file_exists($env)) {
43+
foreach ($this->shopifyVariables() as $key => $variable) {
44+
if (Str::contains(file_get_contents($env), $key) === false) {
45+
file_put_contents($env, PHP_EOL."$key=$variable", FILE_APPEND);
46+
} else {
47+
if ($this->option('always-no')) {
48+
$this->comment("Variable $key already exists. Skipping...");
49+
50+
continue;
51+
}
52+
53+
if ($this->isConfirmed($key) === false) {
54+
$this->comment('There has been no change.');
55+
56+
continue;
57+
}
58+
}
59+
}
60+
}
61+
62+
$this->successResult();
63+
}
64+
65+
/**
66+
* Display result.
67+
*
68+
* @return void
69+
*/
70+
protected function successResult(): void
71+
{
72+
$this->info('All variables will be set');
73+
}
74+
75+
/**
76+
* Check if the modification is confirmed.
77+
*
78+
* @param string $key
79+
*
80+
* @return bool
81+
*/
82+
protected function isConfirmed(string $key): bool
83+
{
84+
return $this->option('force')
85+
? true
86+
: $this->confirm(
87+
"This will invalidate {$key} variable. Are you sure you want to override {$key}?"
88+
);
89+
}
90+
91+
/**
92+
* Get shopify env variables
93+
*
94+
* @return array
95+
*/
96+
public function shopifyVariables(): array
97+
{
98+
return [
99+
'SHOPIFY_APP_NAME' => config('app.name'),
100+
'SHOPIFY_API_KEY' => '',
101+
'SHOPIFY_API_SECRET' => '',
102+
'SHOPIFY_API_SCOPES' => config('shopify-app.api_scopes'),
103+
'AFTER_AUTHENTICATE_JOB' => "\App\Jobs\AfterAuthenticateJob",
104+
];
105+
}
106+
107+
/**
108+
* Get the .env file path.
109+
*
110+
* @return string
111+
*/
112+
protected function envPath()
113+
{
114+
$enviromemtFile = $this->laravel->environmentFile();
115+
$baseEnvFile = $this->laravel->basePath('.env');
116+
117+
if (file_exists($enviromemtFile) && method_exists($this->laravel, 'environmentFile')) {
118+
return $enviromemtFile;
119+
}
120+
121+
return $baseEnvFile;
122+
}
123+
}

src/ShopifyAppProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Osiset\ShopifyApp\Actions\DispatchWebhooks as DispatchWebhooksAction;
2020
use Osiset\ShopifyApp\Actions\GetPlanUrl as GetPlanUrlAction;
2121
use Osiset\ShopifyApp\Actions\InstallShop as InstallShopAction;
22+
use Osiset\ShopifyApp\Console\AddVariablesCommand;
2223
use Osiset\ShopifyApp\Console\WebhookJobMakeCommand;
2324
use Osiset\ShopifyApp\Contracts\ApiHelper as IApiHelper;
2425
use Osiset\ShopifyApp\Contracts\Commands\Charge as IChargeCommand;
@@ -78,6 +79,7 @@ public function register()
7879
$this->mergeConfigFrom(__DIR__.'/resources/config/shopify-app.php', 'shopify-app');
7980

8081
$this->commands([
82+
AddVariablesCommand::class,
8183
WebhookJobMakeCommand::class,
8284
]);
8385

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Osiset\ShopifyApp\Test\Console;
4+
5+
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
6+
use Osiset\ShopifyApp\Console\AddVariablesCommand;
7+
use Osiset\ShopifyApp\Test\TestCase;
8+
9+
class AddVariablesCommandTest extends TestCase
10+
{
11+
public function testItShouldRun(): void
12+
{
13+
$tempEnv = tempnam(sys_get_temp_dir(), 'ENV');
14+
15+
$this->app->loadEnvironmentFrom($tempEnv);
16+
$this->app->bootstrapWith([LoadEnvironmentVariables::class]);
17+
18+
$this
19+
->artisan('shopify-app:add:variables')
20+
->expectsOutput('All variables will be set')
21+
->assertExitCode(0);
22+
}
23+
24+
public function testItShouldRunWithAlwaysNo(): void
25+
{
26+
$tempEnv = tempnam(sys_get_temp_dir(), 'ENV');
27+
$command = new AddVariablesCommand();
28+
29+
foreach ($command->shopifyVariables() as $key => $variable) {
30+
file_put_contents($tempEnv, PHP_EOL."$key=$variable", FILE_APPEND);
31+
}
32+
33+
$this->app->loadEnvironmentFrom($tempEnv);
34+
$this->app->bootstrapWith([LoadEnvironmentVariables::class]);
35+
36+
$this
37+
->artisan('shopify-app:add:variables --always-no')
38+
->expectsOutput('Variable SHOPIFY_APP_NAME already exists. Skipping...')
39+
->expectsOutput('Variable SHOPIFY_API_KEY already exists. Skipping...')
40+
->expectsOutput('Variable SHOPIFY_API_SECRET already exists. Skipping...')
41+
->expectsOutput('Variable SHOPIFY_API_SCOPES already exists. Skipping...')
42+
->expectsOutput('Variable AFTER_AUTHENTICATE_JOB already exists. Skipping...')
43+
->expectsOutput('All variables will be set')
44+
->assertExitCode(0);
45+
}
46+
47+
public function testItShouldRunWithForce(): void
48+
{
49+
$tempEnv = tempnam(sys_get_temp_dir(), 'ENV');
50+
$command = new AddVariablesCommand();
51+
52+
foreach ($command->shopifyVariables() as $key => $variable) {
53+
file_put_contents($tempEnv, PHP_EOL."$key=$variable", FILE_APPEND);
54+
}
55+
56+
$this->app->loadEnvironmentFrom($tempEnv);
57+
$this->app->bootstrapWith([LoadEnvironmentVariables::class]);
58+
59+
$this
60+
->artisan('shopify-app:add:variables --force')
61+
->expectsOutput('All variables will be set')
62+
->assertExitCode(0);
63+
}
64+
65+
public function testItShouldRunWithoutForceAndNo(): void
66+
{
67+
$tempEnv = tempnam(sys_get_temp_dir(), 'ENV');
68+
$command = new AddVariablesCommand();
69+
70+
foreach ($command->shopifyVariables() as $key => $variable) {
71+
file_put_contents($tempEnv, PHP_EOL."$key=$variable", FILE_APPEND);
72+
}
73+
74+
$this->app->loadEnvironmentFrom($tempEnv);
75+
$this->app->bootstrapWith([LoadEnvironmentVariables::class]);
76+
77+
$this
78+
->artisan('shopify-app:add:variables')
79+
->expectsQuestion('This will invalidate SHOPIFY_APP_NAME variable. Are you sure you want to override SHOPIFY_APP_NAME?', 'no')
80+
->expectsQuestion('This will invalidate SHOPIFY_API_KEY variable. Are you sure you want to override SHOPIFY_API_KEY?', 'no')
81+
->expectsQuestion('This will invalidate SHOPIFY_API_SECRET variable. Are you sure you want to override SHOPIFY_API_SECRET?', 'no')
82+
->expectsQuestion('This will invalidate SHOPIFY_API_SCOPES variable. Are you sure you want to override SHOPIFY_API_SCOPES?', 'no')
83+
->expectsQuestion('This will invalidate AFTER_AUTHENTICATE_JOB variable. Are you sure you want to override AFTER_AUTHENTICATE_JOB?', 'no')
84+
->assertExitCode(0);
85+
}
86+
87+
public function testItShouldRunWithoutForceAndYes(): void
88+
{
89+
$tempEnv = tempnam(sys_get_temp_dir(), 'ENV');
90+
$command = new AddVariablesCommand();
91+
92+
foreach ($command->shopifyVariables() as $key => $variable) {
93+
file_put_contents($tempEnv, PHP_EOL."$key=$variable", FILE_APPEND);
94+
}
95+
96+
$this->app->loadEnvironmentFrom($tempEnv);
97+
$this->app->bootstrapWith([LoadEnvironmentVariables::class]);
98+
99+
$this
100+
->artisan('shopify-app:add:variables')
101+
->expectsQuestion('This will invalidate SHOPIFY_APP_NAME variable. Are you sure you want to override SHOPIFY_APP_NAME?', 'yes')
102+
->expectsQuestion('This will invalidate SHOPIFY_API_KEY variable. Are you sure you want to override SHOPIFY_API_KEY?', 'yes')
103+
->expectsQuestion('This will invalidate SHOPIFY_API_SECRET variable. Are you sure you want to override SHOPIFY_API_SECRET?', 'yes')
104+
->expectsQuestion('This will invalidate SHOPIFY_API_SCOPES variable. Are you sure you want to override SHOPIFY_API_SCOPES?', 'yes')
105+
->expectsQuestion('This will invalidate AFTER_AUTHENTICATE_JOB variable. Are you sure you want to override AFTER_AUTHENTICATE_JOB?', 'yes')
106+
->expectsOutput('All variables will be set')
107+
->assertExitCode(0);
108+
}
109+
110+
public function testItShouldRunWithMissingEnv(): void
111+
{
112+
$this
113+
->artisan('shopify-app:add:variables')
114+
->expectsOutput('All variables will be set')
115+
->assertExitCode(0);
116+
}
117+
}

0 commit comments

Comments
 (0)