Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from walterdolce/feature/throw-exception-if-re…
Browse files Browse the repository at this point in the history
…quested-state-is-missing

Throw exception when asked for missing state
  • Loading branch information
vincentchalamon authored Nov 15, 2016
2 parents eb2cf39 + a98f383 commit 73064b6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/ScenarioState.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Gorghoa\ScenarioStateBehatExtension;

use Gorghoa\ScenarioStateBehatExtension\ScenarioState\Exception\MissingStateException;

/**
* @author Rodrigue Villetard <rodrigue.villetard@gmail.com>
*/
Expand All @@ -26,14 +28,18 @@ class ScenarioState implements ScenarioStateInterface
*/
public function hasStateFragment($key)
{
return isset($this->store[$key]);
return array_key_exists($key, $this->store);
}

/**
* {@inheritdoc}
*/
public function getStateFragment($key)
{
if (!$this->hasStateFragment($key)) {
throw new MissingStateException("Missing {$key} state fragment was requested from store.");
}

return $this->store[$key];
}

Expand Down
17 changes: 17 additions & 0 deletions src/ScenarioState/Exception/MissingStateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/*
* This file is part of the ScenarioStateBehatExtension project.
*
* (c) Rodrigue Villetard <rodrigue.villetard@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Gorghoa\ScenarioStateBehatExtension\ScenarioState\Exception;

/**
* @author Walter Dolce <walterdolce@gmail.com>
*/
class MissingStateException extends \LogicException
{
}
3 changes: 2 additions & 1 deletion src/ScenarioStateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

namespace Gorghoa\ScenarioStateBehatExtension;
use Gorghoa\ScenarioStateBehatExtension\ScenarioState\Exception\MissingStateException;

/**
* @author Rodrigue Villetard <rodrigue.villetard@gmail.com>
Expand All @@ -24,7 +25,7 @@ public function provideStateFragment($key, $value);

/**
* @param string $key
*
* @throws MissingStateException
* @return mixed
*/
public function getStateFragment($key);
Expand Down
24 changes: 24 additions & 0 deletions tests/ScenarioStateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the ScenarioStateBehatExtension project.
*
* (c) Rodrigue Villetard <rodrigue.villetard@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Gorghoa\ScenarioStateBehatExtension;

use Gorghoa\ScenarioStateBehatExtension\ScenarioState\Exception\MissingStateException;

/**
* @author Walter Dolce <walterdolce@gmail.com>
*/
class ScenarioStateTest extends \PHPUnit_Framework_TestCase
{
public function testItThrowsExceptionWhenStateIsMissing()
{
$this->setExpectedException(MissingStateException::class);
(new ScenarioState())->getStateFragment('not_existing_state');
}
}

0 comments on commit 73064b6

Please sign in to comment.