-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Here we add our first Value Object, Dimensions. Again, using PHPSpec as the crutch to aid the design.
- Loading branch information
Showing
7 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ public static function named($name) | |
{ | ||
return new Garden($name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters