Last active
December 27, 2015 19:09
-
-
Save peterjmit/7375500 to your computer and use it in GitHub Desktop.
Revisions
-
peterjmit revised this gist
Nov 8, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -26,8 +26,8 @@ function it_counts_its_items() $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'); -
peterjmit revised this gist
Nov 8, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ /* 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! */ -
peterjmit revised this gist
Nov 8, 2013 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,10 +6,12 @@ $collection->add('another item'); $collection->add('item'); /* What would you expect here? Probably a count of 3... or if your collection only tracked uniqueitems, you would expect 2. This is the behaviour that you are defining and therefore it is important to test it! */ echo count($collection); // or -
peterjmit revised this gist
Nov 8, 2013 . 2 changed files with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,9 @@ $collection->add('item'); // What would you expect here? Probably 3... // or if your collection only tracked unique // items, you would expect 2. // This is the behaviour that you are defining echo count($collection); // or This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -25,7 +25,9 @@ function it_counts_its_items() $this->count()->shouldReturn(3); } // Using the first version of the collection // https://gist.github.com/peterjmit/7375500#file-collection1-php function it_should_only_count_unique_items() { $this->add('item'); -
peterjmit created this gist
Nov 8, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ <?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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ <?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); } function it_should_only_count_unique_items() { $this->add('item'); $this->add('item'); $this->add('item'); $this->count()->shouldReturn(1); } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ <?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)); } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,13 @@ <?php $collection = new Collection(); $collection->add('item'); $collection->add('another item'); $collection->add('item'); // What would you expect here? Probably 3... echo count($collection); // or echo $collection->count();