I'm running into an issue where PHPUnit is unable to mock my simple class:
class.php
<?php
class SUT {
function doThing() {
$db = new DB();
return $db->query();
}
}
class DB {
function query() {
return "failed";
}
}
?>
test.php
<?php
use PHPUnit\Framework\TestCase;
require 'API/class.php';
class SampleTest extends TestCase {
public function testTest() {
$db = $this->createMock(DB::class);
$db->method('query')
->willReturn("win");
$sut = new SUT();
$result = $sut->doThing();
$this->assertEquals("win", $result);
}
}
Output:
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
Runtime: PHP 7.1.16
Configuration: ~/API_tests/config.xml
F............... 16 / 16 (100%)
Time: 604 ms, Memory: 10.00MB
There was 1 failure:
1) SampleTest::testTest
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'win'
+'failed'
~/API_tests/TestTest.php:14
Am I missing something basic here? Am I required to pass the mock object to the SUT?