generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tests for console commands * Add tests for filter handlers * Add tests for factories * Fix cs * Fix PHPDoc
- Loading branch information
Showing
32 changed files
with
1,377 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.