Skip to content

Commit

Permalink
Merge 1adae72 into 1490ff3
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Apr 12, 2022
2 parents 1490ff3 + 1adae72 commit 646b83e
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 22 deletions.
7 changes: 5 additions & 2 deletions src/Entity/Bug.php
Expand Up @@ -6,7 +6,9 @@
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Tienvx\Bundle\MbtBundle\Entity\Bug\Video;
use Tienvx\Bundle\MbtBundle\Model\Bug as BugModel;
use Tienvx\Bundle\MbtBundle\Model\Bug\VideoInterface;
use Tienvx\Bundle\MbtBundle\Model\ProgressInterface;
use Tienvx\Bundle\MbtBundle\Model\TaskInterface;
use Tienvx\Bundle\MbtBundle\Repository\BugRepository;
Expand Down Expand Up @@ -61,9 +63,9 @@ class Bug extends BugModel
protected bool $closed = false;

/**
* @ORM\Column(type="boolean")
* @ORM\Embedded(class="\Tienvx\Bundle\MbtBundle\Entity\Bug\Video")
*/
protected bool $recording = false;
protected VideoInterface $video;

/**
* @ORM\Column(name="created_at", type="datetime")
Expand All @@ -79,6 +81,7 @@ public function __construct()
{
parent::__construct();
$this->progress = new Progress();
$this->video = new Video();
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/Entity/Bug/Video.php
@@ -0,0 +1,23 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Entity\Bug;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Embeddable;
use Tienvx\Bundle\MbtBundle\Model\Bug\Video as VideoModel;

/**
* @Embeddable
*/
class Video extends VideoModel
{
/**
* @ORM\Column(type="boolean")
*/
protected bool $recording = false;

/**
* @ORM\Column(type="text", nullable=true)
*/
protected ?string $errorMessage = null;
}
13 changes: 8 additions & 5 deletions src/Model/Bug.php
Expand Up @@ -4,6 +4,8 @@

use DateTimeInterface;
use Tienvx\Bundle\MbtBundle\Model\Bug\StepInterface;
use Tienvx\Bundle\MbtBundle\Model\Bug\Video;
use Tienvx\Bundle\MbtBundle\Model\Bug\VideoInterface;

class Bug implements BugInterface
{
Expand All @@ -14,13 +16,14 @@ class Bug implements BugInterface
protected string $message;
protected ProgressInterface $progress;
protected bool $closed = false;
protected bool $recording = false;
protected VideoInterface $video;
protected DateTimeInterface $updatedAt;
protected DateTimeInterface $createdAt;

public function __construct()
{
$this->progress = new Progress();
$this->video = new Video();
}

public function setId(int $id)
Expand Down Expand Up @@ -101,14 +104,14 @@ public function setClosed(bool $closed): void
$this->closed = $closed;
}

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

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

public function setCreatedAt(DateTimeInterface $createdAt): void
Expand Down
29 changes: 29 additions & 0 deletions src/Model/Bug/Video.php
@@ -0,0 +1,29 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Model\Bug;

class Video implements VideoInterface
{
protected bool $recording = false;
protected ?string $errorMessage = null;

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

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

public function getErrorMessage(): ?string
{
return $this->errorMessage;
}

public function setErrorMessage(?string $errorMessage = null): void
{
$this->errorMessage = $errorMessage;
}
}
14 changes: 14 additions & 0 deletions src/Model/Bug/VideoInterface.php
@@ -0,0 +1,14 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Model\Bug;

interface VideoInterface
{
public function isRecording(): bool;

public function setRecording(bool $recording): void;

public function getErrorMessage(): ?string;

public function setErrorMessage(?string $errorMessage = null): void;
}
5 changes: 3 additions & 2 deletions src/Model/BugInterface.php
Expand Up @@ -4,6 +4,7 @@

use DateTimeInterface;
use Tienvx\Bundle\MbtBundle\Model\Bug\StepInterface;
use Tienvx\Bundle\MbtBundle\Model\Bug\VideoInterface;

interface BugInterface
{
Expand Down Expand Up @@ -40,9 +41,9 @@ public function isClosed(): bool;

public function setClosed(bool $closed): void;

public function isRecording(): bool;
public function getVideo(): VideoInterface;

public function setRecording(bool $recording): void;
public function setVideo(VideoInterface $video): void;

public function setCreatedAt(DateTimeInterface $createdAt): void;

Expand Down
10 changes: 8 additions & 2 deletions src/Repository/BugRepository.php
Expand Up @@ -54,15 +54,21 @@ public function increaseTotal(BugInterface $bug, int $total): void

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

public function stopRecording(BugInterface $bug): void
{
$bug->setRecording(false);
$bug->getVideo()->setRecording(false);
// Recording bug may take long time. Reconnect to flush changes.
$this->getEntityManager()->getConnection()->connect();
$this->getEntityManager()->flush();
}

public function updateVideoErrorMessage(BugInterface $bug, string $videoErrorMessage): void
{
$bug->getVideo()->setErrorMessage($videoErrorMessage);
$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 $videoErrorMessage): void;
}
7 changes: 5 additions & 2 deletions src/Service/Bug/BugHelper.php
Expand Up @@ -4,6 +4,7 @@

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

if ($bug->isRecording()) {
if ($bug->getVideo()->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->stepsRunner->run($bug->getSteps(), $bug, true, function (Throwable $throwable) use ($bug): void {
$this->bugRepository->updateVideoErrorMessage($bug, $throwable->getMessage());
});
$this->bugRepository->stopRecording($bug);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Entity/Bug/VideoTest.php
@@ -0,0 +1,20 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Tests\Entity\Bug;

use Tienvx\Bundle\MbtBundle\Entity\Bug\Video;
use Tienvx\Bundle\MbtBundle\Model\Bug\VideoInterface;
use Tienvx\Bundle\MbtBundle\Tests\Model\Bug\VideoTest as VideoModelTest;

/**
* @covers \Tienvx\Bundle\MbtBundle\Entity\Bug\Video
*
* @uses \Tienvx\Bundle\MbtBundle\Model\Bug\Video
*/
class VideoTest extends VideoModelTest
{
protected function createVideo(): VideoInterface
{
return new Video();
}
}
39 changes: 39 additions & 0 deletions tests/Model/Bug/VideoTest.php
@@ -0,0 +1,39 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Tests\Model\Bug;

use PHPUnit\Framework\TestCase;
use Tienvx\Bundle\MbtBundle\Model\Bug\Video;
use Tienvx\Bundle\MbtBundle\Model\Bug\VideoInterface;

/**
* @covers \Tienvx\Bundle\MbtBundle\Model\Bug\Video
*/
class VideoTest extends TestCase
{
protected VideoInterface $video;

protected function setUp(): void
{
$this->video = $this->createVideo();
}

public function testRecording(): void
{
$this->assertFalse($this->video->isRecording());
$this->video->setRecording(true);
$this->assertTrue($this->video->isRecording());
}

public function testErrorMessage(): void
{
$this->assertNull($this->video->getErrorMessage());
$this->video->setErrorMessage('Something wrong');
$this->assertSame('Something wrong', $this->video->getErrorMessage());
}

protected function createVideo(): VideoInterface
{
return new Video();
}
}
7 changes: 5 additions & 2 deletions tests/Model/BugTest.php
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use SingleColorPetrinet\Model\Color;
use Tienvx\Bundle\MbtBundle\Entity\Bug\Video;
use Tienvx\Bundle\MbtBundle\Entity\Progress;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Model\Bug;
Expand All @@ -28,6 +29,7 @@ class BugTest extends TestCase
protected array $steps;
protected TaskInterface $task;
protected ProgressInterface $progress;
protected Bug\VideoInterface $video;

protected function setUp(): void
{
Expand All @@ -37,15 +39,16 @@ protected function setUp(): void
];
$this->task = new Task();
$this->progress = new Progress();
$this->video = new Video();
$this->bug = $this->createBug();
$this->bug->setId(123);
$this->bug->setTitle('bug title');
$this->bug->setSteps($this->steps);
$this->bug->setTask($this->task);
$this->bug->setMessage('bug message');
$this->bug->setProgress($this->progress);
$this->bug->setVideo($this->video);
$this->bug->setClosed(true);
$this->bug->setRecording(true);
}

public function testProperties(): void
Expand All @@ -57,7 +60,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());
$this->assertSame($this->video, $this->bug->getVideo());
}

protected function createBug(): BugInterface
Expand Down
17 changes: 13 additions & 4 deletions tests/Repository/BugRepositoryTest.php
Expand Up @@ -21,6 +21,7 @@
* @uses \Tienvx\Bundle\MbtBundle\Entity\Bug
* @uses \Tienvx\Bundle\MbtBundle\Model\Bug
* @uses \Tienvx\Bundle\MbtBundle\Model\Progress
* @uses \Tienvx\Bundle\MbtBundle\Model\Bug\Video
*/
class BugRepositoryTest extends TestCase
{
Expand Down Expand Up @@ -154,20 +155,28 @@ public function testIncreaseTotal(): void

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

public function testStopRecordingBug(): void
{
$connection = $this->createMock(Connection::class);
$connection->expects($this->once())->method('connect');
$this->bug->setRecording(true);
$this->bug->getVideo()->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());
$this->assertFalse($this->bug->getVideo()->isRecording());
}

public function testUpdateVideoErrorMessage(): void
{
$this->assertNull($this->bug->getVideo()->getErrorMessage());
$this->manager->expects($this->once())->method('flush');
$this->bugRepository->updateVideoErrorMessage($this->bug, 'Something wrong');
$this->assertSame('Something wrong', $this->bug->getVideo()->getErrorMessage());
}
}

0 comments on commit 646b83e

Please sign in to comment.