PL
apps/winzik / tests / LyricsEditorTest.php · commit 6576d6f
<?phpnamespace Tests;use App\Form\LyricsEditorForm;use App\Form\MainForm;use App\Model\Lyrics;use JumPHP\Gui\Pointer;/** The lyrics editor: lines given their time while the song plays, saved as a .LYR the lyrics bar then shows. */final class LyricsEditorTest extends PlayerTestCase{    public function testLinesStampedWhileTheSongPlaysBecomeItsLyrics(): void    {        $this->startPlayer();        $this->wz->playIndex(1);            // archettes-city.ogg: no lyrics        $this->form->openLyricsEditor();        $this->game->run(2);        $editor = $this->game->scene();        self::assertInstanceOf(LyricsEditorForm::class, $editor);        $editor->widget('text')->text = "Première ligne\nDeuxième ligne\n\nTroisième ligne";        $this->game->run(1);        self::assertCount(3, $editor->widget('synced')->props['items'], 'the empty line is not sung');        $editor->click($editor->widget('start'));        $this->game->wait(1);        $editor->click($editor->widget('stamp'));        $this->game->wait(2);        $editor->click($editor->widget('stamp'));        $this->game->wait(1.5);        $editor->click($editor->widget('stamp'));        $this->game->shot('lyrics-editor');        $lyrics = $editor->preview();        self::assertCount(3, $lyrics->lines);        self::assertEqualsWithDelta(1.0, $lyrics->lines[0][0], 0.1);        self::assertEqualsWithDelta(3.0, $lyrics->lines[1][0], 0.1);        $editor->click($editor->widget('save'));        self::assertFileExists($this->wz->lyricsCachePath((string)$this->wz->player->file), 'a song of the application: kept in var/lyr');        $editor->click($editor->widget('quit'));        $this->game->run(1);        $this->wz->seek(3.5);        self::assertSame('Deuxième ligne', $this->wz->lyricLine());        self::assertSame('Deuxième ligne', Lyrics::load($this->wz->lyricsCachePath((string)$this->wz->player->file))?->lineAt(3.5), 'written in Windows-1252, read back');        unlink($this->wz->lyricsCachePath((string)$this->wz->player->file));    // the other tests: a song without lyrics        $this->wz->refreshLyrics();    }    public function testTheEditorOpensTheLyricsTheSongAlreadyHas(): void    {        $this->startPlayer();        // the demo lyrics as shipped: a copy shifted by another test (the -0.1 s of the lyrics bar) is not them        $this->wz->playIndex(0);        @unlink($this->wz->storagePath('lyr/winzik.lyr'));        @unlink($this->wz->lyricsCachePath((string)$this->wz->player->file));        $this->wz->refreshLyrics();        $this->form->openLyricsEditor();        $this->game->run(2);        $editor = $this->game->scene();        self::assertInstanceOf(LyricsEditorForm::class, $editor);        self::assertCount(12, $editor->widget('synced')->props['items']);        self::assertSame("[  24.00 ]  WinZik, WinZik, le son revient", $editor->widget('synced')->props['items'][6]);    }    public function testASyllableTakesItsMomentWhileHeardThenAMillisecondTuneAndComesBackSaved(): void    {        $this->startPlayer();        $this->wz->playIndex(1);            // archettes-city: no lyrics        $lyr = $this->wz->lyricsCachePath((string)$this->wz->player->file);        @mkdir(dirname($lyr), 0777, true);        // lyrics with an empty first line (its time kept the next ones one line late before)        file_put_contents($lyr, "[LYR]\r\n[ 0.5 ]\r\n\r\n[ 2 ]\r\nMistral gagnant\r\n[ 6 ]\r\nLe retour\r\n");        $this->wz->refreshLyrics();        $this->form->openLyricsEditor();        $this->game->run(2);        $editor = $this->game->scene();        self::assertInstanceOf(LyricsEditorForm::class, $editor);        self::assertSame('[   2.00 ]  Mistral gagnant', $editor->widget('synced')->props['items'][0], 'its own time, not the empty line\'s');        // the first line chosen: its syllables in the strip        $editor->widget('synced')->selected = 0;        $this->game->run(1);        self::assertSame(['Mis', 'tral ', 'ga', 'gnant'], $editor->widget('strip')->props['syllables']);        // heard: "ga" at 3.0 s, then +10 ms        $editor->sylAt = 2;        $this->wz->seek(3.0);        $editor->click($editor->widget('sylStamp'));        self::assertSame(3, $editor->sylAt, 'the next syllable chosen');        $editor->sylAt = 2;        $editor->click($editor->widget('p10'));        self::assertEqualsWithDelta(3.01, $editor->syllableTimes(0)[2], 0.002);        $editor->click($editor->widget('save'));        $saved = (string)file_get_contents($lyr);        self::assertStringContainsString('<00:03.01>ga', $saved, 'each syllable with its moment (enhanced LRC)');        $editor->click($editor->widget('quit'));        $this->game->run(1);        // shown without the moments, and found again by the editor        $this->wz->seek(3.2);        self::assertSame('Mistral gagnant', $this->wz->lyricLine());        $this->form->openLyricsEditor();        $this->game->run(2);        $editor = $this->game->scene();        self::assertInstanceOf(LyricsEditorForm::class, $editor);        self::assertEqualsWithDelta(3.01, $editor->syllableTimes(0)[2], 0.002);        $this->game->shot('lyrics-editor-syllables');        $editor->click($editor->widget('quit'));        unlink($lyr);        $this->wz->refreshLyrics();    }    /** The editor on archettes-city (no lyrics), with these lines typed. */    private function editorWith(string $text): LyricsEditorForm    {        $this->startPlayer();        $this->wz->playIndex(1);        $this->form->openLyricsEditor();        $this->game->run(2);        $editor = $this->game->scene();        self::assertInstanceOf(LyricsEditorForm::class, $editor);        $editor->widget('text')->text = $text;        $this->game->run(1);        return $editor;    }    /** A key as the form gets it (Pointer::RETURN, an arrow, a letter with Ctrl). */    private function press(LyricsEditorForm $editor, int $key, bool $ctrl = false, bool $shift = false): void    {        $p = new Pointer();        [$p->keys, $p->ctrl, $p->shift] = [[$key], $ctrl, $shift];        $editor->handle($p);    }    public function testEntreeTimesEachLineWhileItPlaysAndCtrlZTakesBackAWrongTap(): void    {        $editor = $this->editorWith("Un\nDeux\nTrois");        $editor->click($editor->widget('start'));        $this->game->wait(1);        $this->press($editor, Pointer::RETURN);        $this->game->wait(1);        $this->press($editor, Pointer::RETURN);        $this->press($editor, Pointer::RETURN);    // one tap too many: "Trois" too early        self::assertSame(3, $editor->next);        $this->press($editor, ord('z'), true);        self::assertSame(2, $editor->next, 'Ctrl+Z: "Trois" is to be given again');        self::assertCount(2, $editor->preview()->lines);        $this->game->wait(1);        $this->press($editor, Pointer::RETURN);        $lines = $editor->preview()->lines;        self::assertEqualsWithDelta(1.0, $lines[0][0], 0.1);        self::assertEqualsWithDelta(2.0, $lines[1][0], 0.1);        self::assertEqualsWithDelta(3.0, $lines[2][0], 0.1);        // back and forth        $this->press($editor, ord('z'), true);        $this->press($editor, ord('y'), true);        self::assertEqualsWithDelta(3.0, $editor->preview()->lines[2][0], 0.1);        self::assertSame([4, 0], $editor->history());        $this->game->shot('lyrics-editor-keys');    }    public function testTheArrowsNudgeTheTimeOfTheLineChosenWithoutWritingItsSyllables(): void    {        $this->startPlayer();        $this->wz->playIndex(1);        $lyr = $this->wz->lyricsCachePath((string)$this->wz->player->file);        @mkdir(dirname($lyr), 0777, true);        file_put_contents($lyr, "[LYR]\r\n[ 2 ]\r\nMistral gagnant\r\n[ 6 ]\r\nLe retour\r\n");        $this->wz->refreshLyrics();        $this->form->openLyricsEditor();        $this->game->run(2);        $editor = $this->game->scene();        self::assertInstanceOf(LyricsEditorForm::class, $editor);        $editor->widget('synced')->selected = 1;        $this->game->run(1);        $this->press($editor, Pointer::RIGHT);        $this->press($editor, Pointer::RIGHT);        $this->press($editor, Pointer::LEFT, false, true);    // Maj: 10 ms        self::assertEqualsWithDelta(6.19, $editor->preview()->lines[1][0], 0.001);        self::assertSame('[   6.19 ]  Le retour', $editor->widget('synced')->props['items'][1]);        self::assertStringContainsString('-10 ms', $editor->widget('status')->props['text']);        self::assertTrue($editor->hasChanges());        $this->press($editor, ord('s'), true);    // Ctrl+S        $saved = (string)file_get_contents($lyr);        self::assertStringContainsString("[ 6.19 ]\r\nLe retour", $saved);        self::assertStringNotContainsString('<', $saved, 'the line moved, its syllables not written one by one');        self::assertFalse($editor->hasChanges());        unlink($lyr);        $this->wz->refreshLyrics();    }    public function testEntreeOnALineChosenWhileTheSongPlaysTimesFromThatLine(): void    {        $editor = $this->editorWith("Un\nDeux\nTrois");        $this->wz->play();        $this->wz->seek(10);        $this->game->run(1);        $list = $editor->widget('synced');        $list->selected = 1;        $editor->setFocus($list);        $this->press($editor, Pointer::RETURN);        self::assertSame(2, $editor->next);        self::assertEqualsWithDelta(10.0, $editor->preview()->lines[0][0], 0.2);        self::assertSame('Deux', $editor->preview()->lines[0][1]);        self::assertSame($editor->widget('stamp'), $editor->focused(), 'the next Entrée gives the next line');    }    public function testClosingWithLyricsNotSavedAsksFirstThenEchapAgainCloses(): void    {        $editor = $this->editorWith("Un\nDeux");        $editor->click($editor->widget('start'));        $this->game->wait(0.5);        $this->press($editor, Pointer::RETURN);        $editor->key(Pointer::ESCAPE);        $this->game->run(2);        self::assertSame($editor, $this->game->scene(), 'not closed: not saved');        self::assertStringContainsString('Pas enregistré', $editor->widget('status')->props['text']);        $this->game->shot('lyrics-editor-quit-asked');        $editor->key(Pointer::ESCAPE);        $this->game->run(3);        self::assertInstanceOf(MainForm::class, $this->game->scene());    }    public function testASaveThatCannotBeWrittenSaysSoInFrenchWithoutAPhpWarning(): void    {        $editor = $this->editorWith("Un\nDeux");        $editor->click($editor->widget('start'));        $this->game->wait(0.5);        $this->press($editor, Pointer::RETURN);        // where the lyrics go, a folder already: no file can be written there        $lyr = $this->wz->lyricsCachePath((string)$this->wz->player->file);        @mkdir($lyr, 0777, true);        $warnings = [];        set_error_handler(function (int $no, string $message) use (&$warnings): bool {            if ((error_reporting() & $no) !== 0) {                $warnings[] = $message;            }            return true;        });        try {            $editor->click($editor->widget('save'));        } finally {            restore_error_handler();            rmdir($lyr);        }        self::assertSame([], $warnings);        self::assertStringContainsString('Paroles NON enregistrées', $editor->widget('status')->props['text']);        self::assertTrue($editor->hasChanges(), 'still to be saved');    }}