Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public function handle(): void

$this->setAutoDocContactEmail($this->codeOwnerEmail);

$this->addDefaultHttpExceptionRender();

Artisan::call('migrate');
}

Expand All @@ -204,6 +206,50 @@ protected function setAutoDocContactEmail(string $email): void
$config->write();
}

protected function addDefaultHttpExceptionRender(): void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DenTray

InitCommand class have 600+ strings. mb can we move this functionality in different class?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AZabolotnikov sure, but as a separatly described task :)

{
$file = base_path('bootstrap/app.php');

$content = file_get_contents($file);

$find = [
'$exceptions->render(function (HttpException $exception, Request $request)',
'$exceptions->render(function (\Symfony\Component\HttpKernel\Exception\HttpException $exception, \Illuminate\Http\Request $request)',
];

if (Str::contains($content, $find)) {
return;
}

$imports = [
'use Symfony\Component\HttpKernel\Exception\HttpException;',
'use Illuminate\Http\Request;',
];

foreach ($imports as $import) {
if (!Str::contains($content, $import)) {
$content = preg_replace('/<\?php\s*/', "<?php\n\n{$import}\n", $content, 1);
}
}

$codeToAdd = <<<'PHP'
$exceptions->render(function (HttpException $exception, Request $request) {
return ($request->expectsJson())
? response()->json(['error' => $exception->getMessage()], $exception->getStatusCode())
: null;
});
PHP;

$content = preg_replace(
'/(->withExceptions\(function \(Exceptions \$exceptions\)(?:\: void)? \{)/',
"$1\n" . $codeToAdd . "\n",
$content,
1
);

file_put_contents($file, $content);
}

protected function createAdminUser(string $kebabName): void
{
$defaultPassword = substr(md5(uniqid()), 0, 8);
Expand Down
60 changes: 58 additions & 2 deletions tests/InitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ public function testRunWithoutAdminAndReadmeCreationConvertAppNameToPascalCaseTe
'arguments' => ['.env.development'],
'result' => $this->getFixture('env.development_app_name_pascal_case.yml'),
],
[
'arguments' => [base_path('bootstrap/app.php')],
'result' => $this->getFixture('app.php'),
],
);

$this->mockFilePutContent(
'env.example_app_name_pascal_case.yml',
'env.development_app_name_pascal_case.yml',
[
base_path('bootstrap/app.php'),
$this->getFixture('app_after_changes.php'),
],
);

$this->mockClassExists([
Expand Down Expand Up @@ -67,6 +75,10 @@ public function testRunWithoutAdminAndReadmeCreation()
'arguments' => ['.env.development'],
'result' => $this->getFixture('env.development_app_name_pascal_case.yml'),
],
[
'arguments' => [base_path('bootstrap/app.php')],
'result' => $this->getFixture('app.php'),
],
);

$this->mockFilePutContent(
Expand All @@ -76,6 +88,10 @@ public function testRunWithoutAdminAndReadmeCreation()
'renovate.json',
$this->getFixture('renovate.json'),
],
[
base_path('bootstrap/app.php'),
$this->getFixture('app_after_changes.php'),
],
);

$this->mockShellExec(
Expand Down Expand Up @@ -115,6 +131,10 @@ public function testRunWithAdminAndWithoutReadmeCreation()
'arguments' => ['.env.development'],
'result' => $this->getFixture('env.development.yml'),
],
[
'arguments' => [base_path('bootstrap/app.php')],
'result' => $this->getFixture('app.php'),
],
);

$this->mockFilePutContent(
Expand All @@ -124,6 +144,10 @@ public function testRunWithAdminAndWithoutReadmeCreation()
'database/migrations/2018_11_11_111111_add_default_user.php',
$this->getFixture('migration.php'),
],
[
base_path('bootstrap/app.php'),
$this->getFixture('app_after_changes.php'),
],
);

$this->mockShellExec(
Expand Down Expand Up @@ -215,6 +239,10 @@ public function testRunWithAdminAndDefaultReadmeCreation()
'arguments' => [base_path('/vendor/ronasit/laravel-project-initializator/resources/md/readme/RENOVATE.md')],
'result' => $this->getTemplate('RENOVATE.md'),
],
[
'arguments' => [base_path('bootstrap/app.php')],
'result' => $this->getFixture('app.php'),
],
);

$this->mockFilePutContent(
Expand Down Expand Up @@ -248,6 +276,10 @@ public function testRunWithAdminAndDefaultReadmeCreation()
'README.md',
$this->getFixture('default_readme_after_using_renovate.md'),
],
[
base_path('bootstrap/app.php'),
$this->getFixture('app_after_changes.php'),
],
);

$this->mockShellExec(
Expand Down Expand Up @@ -369,6 +401,10 @@ public function testRunWithAdminAndPartialReadmeCreation()
'arguments' => [base_path('/vendor/ronasit/laravel-project-initializator/resources/md/readme/CREDENTIALS_AND_ACCESS.md')],
'result' => $this->getTemplate('CREDENTIALS_AND_ACCESS.md'),
],
[
'arguments' => [base_path('bootstrap/app.php')],
'result' => $this->getFixture('app.php'),
],
);

$this->mockFilePutContent(
Expand All @@ -377,7 +413,11 @@ public function testRunWithAdminAndPartialReadmeCreation()
[
'README.md',
$this->getFixture('partial_readme.md'),
]
],
[
base_path('bootstrap/app.php'),
$this->getFixture('app_after_changes.php'),
],
);

$this->mockShellExec(
Expand Down Expand Up @@ -498,6 +538,10 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns
'arguments' => [base_path('/vendor/ronasit/laravel-project-initializator/resources/md/readme/RENOVATE.md')],
'result' => $this->getTemplate('RENOVATE.md'),
],
[
'arguments' => [base_path('bootstrap/app.php')],
'result' => $this->getFixture('app.php'),
],
);

$this->mockFilePutContent(
Expand All @@ -519,6 +563,10 @@ public function testRunWithAdminAndFullReadmeCreationAndRemovingInitializatorIns
'README.md',
$this->getFixture('full_readme_after_using_renovate.md'),
],
[
base_path('bootstrap/app.php'),
$this->getFixture('app_after_changes.php'),
],
);

$this->mockShellExec(
Expand Down Expand Up @@ -637,6 +685,10 @@ public function testRunWithoutAdminAndUsingTelescope()
'arguments' => [base_path('/vendor/ronasit/laravel-project-initializator/resources/md/readme/CREDENTIALS_AND_ACCESS.md')],
'result' => $this->getTemplate('CREDENTIALS_AND_ACCESS.md'),
],
[
'arguments' => [base_path('bootstrap/app.php')],
'result' => $this->getFixture('app.php'),
],
);

$this->mockFilePutContent(
Expand All @@ -645,7 +697,11 @@ public function testRunWithoutAdminAndUsingTelescope()
[
'README.md',
$this->getFixture('partial_readme_with_telescope.md'),
]
],
[
base_path('bootstrap/app.php'),
$this->getFixture('app_after_changes.php'),
],
);

$this->mockShellExec(
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/InitCommandTest/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
//
})
->withExceptions(function (Exceptions $exceptions): void {
//
})->create();
26 changes: 26 additions & 0 deletions tests/fixtures/InitCommandTest/app_after_changes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware): void {
//
})
->withExceptions(function (Exceptions $exceptions): void {
$exceptions->render(function (HttpException $exception, Request $request) {
return ($request->expectsJson())
? response()->json(['error' => $exception->getMessage()], $exception->getStatusCode())
: null;
});

//
})->create();