Skip to content

Commit dab1639

Browse files
[4.x] Adds --pest to use Pest (#251)
* Adds `--pest` to use Pest * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent b5e2f2d commit dab1639

File tree

3 files changed

+100
-3
lines changed

3 files changed

+100
-3
lines changed

src/NewCommand.php

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ protected function configure()
3333
->addOption('jet', null, InputOption::VALUE_NONE, 'Installs the Laravel Jetstream scaffolding')
3434
->addOption('stack', null, InputOption::VALUE_OPTIONAL, 'The Jetstream stack that should be installed')
3535
->addOption('teams', null, InputOption::VALUE_NONE, 'Indicates whether Jetstream should be scaffolded with team support')
36+
->addOption('pest', null, InputOption::VALUE_NONE, 'Installs the Pest testing framework')
3637
->addOption('prompt-jetstream', null, InputOption::VALUE_NONE, 'Issues a prompt to determine if Jetstream should be installed')
3738
->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces install even if the directory already exists');
3839
}
@@ -57,6 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5758
`---'`---'`---'`---'`---'` `---'`---^` ' '</>".PHP_EOL.PHP_EOL);
5859

5960
$stack = $this->jetstreamStack($input, $output);
61+
$testingFramework = $this->jetstreamTestingFramework($input, $output);
6062

6163
$teams = $input->getOption('teams') === true
6264
? (bool) $input->getOption('teams')
@@ -130,7 +132,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
130132
}
131133

132134
if ($installJetstream) {
133-
$this->installJetstream($directory, $stack, $teams, $input, $output);
135+
$this->installJetstream($directory, $stack, $testingFramework, $teams, $input, $output);
136+
} elseif ($input->getOption('pest')) {
137+
$this->installPest($directory, $input, $output);
134138
}
135139

136140
if ($input->getOption('github') !== false) {
@@ -165,18 +169,24 @@ protected function defaultBranch()
165169
*
166170
* @param string $directory
167171
* @param string $stack
172+
* @param string $testingFramework
168173
* @param bool $teams
169174
* @param \Symfony\Component\Console\Input\InputInterface $input
170175
* @param \Symfony\Component\Console\Output\OutputInterface $output
171176
* @return void
172177
*/
173-
protected function installJetstream(string $directory, string $stack, bool $teams, InputInterface $input, OutputInterface $output)
178+
protected function installJetstream(string $directory, string $stack, string $testingFramework, bool $teams, InputInterface $input, OutputInterface $output)
174179
{
175180
chdir($directory);
176181

177182
$commands = array_filter([
178183
$this->findComposer().' require laravel/jetstream',
179-
trim(sprintf(PHP_BINARY.' artisan jetstream:install %s %s', $stack, $teams ? '--teams' : '')),
184+
trim(sprintf(
185+
PHP_BINARY.' artisan jetstream:install %s %s %s',
186+
$stack,
187+
$teams ? '--teams' : '',
188+
$testingFramework == 'pest' ? '--pest' : '',
189+
)),
180190
]);
181191

182192
$this->runCommands($commands, $input, $output);
@@ -211,6 +221,64 @@ protected function jetstreamStack(InputInterface $input, OutputInterface $output
211221
return $helper->ask($input, new SymfonyStyle($input, $output), $question);
212222
}
213223

224+
/**
225+
* Determine the testing framework for Jetstream.
226+
*
227+
* @param \Symfony\Component\Console\Input\InputInterface $input
228+
* @param \Symfony\Component\Console\Output\OutputInterface $output
229+
* @return string
230+
*/
231+
protected function jetstreamTestingFramework(InputInterface $input, OutputInterface $output)
232+
{
233+
if ($input->getOption('pest')) {
234+
return 'pest';
235+
}
236+
237+
$testingFrameworks = [
238+
'pest',
239+
'phpunit',
240+
];
241+
242+
$helper = $this->getHelper('question');
243+
244+
$question = new ChoiceQuestion('Which testing framework do you prefer?', $testingFrameworks);
245+
246+
$output->write(PHP_EOL);
247+
248+
return $helper->ask($input, new SymfonyStyle($input, $output), $question);
249+
}
250+
251+
/**
252+
* Install Pest into the application.
253+
*
254+
* @param \Symfony\Component\Console\Input\InputInterface $input
255+
* @param \Symfony\Component\Console\Output\OutputInterface $output
256+
* @return void
257+
*/
258+
protected function installPest(string $directory, InputInterface $input, OutputInterface $output)
259+
{
260+
chdir($directory);
261+
262+
$commands = array_filter([
263+
$this->findComposer().' require pestphp/pest pestphp/pest-plugin-laravel --dev',
264+
PHP_BINARY.' artisan pest:install --no-interaction',
265+
]);
266+
267+
$this->runCommands($commands, $input, $output);
268+
269+
$this->replaceFile(
270+
'pest/Feature.php',
271+
$directory.'/tests/Feature/ExampleTest.php',
272+
);
273+
274+
$this->replaceFile(
275+
'pest/Unit.php',
276+
$directory.'/tests/Unit/ExampleTest.php',
277+
);
278+
279+
$this->commitChanges('Install Pest', $directory, $input, $output);
280+
}
281+
214282
/**
215283
* Create a Git repository and commit the base Laravel skeleton.
216284
*
@@ -408,6 +476,23 @@ protected function runCommands($commands, InputInterface $input, OutputInterface
408476
return $process;
409477
}
410478

479+
/**
480+
* Replace the given file.
481+
*
482+
* @param string $replace
483+
* @param string $file
484+
* @return void
485+
*/
486+
protected function replaceFile(string $replace, string $file)
487+
{
488+
$stubs = dirname(__DIR__).'/stubs';
489+
490+
file_put_contents(
491+
$file,
492+
file_get_contents("$stubs/$replace"),
493+
);
494+
}
495+
411496
/**
412497
* Replace the given string in the given file.
413498
*

stubs/pest/Feature.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
it('returns a successful response', function () {
4+
$response = $this->get('/');
5+
6+
$response->assertStatus(200);
7+
});

stubs/pest/Unit.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
test('that true is true', function () {
4+
expect(true)->toBeTrue(true);
5+
});

0 commit comments

Comments
 (0)