Skip to content

Commit

Permalink
[4.x] Adds --pest to use Pest (#251)
Browse files Browse the repository at this point in the history
* Adds `--pest` to use Pest

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
nunomaduro and StyleCIBot authored Feb 3, 2023
1 parent b5e2f2d commit dab1639
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
91 changes: 88 additions & 3 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function configure()
->addOption('jet', null, InputOption::VALUE_NONE, 'Installs the Laravel Jetstream scaffolding')
->addOption('stack', null, InputOption::VALUE_OPTIONAL, 'The Jetstream stack that should be installed')
->addOption('teams', null, InputOption::VALUE_NONE, 'Indicates whether Jetstream should be scaffolded with team support')
->addOption('pest', null, InputOption::VALUE_NONE, 'Installs the Pest testing framework')
->addOption('prompt-jetstream', null, InputOption::VALUE_NONE, 'Issues a prompt to determine if Jetstream should be installed')
->addOption('force', 'f', InputOption::VALUE_NONE, 'Forces install even if the directory already exists');
}
Expand All @@ -57,6 +58,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
`---'`---'`---'`---'`---'` `---'`---^` ' '</>".PHP_EOL.PHP_EOL);

$stack = $this->jetstreamStack($input, $output);
$testingFramework = $this->jetstreamTestingFramework($input, $output);

$teams = $input->getOption('teams') === true
? (bool) $input->getOption('teams')
Expand Down Expand Up @@ -130,7 +132,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if ($installJetstream) {
$this->installJetstream($directory, $stack, $teams, $input, $output);
$this->installJetstream($directory, $stack, $testingFramework, $teams, $input, $output);
} elseif ($input->getOption('pest')) {
$this->installPest($directory, $input, $output);
}

if ($input->getOption('github') !== false) {
Expand Down Expand Up @@ -165,18 +169,24 @@ protected function defaultBranch()
*
* @param string $directory
* @param string $stack
* @param string $testingFramework
* @param bool $teams
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function installJetstream(string $directory, string $stack, bool $teams, InputInterface $input, OutputInterface $output)
protected function installJetstream(string $directory, string $stack, string $testingFramework, bool $teams, InputInterface $input, OutputInterface $output)
{
chdir($directory);

$commands = array_filter([
$this->findComposer().' require laravel/jetstream',
trim(sprintf(PHP_BINARY.' artisan jetstream:install %s %s', $stack, $teams ? '--teams' : '')),
trim(sprintf(
PHP_BINARY.' artisan jetstream:install %s %s %s',
$stack,
$teams ? '--teams' : '',
$testingFramework == 'pest' ? '--pest' : '',
)),
]);

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

/**
* Determine the testing framework for Jetstream.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return string
*/
protected function jetstreamTestingFramework(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('pest')) {
return 'pest';
}

$testingFrameworks = [
'pest',
'phpunit',
];

$helper = $this->getHelper('question');

$question = new ChoiceQuestion('Which testing framework do you prefer?', $testingFrameworks);

$output->write(PHP_EOL);

return $helper->ask($input, new SymfonyStyle($input, $output), $question);
}

/**
* Install Pest into the application.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return void
*/
protected function installPest(string $directory, InputInterface $input, OutputInterface $output)
{
chdir($directory);

$commands = array_filter([
$this->findComposer().' require pestphp/pest pestphp/pest-plugin-laravel --dev',
PHP_BINARY.' artisan pest:install --no-interaction',
]);

$this->runCommands($commands, $input, $output);

$this->replaceFile(
'pest/Feature.php',
$directory.'/tests/Feature/ExampleTest.php',
);

$this->replaceFile(
'pest/Unit.php',
$directory.'/tests/Unit/ExampleTest.php',
);

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

/**
* Create a Git repository and commit the base Laravel skeleton.
*
Expand Down Expand Up @@ -408,6 +476,23 @@ protected function runCommands($commands, InputInterface $input, OutputInterface
return $process;
}

/**
* Replace the given file.
*
* @param string $replace
* @param string $file
* @return void
*/
protected function replaceFile(string $replace, string $file)
{
$stubs = dirname(__DIR__).'/stubs';

file_put_contents(
$file,
file_get_contents("$stubs/$replace"),
);
}

/**
* Replace the given string in the given file.
*
Expand Down
7 changes: 7 additions & 0 deletions stubs/pest/Feature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

it('returns a successful response', function () {
$response = $this->get('/');

$response->assertStatus(200);
});
5 changes: 5 additions & 0 deletions stubs/pest/Unit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

test('that true is true', function () {
expect(true)->toBeTrue(true);
});

0 comments on commit dab1639

Please sign in to comment.