TR
games/runtime-demo / tests / RuntimeBadgeTest.php · commit 459a354
<?phpnamespace Tests;use App\Scene\DemoScene;use JumPHP\Net\RuntimeLink;use JumPHP\Runtime\ManualRuntime;use JumPHP\Runtime\RuntimeBadge;use JumPHP\Runtime\RuntimeGuide;use JumPHP\Testing\GameTestCase;/** The badge: its colour and words for each state of the runtime (a fake client), and what a click does. */final class RuntimeBadgeTest extends GameTestCase{    private ManualRuntime $fake;    private function demo(string $state): DemoScene    {        $this->fake = new ManualRuntime($state);        $this->game->engine->setRuntime($this->fake);        /** @var DemoScene $scene */        $scene = $this->game->start(DemoScene::class);        return $scene;    }    /** @return array<string, array{string, string, string}> */    public static function states(): array    {        return [            'ready is green' => [RuntimeLink::READY, 'green', RuntimeBadge::GREEN],            'running is orange' => [RuntimeLink::RUNNING, 'orange', RuntimeBadge::ORANGE],            'refused is orange' => [RuntimeLink::REFUSED, 'orange', RuntimeBadge::ORANGE],            'missing is red' => [RuntimeLink::MISSING, 'red', RuntimeBadge::RED],            'unsupported is grey' => [RuntimeLink::UNSUPPORTED, 'grey', RuntimeBadge::GREY],            'unknown is grey' => [RuntimeLink::UNKNOWN, 'grey', RuntimeBadge::GREY],            'checking is grey' => [RuntimeLink::CHECKING, 'grey', RuntimeBadge::GREY],        ];    }    /** @dataProvider states */    #[\PHPUnit\Framework\Attributes\DataProvider('states')]    public function testTheBadgeShowsTheColourOfTheState(string $state, string $kind, string $color): void    {        $scene = $this->demo($state);        $this->game->run(2);        $look = $scene->badge->current();        self::assertSame($kind, $look['kind']);        self::assertSame($color, $look['color']);        $this->game->shot('badge-' . $state);    }    public function testTheWordsTellWhatToDo(): void    {        self::assertSame('Local network active', RuntimeBadge::look(RuntimeLink::READY)['text']);        self::assertStringContainsString('Allow', RuntimeBadge::look(RuntimeLink::RUNNING)['text']);        self::assertStringContainsString('install', RuntimeBadge::look(RuntimeLink::MISSING)['text']);        self::assertSame('Server network', RuntimeBadge::look(RuntimeLink::UNSUPPORTED)['text']);        // net_mode server: the server's bridge is the choice, whatever the runtime says        self::assertSame('grey', RuntimeBadge::look(RuntimeLink::READY, 'server')['kind']);    }    public function testTheBadgeFollowsTheRuntimeWhenItChanges(): void    {        $scene = $this->demo(RuntimeLink::MISSING);        self::assertSame('red', $scene->badge->current()['kind']);        $this->fake->setState(RuntimeLink::READY);        self::assertSame('green', $scene->badge->current()['kind']);    }    public function testClickingOrangeAsksTheAuthorisationAndItTurnsGreen(): void    {        $scene = $this->demo(RuntimeLink::RUNNING);        $granted = null;        $scene->badge->on('paired', function ($sender, bool $yes) use (&$granted): void { $granted = $yes; });        $this->game->scene()->click($scene->badge);        self::assertSame([['runtime-demo', 'JumPHP runtime - example game']], $this->fake->pairs);        self::assertTrue($granted);        self::assertSame('green', $scene->badge->current()['kind']);    }    public function testARefusalLeavesItOrange(): void    {        $scene = $this->demo(RuntimeLink::RUNNING);        $this->fake->answer = false;        $this->game->scene()->click($scene->badge);        self::assertSame(RuntimeLink::REFUSED, $this->fake->state());        self::assertSame('orange', $scene->badge->current()['kind']);    }    public function testClickingRedOpensTheGuide(): void    {        $scene = $this->demo(RuntimeLink::MISSING);        $this->game->scene()->click($scene->badge);        $this->game->run(2);        self::assertInstanceOf(RuntimeGuide::class, $this->game->scene());        $this->game->shot('guide-opened-by-the-badge');    }    public function testGreyAndGreenDoNothingOnClick(): void    {        foreach ([RuntimeLink::READY, RuntimeLink::UNSUPPORTED] as $state) {            $scene = $this->demo($state);            $this->game->scene()->click($scene->badge);            $this->game->run(2);            self::assertSame([], $this->fake->pairs);            self::assertInstanceOf(DemoScene::class, $this->game->scene());        }    }    public function testTheRealClientIsUnsupportedOutsideTheBrowser(): void    {        $client = new \JumPHP\Runtime\RuntimeClient($this->game->engine->bridge, $this->game->engine->config);        self::assertSame(RuntimeLink::UNSUPPORTED, $client->state());        self::assertSame(RuntimeLink::UNSUPPORTED, $client->check()->result());        self::assertFalse($client->pair('x', 'X')->result());    }    public function testTheSwitchGoesBackToTheServerProxyAndIsRemembered(): void    {        $this->game->engine->options->set('net_use_server', false);        $client = new \JumPHP\Runtime\RuntimeClient($this->game->engine->bridge, $this->game->engine->config, true);        $this->game->engine->bridge->emit('runtime', [['ev' => 'hello', 'port' => 47820, 'apps' => [$this->game->engine->config->id()]]]);        $this->game->engine->setRuntime($client);        $this->game->start(DemoScene::class);        $this->game->run(3);        $badge = $this->game->scene()->widget('badge');        self::assertTrue($badge->switchable());        $this->game->shot('badge-switch');        self::assertSame('green', $badge->current()['kind']);        $badge->setUseServer(true);        self::assertTrue($client->usesServer());        self::assertSame('grey', $badge->current()['kind']);        self::assertTrue((bool)$this->game->engine->options->get('net_use_server', false));        $badge->setUseServer(false);        self::assertSame('green', $badge->current()['kind']);    }}