Skip to content

Commit

Permalink
Version10
Browse files Browse the repository at this point in the history
Here we add our first Value Object, Dimensions. Again, using PHPSpec as
the crutch to aid the design.
  • Loading branch information
jenkoian committed Jan 20, 2015
1 parent 1cd52fe commit 56319d0
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 2 deletions.
37 changes: 37 additions & 0 deletions spec/Jenko/House/DimensionsSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace spec\Jenko\House;

use Jenko\House\Dimensions;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class DimensionsSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedThrough('fromWidthAndHeight', [300, 300]);
}

function it_is_initializable()
{
$this->shouldHaveType('Jenko\House\Dimensions');
}

function it_should_be_created_with_a_width_and_height()
{
$garden = self::fromWidthAndHeight(300, 300);
$garden->shouldHaveType('Jenko\House\Dimensions');
$garden->getWidth()->shouldEqual(300);
$garden->getHeight()->shouldEqual(300);
}

function it_should_compare_width_and_height_for_equality()
{
$newDimensions = Dimensions::fromWidthAndHeight(600, 600);
$this->equals($newDimensions)->shouldEqual(false);

$newerDimensions = Dimensions::fromWidthAndHeight(300, 300);
$this->equals($newerDimensions)->shouldEqual(true);
}
}
9 changes: 9 additions & 0 deletions spec/Jenko/House/GardenSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\Jenko\House;

use Jenko\House\Dimensions;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

Expand All @@ -28,4 +29,12 @@ function it_should_give_information()
{
$this->getInformation()->shouldBeArray();
}

function it_should_be_able_to_set_dimensions()
{
$dimensions = Dimensions::fromWidthAndHeight(350, 300);
$this->setDimensions($dimensions);

$this->getDimensions()->shouldEqual($dimensions);
}
}
9 changes: 9 additions & 0 deletions spec/Jenko/House/RoomSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\Jenko\House;

use Jenko\House\Dimensions;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

Expand All @@ -28,4 +29,12 @@ function it_should_give_an_array_of_information()
{
$this->getInformation()->shouldBeArray();
}

function it_should_be_able_to_set_dimensions()
{
$dimensions = Dimensions::fromWidthAndHeight(350, 300);
$this->setDimensions($dimensions);

$this->getDimensions()->shouldEqual($dimensions);
}
}
68 changes: 68 additions & 0 deletions src/Jenko/House/Dimensions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Jenko\House;

/**
* Value Object
*/
final class Dimensions
{
/**
* @var int
*/
private $width;

/**
* @var int
*/
private $height;

/**
* @param int $width
* @param int $height
*/
private function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}

/**
* @param int $width
* @param int $height
* @return Dimensions
*/
public static function fromWidthAndHeight($width, $height)
{
return new Dimensions($width, $height);
}

/**
* @param Dimensions $dimensions
* @return bool
*/
public function equals(Dimensions $dimensions)
{
if ($dimensions->getWidth() === $this->getWidth() && $dimensions->getHeight() === $this->getHeight()) {
return true;
}

return false;
}

/**
* @return int
*/
public function getWidth()
{
return $this->width;
}

/**
* @return int
*/
public function getHeight()
{
return $this->height;
}
}
2 changes: 1 addition & 1 deletion src/Jenko/House/Garden.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public static function named($name)
{
return new Garden($name);
}
}
}
24 changes: 24 additions & 0 deletions src/Jenko/House/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ abstract class Location
*/
private $name;

/**
* @var Dimensions
*/
private $dimensions;

/**
* @param string $name
*/
Expand All @@ -25,8 +30,27 @@ public function getName()
return $this->name;
}

/**
* @return array
*/
public function getInformation()
{
return ['dimensions' => '', 'exits' => ''];
}

/**
* @param Dimensions $dimensions
*/
public function setDimensions(Dimensions $dimensions)
{
$this->dimensions = $dimensions;
}

/**
* @return Dimensions
*/
public function getDimensions()
{
return $this->dimensions;
}
}
3 changes: 2 additions & 1 deletion tests/contexts/HomeOwnerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\TableNode;
use Jenko\House\Dimensions;
use Jenko\House\Garden;
use Jenko\House\House;
use Jenko\House\Room;
Expand Down Expand Up @@ -101,7 +102,7 @@ public function thereAreTheFollowingLocationsInTheHouse(TableNode $table)
$locations[] = $location;
}

$this->house = House::buildHouse($locations);
$this->house = House::build($locations);
}

/**
Expand Down

0 comments on commit 56319d0

Please sign in to comment.