From ceb83aa9aacf43df820e39a82a743e5bd9421ec4 Mon Sep 17 00:00:00 2001 From: Nuno Maduro <enunomaduro@gmail.com> Date: Mon, 22 Jan 2024 16:43:02 +0000 Subject: [PATCH 1/2] Implies only the new migrations behaviour on L11 --- src/NewCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 9427879..a647f33 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -465,7 +465,7 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i default: $defaultDatabase ); - if ($database !== $defaultDatabase) { + if ($database !== $defaultDatabase && $this->hasMariaDBConfig($directory)) { $migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true); } } From 5bbb002215368d31fd31283500064770211e6e74 Mon Sep 17 00:00:00 2001 From: Nuno Maduro <enunomaduro@gmail.com> Date: Tue, 23 Jan 2024 11:30:27 +0000 Subject: [PATCH 2/2] Improves method `usingLaravel11OrNewer` --- src/NewCommand.php | 22 ++++---- tests/NewCommandTest.php | 13 +++++ tests/fixtures/laravel10/composer.json | 66 ++++++++++++++++++++++++ tests/fixtures/laravel11/composer.json | 69 ++++++++++++++++++++++++++ tests/fixtures/laravel12/composer.json | 69 ++++++++++++++++++++++++++ 5 files changed, 226 insertions(+), 13 deletions(-) create mode 100644 tests/fixtures/laravel10/composer.json create mode 100644 tests/fixtures/laravel11/composer.json create mode 100644 tests/fixtures/laravel12/composer.json diff --git a/src/NewCommand.php b/src/NewCommand.php index a647f33..8370000 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -243,7 +243,7 @@ protected function defaultBranch() protected function configureDefaultDatabaseConnection(string $directory, string $database, string $name, bool $migrate) { // MariaDB configuration only exists as of Laravel 11... - if ($database === 'mariadb' && ! $this->hasMariaDBConfig($directory)) { + if ($database === 'mariadb' && ! $this->usingLaravel11OrNewer($directory)) { $database = 'mysql'; } @@ -308,22 +308,18 @@ protected function configureDefaultDatabaseConnection(string $directory, string } /** - * Determine if the application has a MariaDB configuration entry. + * Determine if the application is using Laravel 11 or newer. * * @param string $directory * @return bool */ - protected function hasMariaDBConfig(string $directory): bool + public function usingLaravel11OrNewer(string $directory): bool { - // Laravel 11+ has moved the configuration files into the framework... - if (! file_exists($directory.'/config/database.php')) { - return true; - } + $version = json_decode(file_get_contents($directory.'/composer.json'), true)['require']['laravel/framework']; + $version = str_replace('^', '', $version); + $version = explode('.', $version)[0]; - return str_contains( - file_get_contents($directory.'/config/database.php'), - "'mariadb' =>" - ); + return $version >= 11; } /** @@ -450,7 +446,7 @@ protected function installJetstream(string $directory, InputInterface $input, Ou protected function promptForDatabaseOptions(string $directory, InputInterface $input) { // Laravel 11.x appliations use SQLite as default... - $defaultDatabase = $this->hasMariaDBConfig($directory) ? 'sqlite' : 'mysql'; + $defaultDatabase = $this->usingLaravel11OrNewer($directory) ? 'sqlite' : 'mysql'; if ($input->isInteractive()) { $database = select( @@ -465,7 +461,7 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i default: $defaultDatabase ); - if ($database !== $defaultDatabase && $this->hasMariaDBConfig($directory)) { + if ($this->usingLaravel11OrNewer($directory) && $database !== $defaultDatabase) { $migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true); } } diff --git a/tests/NewCommandTest.php b/tests/NewCommandTest.php index 4ed37ae..43ddf07 100644 --- a/tests/NewCommandTest.php +++ b/tests/NewCommandTest.php @@ -33,4 +33,17 @@ public function test_it_can_scaffold_a_new_laravel_app() $this->assertDirectoryExists($scaffoldDirectory.'/vendor'); $this->assertFileExists($scaffoldDirectory.'/.env'); } + + public function test_on_at_least_laravel_11() + { + $command = new NewCommand; + + $onLaravel10 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel10'); + $onLaravel11 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel11'); + $onLaravel12 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel12'); + + $this->assertFalse($onLaravel10); + $this->assertTrue($onLaravel11); + $this->assertTrue($onLaravel12); + } } diff --git a/tests/fixtures/laravel10/composer.json b/tests/fixtures/laravel10/composer.json new file mode 100644 index 0000000..8a3d72d --- /dev/null +++ b/tests/fixtures/laravel10/composer.json @@ -0,0 +1,66 @@ +{ + "name": "laravel/laravel", + "type": "project", + "description": "The skeleton application for the Laravel framework.", + "keywords": ["laravel", "framework"], + "license": "MIT", + "require": { + "php": "^8.1", + "guzzlehttp/guzzle": "^7.2", + "laravel/framework": "^10.10", + "laravel/sanctum": "^3.3", + "laravel/tinker": "^2.8" + }, + "require-dev": { + "fakerphp/faker": "^1.9.1", + "laravel/pint": "^1.0", + "laravel/sail": "^1.18", + "mockery/mockery": "^1.4.4", + "nunomaduro/collision": "^7.0", + "phpunit/phpunit": "^10.1", + "spatie/laravel-ignition": "^2.0" + }, + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Factories\\": "database/factories/", + "Database\\Seeders\\": "database/seeders/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "scripts": { + "post-autoload-dump": [ + "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", + "@php artisan package:discover --ansi" + ], + "post-update-cmd": [ + "@php artisan vendor:publish --tag=laravel-assets --ansi --force" + ], + "post-root-package-install": [ + "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" + ], + "post-create-project-cmd": [ + "@php artisan key:generate --ansi" + ] + }, + "extra": { + "laravel": { + "dont-discover": [] + } + }, + "config": { + "optimize-autoloader": true, + "preferred-install": "dist", + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "php-http/discovery": true + } + }, + "minimum-stability": "stable", + "prefer-stable": true +} diff --git a/tests/fixtures/laravel11/composer.json b/tests/fixtures/laravel11/composer.json new file mode 100644 index 0000000..3cdf16e --- /dev/null +++ b/tests/fixtures/laravel11/composer.json @@ -0,0 +1,69 @@ +{ + "name": "laravel/laravel", + "type": "project", + "description": "The skeleton application for the Laravel framework.", + "keywords": ["laravel", "framework"], + "license": "MIT", + "require": { + "php": "^8.2", + "laravel/framework": "^11.0", + "laravel/tinker": "^2.9" + }, + "require-dev": { + "fakerphp/faker": "^1.23", + "laravel/pint": "^1.13", + "laravel/sail": "^1.26", + "mockery/mockery": "^1.6", + "nunomaduro/collision": "^8.0", + "phpunit/phpunit": "^10.5", + "spatie/laravel-ignition": "^2.4" + }, + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Factories\\": "database/factories/", + "Database\\Seeders\\": "database/seeders/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "scripts": { + "post-autoload-dump": [ + "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", + "@php artisan package:discover --ansi" + ], + "post-update-cmd": [ + "@php artisan vendor:publish --tag=laravel-assets --ansi --force" + ], + "post-root-package-install": [ + "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" + ], + "post-create-project-cmd": [ + "@php artisan key:generate --ansi", + "@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"", + "@php artisan migrate --ansi" + ] + }, + "extra": { + "branch-alias": { + "dev-master": "11.x-dev" + }, + "laravel": { + "dont-discover": [] + } + }, + "config": { + "optimize-autoloader": true, + "preferred-install": "dist", + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "php-http/discovery": true + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/tests/fixtures/laravel12/composer.json b/tests/fixtures/laravel12/composer.json new file mode 100644 index 0000000..7181145 --- /dev/null +++ b/tests/fixtures/laravel12/composer.json @@ -0,0 +1,69 @@ +{ + "name": "laravel/laravel", + "type": "project", + "description": "The skeleton application for the Laravel framework.", + "keywords": ["laravel", "framework"], + "license": "MIT", + "require": { + "php": "^8.2", + "laravel/framework": "^12.0.1", + "laravel/tinker": "^2.9" + }, + "require-dev": { + "fakerphp/faker": "^1.23", + "laravel/pint": "^1.13", + "laravel/sail": "^1.26", + "mockery/mockery": "^1.6", + "nunomaduro/collision": "^8.0", + "phpunit/phpunit": "^10.5", + "spatie/laravel-ignition": "^2.4" + }, + "autoload": { + "psr-4": { + "App\\": "app/", + "Database\\Factories\\": "database/factories/", + "Database\\Seeders\\": "database/seeders/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "scripts": { + "post-autoload-dump": [ + "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump", + "@php artisan package:discover --ansi" + ], + "post-update-cmd": [ + "@php artisan vendor:publish --tag=laravel-assets --ansi --force" + ], + "post-root-package-install": [ + "@php -r \"file_exists('.env') || copy('.env.example', '.env');\"" + ], + "post-create-project-cmd": [ + "@php artisan key:generate --ansi", + "@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"", + "@php artisan migrate --ansi" + ] + }, + "extra": { + "branch-alias": { + "dev-master": "11.x-dev" + }, + "laravel": { + "dont-discover": [] + } + }, + "config": { + "optimize-autoloader": true, + "preferred-install": "dist", + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "php-http/discovery": true + } + }, + "minimum-stability": "dev", + "prefer-stable": true +}