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

avoid infinite loop when all steps are skipped #355

Merged
merged 1 commit into from
May 7, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Exception/AllStepsSkippedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Craue\FormFlowBundle\Exception;

/**
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2020 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class AllStepsSkippedException extends \RuntimeException {

public function __construct() {
parent::__construct('All steps are marked as skipped. Please check the flow to make sure at least one step is not skipped.');
}

}
12 changes: 10 additions & 2 deletions Form/FormFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Craue\FormFlowBundle\Event\PostValidateEvent;
use Craue\FormFlowBundle\Event\PreBindEvent;
use Craue\FormFlowBundle\Event\PreviousStepInvalidEvent;
use Craue\FormFlowBundle\Exception\AllStepsSkippedException;
use Craue\FormFlowBundle\Exception\InvalidTypeException;
use Craue\FormFlowBundle\Storage\DataManagerInterface;
use Craue\FormFlowBundle\Util\StringUtil;
Expand Down Expand Up @@ -429,10 +430,11 @@ public function isStepSkipped($stepNumber) {
/**
* @param int $stepNumber Assumed step to which skipped steps shall be applied to.
* @param int $direction Either 1 (to skip forwards) or -1 (to skip backwards).
* @param int $boundsReached Internal counter to avoid endlessly bouncing back and forth.
* @return int Target step number with skipping applied.
* @throws \InvalidArgumentException If the value of <code>$direction</code> is invalid.
*/
protected function applySkipping($stepNumber, $direction = 1) {
protected function applySkipping($stepNumber, $direction = 1, $boundsReached = 0) {
if ($direction !== 1 && $direction !== -1) {
throw new \InvalidArgumentException(sprintf('Argument of either -1 or 1 expected, "%s" given.', $direction));
}
Expand All @@ -445,11 +447,17 @@ protected function applySkipping($stepNumber, $direction = 1) {
// change direction if outer bounds are reached
if ($direction === 1 && $stepNumber > $this->getStepCount()) {
$direction = -1;
++$boundsReached;
} elseif ($direction === -1 && $stepNumber < 1) {
$direction = 1;
++$boundsReached;
}

return $this->applySkipping($stepNumber, $direction);
if ($boundsReached > 2) {
throw new AllStepsSkippedException();
}

return $this->applySkipping($stepNumber, $direction, $boundsReached);
}

return $stepNumber;
Expand Down
23 changes: 23 additions & 0 deletions Tests/Form/FormFlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,29 @@ public function dataApplySkipping_invalidArguments() {
];
}

/**
* @expectedException Craue\FormFlowBundle\Exception\AllStepsSkippedException
* @expectedExceptionMessage All steps are marked as skipped. Please check the flow to make sure at least one step is not skipped.
*/
public function testApplySkipping_bouncing() {
$flow = $this->getFlowWithMockedMethods(['loadStepsConfig']);

$flow
->expects($this->once())
->method('loadStepsConfig')
->will($this->returnValue([
[
'skip' => true,
],
]))
;

$method = new \ReflectionMethod($flow, 'applySkipping');
$method->setAccessible(true);

$method->invoke($flow, 1);
}

/**
* @dataProvider dataGetStep_invalidArguments
* @expectedException \Craue\FormFlowBundle\Exception\InvalidTypeException
Expand Down