diff --git a/.gitattributes b/.gitattributes index 234cbbf88..3654b6893 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8,7 +8,7 @@ .gitattributes export-ignore .gitignore export-ignore phpstan.neon export-ignore -phpunit.xml export-ignore +/phpunit.xml export-ignore CHANGELOG.md export-ignore CONTRIBUTING.md export-ignore README.md export-ignore diff --git a/composer.json b/composer.json index bf12f2c5d..75a7e0396 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,6 @@ "pestphp/pest-plugin": "^1.0", "pestphp/pest-plugin-coverage": "^1.0", "pestphp/pest-plugin-expectations": "^1.6", - "pestphp/pest-plugin-init": "^1.1", "phpunit/phpunit": ">= 9.3.7 <= 9.5.5" }, "autoload": { @@ -77,6 +76,7 @@ }, "pest": { "plugins": [ + "Pest\\Plugins\\Init", "Pest\\Plugins\\Version" ] }, diff --git a/src/Plugins/Init.php b/src/Plugins/Init.php new file mode 100644 index 000000000..06d3cd41e --- /dev/null +++ b/src/Plugins/Init.php @@ -0,0 +1,128 @@ + 'phpunit.xml', + 'Pest.php' => 'tests/Pest.php', + 'ExampleTest.php' => 'tests/ExampleTest.php', + ]; + + /** + * @var OutputInterface + */ + private $output; + + /** + * @var TestSuite + */ + private $testSuite; + + /** + * Creates a new Plugin instance. + */ + public function __construct(TestSuite $testSuite, OutputInterface $output) + { + $this->testSuite = $testSuite; + $this->output = $output; + } + + public function handleArguments(array $arguments): array + { + if (!array_key_exists(1, $arguments) || $arguments[1] !== self::INIT_OPTION) { + return $arguments; + } + + unset($arguments[1]); + + $this->init(); + + return array_values($arguments); + } + + private function init(): void + { + $testsBaseDir = "{$this->testSuite->rootPath}/tests"; + + if (!is_dir($testsBaseDir)) { + if (!mkdir($testsBaseDir) && !is_dir($testsBaseDir)) { + $this->output->writeln(sprintf( + "\n ERROR Directory `%s` was not created.", + $testsBaseDir + )); + + return; + } + + $this->output->writeln( + ' DONE Created `tests` directory.', + ); + } + + foreach (self::STUBS as $from => $to) { + $fromPath = __DIR__ . "/../../stubs/init/{$from}"; + $toPath = "{$this->testSuite->rootPath}/{$to}"; + + if (file_exists($toPath)) { + $this->output->writeln(sprintf( + ' INFO File `%s` already exists, skipped.', + $to + )); + + continue; + } + + if ($from === 'phpunit.xml' && file_exists($toPath . '.dist')) { + $this->output->writeln(sprintf( + ' INFO File `%s` already exists, skipped.', + $to . '.dist' + )); + + continue; + } + + if (!copy($fromPath, $toPath)) { + $this->output->writeln(sprintf( + '[WARNING] Failed to copy stub `%s` to `%s`', + $from, + $toPath + )); + + continue; + } + + $this->output->writeln(sprintf( + ' DONE Created `%s` file.', + $to + )); + } + + $this->output->writeln( + "\n DONE Pest initialised.\n", + ); + + (new Thanks($this->output))(); + + exit(0); + } +} diff --git a/stubs/init/ExampleTest.php b/stubs/init/ExampleTest.php new file mode 100644 index 000000000..61cd84c32 --- /dev/null +++ b/stubs/init/ExampleTest.php @@ -0,0 +1,5 @@ +toBeTrue(); +}); diff --git a/stubs/init/Pest.php b/stubs/init/Pest.php new file mode 100644 index 000000000..5949c6173 --- /dev/null +++ b/stubs/init/Pest.php @@ -0,0 +1,45 @@ +in('Feature'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation API at any time. +| +*/ + +expect()->extend('toBeOne', function () { + return $this->toBe(1); +}); + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can also expose helpers as +| global functions to help you to reduce the number of lines of code in your test files. +| +*/ + +function something() +{ + // .. +} diff --git a/stubs/init/phpunit.xml b/stubs/init/phpunit.xml new file mode 100644 index 000000000..8f4b58c9f --- /dev/null +++ b/stubs/init/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests + + + + + ./app + ./src + + +