Я хочу протестировать функции моего контроллера с помощью PHP-модуля, используя MOCK (в API Symfony 4), но я не могу найти способ. На самом деле я провел несколько исследований и не нашел способа сделать это, я установил модуль PHP и запрограммировал тестовый веб-кейс без подделки, и он работает, но не по подделке. Я хочу использовать насмешки, потому что я не хочу касаться базы данных. Любой может помочь мне, пожалуйста, добиться этого.
Это мой контроллер:
<?php
namespace App\Controller;
use App\Utils\MatchServiceInterface;
use App\Utils\ModeServiceInterface;
use App\Utils\PlayerServiceInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/api/scouters/matchs")
* Manage entity Match
* Class MatchController
* @package App\Controller
*/
class MatchController extends AbstractController
{
private $playerService;
private $modeService;
private $matchService;
/**
* ScouterController constructor.
* @param PlayerServiceInterface $playerService
* @param ModeServiceInterface $modeService
* @param MatchServiceInterface $matchService
*/
public function __construct(PlayerServiceInterface $playerService, ModeServiceInterface $modeService, MatchServiceInterface $matchService)
{
$this->playerService = $playerService;
$this->modeService = $modeService;
$this->matchService = $matchService;
}
/**
* @Route("/create", name="create_match", methods={"POST"})
* @param Request $request
* @return JsonResponse
*/
public function createMatch(Request $request): JsonResponse
{
$content = json_decode($request->getContent(), true);
$message = $this->playerService->assignPlayerToMatch($content);
return new JsonResponse($message, JsonResponse::HTTP_OK);
}
/**
* @Route("/mode", name="mode_match", methods={"POST"})
* @param Request $request
* @return JsonResponse
* @return JsonResponse
*/
public function modeMatch(Request $request): JsonResponse
{
$content = json_decode($request->getContent(), true);
$message = $this->modeService->assignModeToMatch($content);
return new JsonResponse($message, JsonResponse::HTTP_OK);
}
/**
* @Route("/features", name="feat_mode_match", methods={"POST"})
* @param Request $request
* @return JsonResponse
*/
public function featMode(Request $request): JsonResponse
{
$content = json_decode($request->getContent(), true);
$this->matchService->modeFeature($content);
$message = $this->matchService->modeFeature($content);
return new JsonResponse($message, JsonResponse::HTTP_OK);
}
}
это пример теста моей функции входа в систему,
/**
* test ScouterController:login
*/
public function testLogin()
{
$data = ['email' => 'meher.jaber@solixy.com', 'password' => 'FootStat@2019'];
$client = static::createClient();
$request = $client->request('POST', '/api/scouter/login', [], [], [], json_encode($data));
$response = $client->getResponse();
$this->assertSame('"EmailPasswordValidAndHasNotPlayer"', $response->getContent());
$this->assertSame(200, $response->getStatusCode());
}
Функция входа:
/**
* Login Scouter
* @Route("/login", name="login_scouter", methods={"POST"})
* @param Request $request
* @return JsonResponse
*/
public function login(Request $request): JsonResponse
{
$content = json_decode($request->getContent(), true);
$message = $this->securityService->loginScouter($content);
$this->securityService->loginScouter($content);
return new JsonResponse($message, JsonResponse::HTTP_OK);
}
Служба входа:
/**
* @param $content
* @return array|string
*/
public function loginScouter($content)
{
$scouter = $this->em->getRepository(Scouter::class)->findOneBy(['email' => $content['email']]);
if ($scouter) {
/* @var $scouter Scouter */
if ($this->encoder->isPasswordValid($scouter, $content['password'])) {
if (is_null($scouter->getPlayer())) {
$message = "EmailPasswordValidHasNotPlayer";
} else {
/* @var $player Player */
$player = $scouter->getPlayer();
/* @var $lastMatch Match */
$lastMatch = $this->em->getRepository(Match::class)->findLastInserted($player->getId());
$message = ['idPlayer' => $player->getId(), 'firstName' => $player->getFirstName(), 'lastName' => $player->getFamilyName(), 'team' => $lastMatch->getTeam(), 'oppTeam' => $lastMatch->getOppositeTeam()];
}
} else {
$message = "PasswordInvalid";
}
} else {
$message = "EmailInvalid";
}
return $message;
}