Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do nothing if steps not connected during reducing steps #625

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Exception/LogicException.php
@@ -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
@@ -0,0 +1,7 @@
<?php

namespace Tienvx\Bundle\MbtBundle\Exception;

class StepsNotConnectedException extends LogicException
{
}
8 changes: 7 additions & 1 deletion src/Reducer/HandlerTemplate.php
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
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
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
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