ltbxd-actorle/tests/Controller/GameControllerTest.php
2026-04-11 12:40:54 +02:00

96 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Controller;
use App\Controller\GameController;
use App\Entity\Game;
use App\Provider\GameGridProvider;
use App\Repository\GameRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class GameControllerTest extends TestCase
{
public function testStartPassesConfiguredPopularityRangeToGenerator(): void
{
$game = new Game();
$idProperty = new \ReflectionProperty(Game::class, 'id');
$idProperty->setValue($game, 42);
$generator = $this->createMock(GameGridProvider::class);
$generator
->expects($this->once())
->method('generate')
->with(
null,
$this->callback(function (array $config): bool {
$this->assertSame([
'minBucket' => 5,
'maxBucket' => 8,
], $config['popularityRange']);
$this->assertSame(['film', 'character', 'award'], $config['hintTypes']);
$this->assertArrayNotHasKey('watchedOnly', $config);
return true;
})
)
->willReturn($game);
$controller = new class extends GameController {
protected function isCsrfTokenValid(string $id, ?string $token): bool
{
return true;
}
public function getUser(): ?UserInterface
{
return null;
}
};
$controller->setContainer($this->createContainer());
$request = Request::create('/game/start', 'POST', [
'_token' => 'test-token',
'popularity_min_bucket' => '5',
'popularity_max_bucket' => '8',
]);
$request->setSession(new Session(new MockArraySessionStorage()));
$response = $controller->start(
$request,
$generator,
$this->createStub(GameRepository::class),
);
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame('/', $response->getTargetUrl());
$this->assertSame(42, $request->getSession()->get('current_game_id'));
}
private function createContainer(): ContainerInterface
{
$router = $this->createStub(UrlGeneratorInterface::class);
$router->method('generate')->willReturn('/');
$container = $this->createStub(ContainerInterface::class);
$container->method('has')->willReturnCallback(static fn (string $id): bool => match ($id) {
'router' => true,
'security.token_storage' => false,
default => false,
});
$container->method('get')->willReturnCallback(static fn (string $id) => match ($id) {
'router' => $router,
default => null,
});
return $container;
}
}