Skip to content

Commit

Permalink
Merge 89d0bf0 into 31bdf06
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Apr 11, 2022
2 parents 31bdf06 + 89d0bf0 commit f664358
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/Entity/Bug.php
Expand Up @@ -60,6 +60,11 @@ class Bug extends BugModel
*/
protected bool $closed = false;

/**
* @ORM\Column(type="boolean")
*/
protected bool $recording = false;

/**
* @ORM\Column(name="created_at", type="datetime")
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Model/Bug.php
Expand Up @@ -14,6 +14,7 @@ class Bug implements BugInterface
protected string $message;
protected ProgressInterface $progress;
protected bool $closed = false;
protected bool $recording = false;
protected DateTimeInterface $updatedAt;
protected DateTimeInterface $createdAt;

Expand Down Expand Up @@ -100,6 +101,16 @@ public function setClosed(bool $closed): void
$this->closed = $closed;
}

public function isRecording(): bool
{
return $this->recording;
}

public function setRecording(bool $recording): void
{
$this->recording = $recording;
}

public function setCreatedAt(DateTimeInterface $createdAt): void
{
$this->createdAt = $createdAt;
Expand Down
4 changes: 4 additions & 0 deletions src/Model/BugInterface.php
Expand Up @@ -40,6 +40,10 @@ public function isClosed(): bool;

public function setClosed(bool $closed): void;

public function isRecording(): bool;

public function setRecording(bool $recording): void;

public function setCreatedAt(DateTimeInterface $createdAt): void;

public function getCreatedAt(): DateTimeInterface;
Expand Down
14 changes: 14 additions & 0 deletions src/Repository/BugRepository.php
Expand Up @@ -51,4 +51,18 @@ public function increaseTotal(BugInterface $bug, int $total): void
$bug->getProgress()->setTotal($bug->getProgress()->getTotal() + $total);
});
}

public function startRecording(BugInterface $bug): void
{
$bug->setRecording(true);
$this->getEntityManager()->flush();
}

public function stopRecording(BugInterface $bug): void
{
$bug->setRecording(false);
// Recording bug may take long time. Reconnect to flush changes.
$this->getEntityManager()->getConnection()->connect();
$this->getEntityManager()->flush();
}
}
4 changes: 4 additions & 0 deletions src/Repository/BugRepositoryInterface.php
Expand Up @@ -12,4 +12,8 @@ public function updateSteps(BugInterface $bug, array $newSteps): void;
public function increaseProcessed(BugInterface $bug, int $processed = 1): void;

public function increaseTotal(BugInterface $bug, int $total): void;

public function startRecording(BugInterface $bug): void;

public function stopRecording(BugInterface $bug): void;
}
10 changes: 10 additions & 0 deletions src/Service/Bug/BugHelper.php
Expand Up @@ -2,6 +2,7 @@

namespace Tienvx\Bundle\MbtBundle\Service\Bug;

use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException;
use Symfony\Component\Messenger\MessageBusInterface;
use Tienvx\Bundle\MbtBundle\Entity\Bug;
use Tienvx\Bundle\MbtBundle\Exception\ExceptionInterface;
Expand Down Expand Up @@ -85,7 +86,16 @@ public function reportBug(int $bugId): void
public function recordVideo(int $bugId): void
{
$bug = $this->getBug($bugId, 'record video for bug');

if ($bug->isRecording()) {
throw new RecoverableMessageHandlingException(
sprintf('Can not record video for bug %d: bug is recording. Will retry later', $bug->getId())
);
}

$this->bugRepository->startRecording($bug);
$this->stepsRunner->run($bug->getSteps(), $bug, true);
$this->bugRepository->stopRecording($bug);
}

public function createBug(array $steps, string $message): BugInterface
Expand Down
6 changes: 4 additions & 2 deletions src/Service/Task/TaskHelper.php
Expand Up @@ -2,8 +2,8 @@

namespace Tienvx\Bundle\MbtBundle\Service\Task;

use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException;
use Throwable;
use Tienvx\Bundle\MbtBundle\Exception\RuntimeException;
use Tienvx\Bundle\MbtBundle\Exception\UnexpectedValueException;
use Tienvx\Bundle\MbtBundle\Generator\GeneratorManagerInterface;
use Tienvx\Bundle\MbtBundle\Model\Bug\StepInterface;
Expand Down Expand Up @@ -44,7 +44,9 @@ public function run(int $taskId): void
}

if ($task->isRunning()) {
throw new RuntimeException(sprintf('Can not run task %d: task is already running', $task->getId()));
throw new RecoverableMessageHandlingException(
sprintf('Can not run task %d: task is running. Will retry later', $task->getId())
);
}

$this->taskRepository->startRunning($task);
Expand Down
2 changes: 2 additions & 0 deletions tests/Model/BugTest.php
Expand Up @@ -45,6 +45,7 @@ protected function setUp(): void
$this->bug->setMessage('bug message');
$this->bug->setProgress($this->progress);
$this->bug->setClosed(true);
$this->bug->setRecording(true);
}

public function testProperties(): void
Expand All @@ -56,6 +57,7 @@ public function testProperties(): void
$this->assertSame('bug message', $this->bug->getMessage());
$this->assertSame($this->progress, $this->bug->getProgress());
$this->assertSame(true, $this->bug->isClosed());
$this->assertSame(true, $this->bug->isRecording());
}

protected function createBug(): BugInterface
Expand Down
20 changes: 20 additions & 0 deletions tests/Repository/BugRepositoryTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Tienvx\Bundle\MbtBundle\Tests\Repository;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Decorator\EntityManagerDecorator;
use Doctrine\ORM\Mapping\ClassMetadata;
Expand Down Expand Up @@ -150,4 +151,23 @@ public function testIncreaseTotal(): void
$this->assertSame(5, $this->bug->getProgress()->getProcessed());
$this->assertSame(13, $this->bug->getProgress()->getTotal());
}

public function testStartRecordingBug(): void
{
$this->bug->setRecording(false);
$this->manager->expects($this->once())->method('flush');
$this->bugRepository->startRecording($this->bug);
$this->assertTrue($this->bug->isRecording());
}

public function testStopRecordingBug(): void
{
$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('connect');
$this->bug->setRecording(true);
$this->manager->expects($this->once())->method('flush');
$this->manager->expects($this->once())->method('getConnection')->willReturn($connection);
$this->bugRepository->stopRecording($this->bug);
$this->assertFalse($this->bug->isRecording());
}
}
12 changes: 12 additions & 0 deletions tests/Service/Bug/BugHelperTest.php
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException;
use Symfony\Component\Messenger\MessageBusInterface;
use Tienvx\Bundle\MbtBundle\Entity\Bug;
use Tienvx\Bundle\MbtBundle\Entity\Progress;
Expand Down Expand Up @@ -232,13 +233,24 @@ public function testRecordVideoMissingBug(): void
$this->helper->recordVideo(123);
}

public function testRunTaskAlreadyRunning(): void
{
$this->expectException(RecoverableMessageHandlingException::class);
$this->expectExceptionMessage('Can not record video for bug 123: bug is recording. Will retry later');
$this->bug->setRecording(true);
$this->bugRepository->expects($this->once())->method('find')->with(123)->willReturn($this->bug);
$this->helper->recordVideo(123);
}

public function testRecordVideo(): void
{
$this->stepsRunner
->expects($this->once())
->method('run')
->with($this->bug->getSteps(), $this->bug, true);
$this->bugRepository->expects($this->once())->method('find')->with(123)->willReturn($this->bug);
$this->bugRepository->expects($this->once())->method('startRecording')->with($this->bug);
$this->bugRepository->expects($this->once())->method('stopRecording')->with($this->bug);
$this->helper->recordVideo(123);
}
}
6 changes: 3 additions & 3 deletions tests/Service/Task/TaskHelperTest.php
Expand Up @@ -5,10 +5,10 @@
use Exception;
use PHPUnit\Framework\TestCase;
use SingleColorPetrinet\Model\Color;
use Symfony\Component\Messenger\Exception\RecoverableMessageHandlingException;
use Throwable;
use Tienvx\Bundle\MbtBundle\Entity\Bug;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Exception\RuntimeException;
use Tienvx\Bundle\MbtBundle\Exception\UnexpectedValueException;
use Tienvx\Bundle\MbtBundle\Generator\GeneratorInterface;
use Tienvx\Bundle\MbtBundle\Generator\GeneratorManagerInterface;
Expand Down Expand Up @@ -82,8 +82,8 @@ public function testRunNoTask(): void

public function testRunTaskAlreadyRunning(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Can not run task 123: task is already running');
$this->expectException(RecoverableMessageHandlingException::class);
$this->expectExceptionMessage('Can not run task 123: task is running. Will retry later');
$this->task->setRunning(true);
$this->taskRepository->expects($this->once())->method('find')->with(123)->willReturn($this->task);
$this->taskHelper->run(123);
Expand Down

0 comments on commit f664358

Please sign in to comment.