mirror of
https://github.com/pestphp/pest.git
synced 2026-03-06 07:47:22 +01:00
Merge pull request #669 from fabio-ivona/init-tweak
[chore] tweak init command
This commit is contained in:
@ -4,11 +4,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace Pest\Plugins;
|
||||
|
||||
use Composer\InstalledVersions;
|
||||
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
|
||||
@ -26,7 +28,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 +63,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([
|
||||
'',
|
||||
' <fg=white;bg=blue;options=bold> INFO </> Preparing tests directory.</>',
|
||||
'',
|
||||
View::render('components.badge', [
|
||||
'type' => 'INFO',
|
||||
'content' => 'Preparing tests directory.',
|
||||
]);
|
||||
|
||||
foreach (self::STUBS as $from => $to) {
|
||||
@ -98,4 +105,43 @@ final class Init implements HandlesArguments
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
private function initLaravel(): int
|
||||
{
|
||||
$command = [
|
||||
'composer', 'require',
|
||||
'pestphp/pest-plugin-laravel 2.x-dev',
|
||||
'--dev',
|
||||
];
|
||||
|
||||
$result = (new Process($command, $this->testSuite->rootPath, ['COMPOSER_MEMORY_LIMIT' => '-1']))
|
||||
->setTimeout(null)
|
||||
->run(function ($type, $output): void {
|
||||
$this->output->write($output);
|
||||
});
|
||||
|
||||
if ($result > 0) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
$command = [
|
||||
'php', 'artisan',
|
||||
'pest:install',
|
||||
'--ansi', '--no-interaction',
|
||||
];
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
10
stubs/init/TestCase.php
Normal file
10
stubs/init/TestCase.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase as BaseTestCase;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
//
|
||||
}
|
||||
5
stubs/init/Unit/ExampleTest.php
Normal file
5
stubs/init/Unit/ExampleTest.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
test('example', function () {
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
Reference in New Issue
Block a user