Advertisement
Guest User

Mocking Iterator using PHPUnit

a guest
Mar 4th, 2011
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.58 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 are mocking the iterator and making it work. We would want to do
  9.  * this where we were testing some other class that contained an Iterator
  10.  * dependency.
  11.  *
  12.  * @author Dave Gardner <dave@davegardner.me.uk>
  13.  */
  14. class mockedIteratorTest extends PHPUnit_Framework_TestCase
  15. {
  16.     private function buildSystemUnderTest()
  17.     {
  18.         return $this->getMock('exampleList');
  19.     }
  20.  
  21.     public function testConstructs()
  22.     {
  23.         $list = $this->buildSystemUnderTest();
  24.         $this->assertInstanceOf('exampleList', $list);
  25.         $this->assertInstanceOf('Iterator', $list);
  26.     }
  27.  
  28.     public function testHasZeroItemsWhenWeHaventMockedMethods()
  29.     {
  30.         $list = $this->buildSystemUnderTest();
  31.         $counter = 0;
  32.         foreach ($list as $item)
  33.         {
  34.             $counter++;
  35.         }
  36.         $this->assertEquals(0, $counter);
  37.     }
  38.  
  39.     public function testWhenMockOneIterationWithNoKey()
  40.     {
  41.         $list = $this->buildSystemUnderTest();
  42.  
  43.         $list->expects($this->at(0))
  44.              ->method('rewind');
  45.         $list->expects($this->at(1))
  46.              ->method('valid')
  47.              ->will($this->returnValue(TRUE));
  48.         $list->expects($this->at(2))
  49.              ->method('current')
  50.              ->will($this->returnValue('This is the first item'));
  51.         $list->expects($this->at(3))
  52.              ->method('next');
  53.         $list->expects($this->at(4))
  54.              ->method('valid')
  55.              ->will($this->returnValue(FALSE));
  56.  
  57.         $counter = 0;
  58.         foreach ($list as $value)
  59.         {
  60.             $counter++;
  61.         }
  62.         $this->assertEquals(1, $counter);
  63.         $this->assertEquals('This is the first item', $value);
  64.     }
  65.  
  66.     public function testWhenMockOneIterationWithKey()
  67.     {
  68.         $list = $this->buildSystemUnderTest();
  69.  
  70.         $list->expects($this->at(0))
  71.              ->method('rewind');
  72.         $list->expects($this->at(1))
  73.              ->method('valid')
  74.              ->will($this->returnValue(TRUE));
  75.         $list->expects($this->at(2))
  76.              ->method('current')
  77.              ->will($this->returnValue('This is the first item'));
  78.         $list->expects($this->at(3))
  79.              ->method('key')
  80.              ->will($this->returnValue('key1'));
  81.         $list->expects($this->at(4))
  82.              ->method('next');
  83.         $list->expects($this->at(5))
  84.              ->method('valid')
  85.              ->will($this->returnValue(FALSE));
  86.  
  87.         $counter = 0;
  88.         foreach ($list as $key => $value)
  89.         {
  90.             $counter++;
  91.         }
  92.         $this->assertEquals(1, $counter);
  93.         $this->assertEquals('key1', $key);
  94.         $this->assertEquals('This is the first item', $value);
  95.     }
  96.  
  97.     public function testWhenMockThreeIterationWithNoKey()
  98.     {
  99.         $list = $this->buildSystemUnderTest();
  100.  
  101.         $list->expects($this->at(0))
  102.              ->method('rewind');
  103.  
  104.         // iteration 1
  105.         $list->expects($this->at(1))
  106.              ->method('valid')
  107.              ->will($this->returnValue(TRUE));
  108.         $list->expects($this->at(2))
  109.              ->method('current')
  110.              ->will($this->returnValue('This is the first item'));
  111.         $list->expects($this->at(3))
  112.              ->method('next');
  113.  
  114.         // iteration 2
  115.         $list->expects($this->at(4))
  116.              ->method('valid')
  117.              ->will($this->returnValue(TRUE));
  118.         $list->expects($this->at(5))
  119.              ->method('current')
  120.              ->will($this->returnValue('This is the second item'));
  121.         $list->expects($this->at(6))
  122.              ->method('next');
  123.  
  124.         // iteration 2
  125.         $list->expects($this->at(7))
  126.              ->method('valid')
  127.              ->will($this->returnValue(TRUE));
  128.         $list->expects($this->at(8))
  129.              ->method('current')
  130.              ->will($this->returnValue('And the final item'));
  131.         $list->expects($this->at(9))
  132.              ->method('next');
  133.  
  134.         $list->expects($this->at(10))
  135.              ->method('valid')
  136.              ->will($this->returnValue(FALSE));
  137.        
  138.         $counter = 0;
  139.         $values = array();
  140.         foreach ($list as $value)
  141.         {
  142.             $values[] = $value;
  143.             $counter++;
  144.         }
  145.         $this->assertEquals(3, $counter);
  146.  
  147.         $expectedValues = array(
  148.             'This is the first item',
  149.             'This is the second item',
  150.             'And the final item'
  151.         );
  152.         $this->assertEquals($expectedValues, $values);
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement