I would like to test this controller on my symfony 4.4 project :
final class WebhookController extends AbstractController
{
private CustomLoggerService $customLogger;
private EntityManagerInterface $entityManager;
private MyService $myService;
private UserMailer $userMailer;
private AdminMailer $adminMailer;
public function __construct
(
CustomLoggerService $customLogger,
EntityManagerInterface $entityManager,
MyService $myService,
UserMailer $userMailer,
AdminMailer $adminMailer
)
{
$this->customLogger = $customLogger;
$this->myService = $myService;
$this->userMailer = $userMailer;
$this->adminMailer = $adminMailer;
$this->entityManager = $entityManager;
}
/**
* @Route("/webhook/new", name="webhook_new")
* @param Request $request
* @return Response
*/
public function new(Request $request)
{
$uri = $request->getUri();
$this->customLogger->info("new event uri ".$uri);
$query = $request->query->all();
if(isset($query['RessourceId']))
{
//infos
$id = $query['RessourceId'];
$event = $this->myService->getInfos($id);
$infoId = $event->infoId;
$this->customLogger->info("new info id ".$infoId);
$userRepo = $this->entityManager->getRepository(User::class);
$user = $userRepo->findOneByEventUserId((int)$event->owners[0]);
//mail sent
$this->userMailer->sendAdminEvent($event, $user);
$this->customLogger->info("new mail sent");
}
else
{
$this->adminMailer->sendSimpleMessageToAdmin("no ressource id", "no ressource id");
}
return new JsonResponse();
}
}
I need to mock MyService because it calls an external API
Here my test :
class WebhookControllerTest extends WebTestCase
{
use LoginTrait;
public function testNewPayinIRessourceIdExists()
{
self::ensureKernelShutdown();
/** @var KernelBrowser $client */
$client = $this->startClient();
$client->enableProfiler();
$myService = $this->createMock(MyService::class);
$myService->expects($this->once())->method("getInfos")->willReturn([
[
'hi'=>'hi'
]
]);
// if I do this, I can't inject my mocked service
$client->request('GET', '/webhook/new/?RessourceId=1111');
// if I do this, I can't autowire all my dependencies of my controller
$controller = new WebhookController(xxxx);
$controller->new(xxx);
}
}
What is the best way to test my controller ? If I do new WebhookController(xxxx)
I have a lot of dependencies which need other dependencies and it's not maintenable.
Or I should move the customlog and sending emails in other service and keep the controller without these dependencies?
CodePudding user response:
You don't need to instantiate the controller by yourself.
You can make service that you need to mock public for test environment, create mock and override in the container.
In
config/services_test.yaml
add following configApp\Service\MyService: public: true
Mock service
$myService = $this->createMock(MyService::class); $myService->expects($this->once())->method("getInfos")->willReturn([ [ 'hi'=>'hi' ] ]); self::$container->set(MyService::class, $myService);
And make HTTP request
$client->request('GET', '/webhook/new/?RessourceId=1111');