Skip to content

Commit

Permalink
Add __debugInfo() to Collection.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Apr 10, 2015
1 parent d1a17c2 commit eaa9179
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Collection/Collection.php
Expand Up @@ -48,4 +48,17 @@ public function __construct($items)

parent::__construct($items);
}

/**
* Returns an array that can be used to describe the internal state of this
* object.
*
* @return array
*/
public function __debugInfo()
{
return [
'count' => iterator_count($this),
];
}
}
42 changes: 42 additions & 0 deletions tests/TestCase/Collection/CollectionTest.php
Expand Up @@ -1163,4 +1163,46 @@ public function testThroughReturnArray()

$this->assertEquals([1, 2, 3, 1, 2, 3], $collection->toList());
}

/**
* Tests __debugInfo() or debug() usage
*
* @return void
*/
public function testDebug()
{
$items = [1, 2, 3];

$collection = new Collection($items);

$result = $collection->__debugInfo();
$expected = [
'count' => 3,
];
$this->assertSame($expected, $result);

// Calling it again will rewind
$result = $collection->__debugInfo();
$expected = [
'count' => 3,
];
$this->assertSame($expected, $result);

// Make sure it also works with non rewindable iterators
$iterator = new NoRewindIterator(new ArrayIterator($items));
$collection = new Collection($iterator);

$result = $collection->__debugInfo();
$expected = [
'count' => 3,
];
$this->assertSame($expected, $result);

// Calling it again will in this case not rewind
$result = $collection->__debugInfo();
$expected = [
'count' => 0,
];
$this->assertSame($expected, $result);
}
}

0 comments on commit eaa9179

Please sign in to comment.