Skip to content

Commit f82fdf6

Browse files
tnyleanunomaduro
andauthored
Improves pest experience (#413)
Co-authored-by: Nuno Maduro <[email protected]>
1 parent 665f6f4 commit f82fdf6

File tree

3 files changed

+35
-41
lines changed

3 files changed

+35
-41
lines changed

src/NewCommand.php

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Illuminate\Support\Composer;
77
use Illuminate\Support\ProcessUtils;
88
use Illuminate\Support\Str;
9+
use RecursiveDirectoryIterator;
10+
use RecursiveIteratorIterator;
911
use RuntimeException;
1012
use Symfony\Component\Console\Command\Command;
1113
use Symfony\Component\Console\Input\InputArgument;
@@ -149,17 +151,12 @@ protected function interact(InputInterface $input, OutputInterface $output)
149151
}
150152
}
151153

152-
if ($this->usingLaravelStarterKit($input)) {
153-
if (! $input->getOption('phpunit') &&
154-
! $input->getOption('pest')) {
155-
$input->setOption('pest', select(
156-
label: 'Which testing framework do you prefer?',
157-
options: ['Pest', 'PHPUnit'],
158-
default: 'Pest',
159-
) === 'Pest');
160-
}
161-
} else {
162-
$input->setOption('phpunit', true);
154+
if (! $input->getOption('phpunit') && ! $input->getOption('pest')) {
155+
$input->setOption('pest', select(
156+
label: 'Which testing framework do you prefer?',
157+
options: ['Pest', 'PHPUnit'],
158+
default: 'Pest',
159+
) === 'Pest');
163160
}
164161
}
165162

@@ -587,36 +584,45 @@ protected function installPest(string $directory, InputInterface $input, OutputI
587584
$this->phpBinary().' ./vendor/bin/pest --init',
588585
];
589586

590-
if ($this->usingStarterKit($input)) {
591-
$commands[] = $composerBinary.' require pestphp/pest-plugin-drift --dev';
592-
$commands[] = $this->phpBinary().' ./vendor/bin/pest --drift';
593-
$commands[] = $composerBinary.' remove pestphp/pest-plugin-drift --dev';
594-
}
587+
$commands[] = $composerBinary.' require pestphp/pest-plugin-drift --dev';
588+
$commands[] = $this->phpBinary().' ./vendor/bin/pest --drift';
589+
$commands[] = $composerBinary.' remove pestphp/pest-plugin-drift --dev';
595590

596591
$this->runCommands($commands, $input, $output, workingPath: $directory, env: [
597592
'PEST_NO_SUPPORT' => 'true',
598593
]);
599594

600-
$this->replaceFile(
601-
'pest/Feature.php',
602-
$directory.'/tests/Feature/ExampleTest.php',
603-
);
604-
605-
$this->replaceFile(
606-
'pest/Unit.php',
607-
$directory.'/tests/Unit/ExampleTest.php',
608-
);
609-
610595
if ($this->usingStarterKit($input)) {
611596
$this->replaceInFile(
612597
'./vendor/bin/phpunit',
613598
'./vendor/bin/pest',
614599
$directory.'/.github/workflows/tests.yml',
615600
);
616-
}
617601

618-
if ($this->usingStarterKit($input) && $input->getOption('phpunit')) {
619-
$this->deleteFile($directory.'/tests/Pest.php');
602+
$contents = file_get_contents("$directory/tests/Pest.php");
603+
604+
$contents = str_replace(
605+
" // ->use(Illuminate\Foundation\Testing\RefreshDatabase::class)",
606+
" ->use(Illuminate\Foundation\Testing\RefreshDatabase::class)",
607+
$contents,
608+
);
609+
610+
file_put_contents("$directory/tests/Pest.php", $contents);
611+
612+
$directoryIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
613+
614+
foreach ($directoryIterator as $testFile) {
615+
if ($testFile->isDir()) {
616+
continue;
617+
}
618+
619+
$contents = file_get_contents($testFile);
620+
621+
file_put_contents(
622+
$testFile,
623+
str_replace("\n\nuses(\Illuminate\Foundation\Testing\RefreshDatabase::class);", '', $contents),
624+
);
625+
}
620626
}
621627

622628
$this->commitChanges('Install Pest', $directory, $input, $output);

stubs/pest/Feature.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

stubs/pest/Unit.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)