Skip to content

Commit

Permalink
Preventing infinite loops when a requested option does not exist. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmizzell committed Mar 24, 2021
1 parent 6b8da08 commit ade12ff
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,13 @@ private function buildReturn(string $objectClass, $mock, string $method, array $
} else {
$input = json_encode($myInputs);
}
$actualReturn = $return->return($input);

$return = $this->buildReturn($objectClass, $mock, $method, $inputs, $return->return($input));
if (!isset($actualReturn)) {
throw new \Exception("Option {$input} does not exist.");
}

$return = $this->buildReturn($objectClass, $mock, $method, $inputs, $actualReturn);
} elseif ($return instanceof \Exception) {
throw $return;
} elseif (is_string($return)) {
Expand Down
5 changes: 5 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace MockChain;

/**
* Class Options
* @package MockChain
* @todo Options for complex things, like objects.
*/
class Options
{

Expand Down
9 changes: 9 additions & 0 deletions test/ChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,13 @@ public function testUsingAdddIncorrectly() {
$this->expectExceptionMessage("You should use the add method before using addd.");
(new Chain($this))->addd("hello");
}

public function testNonExistentOption() {
$this->expectExceptionMessage('Option digestive does not exist');
$options = new Options();
$mock = (new Chain($this))
->add(Body::class, 'getSystem', $options)
->getMock();
$mock->getSystem("digestive");
}
}

0 comments on commit ade12ff

Please sign in to comment.