Verification done

This commit is contained in:
Frank
2026-01-07 20:06:28 +01:00
parent c6adb00219
commit de4b7bca6a
7 changed files with 726 additions and 8 deletions

View 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);
}
}

View File

@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
namespace App\Tests\Game;
use App\Game\Entity\Player;
use App\Game\Entity\Session;
use App\Game\Entity\SessionSetting;
use App\Game\Enum\SessionSettingType;
use App\Game\Repository\SessionSettingRepository;
use App\Game\Service\GameResponseService;
use App\Game\Service\PlayerService;
use App\Tech\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
class GameResponseServiceChatVerifyCodeTest extends TestCase
{
private $security;
private $playerService;
private $sessionSettingRepository;
private $hub;
private $entityManager;
private $service;
protected function setUp(): void
{
$this->security = $this->createMock(Security::class);
$this->playerService = $this->createMock(PlayerService::class);
$this->sessionSettingRepository = $this->createMock(SessionSettingRepository::class);
$this->hub = $this->createMock(HubInterface::class);
$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->service = new GameResponseService(
$this->security,
$this->playerService,
$this->sessionSettingRepository,
$this->hub,
$this->entityManager
);
$_ENV['MERCURE_TOPIC_BASE'] = 'http://test';
}
public function testChatRegeneratesVerifyCodesIfShared(): void
{
$user = new User();
$user->setUsername('testuser');
$session = $this->createMock(Session::class);
$session->method('getId')->willReturn(123);
$player = $this->createMock(Player::class);
$player->method('getUser')->willReturn($user);
$player->method('getScreen')->willReturn(1);
$player->method('getSession')->willReturn($session);
$this->security->method('getUser')->willReturn($user);
$this->playerService->method('GetCurrentlyActiveAsPlayer')->willReturn($player);
// Mock rights
$rightsSetting = new SessionSetting();
$rightsSetting->setValue(json_encode(['chat']));
$this->sessionSettingRepository->method('getSetting')
->willReturnMap([
[$session, SessionSettingType::RIGHTS_FOR_PLAYER1, $player, $rightsSetting],
]);
// Mock verify codes
$verifyCodesSetting = new SessionSetting();
$initialCodes = ['2' => 'secret123', '3' => 'secret456'];
$verifyCodesSetting->setValue(json_encode($initialCodes));
// Setting repository map for multiple calls
$this->sessionSettingRepository->method('getSetting')
->willReturnCallback(function($s, $t, $p = null) use ($rightsSetting, $verifyCodesSetting) {
if ($t === SessionSettingType::RIGHTS_FOR_PLAYER1) return $rightsSetting;
if ($t === SessionSettingType::VERIFY_CODES_FOR_PLAYER1) return $verifyCodesSetting;
return null;
});
// Expect Mercure updates: 1 for chat, 1 for notification
$this->hub->expects($this->exactly(2))
->method('publish');
$this->entityManager->expects($this->once())
->method('flush');
$raw = json_encode(['message' => '/chat Hello look at my code secret123', 'ts' => '123']);
$result = $this->service->getGameResponse($raw);
$this->assertEquals(['result' => ['succesfully send']], $result);
$newCodes = json_decode($verifyCodesSetting->getValue(), true);
$this->assertNotEquals('secret123', $newCodes['2']);
$this->assertEquals('secret456', $newCodes['3']);
}
}