Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
martenb committed Apr 13, 2020
1 parent 5e3665c commit 833f617
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 103 deletions.
53 changes: 53 additions & 0 deletions tests/cases/CommandTester.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace Adbros\Worker\Tests;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

class CommandTester
{

/** @var Command */
private $command;

public function __construct(Command $command)
{
$this->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;
}

}
48 changes: 29 additions & 19 deletions tests/cases/Jobs/CommandJobTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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'));
}
Expand Down
50 changes: 32 additions & 18 deletions tests/cases/Jobs/ControlJobTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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'));

Expand All @@ -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'));

Expand Down
30 changes: 0 additions & 30 deletions tests/cases/Jobs/JobsTestCase.php

This file was deleted.

50 changes: 32 additions & 18 deletions tests/cases/Jobs/OrmJobTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ namespace Adbros\Worker\Tests\Jobs;
use Adbros\Worker\Command\WorkerCommand;
use Adbros\Worker\FileManager;
use Adbros\Worker\Jobs\OrmJob;
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 OrmJobTest extends JobsTestCase
class OrmJobTest extends TestCase
{

/** @var string[] */
protected $inputs;

/** @var CommandTester */
protected $commandTester;

public function setUp(): void
{
$this->inputs = [
Expand All @@ -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'));

Expand All @@ -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'));

Expand Down
Loading

0 comments on commit 833f617

Please sign in to comment.