Skip to content

Commit

Permalink
Collection implements \Iterator + tests on it
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Aug 24, 2014
1 parent c232438 commit d3bd61f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Response/Collection.php
Expand Up @@ -6,7 +6,7 @@

namespace SocialConnect\Vk\Response;

class Collection implements \Countable
class Collection implements \Iterator, \Countable
{
/**
* @var integer
Expand Down Expand Up @@ -49,6 +49,8 @@ public function toArray()
}

/**
* Set position to first element and return this element
*
* @return mixed
*/
public function first()
Expand Down Expand Up @@ -87,4 +89,20 @@ public function current()
{
return current($this->elements);
}

/**
* Set position to first element
*/
public function rewind()
{
reset($this->elements);
}

/**
* @return bool|void
*/
public function valid()
{
return $this->current();
}
}
35 changes: 35 additions & 0 deletions tests/VkTest/Response/CollectionTest.php
@@ -0,0 +1,35 @@
<?php
/**
* SocialConnect project
* @author: Patsura Dmitry @ovr <talk@dmtry.me>
*/

namespace VkTest\Response;

use SocialConnect\Vk\Response\Collection;

class CollectionTest extends \PHPUnit_Framework_TestCase
{
public function testIterator()
{
$collection = new Collection(array(
(object) array(
'id' => '1'
),
(object) array(
'id' => '2'
),
(object) array(
'id' => '3'
),
), 3, function() {});

$this->assertEquals(3, $collection->count());

$count = 0;
foreach($collection as $key => $value) {
$count++;
}
$this->assertEquals(3, $count);
}
}

0 comments on commit d3bd61f

Please sign in to comment.