Skip to content

Commit

Permalink
Version15
Browse files Browse the repository at this point in the history
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 20, 2015
1 parent 2712efb commit 83cbbd8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
11 changes: 11 additions & 0 deletions features/navigating-house.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 2 additions & 1 deletion spec/Jenko/House/HouseSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
31 changes: 16 additions & 15 deletions src/Jenko/House/House.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class House
*/
private $currentLocation;

/**
* @var Location
*/
private $previousLocation;

/**
* @param array $locations
*/
Expand Down Expand Up @@ -49,6 +54,14 @@ public function whereAmI()
return $this->currentLocation;
}

/**
* @return Location
*/
public function whereWasI()
{
return $this->previousLocation;
}

/**
* @param Room|string $room
* @throws LocationDoesNotExistException
Expand All @@ -63,6 +76,7 @@ public function enterRoom($room)
throw new LocationDoesNotExistException;
}

$this->previousLocation = $this->currentLocation;
$this->currentLocation = $room;
}

Expand Down Expand Up @@ -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);
}
}
10 changes: 9 additions & 1 deletion tests/contexts/HomeOwnerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function iShouldHaveDimensionsAndExits()
public function iLeaveThroughTheFrontDoor()
{
$frontGarden = Garden::named('front garden');
$this->house->exitRoom($frontGarden);
$this->house->exitToRoom($frontGarden);
}

/**
Expand Down Expand Up @@ -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.