Skip to content

Commit 2666126

Browse files
authored
Merge pull request #21 from RonasIT/17_suggest-app-name
feat: suggest app name
2 parents f1ac661 + f5aef19 commit 2666126

File tree

5 files changed

+165
-15
lines changed

5 files changed

+165
-15
lines changed

src/Commands/InitCommand.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,24 @@ class InitCommand extends Command implements Isolatable
6161
'php artisan telescope:install',
6262
];
6363

64+
protected string $appName;
65+
6466
public function handle(): void
6567
{
66-
$appName = $this->argument('application-name');
67-
$kebabName = Str::kebab($appName);
68+
$this->prepareAppName();
69+
70+
$kebabName = Str::kebab($this->appName);
6871

6972
$this->appUrl = $this->ask('Please enter an application URL', "https://api.dev.{$kebabName}.com");
7073

7174
$envFile = (file_exists('.env')) ? '.env' : '.env.example';
7275

7376
$this->updateConfigFile($envFile, '=', [
74-
'APP_NAME' => $appName,
77+
'APP_NAME' => $this->appName,
7578
]);
7679

7780
$this->updateConfigFile('.env.development', '=', [
78-
'APP_NAME' => $appName,
81+
'APP_NAME' => $this->appName,
7982
'APP_URL' => $this->appUrl,
8083
]);
8184

@@ -154,10 +157,9 @@ protected function createAdminUser(string $kebabName): void
154157

155158
protected function fillReadme(): void
156159
{
157-
$appName = $this->argument('application-name');
158160
$file = $this->loadReadmePart('README.md');
159161

160-
$this->setReadmeValue($file, 'project_name', $appName);
162+
$this->setReadmeValue($file, 'project_name', $this->appName);
161163

162164
$type = $this->choice(
163165
question: 'What type of application will your API serve?',
@@ -358,4 +360,15 @@ protected function saveReadme(): void
358360
{
359361
file_put_contents('README.md', $this->readmeContent);
360362
}
363+
364+
protected function prepareAppName(): void
365+
{
366+
$this->appName = $this->argument('application-name');
367+
368+
$pascalCaseAppName = ucfirst(Str::camel($this->appName));
369+
370+
if ($this->appName !== $pascalCaseAppName && $this->confirm("The application name is not in PascalCase, would you like to use {$pascalCaseAppName}")) {
371+
$this->appName = $pascalCaseAppName;
372+
}
373+
}
361374
}

tests/InitCommandTest.php

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,55 @@ public function testRunWithoutAdminAndReadmeCreation()
1313
$this->mockFileGetContent(
1414
[
1515
'arguments' => ['.env.example'],
16-
'result' => $this->getFixture('env.example.yml'),
16+
'result' => $this->getFixture('env.example_app_name_pascal_case.yml'),
1717
],
1818
[
1919
'arguments' => ['.env.development'],
20-
'result' => $this->getFixture('env.development.yml'),
20+
'result' => $this->getFixture('env.development_app_name_pascal_case.yml'),
2121
],
2222
);
2323

24-
$this->mockFilePutContent();
24+
$this->mockFilePutContent(
25+
'env.example_app_name_pascal_case.yml',
26+
'env.development_app_name_pascal_case.yml',
27+
);
28+
29+
$this->mockShellExec(
30+
['arguments' => 'composer require ronasit/laravel-helpers --ansi'],
31+
['arguments' => 'composer require ronasit/laravel-swagger --ansi'],
32+
['arguments' => 'composer require --dev ronasit/laravel-entity-generator --ansi'],
33+
['arguments' => 'composer require laravel/telescope --ansi'],
34+
['arguments' => 'php artisan telescope:install --ansi'],
35+
);
36+
37+
$this
38+
->artisan('init "MyApp"')
39+
->expectsOutput('Project initialized successfully!')
40+
->expectsQuestion('Please enter an application URL', 'https://mysite.com')
41+
->expectsConfirmation('Do you want to generate an admin user?')
42+
->expectsConfirmation('Do you want to generate a README file?')
43+
->expectsConfirmation('Do you want to install media package?')
44+
->expectsConfirmation('Do you want to uninstall project-initializator package?')
45+
->assertExitCode(0);
46+
}
47+
48+
public function testRunWithoutAdminAndReadmeCreationConvertAppNameToPascalCase()
49+
{
50+
$this->mockFileGetContent(
51+
[
52+
'arguments' => ['.env.example'],
53+
'result' => $this->getFixture('env.example_app_name_pascal_case.yml'),
54+
],
55+
[
56+
'arguments' => ['.env.development'],
57+
'result' => $this->getFixture('env.development_app_name_pascal_case.yml'),
58+
],
59+
);
60+
61+
$this->mockFilePutContent(
62+
'env.example_app_name_pascal_case.yml',
63+
'env.development_app_name_pascal_case.yml',
64+
);
2565

2666
$this->mockShellExec(
2767
['arguments' => 'composer require ronasit/laravel-helpers --ansi'],
@@ -33,6 +73,7 @@ public function testRunWithoutAdminAndReadmeCreation()
3373

3474
$this
3575
->artisan('init "My App"')
76+
->expectsConfirmation('The application name is not in PascalCase, would you like to use MyApp', 'yes')
3677
->expectsOutput('Project initialized successfully!')
3778
->expectsQuestion('Please enter an application URL', 'https://mysite.com')
3879
->expectsConfirmation('Do you want to generate an admin user?')
@@ -56,12 +97,14 @@ public function testRunWithAdminAndWithoutReadmeCreation()
5697
);
5798

5899
$this->mockFilePutContent(
100+
'env.example.yml',
101+
'env.development.yml',
59102
[
60103
'database/migrations/2018_11_11_111111_add_default_user.php',
61104
$this->getFixture('migration.php'),
62105
'optionalParameter',
63106
'optionalParameter',
64-
]
107+
],
65108
);
66109

67110
$this->mockShellExec(
@@ -74,6 +117,7 @@ public function testRunWithAdminAndWithoutReadmeCreation()
74117

75118
$this
76119
->artisan('init "My App"')
120+
->expectsConfirmation('The application name is not in PascalCase, would you like to use MyApp')
77121
->expectsOutput('Project initialized successfully!')
78122
->expectsQuestion('Please enter an application URL', 'https://mysite.com')
79123
->expectsConfirmation('Do you want to generate an admin user?', 'yes')
@@ -133,6 +177,8 @@ public function testRunWithAdminAndDefaultReadmeCreation()
133177
);
134178

135179
$this->mockFilePutContent(
180+
'env.example.yml',
181+
'env.development.yml',
136182
[
137183
'database/migrations/2018_11_11_111111_add_default_user.php',
138184
$this->getFixture('migration.php'),
@@ -158,6 +204,7 @@ public function testRunWithAdminAndDefaultReadmeCreation()
158204

159205
$this
160206
->artisan('init "My App"')
207+
->expectsConfirmation('The application name is not in PascalCase, would you like to use MyApp')
161208
->expectsOutput('Project initialized successfully!')
162209
->expectsQuestion('Please enter an application URL', 'https://mysite.com')
163210
->expectsConfirmation('Do you want to generate an admin user?', 'yes')
@@ -263,6 +310,8 @@ public function testRunWithAdminAndPartialReadmeCreation()
263310
);
264311

265312
$this->mockFilePutContent(
313+
'env.example.yml',
314+
'env.development.yml',
266315
[
267316
'README.md',
268317
$this->getFixture('partial_readme.md'),
@@ -281,6 +330,7 @@ public function testRunWithAdminAndPartialReadmeCreation()
281330

282331
$this
283332
->artisan('init "My App"')
333+
->expectsConfirmation('The application name is not in PascalCase, would you like to use MyApp')
284334
->expectsOutput('Project initialized successfully!')
285335
->expectsQuestion('Please enter an application URL', 'https://mysite.com')
286336
->expectsConfirmation('Do you want to generate an admin user?')
@@ -383,6 +433,8 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns
383433
);
384434

385435
$this->mockFilePutContent(
436+
'env.example.yml',
437+
'env.development.yml',
386438
[
387439
'database/migrations/2018_11_11_111111_add_default_user.php',
388440
$this->getFixture('migration.php'),
@@ -410,6 +462,7 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns
410462

411463
$this
412464
->artisan('init "My App"')
465+
->expectsConfirmation('The application name is not in PascalCase, would you like to use MyApp')
413466
->expectsOutput('Project initialized successfully!')
414467
->expectsQuestion('Please enter an application URL', 'https://mysite.com')
415468
->expectsConfirmation('Do you want to generate an admin user?', 'yes')

tests/Support/Traits/InitCommandMockTrait.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ trait InitCommandMockTrait
99
{
1010
use MockTrait;
1111

12-
public function mockFilePutContent(...$arguments): void
13-
{
12+
public function mockFilePutContent(
13+
string $exampleEnvFixtureName = 'env.example.yml',
14+
string $developmentEnvFixtureName = 'env.development.yml',
15+
...$arguments,
16+
): void {
1417
$callChain = [
15-
['.env.example', $this->getFixture('env.example.yml'), 'optionalParameter', 'optionalParameter'],
16-
['.env.development', $this->getFixture('env.development.yml'), 'optionalParameter', 'optionalParameter'],
18+
['.env.example', $this->getFixture($exampleEnvFixtureName), 'optionalParameter', 'optionalParameter'],
19+
['.env.development', $this->getFixture($developmentEnvFixtureName), 'optionalParameter', 'optionalParameter'],
1720
...$arguments,
1821
];
1922

@@ -22,7 +25,7 @@ public function mockFilePutContent(...$arguments): void
2225
callChain: array_map(
2326
fn ($call) => $this->functionCall('file_put_contents', $call),
2427
$callChain,
25-
)
28+
),
2629
);
2730
}
2831

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
APP_NAME=MyApp
2+
APP_ENV=development
3+
APP_KEY=
4+
APP_DEBUG=true
5+
APP_LOG_LEVEL=debug
6+
APP_URL=https://mysite.com
7+
8+
DB_CONNECTION=pgsql
9+
DB_HOST=
10+
DB_PORT=
11+
DB_DATABASE=
12+
DB_USERNAME=
13+
DB_PASSWORD=
14+
15+
BROADCAST_DRIVER=log
16+
CACHE_DRIVER=file
17+
SESSION_DRIVER=redis
18+
QUEUE_CONNECTION=redis
19+
20+
REDIS_HOST=redis
21+
REDIS_PASSWORD=
22+
REDIS_PORT=
23+
24+
MAIL_DRIVER=smtp
25+
MAIL_HOST=
26+
MAIL_PORT=
27+
MAIL_USERNAME=
28+
MAIL_PASSWORD=
29+
MAIL_ENCRYPTION=
30+
31+
FILESYSTEM_DISK=gcs
32+
33+
PUSHER_APP_ID=
34+
PUSHER_APP_KEY=
35+
PUSHER_APP_SECRET=
36+
37+
FRONTEND_URL=
38+
39+
JWT_SHOW_BLACKLIST_EXCEPTION=true
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
APP_NAME=MyApp
2+
APP_ENV=local
3+
APP_KEY=
4+
APP_DEBUG=true
5+
APP_LOG_LEVEL=debug
6+
APP_URL=http://localhost
7+
8+
DB_CONNECTION=pgsql
9+
DB_HOST=pgsql
10+
DB_PORT=5432
11+
DB_DATABASE=postgres
12+
DB_USERNAME=postgres
13+
DB_PASSWORD=""
14+
15+
BROADCAST_DRIVER=log
16+
CACHE_DRIVER=redis
17+
SESSION_DRIVER=redis
18+
QUEUE_CONNECTION=sync
19+
20+
REDIS_HOST=redis
21+
REDIS_PASSWORD=null
22+
REDIS_PORT=6379
23+
24+
MAIL_DRIVER=smtp
25+
MAIL_HOST=smtp.mailtrap.io
26+
MAIL_PORT=2525
27+
MAIL_USERNAME=null
28+
MAIL_PASSWORD=null
29+
MAIL_ENCRYPTION=null
30+
31+
FILESYSTEM_DISK=local
32+
GOOGLE_CLOUD_STORAGE_BUCKET=ronasit-development
33+
GOOGLE_CLOUD_PROJECT_ID=ronas-it-development
34+
35+
PUSHER_APP_ID=
36+
PUSHER_APP_KEY=
37+
PUSHER_APP_SECRET=
38+
39+
FRONTEND_URL=http://localhost
40+
41+
JWT_SECRET=
42+
JWT_SHOW_BLACKLIST_EXCEPTION=true

0 commit comments

Comments
 (0)