Skip to content

Commit

Permalink
[3.x] Pest detection (#1353)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Aug 8, 2023
1 parent 5ed8e82 commit d356a6c
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
}

0 comments on commit d356a6c

Please sign in to comment.