I have this file structure:
- module
-- phpunit.xml
-- blaat.php
-- tests
--- blaatTest.php
contents of blaat.php
class Blaat
{
public function doSomething()
{
return 'my return value';
}
}
contents of tests/blaatTest.php
use PHPUnit\Framework\TestCase;
require_once './blaat.php';
class blaatTest extends TestCase
{
public function testCanBeCreatedFromValidEmailAddress(): void
{
$stub = $this->createMock(Blaat::class);
$this->assertEquals('foo', $stub->doSomething());
}
}
contents of phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="./tests/bootstrap.php">
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<blacklist>
<directory suffix=".php">vendor</directory>
</blacklist>
</filter>
</phpunit>
When I run phpunit
in my terminal (when I'm in the modules
folder) I get this:
PHPUnit 6.0.8 by Sebastian Bergmann and contributors.
F 1 / 1 (100%)
Time: 427 ms, Memory: 22.00MB
There was 1 failure:
1) blaatTest::testCanBeCreatedFromValidEmailAddress
Failed asserting that null matches expected 'foo'.
/path/to/module/tests/blaatTest.php:19
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
How is this possible? My method will always return a string, yet phpunit says it gets a null value as a return. What am I doing wrong here?
From the doc (Chapter 9. Test Doubles):
By default, all methods of the original class are replaced with a dummy implementation that just returns null (without calling the original method). Using the
will($this->returnValue())
method, for instance, you can configure these dummy implementations to return a value when called
So this is the default behaviour, you need to instrument by yourself.