From 64dbcf0a261000d71b75288f7df875a42def2a01 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 20 Feb 2023 23:10:59 +0100 Subject: [PATCH 1/4] init command tweak --- src/Plugins/Init.php | 82 ++++++++++++++++++++++-- stubs/init/{ => Feature}/ExampleTest.php | 0 stubs/init/TestCase.php | 7 ++ stubs/init/Unit/ExampleTest.php | 5 ++ 4 files changed, 89 insertions(+), 5 deletions(-) rename stubs/init/{ => Feature}/ExampleTest.php (100%) create mode 100644 stubs/init/TestCase.php create mode 100644 stubs/init/Unit/ExampleTest.php diff --git a/src/Plugins/Init.php b/src/Plugins/Init.php index 641012e6..9c1c45c5 100644 --- a/src/Plugins/Init.php +++ b/src/Plugins/Init.php @@ -4,6 +4,9 @@ declare(strict_types=1); namespace Pest\Plugins; +use App\Console\Kernel; +use Composer\InstalledVersions; +use Illuminate\Support\Facades\Process; use Pest\Console\Thanks; use Pest\Contracts\Plugins\HandlesArguments; use Pest\Support\View; @@ -26,7 +29,9 @@ final class Init implements HandlesArguments private const STUBS = [ 'phpunit.xml' => 'phpunit.xml', 'Pest.php' => 'tests/Pest.php', - 'ExampleTest.php' => 'tests/ExampleTest.php', + 'TestCase.php' => 'tests/TestCase.php', + 'Unit/ExampleTest.php' => 'tests/Unit/ExampleTest.php', + 'Feature/ExampleTest.php' => 'tests/Feature/ExampleTest.php', ]; /** @@ -59,16 +64,19 @@ final class Init implements HandlesArguments private function init(): void { + if ($this->isLaravelInstalled()) { + exit($this->initLaravel()); + } + $testsBaseDir = "{$this->testSuite->rootPath}/tests"; if (! is_dir($testsBaseDir)) { mkdir($testsBaseDir); } - $this->output->writeln([ - '', - ' INFO Preparing tests directory.', - '', + View::render('components.badge', [ + 'type' => 'INFO', + 'content' => 'Preparing tests directory.', ]); foreach (self::STUBS as $from => $to) { @@ -98,4 +106,68 @@ final class Init implements HandlesArguments exit(0); } + + private function initLaravel(): int + { + View::render('components.badge', [ + 'type' => 'INFO', + 'content' => 'Laravel installation detected, pest-plugin-laravel will be installed.', + ]); + + exec('composer require pestphp/pest-plugin-laravel 2.x-dev', result_code: $result); + + /** @var int $result */ + if ($result > 0) { + View::render('components.badge', [ + 'type' => 'ERROR', + 'content' => 'Something went wrong while installing pest-plugin-laravel package. Please refer the above output for more info.', + ]); + + return $result; + } + + View::render('components.badge', [ + 'type' => 'INFO', + 'content' => 'Running artisan command to install Pest.', + ]); + + $app = require $this->testSuite->rootPath.'/bootstrap/app.php'; + /** @phpstan-ignore-next-line */ + $app->make(Kernel::class)->bootstrap(); + + /** @phpstan-ignore-next-line */ + $result = Process::run('php artisan pest:install --no-interaction'); + + if ($result->failed()) { + $this->output->writeln($result->errorOutput()); + + View::render('components.badge', [ + 'type' => 'ERROR', + 'content' => 'Something went wrong while installing Pest in laravel. Please refer the above output for more info.', + ]); + + return $result->exitCode(); + } + + $this->output->writeln($result->output()); + + View::render('components.two-column-detail', [ + 'left' => 'pest-plugin-laravel', + 'right' => 'Installed', + ]); + + View::render('components.two-column-detail', [ + 'left' => 'Pest', + 'right' => 'Installed in Laravel', + ]); + + View::render('components.new-line'); + + return 0; + } + + private function isLaravelInstalled(): bool + { + return InstalledVersions::isInstalled('laravel/laravel'); + } } diff --git a/stubs/init/ExampleTest.php b/stubs/init/Feature/ExampleTest.php similarity index 100% rename from stubs/init/ExampleTest.php rename to stubs/init/Feature/ExampleTest.php diff --git a/stubs/init/TestCase.php b/stubs/init/TestCase.php new file mode 100644 index 00000000..62e5bd9b --- /dev/null +++ b/stubs/init/TestCase.php @@ -0,0 +1,7 @@ +toBeTrue(); +}); From 69b1c08558ccbc9ec0503300d6feab8f554f4ee2 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 20 Feb 2023 23:44:15 +0100 Subject: [PATCH 2/4] refactor --- src/Plugins/Init.php | 74 ++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 50 deletions(-) diff --git a/src/Plugins/Init.php b/src/Plugins/Init.php index 9c1c45c5..d63e88e8 100644 --- a/src/Plugins/Init.php +++ b/src/Plugins/Init.php @@ -4,14 +4,13 @@ declare(strict_types=1); namespace Pest\Plugins; -use App\Console\Kernel; use Composer\InstalledVersions; -use Illuminate\Support\Facades\Process; use Pest\Console\Thanks; use Pest\Contracts\Plugins\HandlesArguments; use Pest\Support\View; use Pest\TestSuite; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Process\Process; /** * @internal @@ -109,63 +108,38 @@ final class Init implements HandlesArguments private function initLaravel(): int { - View::render('components.badge', [ - 'type' => 'INFO', - 'content' => 'Laravel installation detected, pest-plugin-laravel will be installed.', - ]); + $command = [ + 'composer', 'require', + 'pestphp/pest-plugin-laravel 2.x-dev', + '--dev', + ]; - exec('composer require pestphp/pest-plugin-laravel 2.x-dev', result_code: $result); + $result = (new Process($command, $this->testSuite->rootPath, ['COMPOSER_MEMORY_LIMIT' => '-1'])) + ->setTimeout(null) + ->run(function ($type, $output): void { + $this->output->write($output); + }); - /** @var int $result */ if ($result > 0) { - View::render('components.badge', [ - 'type' => 'ERROR', - 'content' => 'Something went wrong while installing pest-plugin-laravel package. Please refer the above output for more info.', - ]); - return $result; } - View::render('components.badge', [ - 'type' => 'INFO', - 'content' => 'Running artisan command to install Pest.', - ]); + $command = [ + 'php', 'artisan', + 'pest:install', + '--no-interaction', + ]; - $app = require $this->testSuite->rootPath.'/bootstrap/app.php'; - /** @phpstan-ignore-next-line */ - $app->make(Kernel::class)->bootstrap(); - - /** @phpstan-ignore-next-line */ - $result = Process::run('php artisan pest:install --no-interaction'); - - if ($result->failed()) { - $this->output->writeln($result->errorOutput()); - - View::render('components.badge', [ - 'type' => 'ERROR', - 'content' => 'Something went wrong while installing Pest in laravel. Please refer the above output for more info.', - ]); - - return $result->exitCode(); - } - - $this->output->writeln($result->output()); - - View::render('components.two-column-detail', [ - 'left' => 'pest-plugin-laravel', - 'right' => 'Installed', - ]); - - View::render('components.two-column-detail', [ - 'left' => 'Pest', - 'right' => 'Installed in Laravel', - ]); - - View::render('components.new-line'); - - return 0; + return (new Process($command, $this->testSuite->rootPath, ['COMPOSER_MEMORY_LIMIT' => '-1'])) + ->setTimeout(null) + ->run(function ($type, $output): void { + $this->output->write($output); + }); } + /** + * Checks if laravel is installed through Composer + */ private function isLaravelInstalled(): bool { return InstalledVersions::isInstalled('laravel/laravel'); From 46b785f29fb581aa92ed732938a751ea3ee0d45a Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 20 Feb 2023 22:54:24 +0000 Subject: [PATCH 3/4] Update TestCase.php --- stubs/init/TestCase.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/stubs/init/TestCase.php b/stubs/init/TestCase.php index 62e5bd9b..cfb05b6d 100644 --- a/stubs/init/TestCase.php +++ b/stubs/init/TestCase.php @@ -2,6 +2,9 @@ namespace Tests; -class TestCase extends \PHPUnit\Framework\TestCase +use PHPUnit\Framework\TestCase as BaseTestCase; + +abstract class TestCase extends BaseTestCase { + // } From a6e133a19421db05342416f673db669102915cc3 Mon Sep 17 00:00:00 2001 From: Fabio Ivona Date: Mon, 20 Feb 2023 23:56:26 +0100 Subject: [PATCH 4/4] add --ansi option to artisan command call --- src/Plugins/Init.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/Init.php b/src/Plugins/Init.php index d63e88e8..ac1c8ba2 100644 --- a/src/Plugins/Init.php +++ b/src/Plugins/Init.php @@ -127,7 +127,7 @@ final class Init implements HandlesArguments $command = [ 'php', 'artisan', 'pest:install', - '--no-interaction', + '--ansi', '--no-interaction', ]; return (new Process($command, $this->testSuite->rootPath, ['COMPOSER_MEMORY_LIMIT' => '-1']))