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

Clear db during testing #461

Merged
merged 3 commits into from
Jun 17, 2022
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
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);
}
}