Skip to content

Commit

Permalink
Prevent updating steps after recording
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Apr 18, 2022
1 parent 554ee9f commit e8efdee
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/Repository/BugRepository.php
Expand Up @@ -54,15 +54,28 @@ public function increaseTotal(BugInterface $bug, int $total): void

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

public function stopRecording(BugInterface $bug): void
{
// Recording bug may take long time. Reconnect to flush changes.
$this->getEntityManager()->getConnection()->connect();
// Refresh so we don't update other fields while recording.
$this->getEntityManager()->refresh($bug);
$bug->getVideo()->setRecording(false);
$this->getEntityManager()->flush();
}

public function updateVideoErrorMessage(BugInterface $bug, ?string $errorMessage): void
{
// Recording bug may take long time. Reconnect to flush changes.
$this->getEntityManager()->getConnection()->connect();
// Refresh so we don't update other fields while recording.
$this->getEntityManager()->refresh($bug);
$bug->getVideo()->setErrorMessage($errorMessage);
$this->getEntityManager()->flush();
}
}
2 changes: 2 additions & 0 deletions src/Repository/BugRepositoryInterface.php
Expand Up @@ -16,4 +16,6 @@ public function increaseTotal(BugInterface $bug, int $total): void;
public function startRecording(BugInterface $bug): void;

public function stopRecording(BugInterface $bug): void;

public function updateVideoErrorMessage(BugInterface $bug, ?string $errorMessage): void;
}
3 changes: 2 additions & 1 deletion src/Service/Bug/BugHelper.php
Expand Up @@ -96,7 +96,8 @@ public function recordVideo(int $bugId): void
$this->bugRepository->startRecording($bug);
$bug->setDebug(true);
$this->stepsRunner->run($bug->getSteps(), $bug, function (Throwable $throwable) use ($bug) {
$bug->getVideo()->setErrorMessage(
$this->bugRepository->updateVideoErrorMessage(
$bug,
$throwable->getMessage() !== $bug->getMessage() ? $throwable->getMessage() : null
);
});
Expand Down
20 changes: 17 additions & 3 deletions tests/Repository/BugRepositoryTest.php
Expand Up @@ -28,6 +28,7 @@ class BugRepositoryTest extends TestCase
protected EntityManagerDecorator $manager;
protected BugInterface $bug;
protected BugRepositoryInterface $bugRepository;
protected Connection $connection;

protected function setUp(): void
{
Expand All @@ -54,6 +55,7 @@ protected function setUp(): void
$this->createMock(StepInterface::class),
$this->createMock(StepInterface::class),
]);
$this->connection = $this->createMock(Connection::class);
}

public function testUpdateSteps(): void
Expand Down Expand Up @@ -156,19 +158,31 @@ public function testIncreaseTotal(): void
public function testStartRecordingBug(): void
{
$this->bug->getVideo()->setRecording(false);
$this->manager->expects($this->once())->method('refresh')->with($this->bug);
$this->manager->expects($this->once())->method('flush');
$this->bugRepository->startRecording($this->bug);
$this->assertTrue($this->bug->getVideo()->isRecording());
}

public function testStopRecordingBug(): void
{
$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('connect');
$this->connection->expects($this->once())->method('connect');
$this->bug->getVideo()->setRecording(true);
$this->manager->expects($this->once())->method('refresh')->with($this->bug);
$this->manager->expects($this->once())->method('flush');
$this->manager->expects($this->once())->method('getConnection')->willReturn($connection);
$this->manager->expects($this->once())->method('getConnection')->willReturn($this->connection);
$this->bugRepository->stopRecording($this->bug);
$this->assertFalse($this->bug->getVideo()->isRecording());
}

public function testUpdateVideoErrorMessage(): void
{
$this->connection->expects($this->once())->method('connect');
$this->bug->getVideo()->setErrorMessage(null);
$this->manager->expects($this->once())->method('refresh')->with($this->bug);
$this->manager->expects($this->once())->method('flush');
$this->manager->expects($this->once())->method('getConnection')->willReturn($this->connection);
$this->bugRepository->updateVideoErrorMessage($this->bug, 'New error');
$this->assertSame('New error', $this->bug->getVideo()->getErrorMessage());
}
}
16 changes: 11 additions & 5 deletions tests/Service/Bug/BugHelperTest.php
Expand Up @@ -256,13 +256,19 @@ public function testRecordVideo(?Throwable $exception): void
$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);
$this->assertTrue($this->bug->isDebug());
if ($exception && $exception->getMessage() !== $this->bug->getMessage()) {
$this->assertSame($exception->getMessage(), $this->bug->getVideo()->getErrorMessage());
if ($exception) {
$this->bugRepository
->expects($this->once())
->method('updateVideoErrorMessage')
->with(
$this->bug,
$exception->getMessage() !== $this->bug->getMessage() ? $exception->getMessage() : null
);
} else {
$this->assertNull($this->bug->getVideo()->getErrorMessage());
$this->bugRepository->expects($this->never())->method('updateVideoErrorMessage');
}
$this->helper->recordVideo(123);
$this->assertTrue($this->bug->isDebug());
}

public function exceptionProvider(): array
Expand Down

0 comments on commit e8efdee

Please sign in to comment.