a relevant example for iterator

This commit is contained in:
Trismegiste
2013-05-11 12:47:26 +02:00
parent 94edeede4e
commit aa8069fc45
2 changed files with 136 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Tests\Iterator;
use DesignPatterns\Iterator\CardGame;
/**
* IteratorTest the CardGame iterator
*
* @author flo
*/
class IteratorTest extends \PHPUnit_Framework_TestCase
{
protected $game;
protected function setUp()
{
$this->game = new CardGame();
}
/**
* This is the client of the iterator.
* It remains unchanged even if one I decide to use MongoDB to store the
* card.
*/
public function testCardinal()
{
$counter = 0;
foreach ($this->game as $key => $card) {
$counter++;
}
$this->assertEquals(32, $counter);
}
/**
* Some fancy functions of PHP.
*/
public function testExampleOf_PHP_Helper()
{
// PHPUnit works on array or iterator :
$this->assertCount(32, $this->game);
// a easy way to get an array from interator :
$cards = iterator_to_array($this->game);
$this->assertEquals('A of S', $cards['SA']);
// a easy way to get an iterator from an array :
$iterator = new \ArrayIterator($cards);
$this->assertInstanceOf('\Iterator', $iterator);
}
}