Last active
December 27, 2015 19:09
-
-
Save peterjmit/7375500 to your computer and use it in GitHub Desktop.
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
<?php | |
$collection = new Collection(); | |
$collection->add('item'); | |
$collection->add('another item'); | |
$collection->add('item'); | |
/* | |
What would you expect here? Probably a count of 3... | |
or if your collection only tracked unique items, you would expect 2. | |
This is the behaviour that you are defining and therefore it is | |
important to test it! | |
*/ | |
echo count($collection); | |
// or | |
echo $collection->count(); |
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
<?php | |
class Collection implements \Countable | |
{ | |
private $items; | |
public function __construct(array $items = []) | |
{ | |
$this->items = $items; | |
} | |
public function add($item) | |
{ | |
$this->items[] = $item; | |
} | |
public function count() | |
{ | |
return count($this->items); | |
} | |
} |
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
<?php | |
namespace spec; | |
use PhpSpec\ObjectBehavior; | |
use Prophecy\Argument; | |
class CollectionSpec extends ObjectBehavior | |
{ | |
function it_is_initializable() | |
{ | |
$this->shouldHaveType('Collection'); | |
} | |
function it_implements_countable() | |
{ | |
$this->shouldImplement('Countable'); | |
} | |
function it_counts_its_items() | |
{ | |
$this->add('item 1'); | |
$this->add('item 2'); | |
$this->add('item 3'); | |
$this->count()->shouldReturn(3); | |
} | |
// Using our first attempt at implementing a \Countable Collection | |
// (Collection1.php) this spec will fail... | |
function it_should_only_count_unique_items() | |
{ | |
$this->add('item'); | |
$this->add('item'); | |
$this->add('item'); | |
$this->count()->shouldReturn(1); | |
} | |
} |
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
<?php | |
class Collection implements \Countable | |
{ | |
private $items; | |
public function __construct(array $items = []) | |
{ | |
$this->items = $items; | |
} | |
public function add($item) | |
{ | |
if (!in_array($item, $this->items)) { | |
$this->items[] = $item; | |
} | |
} | |
public function count() | |
{ | |
return count(array_unique($this->items)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment