Permalink
Browse files

Version15

One thing that was bothering me was how to exit a room. I didn't want to
have circular references for example. So here I have a property of the
previous location so we know where we've come from and a way of exiting
to a room (notice we've tidied up that duplicated code from earlier).
  • Loading branch information...
jenkoian committed Jan 19, 2015
1 parent 2712efb commit 83cbbd8651af8514211d0c6198b9512e10cb5211
@@ -36,3 +36,14 @@ Feature: Home owner navigating the house
Scenario: Entering a room that doesn't exist
Given I am in the "hallway"
Then I should not be able to enter the "made up" room
Scenario: Entering a new room holds information of your previous location
Given there are the following locations in the house
| name | type | width | height |
| front garden | garden | 300 | 300 |
| hallway | room | 300 | 300 |
| living room | room | 300 | 300 |
| kitchen | room | 300 | 300 |
And I am in the "hallway"
When I enter the "living room" room
Then I should know that I came from the "hallway"
@@ -46,12 +46,13 @@ function it_should_allow_entering_rooms()
$this->enterRoom('living room');
$this->whereAmI()->getName()->shouldBe('living room');
$this->whereWasI()->getName()->shouldBe('kitchen');
}
function it_should_allow_exiting_rooms()
{
$this->enterRoom('hallway');
$this->exitRoom('front garden');
$this->exitToRoom('front garden');
}
function it_should_throw_exception_if_attempting_to_enter_invalid_room()
View
@@ -16,6 +16,11 @@
*/
private $currentLocation;
/**
* @var Location
*/
private $previousLocation;
/**
* @param array $locations
*/
@@ -49,6 +54,14 @@ public function whereAmI()
return $this->currentLocation;
}
/**
* @return Location
*/
public function whereWasI()
{
return $this->previousLocation;
}
/**
* @param Room|string $room
* @throws LocationDoesNotExistException
@@ -63,6 +76,7 @@ public function enterRoom($room)
throw new LocationDoesNotExistException;
}
$this->previousLocation = $this->currentLocation;
$this->currentLocation = $room;
}
@@ -99,22 +113,9 @@ private function containsLocation(Location $location)
/**
* @param Location|string $room
* @throws LocationDoesNotExistException
*/
public function exitRoom($room)
public function exitToRoom($room)
{
if (!$room instanceof Location && is_string($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;
$this->enterRoom($room);
}
}
@@ -74,7 +74,7 @@ public function iShouldHaveDimensionsAndExits()
public function iLeaveThroughTheFrontDoor()
{
$frontGarden = Garden::named('front garden');
$this->house->exitRoom($frontGarden);
$this->house->exitToRoom($frontGarden);
}
/**
@@ -149,4 +149,12 @@ public function iShouldHaveExits()
$information = $this->house->whereAmI()->getInformation();
PHPUnit_Framework_Assert::assertArrayHasKey('exits', $information);
}
/**
* @Then I should know that I came from the :roomName
*/
public function iShouldKnowThatICameFromThe($roomName)
{
PHPUnit_Framework_Assert::assertEquals($this->house->whereWasI()->getName(), $roomName);
}
}

0 comments on commit 83cbbd8

Please sign in to comment.