Skip to content

Commit

Permalink
Merge pull request #374 from tienvx/add-separate-commands-for-testing…
Browse files Browse the repository at this point in the history
…-bug-and-predefined-case

Add separate commands for testing bug and predefined case
  • Loading branch information
tienvx committed Aug 29, 2019
2 parents bbe9fe6 + c91adc6 commit 8bc146b
Show file tree
Hide file tree
Showing 67 changed files with 800 additions and 1,260 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ $ composer create-project symfony/skeleton my-project
Install lastest version of this bundle:

```console
$ composer require tienvx/mbt-bundle "^1.9"
$ composer require tienvx/mbt-bundle "^1.10"
```

### Step 3: Config file storage
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.9-dev"
"dev-master": "1.10-dev"
}
},
"suggest": {
Expand Down
3 changes: 2 additions & 1 deletion src/Command/ApplyBugTransitionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Tienvx\Bundle\MbtBundle\Entity\Bug;
use Tienvx\Bundle\MbtBundle\Workflow\BugWorkflow;

class ApplyBugTransitionCommand extends AbstractCommand
class ApplyBugTransitionCommand extends Command
{
/**
* @var BugWorkflow
Expand Down
3 changes: 2 additions & 1 deletion src/Command/ApplyTaskTransitionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Workflow\TaskWorkflow;

class ApplyTaskTransitionCommand extends AbstractCommand
class ApplyTaskTransitionCommand extends Command
{
/**
* @var TaskWorkflow
Expand Down
43 changes: 9 additions & 34 deletions src/Command/CaptureScreenshotsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,27 @@
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use League\Flysystem\FilesystemInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Workflow\Registry;
use Throwable;
use Tienvx\Bundle\MbtBundle\Entity\Bug;
use Tienvx\Bundle\MbtBundle\Entity\Data;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Helper\WorkflowHelper;
use Tienvx\Bundle\MbtBundle\Subject\AbstractSubject;
use Tienvx\Bundle\MbtBundle\Subject\SubjectManager;

class CaptureScreenshotsCommand extends AbstractCommand
class CaptureScreenshotsCommand extends Command
{
use TokenTrait;
use SubjectTrait;

/**
* @var EntityManagerInterface
*/
private $entityManager;

/**
* @var SubjectManager
*/
protected $subjectManager;

/**
* @var Registry
*/
Expand Down Expand Up @@ -78,17 +75,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new Exception(sprintf('No bug found for id %d', $bugId));
}

$task = $bug->getTask();
if (!$task instanceof Task) {
throw new Exception(sprintf('Task of bug with id %d is missing', $bugId));
}

$workflow = WorkflowHelper::get($this->workflowRegistry, $task->getModel()->getName());
$workflow = WorkflowHelper::get($this->workflowRegistry, $bug->getModel()->getName());
if (WorkflowHelper::checksum($workflow) !== $bug->getModelHash()) {
throw new Exception(sprintf('Model checksum of bug with id %d does not match', $bugId));
}

$subject = $this->getSubject($task->getModel()->getName(), $bug->getId());
$subject = $this->getSubject($bug->getModel()->getName());
$subject->setFilesystem($this->mbtStorage);
$subject->removeScreenshots($bug->getId());

$this->setAnonymousToken();

Expand All @@ -111,23 +105,4 @@ protected function execute(InputInterface $input, OutputInterface $output)
$subject->tearDown();
}
}

/**
* @param string $model
* @param int $bugId
*
* @return AbstractSubject
*
* @throws Exception
*/
private function getSubject(string $model, int $bugId): AbstractSubject
{
$subject = $this->subjectManager->createSubject($model);

$subject->setUp();
$subject->setFilesystem($this->mbtStorage);
$subject->removeScreenshots($bugId);

return $subject;
}
}
22 changes: 13 additions & 9 deletions src/Command/CreateBugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Workflow\Registry;
use Tienvx\Bundle\MbtBundle\Entity\Bug;
use Tienvx\Bundle\MbtBundle\Entity\Model;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Entity\Steps;
use Tienvx\Bundle\MbtBundle\Helper\WorkflowHelper;

class CreateBugCommand extends AbstractCommand
class CreateBugCommand extends Command
{
/**
* @var ValidatorInterface
Expand Down Expand Up @@ -54,6 +56,7 @@ protected function configure()
->addArgument('message', InputArgument::REQUIRED, 'Bug message.')
->addArgument('task-id', InputArgument::REQUIRED, 'Task id.')
->addArgument('status', InputArgument::REQUIRED, 'Bug status.')
->addArgument('model', InputArgument::REQUIRED, 'Model name.')
->setHidden(true);
}

Expand All @@ -70,24 +73,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
$message = $input->getArgument('message');
$taskId = $input->getArgument('task-id');
$status = $input->getArgument('status');
$model = $input->getArgument('model');

$task = $this->entityManager->getRepository(Task::class)->find($taskId);

if (!$task || !$task instanceof Task) {
throw new Exception(sprintf('No task found for id %d', $taskId));
}

$model = $task->getModel()->getName();
$workflow = WorkflowHelper::get($this->workflowRegistry, $model);

$bug = new Bug();
$bug->setTitle($title);
$bug->setSteps(Steps::deserialize($steps));
$bug->setModel(new Model($model));
$bug->setModelHash(WorkflowHelper::checksum($workflow));
$bug->setBugMessage($message);
$bug->setTask($task);
$bug->setStatus($status);

if ($taskId) {
$task = $this->entityManager->getRepository(Task::class)->find($taskId);
if ($task instanceof Task) {
$bug->setTask($task);
}
}

$errors = $this->validator->validate($bug);

if (count($errors) > 0) {
Expand Down
78 changes: 13 additions & 65 deletions src/Command/ExecuteTaskCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,32 @@

use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Workflow\Registry;
use Throwable;
use Tienvx\Bundle\MbtBundle\Entity\Step;
use Tienvx\Bundle\MbtBundle\Entity\Data;
use Tienvx\Bundle\MbtBundle\Entity\Steps;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Generator\GeneratorManager;
use Tienvx\Bundle\MbtBundle\Entity\Steps;
use Tienvx\Bundle\MbtBundle\Helper\StepsRunner;
use Tienvx\Bundle\MbtBundle\Helper\WorkflowHelper;
use Tienvx\Bundle\MbtBundle\Message\CreateBugMessage;
use Tienvx\Bundle\MbtBundle\Message\ApplyTaskTransitionMessage;
use Tienvx\Bundle\MbtBundle\Subject\AbstractSubject;
use Tienvx\Bundle\MbtBundle\Subject\SubjectManager;
use Tienvx\Bundle\MbtBundle\Workflow\BugWorkflow;
use Tienvx\Bundle\MbtBundle\Workflow\TaskWorkflow;

class ExecuteTaskCommand extends AbstractCommand
class ExecuteTaskCommand extends Command
{
use TokenTrait;
use SubjectTrait;
use MessageTrait;

/**
* @var Registry
*/
private $workflowRegistry;

/**
* @var SubjectManager
*/
private $subjectManager;

/**
* @var GeneratorManager
*/
Expand All @@ -45,11 +40,6 @@ class ExecuteTaskCommand extends AbstractCommand
*/
private $entityManager;

/**
* @var MessageBusInterface
*/
private $messageBus;

/**
* @var string
*/
Expand Down Expand Up @@ -108,58 +98,16 @@ protected function execute(InputInterface $input, OutputInterface $output)

$this->setAnonymousToken();

$steps = new Steps();
$steps->addStep(new Step(null, new Data(), $workflow->getDefinition()->getInitialPlaces()));

$recorded = new Steps();
try {
foreach ($generator->generate($workflow, $subject, $task->getGeneratorOptions()) as $step) {
if ($step instanceof Step && $step->getTransition() && $step->getData() instanceof Data) {
try {
$workflow->apply($subject, $step->getTransition(), [
'data' => $step->getData(),
]);
} catch (Throwable $throwable) {
throw $throwable;
} finally {
$places = array_keys(array_filter($workflow->getMarking($subject)->getPlaces()));
$step->setPlaces($places);
$steps->addStep($step);
}
}
}
$steps = $generator->generate($workflow, $subject, $task->getGeneratorOptions());
StepsRunner::record($steps, $workflow, $subject, $recorded);
} catch (Throwable $throwable) {
$this->createBug($steps, $throwable->getMessage(), $taskId);
$this->createBug($this->defaultBugTitle, $recorded, $throwable->getMessage(), $taskId, $task->getModel()->getName());
} finally {
$subject->tearDown();

$this->messageBus->dispatch(new ApplyTaskTransitionMessage($taskId, TaskWorkflow::COMPLETE));
$this->applyTaskTransition($taskId, TaskWorkflow::COMPLETE);
}
}

private function createBug(Steps $steps, string $bugMessage, int $taskId)
{
$message = new CreateBugMessage(
$this->defaultBugTitle,
$steps->serialize(),
$bugMessage,
$taskId,
BugWorkflow::NEW
);
$this->messageBus->dispatch($message);
}

/**
* @param string $model
*
* @return AbstractSubject
*
* @throws Exception
*/
private function getSubject(string $model): AbstractSubject
{
$subject = $this->subjectManager->createSubject($model);
$subject->setUp();

return $subject;
}
}
35 changes: 14 additions & 21 deletions src/Command/FinishReduceBugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@

use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Tienvx\Bundle\MbtBundle\Entity\Bug;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Message\ApplyBugTransitionMessage;
use Tienvx\Bundle\MbtBundle\Message\CaptureScreenshotsMessage;
use Tienvx\Bundle\MbtBundle\Message\ReportBugMessage;
use Tienvx\Bundle\MbtBundle\Workflow\BugWorkflow;

class FinishReduceBugCommand extends AbstractCommand
class FinishReduceBugCommand extends Command
{
use MessageTrait;

/**
* @var EntityManagerInterface
*/
private $entityManager;

/**
* @var MessageBusInterface
*/
private $messageBus;

public function __construct(
EntityManagerInterface $entityManager,
MessageBusInterface $messageBus
Expand Down Expand Up @@ -62,20 +57,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new Exception(sprintf('No bug found for id %d', $bugId));
}

$task = $bug->getTask();
if (!$task instanceof Task) {
throw new Exception(sprintf('Task of bug with id %d is missing', $bugId));
}

$this->messageBus->dispatch(new ApplyBugTransitionMessage($bug->getId(), BugWorkflow::COMPLETE_REDUCE));
$this->applyBugTransition($bug->getId(), BugWorkflow::COMPLETE_REDUCE);

if (!empty($task->getReporters())) {
foreach ($task->getReporters() as $reporter) {
$this->messageBus->dispatch(new ReportBugMessage($bug->getId(), $reporter->getName()));
$task = $bug->getTask();
if ($task instanceof Task) {
if (!empty($task->getReporters())) {
foreach ($task->getReporters() as $reporter) {
$this->reportBug($bug->getId(), $reporter->getName());
}
}
if ($task->getTakeScreenshots()) {
$this->captureScreenshots($bug->getId());
}
}
if ($task->getTakeScreenshots()) {
$this->messageBus->dispatch(new CaptureScreenshotsMessage($bug->getId()));
}
}
}
Loading

0 comments on commit 8bc146b

Please sign in to comment.