PHPUnit - Stub的行为就像一个内置类型


PHPUnit - Stub acting like a build in type

如何使我的存根像ArrayIterator一样工作?我的意思是,我想遍历这个存根。

7.2
Write a EvenIterator which takes a FibonacciIterator an iterates only
on the even-indexed values (returning 0, 1, 3, 8, 21...).
7.3
Write tests for the EvenIterator class, stubbing out the
FibonacciIterator using an ArrayIterator in substitution, which is provided
by the Spl (otherwise it will never terminate!)

谢谢。

如果我理解正确,这里的任务是通过使用ArrayIterator作为FibonacciIterator的存根来测试EvenIterator。因此,例如加载ArrayIterator与偶数值的数组,将其传递给EvenIterator,你应该得到相同的值。然后对奇数值的数组执行相同的操作,您应该得到空结果集。


class EvenIteratorTest extends 'PHPUnit_Framework_TestCase {
  public function testDoesNotRemoveEvens() {
    $data = array(2,4,6,8);
    $arrayIterator = new 'ArrayIterator($data);
    $object = new EvenIterator($arrayIterator);
    $expected = $data;
    $actual = array();
    foreach($object as $v) {
      $actual[] = $v;
    }
    $this->assertEquals($expected,$actual);
  }
  public function testFiltersOutOdds() {
    $data = array(1,3,5,7);
    $arrayIterator = new 'ArrayIterator($data);
    $object = new EvenIterator($arrayIterator);
    $actual = array();
    foreach($object as $v) {
      $actual[] = $v;
    }
    $this->assertEmpty($actual);
  }
}

如您所见,这里有很多重复的代码,因此需要进行一些重构。