diff --git a/tests/cases/CommandTester.php b/tests/cases/CommandTester.php new file mode 100644 index 0000000..ee0d9ae --- /dev/null +++ b/tests/cases/CommandTester.php @@ -0,0 +1,53 @@ +command = $command; + } + + /** + * @param string[] $inputs + */ + public function run(array $inputs, bool $interactive): int + { + $input = new ArrayInput($interactive ? [] : $inputs); + + if ($interactive) { + $input->setStream($this->createStream($inputs)); + } + + $output = new BufferedOutput(); + + return $this->command->run($input, $output); + } + + /** + * @param string[] $inputs + * @return resource + */ + private function createStream(array $inputs) + { + $stream = fopen('php://memory', 'r+', false); + + foreach ($inputs as $input) { + fwrite($stream, $input . PHP_EOL); + } + + rewind($stream); + + return $stream; + } + +} diff --git a/tests/cases/Jobs/CommandJobTest.phpt b/tests/cases/Jobs/CommandJobTest.phpt index 8acf86c..23049c3 100644 --- a/tests/cases/Jobs/CommandJobTest.phpt +++ b/tests/cases/Jobs/CommandJobTest.phpt @@ -5,18 +5,24 @@ namespace Adbros\Worker\Tests\Jobs; use Adbros\Worker\Command\WorkerCommand; use Adbros\Worker\FileManager; use Adbros\Worker\Jobs\CommandJob; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Output\BufferedOutput; +use Adbros\Worker\Tests\CommandTester; use Tester\Assert; +use Tester\TestCase; require_once __DIR__ . '/../../bootstrap.php'; /** * @testCase */ -class CommandJobTest extends JobsTestCase +class CommandJobTest extends TestCase { + /** @var string[] */ + protected $inputs; + + /** @var CommandTester */ + protected $commandTester; + public function setUp(): void { $this->inputs = [ @@ -26,36 +32,40 @@ class CommandJobTest extends JobsTestCase '--namespace' => 'My\\App\\My\\Commands', '--parent' => 'My\\App\\My\\Commands\\BaseCommand', ]; + + $this->commandTester = new CommandTester( + new WorkerCommand( + new CommandJob( + new FileManager('') + ) + ) + ); } public function testNoninteractive(): void { - $fileManager = new FileManager(''); - - $command = new WorkerCommand(new CommandJob($fileManager)); - - $input = new ArrayInput($this->inputs); - - $output = new BufferedOutput(); - - Assert::same(0, $command->run($input, $output)); + Assert::same(0, $this->commandTester->run($this->inputs, false)); Assert::same(file_get_contents(__DIR__ . '/expected/CommandJob.expect'), file_get_contents(OUTPUT_DIR . '/My/Commands/TestCommand.php')); } public function testInteractive(): void { - $fileManager = new FileManager(''); - - $command = new WorkerCommand(new CommandJob($fileManager)); + Assert::same(0, $this->commandTester->run($this->inputs, true)); - $input = new ArrayInput([]); + Assert::same(file_get_contents(__DIR__ . '/expected/CommandJob.expect'), file_get_contents(OUTPUT_DIR . '/My/Commands/TestCommand.php')); + } - $input->setStream($this->createStream($this->inputs)); + public function testInteractiveWithErrors(): void + { + $inputsWithErrors = []; - $output = new BufferedOutput(); + foreach ($this->inputs as $input) { + $inputsWithErrors[] = $input . '!'; + $inputsWithErrors[] = $input; + } - Assert::same(0, $command->run($input, $output)); + Assert::same(0, $this->commandTester->run($inputsWithErrors, true)); Assert::same(file_get_contents(__DIR__ . '/expected/CommandJob.expect'), file_get_contents(OUTPUT_DIR . '/My/Commands/TestCommand.php')); } diff --git a/tests/cases/Jobs/ControlJobTest.phpt b/tests/cases/Jobs/ControlJobTest.phpt index e8e6931..8e59436 100644 --- a/tests/cases/Jobs/ControlJobTest.phpt +++ b/tests/cases/Jobs/ControlJobTest.phpt @@ -5,18 +5,24 @@ namespace Adbros\Worker\Tests\Jobs; use Adbros\Worker\Command\WorkerCommand; use Adbros\Worker\FileManager; use Adbros\Worker\Jobs\ControlJob; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Output\BufferedOutput; +use Adbros\Worker\Tests\CommandTester; use Tester\Assert; +use Tester\TestCase; require_once __DIR__ . '/../../bootstrap.php'; /** * @testCase */ -class ControlJobTest extends JobsTestCase +class ControlJobTest extends TestCase { + /** @var string[] */ + protected $inputs; + + /** @var CommandTester */ + protected $commandTester; + public function setUp(): void { $this->inputs = [ @@ -27,19 +33,19 @@ class ControlJobTest extends JobsTestCase '--control-parent' => 'My\\App\\My\\Controls\\BaseControl', '--factory-parent' => 'My\\App\\My\\Controls\\BaseFactory', ]; + + $this->commandTester = new CommandTester( + new WorkerCommand( + new ControlJob( + new FileManager('') + ) + ) + ); } public function testNoninteractive(): void { - $fileManager = new FileManager(''); - - $command = new WorkerCommand(new ControlJob($fileManager)); - - $input = new ArrayInput($this->inputs); - - $output = new BufferedOutput(); - - Assert::same(0, $command->run($input, $output)); + Assert::same(0, $this->commandTester->run($this->inputs, false)); Assert::same(file_get_contents(__DIR__ . '/expected/ControlJob.control.expect'), file_get_contents(OUTPUT_DIR . '/My/Controls/Test/TestControl.php')); @@ -50,17 +56,25 @@ class ControlJobTest extends JobsTestCase public function testInteractive(): void { - $fileManager = new FileManager(''); + Assert::same(0, $this->commandTester->run($this->inputs, true)); - $command = new WorkerCommand(new ControlJob($fileManager)); + Assert::same(file_get_contents(__DIR__ . '/expected/ControlJob.control.expect'), file_get_contents(OUTPUT_DIR . '/My/Controls/Test/TestControl.php')); + + Assert::same(file_get_contents(__DIR__ . '/expected/ControlJob.factory.expect'), file_get_contents(OUTPUT_DIR . '/My/Controls/Test/TestFactory.php')); - $input = new ArrayInput([]); + Assert::same(file_get_contents(__DIR__ . '/expected/ControlJob.template.expect'), file_get_contents(OUTPUT_DIR . '/My/Controls/Test/TestControl.latte')); + } - $input->setStream($this->createStream($this->inputs)); + public function testInteractiveWithErrors(): void + { + $inputsWithErrors = []; - $output = new BufferedOutput(); + foreach ($this->inputs as $input) { + $inputsWithErrors[] = $input . '!'; + $inputsWithErrors[] = $input; + } - Assert::same(0, $command->run($input, $output)); + Assert::same(0, $this->commandTester->run($inputsWithErrors, true)); Assert::same(file_get_contents(__DIR__ . '/expected/ControlJob.control.expect'), file_get_contents(OUTPUT_DIR . '/My/Controls/Test/TestControl.php')); diff --git a/tests/cases/Jobs/JobsTestCase.php b/tests/cases/Jobs/JobsTestCase.php deleted file mode 100644 index 8c52252..0000000 --- a/tests/cases/Jobs/JobsTestCase.php +++ /dev/null @@ -1,30 +0,0 @@ -inputs = [ @@ -29,19 +35,19 @@ class OrmJobTest extends JobsTestCase '--repository-parent' => 'My\\App\\My\\Model\\Orm\\BaseRepository', '--mapper-parent' => 'My\\App\\My\\Model\\Orm\\BaseMapper', ]; + + $this->commandTester = new CommandTester( + new WorkerCommand( + new OrmJob( + new FileManager('') + ) + ) + ); } public function testNoninteractive(): void { - $fileManager = new FileManager(''); - - $command = new WorkerCommand(new OrmJob($fileManager)); - - $input = new ArrayInput($this->inputs); - - $output = new BufferedOutput(); - - Assert::same(0, $command->run($input, $output)); + Assert::same(0, $this->commandTester->run($this->inputs, false)); Assert::same(file_get_contents(__DIR__ . '/expected/OrmJob.entity.expect'), file_get_contents(OUTPUT_DIR . '/My/Model/Orm/Test/Test.php')); @@ -52,17 +58,25 @@ class OrmJobTest extends JobsTestCase public function testInteractive(): void { - $fileManager = new FileManager(''); + Assert::same(0, $this->commandTester->run($this->inputs, true)); - $command = new WorkerCommand(new OrmJob($fileManager)); + Assert::same(file_get_contents(__DIR__ . '/expected/OrmJob.entity.expect'), file_get_contents(OUTPUT_DIR . '/My/Model/Orm/Test/Test.php')); + + Assert::same(file_get_contents(__DIR__ . '/expected/OrmJob.mapper.expect'), file_get_contents(OUTPUT_DIR . '/My/Model/Orm/Test/TestsMapper.php')); - $input = new ArrayInput([]); + Assert::same(file_get_contents(__DIR__ . '/expected/OrmJob.repository.expect'), file_get_contents(OUTPUT_DIR . '/My/Model/Orm/Test/TestsRepository.php')); + } - $input->setStream($this->createStream($this->inputs)); + public function testInteractiveWithErrors(): void + { + $inputsWithErrors = []; - $output = new BufferedOutput(); + foreach ($this->inputs as $input) { + $inputsWithErrors[] = $input . '!'; + $inputsWithErrors[] = $input; + } - Assert::same(0, $command->run($input, $output)); + Assert::same(0, $this->commandTester->run($inputsWithErrors, true)); Assert::same(file_get_contents(__DIR__ . '/expected/OrmJob.entity.expect'), file_get_contents(OUTPUT_DIR . '/My/Model/Orm/Test/Test.php')); diff --git a/tests/cases/Jobs/PresenterJobTest.phpt b/tests/cases/Jobs/PresenterJobTest.phpt index 6811e07..ce4d709 100644 --- a/tests/cases/Jobs/PresenterJobTest.phpt +++ b/tests/cases/Jobs/PresenterJobTest.phpt @@ -5,18 +5,24 @@ namespace Adbros\Worker\Tests\Jobs; use Adbros\Worker\Command\WorkerCommand; use Adbros\Worker\FileManager; use Adbros\Worker\Jobs\PresenterJob; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Output\BufferedOutput; +use Adbros\Worker\Tests\CommandTester; use Tester\Assert; +use Tester\TestCase; require_once __DIR__ . '/../../bootstrap.php'; /** * @testCase */ -class PresenterJobTest extends JobsTestCase +class PresenterJobTest extends TestCase { + /** @var string[] */ + protected $inputs; + + /** @var CommandTester */ + protected $commandTester; + public function setUp(): void { $this->inputs = [ @@ -26,19 +32,19 @@ class PresenterJobTest extends JobsTestCase '--namespace' => 'My\\App\\My\\Presenters', '--parent' => 'My\\App\\My\\Presenters\\BasePresenter', ]; + + $this->commandTester = new CommandTester( + new WorkerCommand( + new PresenterJob( + new FileManager('') + ) + ) + ); } public function testNoninteractive(): void { - $fileManager = new FileManager(''); - - $command = new WorkerCommand(new PresenterJob($fileManager)); - - $input = new ArrayInput($this->inputs); - - $output = new BufferedOutput(); - - Assert::same(0, $command->run($input, $output)); + Assert::same(0, $this->commandTester->run($this->inputs, false)); Assert::same(file_get_contents(__DIR__ . '/expected/PresenterJob.expect'), file_get_contents(OUTPUT_DIR . '/My/Presenters/TestPresenter.php')); @@ -47,17 +53,23 @@ class PresenterJobTest extends JobsTestCase public function testInteractive(): void { - $fileManager = new FileManager(''); + Assert::same(0, $this->commandTester->run($this->inputs, true)); - $command = new WorkerCommand(new PresenterJob($fileManager)); + Assert::same(file_get_contents(__DIR__ . '/expected/PresenterJob.expect'), file_get_contents(OUTPUT_DIR . '/My/Presenters/TestPresenter.php')); - $input = new ArrayInput([]); + Assert::same(file_get_contents(__DIR__ . '/expected/PresenterJob.template.expect'), file_get_contents(OUTPUT_DIR . '/My/Presenters/templates/Test/default.latte')); + } - $input->setStream($this->createStream($this->inputs)); + public function testInteractiveWithErrors(): void + { + $inputsWithErrors = []; - $output = new BufferedOutput(); + foreach ($this->inputs as $input) { + $inputsWithErrors[] = $input . '!'; + $inputsWithErrors[] = $input; + } - Assert::same(0, $command->run($input, $output)); + Assert::same(0, $this->commandTester->run($inputsWithErrors, true)); Assert::same(file_get_contents(__DIR__ . '/expected/PresenterJob.expect'), file_get_contents(OUTPUT_DIR . '/My/Presenters/TestPresenter.php'));