Archived
1
0
Fork 0

Initial Commit

This commit is contained in:
Henrik Hautakoski 2021-10-18 11:53:33 +02:00
commit ddf09fe00c
113 changed files with 187148 additions and 0 deletions

View file

@ -0,0 +1,22 @@
<?php
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
}

View file

@ -0,0 +1,33 @@
<?php
namespace Tests\Feature\Game;
use App\Models\Card;
use App\Game\GameBoard;
use App\Game\GameBoardState;
use App\Game\Exceptions\NotEnoughCardsException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class GameBoardTest extends TestCase
{
use RefreshDatabase;
public function test_exception_is_thrown_if_there_is_not_enough_cards_to_fill_it()
{
Card::factory()->count(10)->create();
$this->expectException(NotEnoughCardsException::class);
$board = new GameBoard(new GameBoardState(5, 5));
}
public function test_exception_is_not_thrown_if_there_is_enough_cards_to_fill_it()
{
Card::factory()->count(32)->create();
$board = new GameBoard(new GameBoardState(4, 4));
$this->assertSame(16, $board->getSize());
}
}

View file

@ -0,0 +1,158 @@
<?php
namespace Tests\Feature\Models;
use App\Models\Card;
use App\Models\Character;
use App\Models\Raid;
use App\Game\GameSettings;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CardTest extends TestCase
{
use RefreshDatabase;
protected $raids;
protected $characters;
public function setUp() : void
{
parent::setUp();
$raids = Raid::factory(2)->create();
$characters = Character::factory(2)->create();
Card::factory()->count(2)->for($raids[0])->create();
Card::factory()->count(2)->for($raids[1])->create();
Card::factory()->count(2)->for($characters[0])->create();
Card::factory()->count(2)->for($characters[1])->create();
Card::factory()->count(2)->create(['role' => 'dps']);
Card::factory()->count(2)->create(['role' => 'tank']);
Card::factory()->count(2)->create(['class' => 'priest']);
Card::factory()->count(2)->create(['class' => 'warrior']);
Card::factory()->count(2)->create();
$this->raids = $raids->pluck('id');
$this->characters = $characters->pluck('id');
}
public function provider(): array
{
return [
[ [ 0 ], 16 ],
[ [ 0, 1 ], 18 ],
];
}
public function providerClasses(): array
{
return [
[ [ 'priest' ], 16 ],
[ [ 'priest', 'warrior' ], 18 ],
];
}
public function providerRoles(): array
{
return [
[ [ 'dps' ], 16 ],
[ [ 'dps', 'tank' ], 18 ],
];
}
/**
* @dataProvider provider
*/
public function test_get_with_character_settings($characters, $count)
{
$settings = new GameSettings();
foreach($characters as $i) {
$settings->characters->set($this->characters[$i]);
}
$cards = Card::getBySettings($settings);
$this->assertSame($count, count($cards));
$expected = collect($characters)->map(function($i) {
return $this->characters[$i];
})->push(null);
foreach($cards as $card) {
$this->assertContains($card->character_id, $expected, $cards->pluck('character_id'));
}
}
/**
* @dataProvider provider
*/
public function test_get_with_raid_settings($raids, $count)
{
$settings = new GameSettings();
foreach($raids as $i) {
$settings->raids->set($this->raids[$i]);
}
$cards = Card::getBySettings($settings);
$this->assertSame($count, count($cards));
$expected = collect($raids)->map(function($item) {
return $this->raids[$item];
})->push(null);
foreach($cards as $card) {
$this->assertContains($card->raid_id, $expected);
}
}
/**
* @dataProvider providerClasses
*/
public function test_get_with_class_settings($classes, $count)
{
$settings = new GameSettings();
foreach($classes as $class) {
$settings->classes->set($class);
}
$cards = Card::getBySettings($settings);
$this->assertSame($count, count($cards));
$expected = collect($classes)->push(null);
foreach($cards as $card) {
$this->assertContains($card->class, $expected);
}
}
/**
* @dataProvider providerRoles
*/
public function test_get_with_role_settings($roles, $count)
{
$settings = new GameSettings();
foreach($roles as $role) {
$settings->roles->set($role);
}
$cards = Card::getBySettings($settings);
$this->assertSame($count, count($cards));
$expected = collect($roles)->push(null);
foreach($cards as $card) {
$this->assertContains($card->role, $expected);
}
}
}

10
tests/TestCase.php Normal file
View file

@ -0,0 +1,10 @@
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}

View file

@ -0,0 +1,152 @@
<?php
namespace Tests\Unit\Game;
use App\Game\GameBoardState;
use PHPUnit\Framework\TestCase;
class GameBoardStateTest extends TestCase
{
public function state(): array
{
return [
// 4x4 board
// ---------------
// 0, 1, 2, 3
// 4, 5, 6, 7
// 8, 9, 10, 11
// 12, 13, 14, 15
[ 4, 4, [], [ 1, 5, 8 ], 0 ],
[ 4, 4, [ 4, 5, 6, 7 ], [2, 3], 1 ],
[ 4, 4, [ 8, 9, 10, 11, 2, 6, 14 ], [ 15 ], 2 ],
// 5x3 board
// ---------------
// 0, 1, 2, 3, 4
// 5, 6, 7, 8, 9
// 10, 11, 12, 13, 14
[ 5, 3, [], [ 2, 4, 5, 8, 13 ], 0 ],
[ 5, 3, [ 5, 6, 7, 8, 9 ], [ 0, 3, 4, 10], 1 ],
[ 5, 3, [ 0, 5, 10, 3, 8, 13, 4, 9, 14 ], [ 6, 12 ], 3 ],
// 10x10 board
// --------------------------------------
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
// 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
// 20, 21, 22, 23, 24, 25, 26, 27, 28, 29
// 30, 31, 32, 33, 34, 35, 36, 37, 38, 39
// 40, 41, 42, 43, 44, 45, 46, 47, 48, 49
// 50, 51, 52, 53, 54, 55, 56, 57, 58, 59
// 60, 61, 62, 63, 64, 65, 66, 67, 68, 69
// 70, 71, 72, 73, 74, 75, 76, 77, 78, 79
// 80, 81, 82, 83, 84, 85, 86, 87, 88, 89
// 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
[ 10, 10, [], [ 59, 31, 52 ], 0 ],
[ 10, 10, [ 3, 13, 23, 33, 43, 53, 63, 73, 83, 93 ], [7, 11, 24, 25, 31, 32, 57, 66, 90, 91 ], 1 ],
[ 10, 10, [ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 4, 14, 34, 44, 54, 64, 74, 84, 94 ], [ 0, 32, 61, 78, 98 ], 2 ]
];
}
/**
* @dataProvider state
*/
public function test_is_win(int $w, int $h, array $state_win, array $state_press, int $winning)
{
$board = new GameBoardState($w, $h);
foreach($state_win + $state_press as $pos) {
$board->set($pos);
}
foreach($state_win as $pos) {
$this->assertTrue($board->isWin($pos));
}
foreach($state_press as $pos) {
$this->assertFalse($board->isWin($pos));
}
}
/**
* @dataProvider state
*/
public function test_is_pressed(int $w, int $h, array $state_win, array $state_press, int $winning)
{
$board = new GameBoardState($w, $h);
foreach($state_win + $state_press as $pos) {
$board->set($pos);
}
foreach($state_win + $state_press as $pos) {
$this->assertTrue($board->isPressed($pos));
}
}
/**
* @dataProvider state
*/
public function test_get_num_win(int $w, int $h, array $state_win, array $state_press, int $winning)
{
$board = new GameBoardState($w, $h);
foreach($state_win + $state_press as $pos) {
$board->set($pos);
}
$this->assertSame($winning, $board->getNumWinRows());
}
public function test_deselect_winning_pattern()
{
$board = new GameBoardState(3, 3);
// 0 [1] 2
// [3] [4] [5]
// 6 [7] 8
$board->set(1);
$board->set(4);
$board->set(7);
$board->set(3);
$board->set(5);
$this->assertTrue($board->isPressed(1));
$this->assertTrue($board->isPressed(3));
$this->assertTrue($board->isPressed(4));
$this->assertTrue($board->isPressed(5));
$this->assertTrue($board->isPressed(7));
$this->assertTrue($board->isWin(1));
$this->assertTrue($board->isWin(3));
$this->assertTrue($board->isWin(4));
$this->assertTrue($board->isWin(5));
$this->assertTrue($board->isWin(7));
$this->assertSame(2, $board->getNumWinRows());
// 0 [1] 2
// [3] 4 [5]
// 6 [7] 8
$board->set(4, false);
$this->assertFalse($board->isWin(1));
$this->assertFalse($board->isWin(3));
$this->assertFalse($board->isWin(4));
$this->assertFalse($board->isWin(5));
$this->assertFalse($board->isWin(7));
$this->assertTrue($board->isPressed(1));
$this->assertTrue($board->isPressed(3));
$this->assertFalse($board->isPressed(4));
$this->assertTrue($board->isPressed(5));
$this->assertTrue($board->isPressed(7));
$this->assertSame(0, $board->getNumWinRows());
}
}

View file

@ -0,0 +1,81 @@
<?php
namespace Tests\Unit\Support;
use App\Support\Set;
use PHPUnit\Framework\TestCase;
class SetTest extends TestCase
{
public function test_constructor_sets_values()
{
$set = new Set(['one' => true, 'two' => false, 'three' => true]);
$this->assertSame(['one', 'three'], $set->all()->toArray());
}
public function test_set_method()
{
$set = new Set(['one' => true, 'two' => true, 'three' => true]);
$set->set("two", false);
$this->assertSame(['one', 'three'], $set->all()->toArray());
$set->set("four");
$this->assertSame(['one', 'three', 'four'], $set->all()->toArray());
}
public function test_fill_method()
{
$set = new Set(['one' => true, 'two' => true, 'three' => true]);
$set->fill(["five" => true, "six" => true, "seven" => false ]);
$this->assertSame(['five', 'six'], $set->all()->toArray());
}
public function test_has_method()
{
$set = new Set(['one' => true, 'two' => true, 'three' => true]);
$this->assertTrue($set->has('one'));
$this->assertFalse($set->has('four'));
}
public function test_toggle_method()
{
$set = new Set(['one' => true, 'two' => true, 'three' => true]);
$set->toggle('two');
$set->toggle('four');
$this->assertSame(['one', 'three', 'four'], $set->all()->toArray());
}
public function test_count_method()
{
$set = new Set(['one' => true, 'two' => true, 'three' => true]);
$this->assertSame(3, $set->count());
}
public function test_clear_method()
{
$set = new Set(['one' => true, 'two' => true, 'three' => true]);
$set->clear();
$this->assertSame([], $set->all()->toArray());
}
public function test_to_array_method()
{
$arr = ['one' => true, 'two' => true];
$set = new Set($arr);
$this->assertSame($arr, $set->toArray());
}
}