【发布时间】:2017-08-10 09:36:44
【问题描述】:
我正在尝试为我的应用程序创建单元测试,但我想获得一些建议。
我有这些方法:
/**
* 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;
}
如您所见,第一个 (canApply) 调用第二个 (isAlreadyApplicant)。
但是当我尝试测试 canApply 时,我遇到了一些麻烦:这个方法调用 isAlreadyApplicant,在这个方法中,我根据比赛的 id 进行比较。
在我的测试类中,我不能“->setId()”,因为它是一个私有方法。那我该怎么处理呢? 从我的数据库中获取元素会更好吗?
目前,我的一个 testMethod 看起来像这样:
/**
* 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));
}
如果您有任何问题,请不要犹豫! 祝你有美好的一天!
【问题讨论】:
-
通常不鼓励这样做,但您可以使用 Phpunit 模拟库设置私有属性。
-
这不是单元测试,而是集成测试。
-
对不起,我认为集成测试是单元测试
标签: php symfony phpunit symfony-3.2