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.
The error message seems clear to me. You only did setup a expectation for the put
method, but not exists
. The exists
method is called by the class under test in all code paths.
public function testGeneratorFire()
{
$fileMock = \Mockery::mock('\stats\jway\File');
$fileMock->shouldReceive('put')->with('foo.txt', 'foo bar')->once();
//Add the line below
$fileMock->shouldReceive('exists')->once()->andReturn(false);
$generator = new Generator($fileMock);
$generator->fire();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With