2017-07-24 97 views
0

我創建了一個實現IteratorArrayAccess的類。迭代器測試失敗。當我在current($object)上執行print_r()時,我得到底層數組,而不是第一個對象。PHP Iterator接口:current()返回底層數組而不是第一個值

下面的代碼是來自我的實際類的示例,演示了這個問題。這是我第一次在我的一個類中實現Iterator,所以我可能在某處做錯了。我需要改變以使迭代器正常工作?

代碼:

class Collection implements \ArrayAccess, \Iterator 
{ 
    private $_array = array(); 

    public function __invoke() { return $this->_array; } 
    public function offsetExists($offset) { return array_key_exists($offset, $this->_array); } 
    public function offsetGet($offset) { return $this->_array[$offset]; } 
    public function offsetSet($offset, $value) { $this->_array[$offset] = $value; } 
    public function offsetUnset($offset) { unset($this->_array[$offset]); } 
    public function current() { return current($this->_array); } 
    public function key() { return key($this->_array); } 
    public function next() { return next($this->_array); } 
    public function rewind() { return reset($this->_array); } 
    public function valid() { return is_null(key($this->_array)); } 
} 

class TemporaryTest extends \PHPUnit\Framework\TestCase 
{ 

    private $_test_object; 

    public function setUp() 
    { 
     $this->_test_object = new Collection(); 
     $this->_test_object['alpha'] = 'blah'; 
     $this->_test_object['beta'] = 'yada'; 
     $this->_test_object['gamma'] = 'woot'; 
    } 

    public function testIteratorOnInternalArray() 
    { 
     $o = $this->_test_object; 
     $a = $o(); 
     $this->assertEquals('blah', current($a)); 
     $this->assertEquals('yada', next($a)); 
     $this->assertEquals('woot', next($a)); 
    } 

    public function testIterator() 
    { 
     print_r(current($this->_test_object)); 
     $this->assertEquals('blah', current($this->_test_object)); 
     $this->assertEquals('yada', next($this->_test_object)); 
     $this->assertEquals('woot', next($this->_test_object)); 
     $this->assertFalse($this->_test_object->valid()); 
     reset($this->_test_object); 
     $this->assertEquals('blah', current($this->_test_object)); 
    } 

    public function testForEach() 
    { 
     $actual = array(); 
     foreach ($this->_test_object as $key => $value) { $actual[$key] = $value; } 
     $this->assertEquals(array('alpha' => 'blah', 'beta' => 'yada','gamma' => 'woot'), $actual); 
    } 
} 

單元測試輸出:

.FArray 
(
    [alpha] => blah 
    [beta] => yada 
    [gamma] => woot 
) 
F                 3/3 (100%) 

There were 2 failures: 

1) TemporaryTest::testIterator 
Array (...) does not match expected type "string". 

/Users/mac/Projects/NetShapers/Gears/core/Tests/Unit/TemporaryTest.php:83 

2) TemporaryTest::testForEach 
Failed asserting that two arrays are equal. 
--- Expected 
+++ Actual 
@@ @@ 
Array (
- 'alpha' => 'blah' 
- 'beta' => 'yada' 
- 'gamma' => 'woot' 
) 

/Users/mac/Projects/NetShapers/Gears/core/Tests/Unit/TemporaryTest.php:97 

回答

1

什麼辦法?

當您致電current($a)時,您實際上調用了系統功能current()而不是內部方法Collection::current()

就calll像這樣得到你想要的東西:

$this->assertEquals('blah', $a->current()); 

這些方法將會被系統自動調用,如果你可以用你的收藏foreach例如。


評論後,其他修復。

您在valid方法中有錯誤,這就是爲什麼foreach不適合您。

宣言應該看起來像:

public function valid() 
{ 
    return !is_null(key($this->_array)); 
} 
+0

是的,這正是我所期望的。你有沒有看到'foreach'測試失敗的部分? –

+1

@JayBienvenu我已經更新了你的'foreach'失敗的原因 –

+0

因此,答案是我在'valid()'中有一個輸入錯誤,'current()'不能和像count一樣的Iterator ()'等功能呢? –

相關問題