Permalink
Browse files

Version11

What happens if we try and enter a room which doesn't exist? Let's add
an exception for this.

It's important to note that at all points between versions we are
running `bin/behat` and `bin/phpspec run` constantly and fixing any
errors or discrepencies that may arise and using this whole process to
shape our domain models.
  • Loading branch information...
jenkoian committed Jan 19, 2015
1 parent 56319d0 commit a81431f281b154644c0f899afb64ace67b74d50d
@@ -59,6 +59,12 @@ function it_should_allow_entering_rooms()
function it_should_allow_exiting_rooms()
{
$this->enterRoom('hallway');
$this->exitRoom('garden');
$this->exitRoom('front garden');
}
}
function it_should_throw_exception_if_attempting_to_enter_invalid_room()
{
$madeUpRoom = Room::named('made up room');
$this->shouldThrow('\Jenko\House\Exception\LocationDoesNotExistException')->during('enterRoom', [$madeUpRoom]);
}
}
@@ -0,0 +1,8 @@
<?php
namespace Jenko\House\Exception;
class LocationDoesNotExistException extends \Exception
{
}
View
@@ -2,6 +2,8 @@
namespace Jenko\House;
use Jenko\House\Exception\LocationDoesNotExistException;
final class House
{
/**
@@ -60,29 +62,70 @@ public function whereAmI()
/**
* @param Room|string $room
* @throws LocationDoesNotExistException
*/
public function enterRoom($room)
{
if (!$room instanceof Room && is_string($room)) {
$room = Room::named($room);
$room = $this->findLocationFromName($room);
}
if (!$this->containsLocation($room)) {
throw new LocationDoesNotExistException;
}
$this->currentLocation = $room;
}
/**
* @param string $roomName
* @return Location|null
* @throws LocationDoesNotExistException
*/
private function findLocationFromName($roomName)
{
foreach ($this->getLocations() as $location) {
if ($location->getName() === $roomName) {
return $location;
}
}
throw new LocationDoesNotExistException;
}
/**
* @param Location $location
* @return bool
*/
private function containsLocation(Location $location)
{
foreach ($this->getLocations() as $existingLocation) {
if ($location == $existingLocation) {
return true;
}
}
return false;
}
/**
* @param Location|string $room
* @throws LocationDoesNotExistException
*/
public function exitRoom($room)
{
if (!$room instanceof Location && is_string($room)) {
if (false !== strpos('garden', $room)) {
if (false !== strpos($room, 'garden')) {
$room = Garden::named($room);
} else{
$room = Room::named($room);
}
}
if (!$this->containsLocation($room)) {
throw new LocationDoesNotExistException;
}
$this->currentLocation = $room;
}
}
@@ -7,6 +7,7 @@
use Jenko\House\Garden;
use Jenko\House\House;
use Jenko\House\Room;
use Jenko\House\Exception\LocationDoesNotExistException;
class HomeOwnerContext implements Context, SnippetAcceptingContext
{
@@ -28,7 +29,7 @@ public function __construct()
*/
public function iAmInThe($location)
{
if (false !== strpos('garden', $location)) {
if (false !== strpos($location, 'garden')) {
$location = Room::named($location);
} else {
$location = Garden::named($location);
@@ -118,8 +119,7 @@ public function iShouldBeAbleToEnterTheRoom($roomName)
*/
public function iEnterTheRoom($roomName)
{
$room = Room::named($roomName);
$this->house->enterRoom($room);
$this->house->enterRoom($roomName);
}
/**
@@ -129,7 +129,8 @@ public function iShouldNotBeAbleToEnterTheRoom($roomName)
{
try {
$this->iEnterTheRoom($roomName);
} catch (RoomDoesNotExistException $e) {
throw new \RuntimeException();
} catch (LocationDoesNotExistException $e) {
return true;
}
}

0 comments on commit a81431f

Please sign in to comment.