Skip to content

Commit eec24b3

Browse files
authored
Merge pull request #66 from RonasIT/update-pgsql-migrations-if-not-set
fix: update DB env vars during initialization
2 parents 20e47a3 + 561de31 commit eec24b3

File tree

3 files changed

+50
-66
lines changed

3 files changed

+50
-66
lines changed

src/Commands/InitCommand.php

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Contracts\View\View;
88
use Illuminate\Support\Arr;
99
use Illuminate\Support\Carbon;
10-
use Illuminate\Support\Facades\Artisan;
1110
use Illuminate\Support\Facades\Validator;
1211
use Illuminate\Support\Str;
1312
use RonasIT\ProjectInitializator\Enums\AuthTypeEnum;
@@ -79,6 +78,12 @@ class InitCommand extends Command implements Isolatable
7978

8079
protected string $appName;
8180

81+
protected string $dbConnection = 'pgsql';
82+
protected string $dbHost = 'pgsql';
83+
protected string $dbPort = '5432';
84+
protected string $dbName = 'postgres';
85+
protected string $dbUserName = 'postgres';
86+
8287
protected AppTypeEnum $appType;
8388

8489
public function handle(): void
@@ -99,11 +104,11 @@ public function handle(): void
99104

100105
$this->updateEnvFile($envFile, [
101106
'APP_NAME' => $this->appName,
102-
'DB_CONNECTION' => 'pgsql',
103-
'DB_HOST' => 'pgsql',
104-
'DB_PORT' => '5432',
105-
'DB_DATABASE' => 'postgres',
106-
'DB_USERNAME' => 'postgres',
107+
'DB_CONNECTION' => $this->dbConnection,
108+
'DB_HOST' => $this->dbHost,
109+
'DB_PORT' => $this->dbPort,
110+
'DB_DATABASE' => $this->dbName,
111+
'DB_USERNAME' => $this->dbUserName,
107112
'DB_PASSWORD' => '',
108113
]);
109114

@@ -118,7 +123,7 @@ public function handle(): void
118123
'CACHE_STORE' => 'redis',
119124
'QUEUE_CONNECTION' => 'redis',
120125
'SESSION_DRIVER' => 'redis',
121-
'DB_CONNECTION' => 'pgsql',
126+
'DB_CONNECTION' => $this->dbConnection,
122127
]);
123128

124129
$this->info('Project initialized successfully!');
@@ -241,11 +246,11 @@ public function handle(): void
241246

242247
$this->publishWebLogin();
243248

244-
Artisan::call('migrate');
245-
246249
if ($this->shouldUninstallPackage) {
247-
shell_exec('composer remove --dev ronasit/laravel-project-initializator --no-script --ansi');
250+
shell_exec('composer remove --dev ronasit/laravel-project-initializator --ansi');
248251
}
252+
253+
$this->runMigrations();
249254
}
250255

251256
protected function setupComposerHooks(): void
@@ -286,6 +291,23 @@ protected function setAutoDocContactEmail(string $email): void
286291
$config->write();
287292
}
288293

294+
protected function runMigrations(): void
295+
{
296+
config([
297+
'database.default' => $this->dbConnection,
298+
'database.connections.pgsql' => [
299+
'driver' => $this->dbConnection,
300+
'host' => $this->dbHost,
301+
'port' => $this->dbPort,
302+
'database' => $this->dbName,
303+
'username' => $this->dbUserName,
304+
'password' => '',
305+
],
306+
]);
307+
308+
shell_exec('php artisan migrate --ansi');
309+
}
310+
289311
protected function createAdminUser(string $kebabName): void
290312
{
291313
$defaultPassword = substr(md5(uniqid()), 0, 8);
@@ -604,10 +626,7 @@ protected function enableClerk(): void
604626

605627
protected function publishWebLogin(): void
606628
{
607-
Artisan::call('vendor:publish', [
608-
'--tag' => 'initializator-web-login',
609-
'--force' => true,
610-
]);
629+
shell_exec('php artisan vendor:publish --tag=initializator-web-login --force');
611630

612631
file_put_contents(base_path('routes/web.php'), "\nAuth::routes();\n", FILE_APPEND);
613632
}

tests/InitCommandTest.php

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace RonasIT\ProjectInitializator\Tests;
44

5-
use Illuminate\Support\Facades\File;
65
use RonasIT\ProjectInitializator\Tests\Support\Traits\InitCommandMockTrait;
76

87
class InitCommandTest extends TestCase
@@ -42,6 +41,8 @@ public function testRunWithoutAdminAndReadmeCreationConvertAppNameToPascalCaseTe
4241
$this->callShellExec('composer require --dev brainmaestro/composer-git-hooks --ansi'),
4342
$this->callShellExec('./vendor/bin/cghooks update --ansi'),
4443
$this->callShellExec('php artisan lang:publish --ansi'),
44+
$this->callShellExec('php artisan vendor:publish --tag=initializator-web-login --force'),
45+
$this->callShellExec('php artisan migrate --ansi'),
4546
);
4647

4748
$this
@@ -58,8 +59,6 @@ public function testRunWithoutAdminAndReadmeCreationConvertAppNameToPascalCaseTe
5859
->expectsConfirmation('Do you want to install media package?')
5960
->expectsConfirmation('Do you want to uninstall project-initializator package?')
6061
->assertExitCode(0);
61-
62-
$this->assertWebLoginPublished();
6362
}
6463

6564
public function testRunWithoutAdminAndReadmeCreation()
@@ -94,6 +93,8 @@ public function testRunWithoutAdminAndReadmeCreation()
9493
$this->callShellExec('php artisan lang:publish --ansi'),
9594
$this->callShellExec('composer require ronasit/laravel-telescope-extension --ansi'),
9695
$this->callShellExec('php artisan telescope:install --ansi'),
96+
$this->callShellExec('php artisan vendor:publish --tag=initializator-web-login --force'),
97+
$this->callShellExec('php artisan migrate --ansi'),
9798
);
9899

99100
$this
@@ -110,8 +111,6 @@ public function testRunWithoutAdminAndReadmeCreation()
110111
->expectsConfirmation('Do you want to install media package?')
111112
->expectsConfirmation('Do you want to uninstall project-initializator package?')
112113
->assertExitCode(0);
113-
114-
$this->assertWebLoginPublished();
115114
}
116115

117116
public function testRunWithAdminAndWithoutReadmeCreation()
@@ -146,6 +145,8 @@ public function testRunWithAdminAndWithoutReadmeCreation()
146145
$this->callShellExec('php artisan lang:publish --ansi'),
147146
$this->callShellExec('composer require ronasit/laravel-telescope-extension --ansi'),
148147
$this->callShellExec('php artisan telescope:install --ansi'),
148+
$this->callShellExec('php artisan vendor:publish --tag=initializator-web-login --force'),
149+
$this->callShellExec('php artisan migrate --ansi'),
149150
);
150151

151152
$this
@@ -166,8 +167,6 @@ public function testRunWithAdminAndWithoutReadmeCreation()
166167
->expectsConfirmation('Do you want to install media package?')
167168
->expectsConfirmation('Do you want to uninstall project-initializator package?')
168169
->assertExitCode(0);
169-
170-
$this->assertWebLoginPublished();
171170
}
172171

173172
public function testRunWithAdminAndDefaultReadmeCreation()
@@ -222,6 +221,8 @@ public function testRunWithAdminAndDefaultReadmeCreation()
222221
$this->callShellExec('php artisan laravel-clerk:install --ansi'),
223222
$this->callShellExec('composer require ronasit/laravel-telescope-extension --ansi'),
224223
$this->callShellExec('php artisan telescope:install --ansi'),
224+
$this->callShellExec('php artisan vendor:publish --tag=initializator-web-login --force'),
225+
$this->callShellExec('php artisan migrate --ansi'),
225226
);
226227

227228
$this
@@ -292,8 +293,6 @@ public function testRunWithAdminAndDefaultReadmeCreation()
292293
->expectsConfirmation('Do you want to install media package?')
293294
->expectsConfirmation('Do you want to uninstall project-initializator package?')
294295
->assertExitCode(0);
295-
296-
$this->assertWebLoginPublished();
297296
}
298297

299298
public function testRunWithAdminAndPartialReadmeCreation()
@@ -334,6 +333,8 @@ public function testRunWithAdminAndPartialReadmeCreation()
334333
$this->callShellExec('php artisan lang:publish --ansi'),
335334
$this->callShellExec('composer require ronasit/laravel-telescope-extension --ansi'),
336335
$this->callShellExec('php artisan telescope:install --ansi'),
336+
$this->callShellExec('php artisan vendor:publish --tag=initializator-web-login --force'),
337+
$this->callShellExec('php artisan migrate --ansi'),
337338
);
338339

339340
$this
@@ -394,8 +395,6 @@ public function testRunWithAdminAndPartialReadmeCreation()
394395
->expectsConfirmation('Do you want to install media package?')
395396
->expectsConfirmation('Do you want to uninstall project-initializator package?')
396397
->assertExitCode(0);
397-
398-
$this->assertWebLoginPublished();
399398
}
400399

401400
public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorInstallationMedia()
@@ -444,7 +443,9 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns
444443
$this->callShellExec('composer require ronasit/laravel-telescope-extension --ansi'),
445444
$this->callShellExec('php artisan telescope:install --ansi'),
446445
$this->callShellExec('composer require ronasit/laravel-media --ansi'),
447-
$this->callShellExec('composer remove --dev ronasit/laravel-project-initializator --no-script --ansi'),
446+
$this->callShellExec('php artisan vendor:publish --tag=initializator-web-login --force'),
447+
$this->callShellExec('composer remove --dev ronasit/laravel-project-initializator --ansi'),
448+
$this->callShellExec('php artisan migrate --ansi'),
448449
);
449450

450451
$this
@@ -512,8 +513,6 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns
512513
->expectsConfirmation('Do you want to install media package?', 'yes')
513514
->expectsConfirmation('Do you want to uninstall project-initializator package?', 'yes')
514515
->assertExitCode(0);
515-
516-
$this->assertWebLoginPublished();
517516
}
518517

519518
public function testRunWithoutAdminAndUsingTelescope()
@@ -554,6 +553,8 @@ public function testRunWithoutAdminAndUsingTelescope()
554553
$this->callShellExec('php artisan lang:publish --ansi'),
555554
$this->callShellExec('composer require ronasit/laravel-telescope-extension --ansi'),
556555
$this->callShellExec('php artisan telescope:install --ansi'),
556+
$this->callShellExec('php artisan vendor:publish --tag=initializator-web-login --force'),
557+
$this->callShellExec('php artisan migrate --ansi'),
557558
);
558559

559560
$this
@@ -618,8 +619,6 @@ public function testRunWithoutAdminAndUsingTelescope()
618619
->expectsConfirmation('Do you want to install media package?')
619620
->expectsConfirmation('Do you want to uninstall project-initializator package?')
620621
->assertExitCode(0);
621-
622-
$this->assertWebLoginPublished();
623622
}
624623

625624
public function testRunWithClerkMobileAppWithPintInstalled(): void
@@ -674,6 +673,8 @@ public function testRunWithClerkMobileAppWithPintInstalled(): void
674673
$this->callShellExec('php artisan laravel-clerk:install --ansi'),
675674
$this->callShellExec('composer require ronasit/laravel-telescope-extension --ansi'),
676675
$this->callShellExec('php artisan telescope:install --ansi'),
676+
$this->callShellExec('php artisan vendor:publish --tag=initializator-web-login --force'),
677+
$this->callShellExec('php artisan migrate --ansi'),
677678
);
678679

679680
$this
@@ -744,21 +745,5 @@ public function testRunWithClerkMobileAppWithPintInstalled(): void
744745
->expectsConfirmation('Do you want to install media package?')
745746
->expectsConfirmation('Do you want to uninstall project-initializator package?')
746747
->assertExitCode(0);
747-
748-
$this->assertWebLoginPublished();
749-
}
750-
751-
protected function assertWebLoginPublished(): void
752-
{
753-
$this->assertFileEqualsFixture('login_controller.php', app_path('Http/Controllers/Auth/LoginController.php'));
754-
$this->assertFileEqualsFixture('app.css', public_path('app.css'));
755-
$this->assertFileEqualsFixture('app.js', public_path('app.js'));
756-
$this->assertFileEqualsFixture('app.blade.php', resource_path('views/layouts/app.blade.php'));
757-
$this->assertFileEqualsFixture('login.blade.php', resource_path('views/auth/login.blade.php'));
758-
759-
File::deleteDirectory(app_path());
760-
File::deleteDirectory(public_path());
761-
File::deleteDirectory(resource_path('views/layouts'));
762-
File::deleteDirectory(resource_path('views/auth'));
763748
}
764749
}

tests/TestCase.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,6 @@ protected function getPackageProviders($app): array
3131
];
3232
}
3333

34-
protected function assertFileEqualsFixture(string $fixture, string $fileName, bool $exportMode = false): void
35-
{
36-
$this->assertFileExists($fileName);
37-
38-
$data = File::get($fileName);
39-
40-
if ($this->globalExportMode || $exportMode) {
41-
$this->exportContent($data, $fixture);
42-
}
43-
44-
$fixturePath = $this->prepareFixtureName($this->getFixturePath($fixture));
45-
$assertFailedMessage = "Failed asserting that the provided file {$fileName} equal to fixture: {$fixturePath}";
46-
47-
$this->assertEquals(
48-
expected: $this->getFixture($fixture),
49-
actual: $data,
50-
message: $assertFailedMessage,
51-
);
52-
}
53-
5434
protected function getReadmeTemplateContent(string $templateName): string
5535
{
5636
return file_get_contents(base_path("/resources/md/readme/{$templateName}"));

0 commit comments

Comments
 (0)