IT
games/dmmotor / tests / WadFolderTest.php · commit 9d248a4
<?phpnamespace Tests;use App\Dm\Wad;use App\Scene\PlayScene;use App\Scene\TitleScene;use App\Service\DmGame;use JumPHP\Externals\Entry;use JumPHP\Kernel\Engine;use JumPHP\Testing\GameTestCase;/** * Game tests of the wads/ folder: the WADs put there are found, analysed and played (a mod over its base game). */final class WadFolderTest extends GameTestCase{    private function dm(): DmGame    {        /** @var DmGame $game */        $game = $this->game->peek('game');        return $game;    }    /** A small mod: the first level of Phase 1 as MAP07 of a PWAD... no: as E1M3, alone. Returns its folder. */    private function makeMod(): string    {        $root = Engine::get()->config->path('assets/wads/freedoom1.wad');        $iwad = Wad::fromParts($root);        $start = $iwad->check('E1M1');        $lumps = [];        $lumps[] = ['E1M3', ''];        for ($i = 1; $i <= 10; $i++) {            $lumps[] = [$iwad->name($start + $i), $iwad->lumpNum($start + $i)];        }        $data = '';        $dir = '';        $pos = 12;        foreach ($lumps as [$name, $bytes]) {            $dir .= pack('VV', $pos, strlen($bytes)) . str_pad($name, 8, "\0");            $data .= $bytes;            $pos += strlen($bytes);        }        $folder = sys_get_temp_dir() . '/dmmotor-wads-' . getmypid();        @mkdir($folder, 0777, true);        file_put_contents("$folder/testmod.wad", 'PWAD' . pack('VV', count($lumps), $pos) . $data . $dir);        return $folder;    }    public function testAWadPutInTheFolderIsFoundAndAnalysed(): void    {        $this->game->start(TitleScene::class);        $dm = $this->dm();        $dm->catalog->scan([Engine::get()->config->path('assets/wads'), $this->makeMod()]);        self::assertContains('testmod', $dm->catalog->ids());        self::assertTrue($dm->catalog->isMod('testmod'));        self::assertSame(['E1M3'], $dm->catalog->analysis('testmod')['maps']);        self::assertSame('freedoom1', $dm->catalog->baseFor('testmod'));        self::assertStringContainsString('OVER PHASE 1', $dm->catalog->summary('testmod'));    }    public function testAModIsPlayedOverItsBaseGame(): void    {        $this->game->start(TitleScene::class);        $dm = $this->dm();        $dm->catalog->scan([Engine::get()->config->path('assets/wads'), $this->makeMod()]);        $this->game->start(PlayScene::class, ['iwad' => 'testmod', 'episode' => 1, 'map' => 3, 'skill' => 2, 'newGame' => true]);        self::assertSame('testmod', $dm->iwad);        self::assertSame([1, 3], $dm->firstMap);        // the mod's E1M3 is the first level of Phase 1: the player starts where E1M1's player does        self::assertEqualsWithDelta(-416, $dm->world->player->mo->x, 0.01);        $this->game->hold('up', 30);        $this->game->shot('mod');    }    public function testTheMenuListsTheWadsFolderThenTheGameWadsThenTheServerEachByName(): void    {        $this->game->start(TitleScene::class);        $dm = $this->dm();        $folder = $this->makeMod();        copy("$folder/testmod.wad", "$folder/amod.wad");        $dm->catalog->scan([Engine::get()->config->path('assets/wads'), $folder]);        $dm->catalog->addRemote([new Entry('dmmotor/wads/zmod.wad', 'file'), new Entry('dmmotor/wads/bmod.wad', 'file')]);        $ids = $dm->catalog->ids();        $mine = ['amod', 'testmod', 'freedoom1', 'freedoom2', 'net/bmod', 'net/zmod'];        self::assertSame($mine, array_values(array_intersect($ids, $mine)));        // the groups never mix (other tests may have left WADs of the server in the list)        $sources = array_map(fn (string $id): int => [1 => 0, 0 => 1, 2 => 2][$dm->catalog->source($id)], $ids);        $sorted = $sources;        sort($sorted);        self::assertSame($sorted, $sources);    }}