From e8efdee1f7f5f34a78fda8c14708e163ccd263b0 Mon Sep 17 00:00:00 2001 From: tienvx Date: Mon, 18 Apr 2022 22:37:12 +0700 Subject: [PATCH] Prevent updating steps after recording --- src/Repository/BugRepository.php | 13 +++++++++++++ src/Repository/BugRepositoryInterface.php | 2 ++ src/Service/Bug/BugHelper.php | 3 ++- tests/Repository/BugRepositoryTest.php | 20 +++++++++++++++++--- tests/Service/Bug/BugHelperTest.php | 16 +++++++++++----- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/Repository/BugRepository.php b/src/Repository/BugRepository.php index 4acfb889..dee6d7ca 100644 --- a/src/Repository/BugRepository.php +++ b/src/Repository/BugRepository.php @@ -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(); } } diff --git a/src/Repository/BugRepositoryInterface.php b/src/Repository/BugRepositoryInterface.php index f834c7c8..6e074a55 100644 --- a/src/Repository/BugRepositoryInterface.php +++ b/src/Repository/BugRepositoryInterface.php @@ -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; } diff --git a/src/Service/Bug/BugHelper.php b/src/Service/Bug/BugHelper.php index 9dd6ed10..3d9250e3 100644 --- a/src/Service/Bug/BugHelper.php +++ b/src/Service/Bug/BugHelper.php @@ -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 ); }); diff --git a/tests/Repository/BugRepositoryTest.php b/tests/Repository/BugRepositoryTest.php index 32aa9766..413986e3 100644 --- a/tests/Repository/BugRepositoryTest.php +++ b/tests/Repository/BugRepositoryTest.php @@ -28,6 +28,7 @@ class BugRepositoryTest extends TestCase protected EntityManagerDecorator $manager; protected BugInterface $bug; protected BugRepositoryInterface $bugRepository; + protected Connection $connection; protected function setUp(): void { @@ -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 @@ -156,6 +158,7 @@ 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()); @@ -163,12 +166,23 @@ public function testStartRecordingBug(): void 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()); + } } diff --git a/tests/Service/Bug/BugHelperTest.php b/tests/Service/Bug/BugHelperTest.php index ffff182d..e3fe7c93 100644 --- a/tests/Service/Bug/BugHelperTest.php +++ b/tests/Service/Bug/BugHelperTest.php @@ -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