Verification done
This commit is contained in:
135
tests/Game/GameDashboardServiceTest.php
Normal file
135
tests/Game/GameDashboardServiceTest.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Game;
|
||||
|
||||
use App\Game\Entity\Game;
|
||||
use App\Game\Entity\Player;
|
||||
use App\Game\Entity\Session;
|
||||
use App\Game\Entity\SessionSetting;
|
||||
use App\Game\Enum\GameStatus;
|
||||
use App\Game\Enum\SessionSettingType;
|
||||
use App\Game\Service\GameDashboardService;
|
||||
use App\Tech\Entity\User;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use App\Game\Repository\GameRepository;
|
||||
use App\Game\Repository\SessionRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class GameDashboardServiceTest extends TestCase
|
||||
{
|
||||
private $entityManager;
|
||||
private $gameRepository;
|
||||
private $sessionRepository;
|
||||
private $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
$this->gameRepository = $this->createMock(GameRepository::class);
|
||||
$this->sessionRepository = $this->createMock(SessionRepository::class);
|
||||
|
||||
$this->service = new GameDashboardService(
|
||||
$this->gameRepository,
|
||||
$this->sessionRepository,
|
||||
$this->entityManager
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreateSessionInitializesRightsAndPwd(): void
|
||||
{
|
||||
$game = new Game();
|
||||
$game->setStatus(GameStatus::OPEN);
|
||||
|
||||
$user = new User();
|
||||
$user->setUsername('testuser');
|
||||
|
||||
$this->entityManager->expects($this->exactly(7))
|
||||
->method('persist');
|
||||
// 1. Session, 2. Player, 3. SessionSetting (rights), 4. SessionSetting (pwd), 5. SessionSetting (chat tracking), 6. SessionSetting (verify codes), 7. SessionSetting (verification progress)
|
||||
|
||||
$session = $this->service->createSession($game, $user, false);
|
||||
|
||||
$this->assertInstanceOf(Session::class, $session);
|
||||
}
|
||||
|
||||
public function testJoinSessionInitializesRightsAndPwd(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->setUsername('testuser');
|
||||
$session = new Session();
|
||||
|
||||
$setting = new SessionSetting();
|
||||
$setting->setSession($session);
|
||||
$setting->setName(SessionSettingType::INVITE_CODE);
|
||||
$setting->setValue('abc-123');
|
||||
|
||||
$repo = $this->createMock(\Doctrine\ORM\EntityRepository::class);
|
||||
$this->entityManager->method('getRepository')
|
||||
->willReturn($repo);
|
||||
|
||||
$repo->method('findOneBy')
|
||||
->willReturn($setting);
|
||||
|
||||
$this->entityManager->expects($this->exactly(6))
|
||||
->method('persist');
|
||||
// 1. Player, 2. SessionSetting (rights), 3. SessionSetting (pwd), 4. SessionSetting (chat tracking), 5. SessionSetting (verify codes), 6. SessionSetting (verification progress)
|
||||
|
||||
$result = $this->service->joinSession('abc-123', $user);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testLeaveSession(): void
|
||||
{
|
||||
$user = new User();
|
||||
$session = new Session();
|
||||
$session->setStatus(\App\Game\Enum\SessionStatus::CREATED);
|
||||
$session->setTimer(0);
|
||||
|
||||
$player1 = new Player();
|
||||
$player1->setUser($user);
|
||||
$player1->setSession($session);
|
||||
$session->addPlayer($player1);
|
||||
|
||||
$player2 = new Player();
|
||||
$player2->setUser(new User());
|
||||
$player2->setSession($session);
|
||||
$session->addPlayer($player2);
|
||||
|
||||
$setting = new SessionSetting();
|
||||
$setting->setPlayer($player1);
|
||||
$setting->setSession($session);
|
||||
$session->addSetting($setting);
|
||||
|
||||
$this->entityManager->expects($this->exactly(2))
|
||||
->method('remove');
|
||||
// 1. SessionSetting, 2. Player
|
||||
|
||||
$result = $this->service->leaveSession($session, $user);
|
||||
|
||||
$this->assertTrue($result);
|
||||
$this->assertCount(1, $session->getPlayers());
|
||||
}
|
||||
|
||||
public function testLeaveSessionDeletesSessionIfLastPlayer(): void
|
||||
{
|
||||
$user = new User();
|
||||
$session = new Session();
|
||||
$session->setStatus(\App\Game\Enum\SessionStatus::CREATED);
|
||||
$session->setTimer(0);
|
||||
|
||||
$player = new Player();
|
||||
$player->setUser($user);
|
||||
$player->setSession($session);
|
||||
$session->addPlayer($player);
|
||||
|
||||
$this->entityManager->expects($this->exactly(2))
|
||||
->method('remove');
|
||||
// 1. Player, 2. Session
|
||||
|
||||
$result = $this->service->leaveSession($session, $user);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user