mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-25 17:21:19 +02:00
a relevant example for iterator
This commit is contained in:
56
Tests/Iterator/IteratorTest.php
Normal file
56
Tests/Iterator/IteratorTest.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user