Skip to content

Commit

Permalink
Version11
Browse files Browse the repository at this point in the history
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 20, 2015
1 parent 56319d0 commit a81431f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
10 changes: 8 additions & 2 deletions spec/Jenko/House/HouseSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
8 changes: 8 additions & 0 deletions src/Jenko/House/Exception/LocationDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Jenko\House\Exception;

class LocationDoesNotExistException extends \Exception
{

}
47 changes: 45 additions & 2 deletions src/Jenko/House/House.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Jenko\House;

use Jenko\House\Exception\LocationDoesNotExistException;

final class House
{
/**
Expand Down Expand Up @@ -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;
}
}
9 changes: 5 additions & 4 deletions tests/contexts/HomeOwnerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down Expand Up @@ -118,8 +119,7 @@ public function iShouldBeAbleToEnterTheRoom($roomName)
*/
public function iEnterTheRoom($roomName)
{
$room = Room::named($roomName);
$this->house->enterRoom($room);
$this->house->enterRoom($roomName);
}

/**
Expand All @@ -129,7 +129,8 @@ public function iShouldNotBeAbleToEnterTheRoom($roomName)
{
try {
$this->iEnterTheRoom($roomName);
} catch (RoomDoesNotExistException $e) {
throw new \RuntimeException();
} catch (LocationDoesNotExistException $e) {
return true;
}
}
Expand Down

0 comments on commit a81431f

Please sign in to comment.