|
|
| (11 intermediate revisions by 2 users not shown) |
| Line 23: |
Line 23: |
|
| |
|
| == Automated Testing == | | == Automated Testing == |
| ===JavaScript=== | | [https://doc.studio.boardgamearena.com/framework-documentation/back-end/php-unit-testing/ <span class="mw-ui-button mw-ui-progressive mw-ui-big">Back-end (PHP)</span>] |
|
| |
|
| I have not tried it. If you have any success please add instructions here. Theoretically its is possible to hook something like selenium to studio games
| | [https://doc.studio.boardgamearena.com/framework-documentation/front-end/unit-testing/ <span class="mw-ui-button mw-ui-progressive mw-ui-big">Front-end (JS/TS)</span>] |
| | |
| ===PHP===
| |
| | |
| ====Install PHPunit====
| |
| | |
| Install PHPunit via composer somewhere in home directory, i.e.
| |
| cd /home/<yourusername>/php-composer
| |
| composer require --dev phpunit/phpunit ^13
| |
| | |
| Then add this to your .bashrc (alias function to be able to test phpunit easily)
| |
| phpunit() {
| |
| /home/<yourusername>/php-composer/vendor/bin/phpunit \
| |
| -c "$PWD/phpunit.xml.dist" \
| |
| --bootstrap "$PWD/tests/bootstrap.php" \
| |
| "$@"
| |
| }
| |
| | |
| With that, on a new terminal you will be able to run the tests with one of the following commands at the root of your project :
| |
| phpunit
| |
| phpunit tests/BoardManagerTest.php
| |
| phpunit --filter testGivesExtraTime
| |
| | |
| | |
| ====Setup minimal files====
| |
| In the tests folder of your project (create it if needed), add :
| |
| | |
| '''tests/bootstrap.php'''
| |
| <pre> | |
| <?php
| |
| | |
| declare(strict_types=1);
| |
| | |
| require_once __DIR__ . '/stubs/BgaFrameworkStubs.php';
| |
| | |
| $autoload = __DIR__ . '/../vendor/autoload.php';
| |
| if (file_exists($autoload)) {
| |
| require_once $autoload;
| |
| }
| |
| | |
| spl_autoload_register(static function (string $class): void {
| |
| $prefix = 'Bga\\Games\\Reversi\\';
| |
| | |
| if (!str_starts_with($class, $prefix)) {
| |
| return;
| |
| }
| |
| | |
| $relativeClass = substr($class, strlen($prefix));
| |
| $path = __DIR__ . '/../modules/php/' . str_replace('\\', '/', $relativeClass) . '.php';
| |
| | |
| if (file_exists($path)) {
| |
| require_once $path;
| |
| }
| |
| });
| |
| </pre>
| |
| (replace Reversi by your project name)
| |
| | |
| '''tests/stubs/BgaFrameworkStubs.php'''
| |
| <pre>
| |
| <?php
| |
| | |
| declare(strict_types=1);
| |
| | |
| require_once __DIR__ . '/stubs/BgaFrameworkStubs.php';
| |
| | |
| $autoload = __DIR__ . '/../vendor/autoload.php';
| |
| if (file_exists($autoload)) {
| |
| require_once $autoload;
| |
| }
| |
| | |
| spl_autoload_register(static function (string $class): void {
| |
| $prefix = 'Bga\\Games\\Reversi\\';
| |
| | |
| if (!str_starts_with($class, $prefix)) {
| |
| return;
| |
| }
| |
| | |
| $relativeClass = substr($class, strlen($prefix));
| |
| $path = __DIR__ . '/../modules/php/' . str_replace('\\', '/', $relativeClass) . '.php';
| |
| | |
| if (file_exists($path)) {
| |
| require_once $path;
| |
| }
| |
| });
| |
| </pre>
| |
| | |
| ====Create first tests====
| |
| Example of tests for Reversi:
| |
| | |
| '''tests/BoardManagerTest.php'''
| |
| <pre>
| |
| <?php
| |
| | |
| declare(strict_types=1);
| |
| | |
| namespace Tests;
| |
| | |
| use Bga\Games\Reversi\BoardManager;
| |
| use Bga\Games\Reversi\Game;
| |
| use PHPUnit\Framework\TestCase;
| |
| | |
| final class BoardManagerTest extends TestCase
| |
| {
| |
| public function testGetPossibleMovesOnInitialBoardForBlackPlayer(): void
| |
| {
| |
| $board = $this->createEmptyBoard(8);
| |
| $board[4][4] = 2;
| |
| $board[5][5] = 2;
| |
| $board[4][5] = 1;
| |
| $board[5][4] = 1;
| |
| | |
| $game = $this->createGame(8);
| |
| $boardManager = $this->createBoardManager($game, $board);
| |
| | |
| $moves = $boardManager->getPossibleMoves(1);
| |
| | |
| self::assertSame(
| |
| [
| |
| 3 => [4 => true],
| |
| 4 => [3 => true],
| |
| 5 => [6 => true],
| |
| 6 => [5 => true],
| |
| ],
| |
| $moves,
| |
| );
| |
| }
| |
| | |
| public function testGetPossibleMovesReturnsEmptyArrayWhenNoMoveExists(): void
| |
| {
| |
| $board = $this->createEmptyBoard(4);
| |
| for ($x = 1; $x <= 4; $x++) {
| |
| for ($y = 1; $y <= 4; $y++) {
| |
| $board[$x][$y] = 1;
| |
| }
| |
| }
| |
| | |
| $game = $this->createGame(4);
| |
| $boardManager = $this->createBoardManager($game, $board);
| |
| | |
| self::assertSame([], $boardManager->getPossibleMoves(2));
| |
| }
| |
| | |
| private function createGame(int $boardSize): Game
| |
| {
| |
| $game = $this->createStub(Game::class);
| |
| $game->method('getBoardSize')->willReturn($boardSize);
| |
| | |
| return $game;
| |
| }
| |
| | |
| private function createBoardManager(Game $game, array $board): BoardManager
| |
| {
| |
| return new class ($game, $board) extends BoardManager {
| |
| public function __construct(Game $game, private array $board)
| |
| {
| |
| parent::__construct($game);
| |
| }
| |
| | |
| public function getBoard(): array
| |
| {
| |
| return $this->board;
| |
| }
| |
| };
| |
| }
| |
| | |
| private function createEmptyBoard(int $size): array
| |
| {
| |
| $board = [];
| |
| for ($x = 1; $x <= $size; $x++) {
| |
| $board[$x] = [];
| |
| for ($y = 1; $y <= $size; $y++) {
| |
| $board[$x][$y] = null;
| |
| }
| |
| }
| |
| | |
| return $board;
| |
| }
| |
| }
| |
| </pre>
| |
| | |
| | |
| '''tests/States/NextPlayerTest.php'''
| |
| <pre>
| |
| <?php
| |
| | |
| declare(strict_types=1);
| |
| | |
| namespace Tests\States;
| |
| | |
| use Bga\Games\Reversi\BoardManager;
| |
| use Bga\Games\Reversi\Game;
| |
| use Bga\Games\Reversi\States\NextPlayer;
| |
| use PHPUnit\Framework\TestCase;
| |
| | |
| final class NextPlayerTest extends TestCase
| |
| {
| |
| public function testReturnsEndScoreWhenNoFreeSquareLeft(): void
| |
| {
| |
| $game = $this->createGame();
| |
| $this->attachBoardManager($game, $this->createBoardManagerStub(0, []));
| |
| | |
| $state = new NextPlayer($game);
| |
| | |
| self::assertSame('Bga\\Games\\Reversi\\States\\EndScore', $state->onEnteringState());
| |
| }
| |
| | |
| public function testReturnsEndScoreWhenActivePlayerHasNoDisc(): void
| |
| {
| |
| $game = $this->createGame();
| |
| $this->attachBoardManager($game, $this->createBoardManagerStub(10, [2 => 5]));
| |
| | |
| $state = new NextPlayer($game);
| |
| | |
| self::assertSame('Bga\\Games\\Reversi\\States\\EndScore', $state->onEnteringState());
| |
| }
| |
| | |
| public function testReturnsEndScoreWhenNeitherPlayerCanMove(): void
| |
| {
| |
| $game = $this->createGame(playerAfterId: 2);
| |
| $this->attachBoardManager($game, $this->createBoardManagerStub(5, [1 => 4, 2 => 4], []));
| |
| | |
| $state = new NextPlayer($game);
| |
| | |
| self::assertSame('Bga\\Games\\Reversi\\States\\EndScore', $state->onEnteringState());
| |
| }
| |
| | |
| public function testPassesTurnWhenActivePlayerCannotMoveButOpponentCan(): void
| |
| {
| |
| $game = $this->createGameMock(playerAfterId: 2);
| |
| $game->expects(self::never())->method('giveExtraTime');
| |
| $this->attachBoardManager($game, $this->createBoardManagerStub(5, [1 => 4, 2 => 4], [2 => [4 => [3 => true]]]));
| |
| | |
| $state = new NextPlayer($game);
| |
| | |
| self::assertSame(NextPlayer::class, $state->onEnteringState());
| |
| }
| |
| | |
| public function testGivesExtraTimeAndReturnsPlayDiscWhenPlayerCanMove(): void
| |
| {
| |
| $game = $this->createGameMock();
| |
| $game->expects(self::once())
| |
| ->method('giveExtraTime')
| |
| ->with(1, null);
| |
| $this->attachBoardManager($game, $this->createBoardManagerStub(5, [1 => 4, 2 => 4], [1 => [4 => [3 => true]]]));
| |
| | |
| $state = new NextPlayer($game);
| |
| | |
| self::assertSame('Bga\\Games\\Reversi\\States\\PlayDisc', $state->onEnteringState());
| |
| }
| |
| | |
| private function createGame(int $activePlayerId = 1, int $playerAfterId = 2): Game
| |
| {
| |
| $game = $this->createStub(Game::class);
| |
| $game->method('activeNextPlayer')->willReturn($activePlayerId);
| |
| $game->method('getPlayerAfter')->willReturn($playerAfterId);
| |
| | |
| return $game;
| |
| }
| |
| | |
| private function createGameMock(int $activePlayerId = 1, int $playerAfterId = 2): Game
| |
| {
| |
| $game = $this->getMockBuilder(Game::class)
| |
| ->disableOriginalConstructor()
| |
| ->onlyMethods(['activeNextPlayer', 'getPlayerAfter', 'giveExtraTime'])
| |
| ->getMock();
| |
| $game->method('activeNextPlayer')->willReturn($activePlayerId);
| |
| $game->method('getPlayerAfter')->willReturn($playerAfterId);
| |
| | |
| return $game;
| |
| }
| |
| | |
| private function createBoardManagerStub(int $freeSquares, array $discsByPlayer, array $possibleMovesByPlayer = []): BoardManager
| |
| {
| |
| $boardManager = $this->createStub(BoardManager::class);
| |
| $boardManager->method('countFreeSquares')->willReturn($freeSquares);
| |
| $boardManager->method('getDiscCountsByPlayer')->willReturn($discsByPlayer);
| |
| $boardManager->method('getPossibleMoves')->willReturnCallback(
| |
| static fn (int $playerId): array => $possibleMovesByPlayer[$playerId] ?? []
| |
| );
| |
| | |
| return $boardManager;
| |
| }
| |
| | |
| private function attachBoardManager(Game $game, BoardManager $boardManager): void
| |
| {
| |
| $game->boardManager = $boardManager;
| |
| }
| |
| }
| |
| </pre>
| |
| | |
| | |
| You can hook up phpunit and write unit tests for php, for that you need stubs for framework functions and classes.
| |
| | |
| * Install php-cli (you need a verson that bga runs - consult https://en.doc.boardgamearena.com/Studio#Software_Versions)
| |
| * Install phpunit matching this specific php version (the latest does not work), if you use composer DO not install it in the game directory - install it somewhere else
| |
| * Create directory tests (at the root of the game, not in modules)
| |
| * Checkout/get stubs from github (see https://en.doc.boardgamearena.com/Setting_up_BGA_Development_environment_using_VSCode), i.e
| |
| | |
| * So the main issue - php unit needs to find the parts of bga framework which is not available, in this example we will use stubs above
| |
| * Create autoload.php in modules/ directory
| |
| <pre>
| |
| <?php
| |
| | |
| define("APP_GAMEMODULE_PATH", getenv('APP_GAMEMODULE_PATH'));
| |
| | |
| spl_autoload_register(function ($class_name) {
| |
|
| |
| switch ($class_name) {
| |
| case "APP_DbObject":
| |
| //var_dump($class_name);
| |
| //var_dump(APP_GAMEMODULE_PATH);
| |
| include APP_GAMEMODULE_PATH."/module/table/table.game.php";
| |
| break;
| |
| default:
| |
| include $class_name . ".php";
| |
| break;
| |
| }
| |
| });
| |
| | |
| ?>
| |
| </pre>
| |
| * Set env var APP_GAMEMODULE_PATH to point to where stubs are, in this case APP_GAMEMODULE_PATH=$HOME/git/bga-sharedcode/misc/
| |
| * Create a test in tests/ folder (see phpunit examples)
| |
| <pre>
| |
| <?php declare(strict_types=1);
| |
| use PHPUnit\Framework\TestCase;
| |
| | |
| require_once "../mygame.game.php";
| |
| | |
| class GameUT extends MyGame {
| |
| function __construct() {
| |
| parent::__construct();
| |
| include "../material.inc.php";
| |
| }
| |
| // override/stub methods here that access db and stuff
| |
| }
| |
| | |
| final class GameTest extends TestCase {
| |
| public function testGameProgression() {
| |
| $m = new GameUT();
| |
| $this->assertEquals(0,$m->getGameProgression());
| |
| }
| |
| // more tests
| |
| }
| |
| | |
| | |
| </pre> | |
| * Run this from modules/ dir (this assumes env var above already set globally)
| |
| phpunit --bootstrap autoload.php tests/
| |
|
| |
|
| == Play testing on studio == | | == Play testing on studio == |