I have an commandbus handler, which injects some service:
class SomeHandler
{
private $service;
public function __construct(SomeService $service)
{
$this->service = $service;
}
public test(CommandTest $command)
{
$this->service->doSomeStuff();
}
}
SomeService has method doSomeStuff with external calls, which I want not to use during testing.
class SomeService
{
private $someBindedVariable;
public function __construct($someBindedVariable)
{
$this->someBindedVariable = $someBindedVariable;
}
public function doSomeStuff()
{
//TODO: some stuff
}
}
There is in the test I try to replace service with mock object
public function testTest()
{
$someService = $this->getMockBuilder(SomeService::class)->getMock();
$this->getContainer()->set(SomeService::class, $someService);
//TODO: functional test for the route, which uses SomeHandler
}
The first problem is this code will throws exception "The "App\Service\SomeService" service is private, you cannot replace it."
Ok, let's try to make it public:
services.yaml:
App\Service\SomeService:
public: true
arguments:
$someBindedVariable: 200
But it doesn't help. I get response from native SomeService. Let's try with aliases:
some_service:
class: App\Service\SomeService
public: true
arguments:
$someBindedVariable: 200
App\Service\SomeService:
alias: some_service
And again the mock object does not use by test. I see response from native SomeService.
I tried to append autowire option, but it did not help.
What should I do to replace my SomeService with some mock object all over the project during test?