Skip to content

Commit

Permalink
Merge pull request #625 from tienvx/do-nothing-if-steps-not-connected…
Browse files Browse the repository at this point in the history
…-during-reducing-steps

Do nothing if steps not connected during reducing steps
  • Loading branch information
tienvx committed May 6, 2022
2 parents f83e41d + 187063e commit 918b594
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Exception;

class LogicException extends \LogicException implements ExceptionInterface
{
}
7 changes: 7 additions & 0 deletions src/Exception/StepsNotConnectedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Exception;

class StepsNotConnectedException extends LogicException
{
}
8 changes: 7 additions & 1 deletion src/Reducer/HandlerTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\Messenger\MessageBusInterface;
use Throwable;
use Tienvx\Bundle\MbtBundle\Exception\StepsNotConnectedException;
use Tienvx\Bundle\MbtBundle\Message\ReduceBugMessage;
use Tienvx\Bundle\MbtBundle\Model\BugInterface;
use Tienvx\Bundle\MbtBundle\Repository\BugRepositoryInterface;
Expand Down Expand Up @@ -31,7 +32,12 @@ public function __construct(

public function handle(BugInterface $bug, int $from, int $to): void
{
$newSteps = iterator_to_array($this->stepsBuilder->create($bug, $from, $to));
try {
$newSteps = iterator_to_array($this->stepsBuilder->create($bug, $from, $to));
} catch (StepsNotConnectedException $exception) {
return;
}

if (count($newSteps) >= count($bug->getSteps())) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Step/Builder/ShortestPathStepsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use JMGQ\AStar\AStar;
use SingleColorPetrinet\Model\PetrinetInterface;
use SingleColorPetrinet\Service\GuardedTransitionServiceInterface;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Tienvx\Bundle\MbtBundle\Exception\ExceptionInterface;
use Tienvx\Bundle\MbtBundle\Exception\OutOfRangeException;
use Tienvx\Bundle\MbtBundle\Exception\StepsNotConnectedException;
use Tienvx\Bundle\MbtBundle\Model\Bug\Step;
use Tienvx\Bundle\MbtBundle\Model\Bug\StepInterface;
use Tienvx\Bundle\MbtBundle\Model\BugInterface;
Expand Down Expand Up @@ -69,7 +69,7 @@ protected function getRemainingSteps(array $steps, StepInterface $lastStep, Petr
}
$transition = $petrinet->getTransitionById($step->getTransition());
if (!$this->transitionService->isEnabled($transition, $marking)) {
throw new UnrecoverableMessageHandlingException('Can not connect remaining steps');
throw new StepsNotConnectedException('Can not connect remaining steps');
}
$this->transitionService->fire($transition, $marking);
yield new Step(
Expand Down
20 changes: 20 additions & 0 deletions tests/Reducer/HandlerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tienvx\Bundle\MbtBundle\Tests\Reducer;

use Exception;
use PHPUnit\Framework\MockObject\Stub\Stub;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -11,6 +12,7 @@
use Tienvx\Bundle\MbtBundle\Entity\Model\Revision;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Exception\RuntimeException;
use Tienvx\Bundle\MbtBundle\Exception\StepsNotConnectedException;
use Tienvx\Bundle\MbtBundle\Message\ReduceBugMessage;
use Tienvx\Bundle\MbtBundle\Model\Bug\StepInterface;
use Tienvx\Bundle\MbtBundle\Model\BugInterface;
Expand Down Expand Up @@ -64,13 +66,21 @@ protected function setUp(): void
->willReturn((fn () => yield from $this->newSteps)());
}

public function testHandleNotConnectedSteps(): void
{
$this->expectStepsBuilder($this->throwException(new StepsNotConnectedException()));
$this->stepsRunner->expects($this->never())->method('run');
$this->handler->handle($this->bug, 1, 2);
}

public function testHandleOldBug(): void
{
$this->bug->setSteps([
$this->createMock(StepInterface::class),
$this->createMock(StepInterface::class),
$this->createMock(StepInterface::class),
]);
$this->expectStepsBuilder($this->returnValue((fn () => yield from $this->newSteps)()));
$this->stepsRunner->expects($this->never())->method('run');
$this->handler->handle($this->bug, 1, 2);
}
Expand All @@ -80,6 +90,7 @@ public function testHandleOldBug(): void
*/
public function testHandle(?Throwable $exception, bool $updateSteps): void
{
$this->expectStepsBuilder($this->returnValue((fn () => yield from $this->newSteps)()));
$this->stepsRunner->expects($this->once())
->method('run')
->with(
Expand Down Expand Up @@ -119,4 +130,13 @@ public function exceptionProvider(): array
[new Exception('Something wrong'), true],
];
}

protected function expectStepsBuilder(Stub $will): void
{
$this->stepsBuilder
->expects($this->once())
->method('create')
->with($this->bug, 1, 2)
->will($will);
}
}
4 changes: 2 additions & 2 deletions tests/Service/Step/Builder/ShortestPathStepsBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
use SingleColorPetrinet\Model\PetrinetInterface;
use SingleColorPetrinet\Service\GuardedTransitionService;
use SingleColorPetrinet\Service\GuardedTransitionServiceInterface;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
use Tienvx\Bundle\MbtBundle\Entity\Bug;
use Tienvx\Bundle\MbtBundle\Entity\Model\Revision;
use Tienvx\Bundle\MbtBundle\Entity\Task;
use Tienvx\Bundle\MbtBundle\Exception\OutOfRangeException;
use Tienvx\Bundle\MbtBundle\Exception\StepsNotConnectedException;
use Tienvx\Bundle\MbtBundle\Model\Bug\Step;
use Tienvx\Bundle\MbtBundle\Service\Petrinet\MarkingHelper;
use Tienvx\Bundle\MbtBundle\Service\Petrinet\MarkingHelperInterface;
Expand Down Expand Up @@ -294,7 +294,7 @@ public function testGetShortestPathBetweenSameSteps(): void
public function testGetShortestPathFromCartEmptyToFourProductsInCart(): void
{
$this->expectsPetrinetHelper();
$this->expectExceptionObject(new UnrecoverableMessageHandlingException('Can not connect remaining steps'));
$this->expectExceptionObject(new StepsNotConnectedException('Can not connect remaining steps'));
iterator_to_array($this->stepsBuilder->create($this->bug, 0, 6));
}

Expand Down

0 comments on commit 918b594

Please sign in to comment.