diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index e7f711bdd..70ac82ee6 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -106,8 +106,10 @@ public function handle() // Tests... $stubs = $this->getTestStubsPath(); - if ($this->option('pest')) { - $this->removeComposerDevPackages(['phpunit/phpunit']); + if ($this->option('pest') || $this->isUsingPest()) { + if ($this->hasComposerPackage('phpunit/phpunit')) { + $this->removeComposerDevPackages(['phpunit/phpunit']); + } if (! $this->requireComposerDevPackages(['pestphp/pest:^2.0', 'pestphp/pest-plugin-laravel:^2.0'])) { return 1; @@ -649,11 +651,25 @@ protected function installMiddlewareAfter($after, $name, $group = 'web') */ protected function getTestStubsPath() { - return $this->option('pest') + return $this->option('pest') || $this->isUsingPest() ? __DIR__.'/../../stubs/pest-tests' : __DIR__.'/../../stubs/tests'; } + /** + * Determine if the given Composer package is installed. + * + * @param string $package + * @return bool + */ + protected function hasComposerPackage($package) + { + $packages = json_decode(file_get_contents(base_path('composer.json')), true); + + return array_key_exists($package, $packages['require'] ?? []) + || array_key_exists($package, $packages['require-dev'] ?? []); + } + /** * Installs the given Composer Packages into the application. * @@ -880,6 +896,17 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp $input->setOption('pest', select( label: 'Which testing framework do you prefer?', options: ['PHPUnit', 'Pest'], + default: $this->isUsingPest() ? 'Pest' : 'PHPUnit', ) === 'Pest'); } + + /** + * Determine whether the project is already using Pest. + * + * @return bool + */ + protected function isUsingPest() + { + return class_exists(\Pest\TestSuite::class); + } }