Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests #176

Merged
merged 5 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Command/Migration/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// check any executed migration
foreach (array_reverse($migrations) as $migration) {
if ($migration->getState()->getStatus() === State::STATUS_EXECUTED) {
$exist = true;
break;
}
}
Expand All @@ -62,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$this->eventDispatcher->dispatch(new BeforeMigrate());
try {
$migrator->rollback();
$migration = $migrator->rollback();
if (!$migration instanceof MigrationInterface) {
throw new \Exception('Migration not found');
}
Expand Down
6 changes: 3 additions & 3 deletions src/Command/Migration/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
'<info>If you want to create new empty migration, use <fg=yellow>migrate/create</></info>'
);

/** @var QuestionHelper $qaHelper */
$qaHelper = $this->getHelper('question');

if ($input->isInteractive() && $input instanceof StreamableInputInterface) {
/** @var QuestionHelper $qaHelper */
$qaHelper = $this->getHelper('question');

Check warning on line 68 in src/Command/Migration/GenerateCommand.php

View check run for this annotation

Codecov / codecov/patch

src/Command/Migration/GenerateCommand.php#L68

Added line #L68 was not covered by tests

$question = new ConfirmationQuestion('Would you like to create empty migration right now? (Y/n)', true);
$answer = $qaHelper->ask($input, $output, $question);
if (!$answer) {
Expand Down
2 changes: 0 additions & 2 deletions src/Factory/RepositoryContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

final class RepositoryContainer implements ContainerInterface
{
private ContainerInterface $rootContainer;
private ORMInterface $orm;

private bool $rolesBuilt = false;
Expand All @@ -25,7 +24,6 @@ final class RepositoryContainer implements ContainerInterface

public function __construct(ContainerInterface $rootContainer)
{
$this->rootContainer = $rootContainer;
$this->orm = $rootContainer->get(ORMInterface::class);
}

Expand Down
129 changes: 129 additions & 0 deletions tests/Command/CycleDependencyProxyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Cycle\Tests\Command;

use Cycle\Database\DatabaseProviderInterface;
use Cycle\Migrations\Config\MigrationConfig;
use Cycle\Migrations\Migrator;
use Cycle\Migrations\RepositoryInterface;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\SchemaInterface;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
use Yiisoft\Yii\Cycle\Schema\SchemaConveyorInterface;
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface;

final class CycleDependencyProxyTest extends TestCase
{
public function testGetDatabaseProvider(): void
{
$databaseProvider = $this->createMock(DatabaseProviderInterface::class);
$container = $this->createMock(ContainerInterface::class);
$container
->expects($this->once())
->method('get')
->with(DatabaseProviderInterface::class)
->willReturn($databaseProvider);

$proxy = new CycleDependencyProxy($container);

$this->assertSame($databaseProvider, $proxy->getDatabaseProvider());
}

public function testGetMigrationConfig(): void
{
$config = new MigrationConfig();
$container = $this->createMock(ContainerInterface::class);
$container
->expects($this->once())
->method('get')
->with(MigrationConfig::class)
->willReturn($config);

$proxy = new CycleDependencyProxy($container);

$this->assertSame($config, $proxy->getMigrationConfig());
}

public function testGetMigrator(): void
{
$migrator = new Migrator(
new MigrationConfig(),
$this->createMock(DatabaseProviderInterface::class),
$this->createMock(RepositoryInterface::class)
);
$container = $this->createMock(ContainerInterface::class);
$container
->expects($this->once())
->method('get')
->with(Migrator::class)
->willReturn($migrator);

$proxy = new CycleDependencyProxy($container);

$this->assertSame($migrator, $proxy->getMigrator());
}

public function testGetOrm(): void
{
$orm = $this->createMock(ORMInterface::class);
$container = $this->createMock(ContainerInterface::class);
$container
->expects($this->once())
->method('get')
->with(ORMInterface::class)
->willReturn($orm);

$proxy = new CycleDependencyProxy($container);

$this->assertSame($orm, $proxy->getORM());
}

public function testGetSchema(): void
{
$schema = $this->createMock(SchemaInterface::class);
$container = $this->createMock(ContainerInterface::class);
$container
->expects($this->once())
->method('get')
->with(SchemaInterface::class)
->willReturn($schema);

$proxy = new CycleDependencyProxy($container);

$this->assertSame($schema, $proxy->getSchema());
}

public function testGetSchemaProvider(): void
{
$schemaProvider = $this->createMock(SchemaProviderInterface::class);
$container = $this->createMock(ContainerInterface::class);
$container
->expects($this->once())
->method('get')
->with(SchemaProviderInterface::class)
->willReturn($schemaProvider);

$proxy = new CycleDependencyProxy($container);

$this->assertSame($schemaProvider, $proxy->getSchemaProvider());
}

public function testGetSchemaConveyor(): void
{
$schemaConveyor = $this->createMock(SchemaConveyorInterface::class);
$container = $this->createMock(ContainerInterface::class);
$container
->expects($this->once())
->method('get')
->with(SchemaConveyorInterface::class)
->willReturn($schemaConveyor);

$proxy = new CycleDependencyProxy($container);

$this->assertSame($schemaConveyor, $proxy->getSchemaConveyor());
}
}
66 changes: 66 additions & 0 deletions tests/Command/Migration/CreateCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

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

use Cycle\Database\DatabaseInterface;
use Cycle\Database\DatabaseProviderInterface;
use Cycle\Migrations\Config\MigrationConfig;
use Cycle\Migrations\Migrator;
use Cycle\Migrations\RepositoryInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Yiisoft\Test\Support\Container\SimpleContainer;
use Yiisoft\Yii\Console\ExitCode;
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
use Yiisoft\Yii\Cycle\Command\Migration\CreateCommand;
use Yiisoft\Yii\Cycle\Tests\Command\Stub\FakeOutput;

final class CreateCommandTest extends TestCase
{
public function testExecute(): void
{
$config = new MigrationConfig(['namespace' => 'Test\\Migration']);

$database = $this->createMock(DatabaseInterface::class);
$database->expects($this->once())->method('getName')->willReturn('testDatabase');

$databaseProvider = $this->createMock(DatabaseProviderInterface::class);
$databaseProvider->expects($this->once())->method('database')->willReturn($database);

$repository = $this->createMock(RepositoryInterface::class);
$repository
->expects($this->once())
->method('registerMigration')
->with(
'testDatabase_foo',
$this->callback(static fn (string $name): bool => \str_contains($name, 'OrmTestDatabase')),
$this->callback(
static fn (string $name): bool =>
\str_contains($name, 'OrmTestDatabase') &&
\str_contains($name, 'namespace Test\\Migration') &&
\str_contains($name, 'use Cycle\\Migrations\\Migration') &&
\str_contains($name, 'protected const DATABASE = \'testDatabase\'') &&
\str_contains($name, 'public function up(): void') &&
\str_contains($name, 'public function down(): void')
)
);

$command = new CreateCommand(new CycleDependencyProxy(new SimpleContainer([
DatabaseProviderInterface::class => $databaseProvider,
Migrator::class => new Migrator(
$config,
$this->createMock(DatabaseProviderInterface::class),
$repository
),
MigrationConfig::class => $config,
])));

$output = new FakeOutput();
$code = $command->run(new ArrayInput(['name' => 'foo']), $output);

$this->assertSame(ExitCode::OK, $code);
$this->assertStringContainsString('New migration file has been created', $output->getBuffer());
}
}
94 changes: 94 additions & 0 deletions tests/Command/Migration/DownCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

declare(strict_types=1);

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

use Cycle\Database\Config\DatabaseConfig;
use Cycle\Database\Config\SQLite\MemoryConnectionConfig;
use Cycle\Database\Config\SQLiteDriverConfig;
use Cycle\Database\DatabaseManager;
use Cycle\Database\DatabaseProviderInterface;
use Cycle\Migrations\Config\MigrationConfig;
use Cycle\Migrations\Migrator;
use Cycle\Migrations\RepositoryInterface;
use Cycle\Migrations\State;
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Yiisoft\Test\Support\Container\SimpleContainer;
use Yiisoft\Yii\Console\ExitCode;
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
use Yiisoft\Yii\Cycle\Command\Migration\DownCommand;
use Yiisoft\Yii\Cycle\Command\Migration\UpCommand;
use Yiisoft\Yii\Cycle\Tests\Command\Stub\FakeMigration;
use Yiisoft\Yii\Cycle\Tests\Command\Stub\FakeOutput;

final class DownCommandTest extends TestCase
{
public function testExecuteWithoutMigrations(): void
{
$repository = $this->createMock(RepositoryInterface::class);
$repository->expects($this->once())->method('getMigrations')->willReturn([]);

$migrator = new Migrator(
new MigrationConfig(),
$this->createMock(DatabaseProviderInterface::class),
$repository
);

$output = new FakeOutput();
$command = new DownCommand(
new CycleDependencyProxy(new SimpleContainer([
Migrator::class => $migrator,
MigrationConfig::class => new MigrationConfig(),
])),
$this->createMock(EventDispatcherInterface::class)
);
$code = $command->run(new ArrayInput([]), $output);

$this->assertSame(ExitCode::OK, $code);
$this->assertStringContainsString('No migration found for rollback', $output->getBuffer());
}

public function testExecute(): void
{
$migration = new FakeMigration();
$migration = $migration->withState(new State('test', new \DateTimeImmutable(), State::STATUS_PENDING));

$repository = $this->createMock(RepositoryInterface::class);
$repository->expects($this->exactly(5))->method('getMigrations')->willReturn([$migration]);

$db = new DatabaseManager(new DatabaseConfig([
'default' => 'default',
'databases' => ['default' => ['connection' => 'sqlite']],
'connections' => [
'sqlite' => new SQLiteDriverConfig(connection: new MemoryConnectionConfig()),
],
]));

$migrator = new Migrator(new MigrationConfig(), $db, $repository);
$migrator->configure();

$promise = new CycleDependencyProxy(new SimpleContainer([
DatabaseProviderInterface::class => $db,
Migrator::class => $migrator,
MigrationConfig::class => new MigrationConfig(),
]));

$input = new ArrayInput([]);
$input->setInteractive(false);

$command = new UpCommand($promise, $this->createMock(EventDispatcherInterface::class));
$command->run($input, new NullOutput());

$command = new DownCommand($promise, $this->createMock(EventDispatcherInterface::class));
$output = new FakeOutput();
$code = $command->run($input, $output);

$this->assertSame(ExitCode::OK, $code);
$this->assertStringContainsString('Total 1 migration(s) found', $output->getBuffer());
$this->assertStringContainsString('test: pending', $output->getBuffer());
}
}
Loading
Loading