J'essaie de créer un test unitaire pour mon application mais j'aimerais avoir quelques conseils.Comparer des entités en fonction de leur ID pendant le test unitaire
J'ai ces méthodes:
/**
* This methods check if the user can apply to the team, it search if he have a username for the game and if he doesn't already applied for another team in the same tournament
* @param User $user
* @param Team $team
* @return bool|string
*/
public function canApply(User $user, Team $team) {
if ($user->getGamingUsername($team->getTournament()->getGame()) === null) {
return "Vous devez avoir un nom d'utilisateur pour pouvoir vous inscrire, renseignez le dans \"Mon profil\"";
} else if (false !== $teamAlreadyIn = $this->isAlreadyApplicant($user, $team)) {
return "Vous avez déjà postulé pour une équipe pour ce tournoi :
<a href=\"".$this->router->generate("mgd_team_show", array("id" => $teamAlreadyIn->getId()))."\">".htmlspecialchars($teamAlreadyIn->getName())."</a>";
}
return true;
}
/**
* This method search if the user is already in a team for the same tournament than the one passed in argument
* @param User $user
* @param Team $team
* @return bool|Team|mixed
*/
public function isAlreadyApplicant($user, Team $team) {
if (!$user || !$this->authorizationChecker->isGranted("ROLE_USER")) {
return false;
}
foreach ($user->getApplications() as $userTeam) {
/** @var Team $userTeam */
if ($userTeam->getTournament()->getId() === $team->getTournament()->getId()) {
return $userTeam;
}
}
foreach ($user->getTeams() as $userTeam) {
/** @var Team $userTeam */
if ($userTeam->getTournament()->getId() === $team->getTournament()->getId()) {
return $userTeam;
}
}
foreach ($user->getManagedTeam() as $userTeam) {
/** @var Team $userTeam */
if ($userTeam->getTournament()->getId() === $team->getTournament()->getId()) {
return $userTeam;
}
}
return false;
}
Comme vous pouvez le voir, la première (canApply) appeler le second (isAlreadyApplicant).
Mais quand j'essaye de tester canApply, j'ai quelques problèmes: cet appel de méthode isAlreadyApplicant et dans celui-ci je compare en fonction de l'id les tournois.
Dans ma classe de test, je ne peux pas "-> setId()" parce que c'est une méthode privée. Alors, comment suis-je censé gérer cela? Vaut-il mieux récupérer un élément de ma base de données?
À l'heure actuelle, l'un de mes testMethod ressemble à ceci:
/**
* The user is connected and try to apply to a team and applied for another team for another game
*/
public function testCanApplySecondTeamAnotherGame() {
$user = new User();
$game = new Game();
$anotherGame = new Game();
$team = new Team();
$anotherTeam = new Team();
$tournament = new TournamentTeam();
$anotherTournament = new TournamentTeam();
$team->setTournament($tournament);
$anotherTeam->setTournament($anotherTournament);
$tournament->setGame($game);
$anotherTournament->setGame($anotherGame);
$game->setName("TestGame");
$anotherGame->setName("AnotherGame");
$gamingProfile = new GamingProfile();
$gamingProfile->setGame($game);
$gamingProfile->setUsername("TestGameUsername");
$anotherGamingProfile = new GamingProfile();
$anotherGamingProfile->setGame($anotherGame);
$anotherGamingProfile->setUsername("TestAnotherGameUsername");
$user->setGamingProfiles(new ArrayCollection(array($gamingProfile, $anotherGamingProfile)));
$user->addApplication($anotherTeam);
$user->addTeam($anotherTeam);
$router = $this->createMock(Router::class);
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
$authorizationChecker->method("isGranted")->willReturn(true);
$applicationChecker = new ApplicationChecker($router, $authorizationChecker);
//Here, we try to apply to a team
$this->assertTrue($applicationChecker->canApply($user, $team));
}
Si vous avez des questions, ne hésitez pas! Passez une bonne journée!
Il est généralement déconseillé, mais vous pouvez définir des propriétés privées avec la bibliothèque de simulation Phpunit. – svgrafov
Ceci n'est pas un test unitaire, c'est un test d'intégration. – localheinz
Désolé je pensais qu'un test d'intégration était un test unitaire – Alexandre