Can you tell me where's the problem? I have a file GeneratorTest.php with the following tests:
<?php
namespace stats\Test;
use stats\jway\File;
use stats\jway\Generator;
class GeneratorTest extends \PHPUnit_Framework_TestCase
{
public function tearDown() {
\Mockery::close();
}
public function testGeneratorFire()
{
$fileMock = \Mockery::mock('\stats\jway\File');
$fileMock->shouldReceive('put')->with('foo.txt', 'foo bar')->once();
$generator = new Generator($fileMock);
$generator->fire();
}
public function testGeneratorDoesNotOverwriteFile()
{
$fileMock = \Mockery::mock('\stats\jway\File');
$fileMock->shouldReceive('exists')
->once()
->andReturn(true);
$fileMock->shouldReceive('put')->never();
$generator = new Generator($fileMock);
$generator->fire();
}
}
and here are File and Generator classes:
File.php:
class File
{
public function put($path, $content)
{
return file_put_contents($path, $content);
}
public function exists($file_path)
{
if (file_exists($file_path)) {
return true;
}
return false;
}
}
Generator.php:
class Generator
{
protected $file;
public function __construct(File $file)
{
$this->file = $file;
}
protected function getContent()
{
// simplified for demo
return 'foo bar';
}
public function fire()
{
$content = $this->getContent();
$file_path = 'foo.txt';
if (! $this->file->exists($file_path)) {
$this->file->put($file_path, $content);
}
}
}
So, when I run these tests, I get the following message: BadMethodCallException: Method ... ::exists() does not exist on this mock object.
withAnyArgs()
to the mocking of the file as example:$fileMock->shouldReceive('exists') ->once()->withAnyArgs() ->andReturn(true);
– Coltson