Upstream spiral/testing state of the touched files before ad70fe2998
This commit is contained in:
1 parent
50c16b7998
commit
362e25d279
2 files changed
+203
No files matched your search
@@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Spiral\Testing\Traits;
|
||||||
|
|
||||||
|
use Spiral\Files\FilesInterface;
|
||||||
|
use Symfony\Component\Console\Input\ArrayInput;
|
||||||
|
use Symfony\Component\Console\Output\BufferedOutput;
|
||||||
|
|
||||||
|
trait InteractsWithScaffolder
|
||||||
|
{
|
||||||
|
public function assertScaffolderCommandSame(
|
||||||
|
string $command,
|
||||||
|
array $args,
|
||||||
|
string $expected,
|
||||||
|
?string $expectedFilename = null,
|
||||||
|
array $expectedOutputStrings = [],
|
||||||
|
): void {
|
||||||
|
$output = $this->mockScaffolder($command, $args, function (
|
||||||
|
string $filename,
|
||||||
|
string $data,
|
||||||
|
) use ($expected, $expectedFilename): bool {
|
||||||
|
$this->assertSame($expected, $data, 'Generated code is not the same with expected.');
|
||||||
|
|
||||||
|
if ($expectedFilename) {
|
||||||
|
$root = $this->getDirectoryByAlias('root');
|
||||||
|
$this->assertSame(
|
||||||
|
\str_replace($root, '', $expectedFilename),
|
||||||
|
\str_replace($root, '', $filename),
|
||||||
|
'Generated filename is not the same with expected.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
foreach ($expectedOutputStrings as $expected) {
|
||||||
|
$this->assertStringContainsString($expected, $output, 'Output does not contain expected string.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function assertScaffolderCommandContains(
|
||||||
|
string $command,
|
||||||
|
array $args,
|
||||||
|
array $expectedStrings,
|
||||||
|
?string $expectedFilename = null,
|
||||||
|
array $expectedOutputStrings = [],
|
||||||
|
): void {
|
||||||
|
$output = $this->mockScaffolder($command, $args, function (
|
||||||
|
string $filename,
|
||||||
|
string $data,
|
||||||
|
) use ($expectedStrings, $expectedFilename): bool {
|
||||||
|
foreach ($expectedStrings as $expected) {
|
||||||
|
$this->assertStringContainsString($expected, $data, 'Generated code does not contain expected string.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($expectedFilename) {
|
||||||
|
$root = $this->getDirectoryByAlias('root');
|
||||||
|
$this->assertSame(
|
||||||
|
\str_replace($root, '', $expectedFilename),
|
||||||
|
\str_replace($root, '', $filename),
|
||||||
|
'Generated filename is not the same with expected.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
foreach ($expectedOutputStrings as $expected) {
|
||||||
|
$this->assertStringContainsString($expected, $output, 'Output does not contain expected string.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function mockScaffolder(string $command, array $args, \Closure $expected): string
|
||||||
|
{
|
||||||
|
$originalFiles = $this->getContainer()->has(FilesInterface::class)
|
||||||
|
? $this->getContainer()->get(FilesInterface::class)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
$files = $this->mockContainer(FilesInterface::class);
|
||||||
|
$files->shouldReceive('exists')->andReturnFalse();
|
||||||
|
$files->shouldReceive('write')->withArgs($expected);
|
||||||
|
|
||||||
|
$this->getConsole()->run($command, new ArrayInput($args), $output = new BufferedOutput());
|
||||||
|
|
||||||
|
$this->getContainer()->removeBinding(FilesInterface::class);
|
||||||
|
if ($originalFiles !== null) {
|
||||||
|
$this->getContainer()->bind(FilesInterface::class, $originalFiles);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output->fetch();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Spiral\Testing\Tests\Scaffolder;
|
||||||
|
|
||||||
|
use Spiral\Files\FilesInterface;
|
||||||
|
use Spiral\Testing\Tests\TestCase;
|
||||||
|
use Symfony\Component\Console\Exception\RuntimeException;
|
||||||
|
|
||||||
|
final class ScaffolderTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testCreateCommand(): void
|
||||||
|
{
|
||||||
|
$this->assertScaffolderCommandSame(
|
||||||
|
'create:command',
|
||||||
|
[
|
||||||
|
'name' => 'TestCommand',
|
||||||
|
],
|
||||||
|
expected: <<<'PHP'
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Spiral\Testing\Command;
|
||||||
|
|
||||||
|
use Spiral\Console\Attribute\Argument;
|
||||||
|
use Spiral\Console\Attribute\AsCommand;
|
||||||
|
use Spiral\Console\Attribute\Option;
|
||||||
|
use Spiral\Console\Attribute\Question;
|
||||||
|
use Spiral\Console\Command;
|
||||||
|
|
||||||
|
#[AsCommand(name: 'test:command')]
|
||||||
|
final class TestCommand extends Command
|
||||||
|
{
|
||||||
|
public function __invoke(): int
|
||||||
|
{
|
||||||
|
// Put your command logic here
|
||||||
|
$this->info('Command logic is not implemented yet');
|
||||||
|
|
||||||
|
return self::SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PHP,
|
||||||
|
expectedFilename: 'app/src/Command/TestCommand.php',
|
||||||
|
expectedOutputStrings: [
|
||||||
|
"Declaration of 'TestCommand' has been successfully written into 'app/src/Command/TestCommand.php",
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateCommandContainsNamespace(): void
|
||||||
|
{
|
||||||
|
$this->assertScaffolderCommandContains(
|
||||||
|
'create:command',
|
||||||
|
[
|
||||||
|
'name' => 'TestCommand',
|
||||||
|
'--namespace' => 'App\Command',
|
||||||
|
],
|
||||||
|
expectedStrings: [
|
||||||
|
'namespace App\Command;',
|
||||||
|
],
|
||||||
|
expectedFilename: 'app/src/TestCommand.php',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCommandNameIsRequired(): void
|
||||||
|
{
|
||||||
|
$this->expectException(RuntimeException::class);
|
||||||
|
$this->expectExceptionMessage('Not enough arguments (missing: "name").');
|
||||||
|
|
||||||
|
$this->assertScaffolderCommandSame(
|
||||||
|
'create:command',
|
||||||
|
['-n' => false],
|
||||||
|
'',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateCommandWithAdditionalOptions(): void
|
||||||
|
{
|
||||||
|
$this->assertScaffolderCommandContains(
|
||||||
|
'create:command',
|
||||||
|
[
|
||||||
|
'name' => 'TestCommand',
|
||||||
|
'-o' => 'foo',
|
||||||
|
],
|
||||||
|
expectedStrings: [
|
||||||
|
"#[Option(description: 'Argument description')]",
|
||||||
|
'private bool $foo;',
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAfterTestFilesShoulBeRestored(): void
|
||||||
|
{
|
||||||
|
$files = $this->mockContainer(FilesInterface::class);
|
||||||
|
|
||||||
|
$this->assertScaffolderCommandContains(
|
||||||
|
'create:command',
|
||||||
|
[
|
||||||
|
'name' => 'TestCommand',
|
||||||
|
],
|
||||||
|
expectedStrings: ['final class TestCommand extends Command'],
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame($files, $this->getContainer()->get(FilesInterface::class));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user