Skip to content

Commit

Permalink
Fix #458: Clear db during testing (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
sankaest authored Jun 17, 2022
1 parent 58cd900 commit 1571a84
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
'user/create' => App\User\Console\CreateCommand::class,
'user/assignRole' => App\User\Console\AssignRoleCommand::class,
'fixture/add' => App\Command\Fixture\AddCommand::class,
'fixture/schema/clear' => App\Command\Fixture\SchemaClearCommand::class,
'router/list' => App\Command\Router\ListCommand::class,
'translator/translate' => App\Command\Translation\TranslateCommand::class,
],
Expand Down
102 changes: 102 additions & 0 deletions src/Command/Fixture/SchemaClearCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace App\Command\Fixture;

use App\Blog\Entity\PostTag;
use App\Blog\Entity\Tag;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Yiisoft\Yii\Console\ExitCode;
use Yiisoft\Yii\Cycle\Command\CycleDependencyProxy;
use App\Blog\Entity\Comment;
use App\Blog\Entity\Post;
use App\User\User;


final class SchemaClearCommand extends Command
{
protected static $defaultName = 'fixture/schema/clear';

private CycleDependencyProxy $promise;


public function __construct(
CycleDependencyProxy $promise,
) {
$this->promise = $promise;
parent::__construct();
}

public function configure(): void
{
$this
->setDescription('Clear database from fixtures')
->setHelp('This command delete all tables');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->promise
->getDatabaseProvider()
->database()
->delete('post')
->run();

$this->promise
->getDatabaseProvider()
->database()
->delete('post_tag')
->run();

$this->promise
->getDatabaseProvider()
->database()
->delete('tag')
->run();

$this->promise
->getDatabaseProvider()
->database()
->delete('user')
->run();

$this->promise
->getDatabaseProvider()
->database()
->delete('comment')
->run();

return 0 === $this->promise
->getORM()
->getRepository(Post::class)
->select()
->count() +
$this->promise
->getORM()
->getRepository(PostTag::class)
->select()
->count() +
$this->promise
->getORM()
->getRepository(Tag::class)
->select()
->count() +
$this->promise
->getORM()
->getRepository(User::class)
->select()
->count() +
$this->promise
->getORM()
->getRepository(Comment::class)
->select()
->count()
? ExitCode::OK
: ExitCode::UNSPECIFIED_ERROR;

}

}
14 changes: 14 additions & 0 deletions tests/Cli/ConsoleCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,18 @@ public function testCommandListCommand(CliTester $I): void
$I->runShellCommand($command . ' list');
$I->seeResultCodeIs(ExitCode::OK);
}

/**
* Clear all data created with testCommandFixtureAdd().
* Clearing database prevents from getting errors during multiple continuous testing with other test,
* what are based on empty database (eg, BlogPageCest)
*
* @param \App\Tests\CliTester $I
*/
public function testCommandCycleSchemaClear(CliTester $I): void
{
$command = dirname(__DIR__, 2) . '/yii';
$I->runShellCommand($command . ' fixture/schema/clear');
$I->seeResultCodeIs(0);
}
}

0 comments on commit 1571a84

Please sign in to comment.