I have these 2 classes :
AbstractTaskDispatcher
<?php
declare(strict_types=1);
namespace MyExample;
abstract class AbstractTaskDispatcher
{
public final function getResult(Task $task) : Result
{
if($worker = $this->getWorker($task))
return $worker->getResult();
else
return Result::getUnprocessableTaskResult();
}
abstract protected function getWorker(Task $task) : Worker;
}
?>
Result
<?php
declare(strict_types=1);
namespace MyExample;
class Result
{
private $code;
public function __construct(int $code = 0)
{
$this->code = $code;
}
public static function getUnprocessableTaskResult() : Result
{
return new Result(1000);
}
public function getCode() : int
{
return $this->code;
}
}
?>
I want to write a unit test with PHPUnit to get sure that AbstractTaskDispatcher::getResult() returns Result::getUnprocessableTaskResult() if no suitable Worker is found to process the Task.
I dont want to do this :
- Arrange : $expectedResult = Result::getUnprocessableTaskResult();
- Act : $result = $dispatcherStub->getResult(New Task());
- Assert : assertEquals($result, $expectedResult);
Because it relies on Result class implementation and would not be a unit test.
I tried to do something :
<?php
use PHPUnit\Framework\TestCase;
use MyExample as ex;
class AbstractDispatcherTest extends TestCase
{
public function test_getResultSouldReturnUnprocessableTaskResultIfNoWorkerFound()
{
$dispatcher = $this->getMockForAbstractClass(ex\AbstractDispatcher::class);
$arbitraryCode = 6666;
$expectedResult = new ex\Result($arbitraryCode);
$resultClass = $this->getMockClass('Result', ['getUnprocessableTaskResult']);
$resultClass::staticExpects($this->any())
->method('getUnprocessableTaskResult')
->will($this->returnValue($expectedResult));
$result = $dispatcher->getResult(new ex\Task([]));
$this->assertEquals($expectedResult, $result);
}
}
?>
But staticExpects() method was deprecated and no longer exists in current PHPUnit version.
How can I write this test ?