Advertisement
Guest User

Mocking Iterator using PHPUnit via a helper method

a guest
Mar 4th, 2011
844
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.76 KB | None | 0 0
  1. <?php
  2.  
  3. include_once dirname(__FILE__) . '/../lib/exampleList.php';
  4.  
  5. /**
  6.  * Tests for _mock_ Iterator
  7.  *
  8.  * Here we have added a helper method (which you could add to your own abstract
  9.  * base test class) that will help us mock iterators quickly and easily.
  10.  *
  11.  * @author Dave Gardner <dave@davegardner.me.uk>
  12.  */
  13. class mockedIteratorTest extends PHPUnit_Framework_TestCase
  14. {
  15.     private function buildSystemUnderTest()
  16.     {
  17.         return $this->getMock('exampleList');
  18.     }
  19.  
  20.     /**
  21.      * Mock iterator
  22.      *
  23.      * This attaches all the required expectations in the right order so that
  24.      * our iterator will act like an iterator!
  25.      *
  26.      * @param Iterator $iterator The iterator object; this is what we attach
  27.      *      all the expectations to
  28.      * @param array An array of items that we will mock up, we will use the
  29.      *      keys (if needed) and values of this array to return
  30.      * @param boolean $includeCallsToKey Whether we want to mock up the calls
  31.      *      to "key"; only needed if you are doing foreach ($foo as $k => $v)
  32.      *      as opposed to foreach ($foo as $v)
  33.      */
  34.     private function mockIterator(
  35.             Iterator $iterator,
  36.             array $items,
  37.             $includeCallsToKey = FALSE
  38.             )
  39.     {
  40.         $iterator->expects($this->at(0))
  41.                  ->method('rewind');
  42.         $counter = 1;
  43.         foreach ($items as $k => $v)
  44.         {
  45.             $iterator->expects($this->at($counter++))
  46.                      ->method('valid')
  47.                      ->will($this->returnValue(TRUE));
  48.             $iterator->expects($this->at($counter++))
  49.                      ->method('current')
  50.                      ->will($this->returnValue($v));
  51.             if ($includeCallsToKey)
  52.             {
  53.                 $iterator->expects($this->at($counter++))
  54.                          ->method('key')
  55.                          ->will($this->returnValue($k));
  56.             }
  57.             $iterator->expects($this->at($counter++))
  58.                      ->method('next');
  59.         }
  60.         $iterator->expects($this->at($counter))
  61.                  ->method('valid')
  62.                  ->will($this->returnValue(FALSE));
  63.     }
  64.  
  65.     public function testConstructs()
  66.     {
  67.         $list = $this->buildSystemUnderTest();
  68.         $this->assertInstanceOf('exampleList', $list);
  69.         $this->assertInstanceOf('Iterator', $list);
  70.     }
  71.  
  72.     public function testHasZeroItemsWhenWeHaventMockedMethods()
  73.     {
  74.         $list = $this->buildSystemUnderTest();
  75.         $counter = 0;
  76.         foreach ($list as $item)
  77.         {
  78.             $counter++;
  79.         }
  80.         $this->assertEquals(0, $counter);
  81.     }
  82.  
  83.     public function testWhenMockOneIterationWithNoKey()
  84.     {
  85.         $list = $this->buildSystemUnderTest();
  86.  
  87.         $this->mockIterator($list, array('This is the first item'));
  88.  
  89.         $counter = 0;
  90.         foreach ($list as $value)
  91.         {
  92.             $counter++;
  93.         }
  94.         $this->assertEquals(1, $counter);
  95.         $this->assertEquals('This is the first item', $value);
  96.     }
  97.  
  98.     public function testWhenMockOneIterationWithKey()
  99.     {
  100.         $list = $this->buildSystemUnderTest();
  101.  
  102.         $this->mockIterator($list, array('key1' => 'This is the first item'), TRUE);
  103.        
  104.         $counter = 0;
  105.         foreach ($list as $key => $value)
  106.         {
  107.             $counter++;
  108.         }
  109.         $this->assertEquals(1, $counter);
  110.         $this->assertEquals('key1', $key);
  111.         $this->assertEquals('This is the first item', $value);
  112.     }
  113.  
  114.     public function testWhenMockThreeIterationWithNoKey()
  115.     {
  116.         $list = $this->buildSystemUnderTest();
  117.  
  118.         $expectedValues = array(
  119.             'This is the first item',
  120.             'This is the second item',
  121.             'And the final item'
  122.         );
  123.         $this->mockIterator($list, $expectedValues);
  124.  
  125.         $counter = 0;
  126.         $values = array();
  127.         foreach ($list as $value)
  128.         {
  129.             $values[] = $value;
  130.             $counter++;
  131.         }
  132.         $this->assertEquals(3, $counter);
  133.  
  134.         $this->assertEquals($expectedValues, $values);
  135.     }
  136.  
  137.     public function testWhenMockThreeIterationWithKey()
  138.     {
  139.         $list = $this->buildSystemUnderTest();
  140.  
  141.         $expectedValues = array(
  142.             'item1' => 'This is the first item',
  143.             'foo'   => 'This is the second item!!',
  144.             'bar'   => 'And the final item'
  145.         );
  146.         $this->mockIterator($list, $expectedValues, TRUE);
  147.  
  148.         $counter = 0;
  149.         $values = array();
  150.         foreach ($list as $key => $value)
  151.         {
  152.             $values[$key] = $value;
  153.             $counter++;
  154.         }
  155.         $this->assertEquals(3, $counter);
  156.  
  157.         $this->assertEquals($expectedValues, $values);
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement