EN
apps/cryo-studio / tests / CryoTestCase.php · commit 76aa68d
<?phpnamespace Tests;use App\Form\MainForm;use JumPHP\Gui\ListBox;use JumPHP\Gui\Widget;use JumPHP\Testing\GameTestCase;/** The window with the demo snapshots, and clicks on what a user clicks: a radio of the navigation, a line of a list. */abstract class CryoTestCase extends GameTestCase{    protected function form(array $params = []): MainForm    {        $this->game->start(MainForm::class, $params);        $this->game->run(2);        /** @var MainForm $form */        $form = $this->game->scene();        return $form;    }    protected function w(MainForm $form, string $id): Widget    {        $w = $form->widget($id);        self::assertNotNull($w, "widget $id");        return $w;    }    /** Clicks a radio / button of the window, then lets a frame run (the form lays itself out again). */    protected function clickOn(MainForm $form, string $id): void    {        $this->game->run(1);        $form->click($this->w($form, $id));        $this->game->run(1);    }    /** Clicks the line $index of a list, where it is drawn (the list is scrolled to it first). */    protected function clickRow(MainForm $form, string $id, int $index): void    {        $this->game->run(1);        $list = $this->w($form, $id);        self::assertInstanceOf(ListBox::class, $list);        $rh = $list->rowHeight($form->theme);        if (($index + 1) * $rh - $list->scrollY > $list->h - 2 || $index * $rh < $list->scrollY) {            $list->scrollY = max(0, $index * $rh);            $this->game->run(1);        }        $offset = $list->parent?->scrollOffset() ?? 0;        $form->clickAt($list->x + $list->w / 2, $list->y + 1 + $index * $rh + $rh / 2 - $list->scrollY - $offset);        $this->game->run(1);    }    /** @return list<string> */    protected function items(MainForm $form, string $id): array    {        return array_values((array)$this->w($form, $id)->props['items']);    }    protected function text(MainForm $form, string $id): string    {        return (string)$this->w($form, $id)->props['text'];    }    /** The index of the first line of a list that contains $needle. */    protected function rowWith(MainForm $form, string $id, string $needle): int    {        foreach ($this->items($form, $id) as $i => $item) {            if (str_contains($item, $needle)) {                return $i;            }        }        self::fail("no line with \"$needle\" in $id:\n" . implode("\n", $this->items($form, $id)));    }}