Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include_once dirname(__FILE__) . '/../lib/exampleList.php';
- /**
- * Tests for _mock_ Iterator
- *
- * Here we are mocking the iterator and making it work. We would want to do
- * this where we were testing some other class that contained an Iterator
- * dependency.
- *
- * @author Dave Gardner <dave@davegardner.me.uk>
- */
- class mockedIteratorTest extends PHPUnit_Framework_TestCase
- {
- private function buildSystemUnderTest()
- {
- return $this->getMock('exampleList');
- }
- public function testConstructs()
- {
- $list = $this->buildSystemUnderTest();
- $this->assertInstanceOf('exampleList', $list);
- $this->assertInstanceOf('Iterator', $list);
- }
- public function testHasZeroItemsWhenWeHaventMockedMethods()
- {
- $list = $this->buildSystemUnderTest();
- $counter = 0;
- foreach ($list as $item)
- {
- $counter++;
- }
- $this->assertEquals(0, $counter);
- }
- public function testWhenMockOneIterationWithNoKey()
- {
- $list = $this->buildSystemUnderTest();
- $list->expects($this->at(0))
- ->method('rewind');
- $list->expects($this->at(1))
- ->method('valid')
- ->will($this->returnValue(TRUE));
- $list->expects($this->at(2))
- ->method('current')
- ->will($this->returnValue('This is the first item'));
- $list->expects($this->at(3))
- ->method('next');
- $list->expects($this->at(4))
- ->method('valid')
- ->will($this->returnValue(FALSE));
- $counter = 0;
- foreach ($list as $value)
- {
- $counter++;
- }
- $this->assertEquals(1, $counter);
- $this->assertEquals('This is the first item', $value);
- }
- public function testWhenMockOneIterationWithKey()
- {
- $list = $this->buildSystemUnderTest();
- $list->expects($this->at(0))
- ->method('rewind');
- $list->expects($this->at(1))
- ->method('valid')
- ->will($this->returnValue(TRUE));
- $list->expects($this->at(2))
- ->method('current')
- ->will($this->returnValue('This is the first item'));
- $list->expects($this->at(3))
- ->method('key')
- ->will($this->returnValue('key1'));
- $list->expects($this->at(4))
- ->method('next');
- $list->expects($this->at(5))
- ->method('valid')
- ->will($this->returnValue(FALSE));
- $counter = 0;
- foreach ($list as $key => $value)
- {
- $counter++;
- }
- $this->assertEquals(1, $counter);
- $this->assertEquals('key1', $key);
- $this->assertEquals('This is the first item', $value);
- }
- public function testWhenMockThreeIterationWithNoKey()
- {
- $list = $this->buildSystemUnderTest();
- $list->expects($this->at(0))
- ->method('rewind');
- // iteration 1
- $list->expects($this->at(1))
- ->method('valid')
- ->will($this->returnValue(TRUE));
- $list->expects($this->at(2))
- ->method('current')
- ->will($this->returnValue('This is the first item'));
- $list->expects($this->at(3))
- ->method('next');
- // iteration 2
- $list->expects($this->at(4))
- ->method('valid')
- ->will($this->returnValue(TRUE));
- $list->expects($this->at(5))
- ->method('current')
- ->will($this->returnValue('This is the second item'));
- $list->expects($this->at(6))
- ->method('next');
- // iteration 2
- $list->expects($this->at(7))
- ->method('valid')
- ->will($this->returnValue(TRUE));
- $list->expects($this->at(8))
- ->method('current')
- ->will($this->returnValue('And the final item'));
- $list->expects($this->at(9))
- ->method('next');
- $list->expects($this->at(10))
- ->method('valid')
- ->will($this->returnValue(FALSE));
- $counter = 0;
- $values = array();
- foreach ($list as $value)
- {
- $values[] = $value;
- $counter++;
- }
- $this->assertEquals(3, $counter);
- $expectedValues = array(
- 'This is the first item',
- 'This is the second item',
- 'And the final item'
- );
- $this->assertEquals($expectedValues, $values);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement