Skip to content

Commit d356a6c

Browse files
jessarchernunomadurotaylorotwell
authored
[3.x] Pest detection (#1353)
* Default to Pest if already installed * Prevent error message and slow operation when PHPUnit has already been removed * Improves `isUsingPest` * Update InstallCommand.php --------- Co-authored-by: Nuno Maduro <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 5ed8e82 commit d356a6c

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/Console/InstallCommand.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,10 @@ public function handle()
106106
// Tests...
107107
$stubs = $this->getTestStubsPath();
108108

109-
if ($this->option('pest')) {
110-
$this->removeComposerDevPackages(['phpunit/phpunit']);
109+
if ($this->option('pest') || $this->isUsingPest()) {
110+
if ($this->hasComposerPackage('phpunit/phpunit')) {
111+
$this->removeComposerDevPackages(['phpunit/phpunit']);
112+
}
111113

112114
if (! $this->requireComposerDevPackages(['pestphp/pest:^2.0', 'pestphp/pest-plugin-laravel:^2.0'])) {
113115
return 1;
@@ -649,11 +651,25 @@ protected function installMiddlewareAfter($after, $name, $group = 'web')
649651
*/
650652
protected function getTestStubsPath()
651653
{
652-
return $this->option('pest')
654+
return $this->option('pest') || $this->isUsingPest()
653655
? __DIR__.'/../../stubs/pest-tests'
654656
: __DIR__.'/../../stubs/tests';
655657
}
656658

659+
/**
660+
* Determine if the given Composer package is installed.
661+
*
662+
* @param string $package
663+
* @return bool
664+
*/
665+
protected function hasComposerPackage($package)
666+
{
667+
$packages = json_decode(file_get_contents(base_path('composer.json')), true);
668+
669+
return array_key_exists($package, $packages['require'] ?? [])
670+
|| array_key_exists($package, $packages['require-dev'] ?? []);
671+
}
672+
657673
/**
658674
* Installs the given Composer Packages into the application.
659675
*
@@ -880,6 +896,17 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp
880896
$input->setOption('pest', select(
881897
label: 'Which testing framework do you prefer?',
882898
options: ['PHPUnit', 'Pest'],
899+
default: $this->isUsingPest() ? 'Pest' : 'PHPUnit',
883900
) === 'Pest');
884901
}
902+
903+
/**
904+
* Determine whether the project is already using Pest.
905+
*
906+
* @return bool
907+
*/
908+
protected function isUsingPest()
909+
{
910+
return class_exists(\Pest\TestSuite::class);
911+
}
885912
}

0 commit comments

Comments
 (0)