Permalink
Browse files

Version10

Here we add our first Value Object, Dimensions. Again, using PHPSpec as
the crutch to aid the design.
  • Loading branch information...
jenkoian committed Jan 19, 2015
1 parent 1cd52fe commit 56319d09ada9b9a08fe682d697a3973bf36d6bb2
@@ -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);
}
}
@@ -2,6 +2,7 @@
namespace spec\Jenko\House;
use Jenko\House\Dimensions;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
@@ -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);
}
}
@@ -2,6 +2,7 @@
namespace spec\Jenko\House;
use Jenko\House\Dimensions;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
@@ -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);
}
}
@@ -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;
}
}
@@ -12,4 +12,4 @@ public static function named($name)
{
return new Garden($name);
}
}
}
@@ -9,6 +9,11 @@
*/
private $name;
/**
* @var Dimensions
*/
private $dimensions;
/**
* @param string $name
*/
@@ -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,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;
@@ -101,7 +102,7 @@ public function thereAreTheFollowingLocationsInTheHouse(TableNode $table)
$locations[] = $location;
}
$this->house = House::buildHouse($locations);
$this->house = House::build($locations);
}
/**

0 comments on commit 56319d0

Please sign in to comment.