Skip to content

Commit

Permalink
Add tests for commands (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladis84 committed Apr 4, 2023
1 parent a5cb853 commit 5e6469e
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/Command/Schema/SchemaClearBaseCommandTest.php
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Cycle\Tests\Command\Schema;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Yiisoft\Test\Support\Container\SimpleContainer;
use Yiisoft\Yii\Console\ExitCode;
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
use Yiisoft\Yii\Cycle\Command\Schema\SchemaClearCommand;
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface;

final class SchemaClearBaseCommandTest extends TestCase
{
public function testExecute(): void
{
$schemaProvider = $this->getMockBuilder(SchemaProviderInterface::class)->getMock();
$schemaProvider->expects($this->once())->method('clear');
$container = new SimpleContainer([SchemaProviderInterface::class => $schemaProvider]);
$promise = new CycleDependencyProxy($container);
$command = new SchemaClearCommand($promise);
$input = $this->getMockBuilder(InputInterface::class)->getMock();
$output = $this->getMockBuilder(OutputInterface::class)->getMock();

$code = $command->run($input, $output);

$this->assertEquals(ExitCode::OK, $code);
}
}
80 changes: 80 additions & 0 deletions tests/Command/Schema/SchemaCommandTest.php
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Cycle\Tests\Command\Schema;

use Cycle\ORM\SchemaInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface;
use Yiisoft\Test\Support\Container\SimpleContainer;
use Yiisoft\Yii\Console\ExitCode;
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
use Yiisoft\Yii\Cycle\Command\Schema\SchemaCommand;

final class SchemaCommandTest extends TestCase
{
private OutputInterface $output;

protected function setUp(): void
{
$this->output = new class extends Output {
private string $buffer = '';

protected function doWrite(string $message, bool $newline): void
{
$this->buffer .= $message;

if ($newline) {
$this->buffer .= \PHP_EOL;
}
}

public function getBuffer(): string
{
return $this->buffer;
}
};
}

public function testExecuteUndefinedRoles(): void
{
$schema = $this->getMockBuilder(SchemaInterface::class)->getMock();
$schema->expects($this->any())->method('getRoles')->willReturn([]);

$container = new SimpleContainer([SchemaInterface::class => $schema]);
$promise = new CycleDependencyProxy($container);
$command = new SchemaCommand($promise);

$code = $command->run(new ArrayInput(['role' => 'foo,bar']), $this->output);

$this->assertEquals(ExitCode::OK, $code);
$this->assertStringContainsString('Undefined roles: foo, bar', $this->output->getBuffer());
}

public function testExecuteGetRoles(): void
{
$schema = $this->getMockBuilder(SchemaInterface::class)->getMock();
$schema->expects($this->any())->method('getRoles')->willReturn(['foo', 'bar']);
$schema->expects($this->any())->method('define')->willReturnCallback(
function (string $role, int $property) {
if ($property === SchemaInterface::ROLE) {
return $role;
}

return null;
}
);

$container = new SimpleContainer([SchemaInterface::class => $schema]);
$promise = new CycleDependencyProxy($container);
$command = new SchemaCommand($promise);

$code = $command->run(new ArrayInput([]), $this->output);

$this->assertEquals(ExitCode::OK, $code);
$this->assertStringNotContainsString('Undefined roles', $this->output->getBuffer());
}
}
31 changes: 31 additions & 0 deletions tests/Command/Schema/SchemaRebuildCommandTest.php
@@ -0,0 +1,31 @@
<?php

namespace Yiisoft\Yii\Cycle\Tests\Command\Schema;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Yiisoft\Test\Support\Container\SimpleContainer;
use Yiisoft\Yii\Console\ExitCode;
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
use Yiisoft\Yii\Cycle\Command\Schema\SchemaRebuildCommand;
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface;

final class SchemaRebuildCommandTest extends TestCase
{
public function testExecute()
{
$provider = $this->getMockBuilder(SchemaProviderInterface::class)->getMock();
$provider->expects($this->once())->method('clear');
$provider->expects($this->once())->method('read');

$container = new SimpleContainer([SchemaProviderInterface::class => $provider]);
$promise = new CycleDependencyProxy($container);

$command = new SchemaRebuildCommand($promise);

$code = $command->run(new ArrayInput([]), new BufferedOutput);

$this->assertEquals(ExitCode::OK, $code);
}
}

0 comments on commit 5e6469e

Please sign in to comment.