forked from fabio-ivona/pest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pestphp#317 from owenvoke/feature/init
feat: move init command out of external plugin
- Loading branch information
Showing
6 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Pest\Plugins; | ||
|
||
use Pest\Console\Thanks; | ||
use Pest\Contracts\Plugins\HandlesArguments; | ||
use Pest\TestSuite; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class Init implements HandlesArguments | ||
{ | ||
/** | ||
* The option the triggers the init job. | ||
*/ | ||
private const INIT_OPTION = '--init'; | ||
|
||
/** | ||
* The files that will be created. | ||
*/ | ||
private const STUBS = [ | ||
'phpunit.xml' => '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 <fg=white;bg=red;options=bold> ERROR </> Directory `%s` was not created.</>", | ||
$testsBaseDir | ||
)); | ||
|
||
return; | ||
} | ||
|
||
$this->output->writeln( | ||
' <fg=black;bg=green;options=bold> 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( | ||
' <fg=black;bg=yellow;options=bold> INFO </> File `%s` already exists, skipped.</>', | ||
$to | ||
)); | ||
|
||
continue; | ||
} | ||
|
||
if ($from === 'phpunit.xml' && file_exists($toPath . '.dist')) { | ||
$this->output->writeln(sprintf( | ||
' <fg=black;bg=yellow;options=bold> INFO </> File `%s` already exists, skipped.</>', | ||
$to . '.dist' | ||
)); | ||
|
||
continue; | ||
} | ||
|
||
if (!copy($fromPath, $toPath)) { | ||
$this->output->writeln(sprintf( | ||
'<fg=black;bg=red>[WARNING] Failed to copy stub `%s` to `%s`</>', | ||
$from, | ||
$toPath | ||
)); | ||
|
||
continue; | ||
} | ||
|
||
$this->output->writeln(sprintf( | ||
' <fg=black;bg=green;options=bold> DONE </> Created `%s` file.</>', | ||
$to | ||
)); | ||
} | ||
|
||
$this->output->writeln( | ||
"\n <fg=black;bg=green;options=bold> DONE </> Pest initialised.</>\n", | ||
); | ||
|
||
(new Thanks($this->output))(); | ||
|
||
exit(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
test('example', function () { | ||
expect(true)->toBeTrue(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Test Case | ||
|-------------------------------------------------------------------------- | ||
| | ||
| The closure you provide to your test functions is always bound to a specific PHPUnit test | ||
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may | ||
| need to change it using the "uses()" function to bind a different classes or traits. | ||
| | ||
*/ | ||
|
||
// uses(Tests\TestCase::class)->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() | ||
{ | ||
// .. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<coverage processUncoveredFiles="true"> | ||
<include> | ||
<directory suffix=".php">./app</directory> | ||
<directory suffix=".php">./src</directory> | ||
</include> | ||
</coverage> | ||
</phpunit> |