here is the following code sample
<?php
interface iFS
{
public function read();
public function write($data);
}
class MyClass
{
protected $_fs = null;
public function __construct(iFS $fs)
{
$this->_fs = $fs;
}
public function run(array $list)
{
foreach ($list as $elm)
{
$this->_fs->write($elm);
}
return $this->_fs->read();
}
}
class MyTests extends PHPUnit_Framework_TestCase
{
public function testFS()
{
$mock = $this->getMock('iFS');
$mock->expects($this->at(0))
->method('read')
->will($this->returnValue('tototatatiti'));
$c = new MyClass($mock);
$result = $c->run(array('toto', 'tata', 'titi'));
$this->assertEquals('tototatatiti', $result);
}
}
This is absolutely not a real case but it make happen something strange with phpunit and at($index) feature.
My question is pretty simple, is it normal that the test fails?
I explicitely asked to return "tototatatiti", but it never happens...
When
- I remove the line $this->_fs->write($elm); or
- I replace $mock->expects($this->at(0)) by $mock->expects($this->once())
The test pass to green
Is there something I don't understand?
EDIT:
$mock->expects($this->at(3)) ->method('read') ->will($this->returnValue('tototatatiti'));
=> Will make the test pass green...