FR
apps/winzik / tests / SpotifyTest.php · commit 6576d6f
<?phpnamespace Tests;use JumPHP\Async\Task;use JumPHP\Http\HttpResponse;/** The user's Spotify account: the browser comes back to WinZik (127.0.0.1:8888), then their playlists and songs. */final class SpotifyTest extends PlayerTestCase{    /** @var list<string> the addresses asked to Spotify */    private array $asked = [];    private function fakeSpotify(): void    {        $this->wz->options['spotifyId'] = 'client123';        $this->wz->spotifyRequest = function (string $method, string $url, ?string $body, array $headers): Task {            $this->asked[] = "$method $url";            $json = match (true) {                str_contains($url, '/api/token') => [200, ['access_token' => 'tok', 'expires_in' => 3600, 'refresh_token' => 'ref']],                str_contains($url, '/me/playlists') => [200, ['items' => [['id' => 'p1', 'name' => 'Ma playlist', 'tracks' => ['total' => 2]]], 'next' => null]],                str_contains($url, '/playlists/p1/tracks') => [404, ['error' => ['message' => 'gone']]],                str_contains($url, '/playlists/p1/items') => [200, ['items' => [                    ['track' => ['type' => 'track', 'name' => 'Mistral gagnant', 'artists' => [['name' => 'Renaud']]]],                    ['track' => ['type' => 'episode', 'name' => 'Un podcast']],                ], 'next' => null]],                default => [500, []],            };            if (str_contains($url, '/api/token')) {                parse_str((string)$body, $form);                // PKCE: the app's id in the form, its code_verifier, no secret                self::assertSame('client123', $form['client_id'] ?? null);                self::assertArrayNotHasKey('Authorization', $headers);            }            $t = new Task();            $t->settle(new HttpResponse($json[0], (string)json_encode($json[1])));            return $t;        };    }    public function testTheBrowserBackFromSpotifyConnectsWinZikThenItsPlaylistsAndSongsCome(): void    {        $this->startPlayer();        $this->fakeSpotify();        $account = $this->wz->spotify();        self::assertNotNull($account);        self::assertTrue($account->login());        self::assertSame('waiting', $account->state);        // the browser, sent back by Spotify to the redirect with the code and the state WinZik gave        $state = (string)(new \ReflectionProperty($account, 'expected'))->getValue($account);        $browser = stream_socket_client('tcp://127.0.0.1:8888', $no, $why, 2);        self::assertIsResource($browser, "WinZik listens on 8888: $why");        fwrite($browser, "GET /callback?code=CODE&state=$state HTTP/1.1\r\nHost: 127.0.0.1:8888\r\n\r\n");        $this->game->run(5);        self::assertStringContainsString('vous pouvez fermer cet onglet', (string)stream_get_contents($browser));        self::assertTrue($account->connected());        self::assertSame('ref', $this->wz->options['spotifyRefresh'], 'kept: no new login at the next start');        $lists = null;        $account->playlists()->then(function ($l) use (&$lists): void {            $lists = $l;        });        $this->game->run(3);        self::assertSame(['Titres likés', 'Ma playlist'], array_column((array)$lists, 'name'));        $songs = null;        $account->songs('p1')->then(function ($s) use (&$songs): void {            $songs = $s;        });        $this->game->run(3);        self::assertSame([['title' => 'Renaud - Mistral gagnant', 'url' => 'ytsearch1:Renaud Mistral gagnant']], $songs, 'the newer address, the podcast left out');    }    public function testTheWindowOfTheImportListsThePlaylistsOnceConnected(): void    {        $this->startPlayer();        $this->fakeSpotify();        $this->wz->options['spotifyRefresh'] = 'ref';       // connected before        $this->form->openSpotify();        $this->game->run(5);        $form = $this->game->scene();        self::assertInstanceOf(\App\Form\SpotifyForm::class, $form);        self::assertSame(['Titres likés', 'Ma playlist   (2)'], $form->widget('lists')->props['items']);        self::assertTrue($form->widget('import')->props['enabled']);        $this->game->shot('spotify-import');    }}