177 lines
8.1 KiB
PHP
177 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace Tests;
|
|
|
|
use App\Model\AudioInfo;
|
|
use App\Model\Id3Tag;
|
|
use App\Model\Lyrics;
|
|
use App\Model\Playlist;
|
|
use App\Model\Skin;
|
|
use App\Model\Text;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/** The files of 2001 (ID3v1, .LYR, .FLS, INFO.txt) and the lengths of the songs, without the engine. */
|
|
final class ModelTest extends TestCase
|
|
{
|
|
public function testAnId3TagWrittenIsReadBackWithItsAccentsAndGenre(): void
|
|
{
|
|
$file = tempnam(sys_get_temp_dir(), 'wz') . '.mp3';
|
|
file_put_contents($file, str_repeat("\0", 1000));
|
|
$tag = new Id3Tag('Ma chanson', 'Hélène', 'Album 2001', '2001', 'Fait avec WinZik', Id3Tag::genreIndex('Techno'));
|
|
self::assertTrue($tag->write($file));
|
|
self::assertSame(1128, filesize($file), 'the tag is added at the end');
|
|
$read = Id3Tag::read($file);
|
|
self::assertTrue($read->present);
|
|
self::assertSame('Hélène', $read->artist);
|
|
self::assertSame('Techno', $read->genreName());
|
|
// written again: replaced, not added a second time
|
|
$read->title = 'Autre titre';
|
|
$read->write($file);
|
|
clearstatcache();
|
|
self::assertSame(1128, filesize($file));
|
|
self::assertSame('Autre titre', Id3Tag::read($file)->title);
|
|
unlink($file);
|
|
}
|
|
|
|
public function testTheGenresAreTheEightyOfWinZik(): void
|
|
{
|
|
self::assertCount(80, Id3Tag::GENRES);
|
|
self::assertSame('00 Blues', Id3Tag::genreItems()[0]);
|
|
self::assertSame('79 Hard Rock', Id3Tag::genreItems()[79]);
|
|
}
|
|
|
|
public function testALyrFileOf2001ShowsTheLineSungAtEachTime(): void
|
|
{
|
|
$lyr = "[LYR]\r\n[ 1.2625274 ]\r\nMy first line\r\n[ 4,30 ]\r\nsecond line\r\n[ 7.1526389 ]\r\nthird line\r\n";
|
|
$lyrics = Lyrics::parse($lyr);
|
|
self::assertNotNull($lyrics);
|
|
self::assertSame('', $lyrics->lineAt(0.5), 'nothing before the first line');
|
|
self::assertSame('My first line', $lyrics->lineAt(2.0));
|
|
self::assertSame('second line', $lyrics->lineAt(4.3), 'a French decimal comma');
|
|
self::assertSame('third line', $lyrics->lineAt(100));
|
|
self::assertNull(Lyrics::parse("not a lyrics file\n"));
|
|
self::assertSame('My first line', Lyrics::parse($lyrics->toLyr())?->lineAt(2.0));
|
|
}
|
|
|
|
public function testTheDemoLyricsAreInWindows1252AndReadAsUtf8(): void
|
|
{
|
|
$lyrics = Lyrics::load(__DIR__ . '/../assets/music/winzik.lyr');
|
|
self::assertNotNull($lyrics);
|
|
self::assertSame("Une fenêtre s'allume ici", $lyrics->lineAt(13));
|
|
}
|
|
|
|
public function testNextAndPreviousGoRoundTheListLikeIn2001(): void
|
|
{
|
|
$list = new Playlist(['a.mp3', 'b.wav', 'c.mid']);
|
|
self::assertSame('a.mp3', $list->current());
|
|
self::assertSame('b.wav', $list->next());
|
|
self::assertSame('c.mid', $list->next());
|
|
self::assertSame('a.mp3', $list->next(), 'after the last one, the first');
|
|
self::assertSame('c.mid', $list->previous(), 'before the first one, the last');
|
|
$list->shuffle = true;
|
|
$list->setRandom(fn (int $max) => 1);
|
|
self::assertSame('b.wav', $list->next(), 'Hasard: any line');
|
|
}
|
|
|
|
public function testRemovingLinesKeepsTheSongPlaying(): void
|
|
{
|
|
$list = new Playlist(['a.mp3', 'b.mp3', 'c.mp3', 'd.mp3']);
|
|
$list->select(2);
|
|
$list->remove([0, 1]);
|
|
self::assertSame(['c.mp3', 'd.mp3'], $list->items);
|
|
self::assertSame('c.mp3', $list->current());
|
|
}
|
|
|
|
public function testAFlsFileIsWrittenAndReadAsVbWroteIt(): void
|
|
{
|
|
$list = new Playlist(['C:\\MP3\\Tête.mp3', '/home/moi/b.ogg']);
|
|
$fls = $list->toFls();
|
|
self::assertStringStartsWith("\"[FLS]\"\r\n\"C:\\MP3\\T", $fls);
|
|
self::assertSame(['C:\\MP3\\Tête.mp3', '/home/moi/b.ogg'], Playlist::parseFls($fls));
|
|
self::assertSame(['x.mp3'], Playlist::parseFls("[FLS]\nx.mp3\n"), 'without quotes too');
|
|
self::assertNull(Playlist::parseFls("[LYR]\n"));
|
|
}
|
|
|
|
public function testTheKindOfEachFileGivesItsIcon(): void
|
|
{
|
|
self::assertSame('mp3', Playlist::kind('a.MP3'));
|
|
self::assertSame('wav', Playlist::kind('a.wav'));
|
|
self::assertSame('mid', Playlist::kind('a.mid'));
|
|
self::assertSame('cd', Playlist::kind('Track01.cda'));
|
|
}
|
|
|
|
public function testTheLengthOfTheDemoSongsComesFromTheirHeaders(): void
|
|
{
|
|
$dir = __DIR__ . '/../assets/music/';
|
|
$mp3 = AudioInfo::of($dir . 'winzik.mp3');
|
|
self::assertEqualsWithDelta(48.5, $mp3->duration, 0.6);
|
|
self::assertSame(96, $mp3->kbps);
|
|
self::assertEqualsWithDelta(34.8, AudioInfo::of($dir . 'archettes-city.ogg')->duration, 0.3);
|
|
$wav = AudioInfo::of($dir . 'chaine-hifi.wav');
|
|
self::assertEqualsWithDelta(19.7, $wav->duration, 0.05);
|
|
self::assertSame(22050, $wav->sampleRate);
|
|
self::assertSame(0.0, AudioInfo::of($dir . 'nothing.mp3')->duration);
|
|
}
|
|
|
|
public function testTheTagOfTheDemoSong(): void
|
|
{
|
|
$tag = Id3Tag::read(__DIR__ . '/../assets/music/winzik.mp3');
|
|
self::assertSame('WinZik Band', $tag->artist);
|
|
self::assertSame('WinZik', $tag->title);
|
|
}
|
|
|
|
public function testVbColoursBecomeWebColours(): void
|
|
{
|
|
self::assertSame('#ffff00', Skin::vbColor('&H0000FFFF&'));
|
|
self::assertSame('#8080ff', Skin::vbColor('&H00FF8080&'));
|
|
self::assertSame('#8080ff', Skin::vbColor('16744576'));
|
|
self::assertNull(Skin::vbColor('Skin Chaine-Hifi'));
|
|
}
|
|
|
|
public function testTheSkinsOfTheApplicationAreFoundWithTheirInfo(): void
|
|
{
|
|
$skins = Skin::findAll([__DIR__ . '/../assets/skins']);
|
|
self::assertSame(['Ambre', 'Ampli 2000 (façon Winamp)', 'Aurore', 'Bois & Laiton', 'Carbone', 'Classique (façon Winamp 2)', 'Corail', 'Givre', 'Menthe', 'Néon 84', 'Phosphore', 'Sakura', 'Skin Chaine-Hifi', 'Skin de base', 'the SKIN', 'WINZIK'], array_map(fn (Skin $s) => $s->name, $skins));
|
|
$skin = $skins[12]; // Skin Chaine-Hifi
|
|
self::assertSame('#ffff00', $skin->colors['title']);
|
|
self::assertSame('#00ff00', $skin->colors['hours']);
|
|
self::assertSame('#ffffff', $skin->colors['active']);
|
|
self::assertCount(23, $skin->lineColors);
|
|
self::assertNotNull($skin->image('main'));
|
|
self::assertNotNull($skin->image('BPLAY'));
|
|
}
|
|
|
|
public function testPathsOfWindowsAndUnix(): void
|
|
{
|
|
self::assertSame('song.mp3', Text::baseName('C:\\MP3\\song.mp3'));
|
|
self::assertSame('song.mp3', Text::baseName('/home/song.mp3'));
|
|
self::assertSame('C:\\MP3\\song', Text::withoutExtension('C:\\MP3\\song.mp3'));
|
|
self::assertSame('mp3', Text::extension('/a/B.MP3'));
|
|
}
|
|
|
|
public function testASkinIsPackedAsAZipAndInstalledBack(): void
|
|
{
|
|
$zip = \App\Model\SkinPack::pack(__DIR__ . '/../assets/skins/classique');
|
|
self::assertStringStartsWith("PK\x03\x04", $zip);
|
|
$files = \App\Model\SkinPack::unzip($zip);
|
|
self::assertArrayHasKey('INFO.txt', $files);
|
|
self::assertArrayHasKey('main.png', $files);
|
|
$dir = sys_get_temp_dir() . '/skinpack-' . bin2hex(random_bytes(3));
|
|
self::assertTrue(\App\Model\SkinPack::install($zip, $dir));
|
|
self::assertSame('Classique (façon Winamp 2)', (new Skin($dir))->name);
|
|
self::assertSame(file_get_contents(__DIR__ . '/../assets/skins/classique/bplay.png'), file_get_contents($dir . '/bplay.png'));
|
|
// a zip made by another program (deflated) and with a folder inside is read too; other files are left out
|
|
if (class_exists(\ZipArchive::class)) {
|
|
$other = sys_get_temp_dir() . '/other-' . bin2hex(random_bytes(3)) . '.zip';
|
|
$z = new \ZipArchive();
|
|
$z->open($other, \ZipArchive::CREATE);
|
|
$z->addFromString('MySkin/INFO.txt', "[INF]\nMy skin\n");
|
|
$z->addFromString('MySkin/main.png', $files['main.png']);
|
|
$z->addFromString('MySkin/evil.php', '<?php echo 1;');
|
|
$z->close();
|
|
$read = \App\Model\SkinPack::unzip((string)file_get_contents($other));
|
|
self::assertSame(['INFO.txt', 'main.png'], array_keys($read));
|
|
}
|
|
self::assertFalse(\App\Model\SkinPack::install(\App\Model\SkinPack::zip(['readme.txt' => 'x']), $dir . '-no'), 'not a skin');
|
|
}
|
|
}
|