Skip to content

Instantly share code, notes, and snippets.

@peterjmit
Last active December 27, 2015 19:09
Show Gist options
  • Save peterjmit/7375500 to your computer and use it in GitHub Desktop.
Save peterjmit/7375500 to your computer and use it in GitHub Desktop.

Revisions

  1. peterjmit revised this gist Nov 8, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions CollectionSpec.php
    Original file line number Diff line number Diff line change
    @@ -26,8 +26,8 @@ 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
    // 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');
  2. peterjmit revised this gist Nov 8, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 0-usage.php
    Original 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 uniqueitems, you would expect 2.
    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!
    */
  3. peterjmit revised this gist Nov 8, 2013. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions 0-usage.php
    Original 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 3...
    // or if your collection only tracked unique
    // items, you would expect 2.
    // This is the behaviour that you are defining
    /*
    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
  4. peterjmit revised this gist Nov 8, 2013. 2 changed files with 6 additions and 1 deletion.
    3 changes: 3 additions & 0 deletions usage.php → 0-usage.php
    Original 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
    4 changes: 3 additions & 1 deletion CollectionSpec.php
    Original 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');
  5. peterjmit created this gist Nov 8, 2013.
    21 changes: 21 additions & 0 deletions Collection1.php
    Original 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);
    }
    }
    37 changes: 37 additions & 0 deletions CollectionSpec.php
    Original 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);
    }
    }
    23 changes: 23 additions & 0 deletions CollectionUniqueCount.php
    Original 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));
    }
    }
    13 changes: 13 additions & 0 deletions usage.php
    Original 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();