Entrar
PT
apps/caramail-chat / tests / SmileysTest.php · commit 6eebcdb
<?phpnamespace Tests;use App\Chat\Smileys;use App\Gui\SmileyPainter;use PHPUnit\Framework\TestCase;/** The smileys of the applet: found as it found them, drawn pixel for pixel as it drew them. */final class SmileysTest extends TestCase{    public function testAComposedFaceNeedsItsNose(): void    {        self::assertSame(['Salut :) toi'], Smileys::parse('Salut :) toi'));        $parts = Smileys::parse('Salut :-) toi');        self::assertSame('Salut ', $parts[0]);        self::assertSame(['kind' => 'face', 'eyes' => 0, 'nose' => 3, 'mouth' => 0, 'color' => 10, 'hands' => 0, 'source' => ':-)'], $parts[1]);        self::assertSame(' toi', $parts[2]);    }    public function testAColourDigitAndHandsFollowTheFace(): void    {        $face = Smileys::parse(';o)2#cd')[0];        self::assertSame([1, 7, 0, 2, 2 | 4, ';o)2#cd'], [$face['eyes'], $face['nose'], $face['mouth'], $face['color'], $face['hands'], $face['source']]);    }    public function testTheDrawnPicturesAndTheirColours(): void    {        self::assertSame(['kind' => 'big', 'n' => 13, 'color' => 1, 'source' => '#13'], Smileys::parse('#13')[0]);        self::assertSame(['kind' => 'big', 'n' => 13, 'color' => 5, 'source' => '#135'], Smileys::parse('#135')[0]);        self::assertSame(['kind' => 'big', 'n' => 5, 'color' => 10, 'source' => '#05'], Smileys::parse('#058')[0]);        self::assertSame(['#15'], Smileys::parse('#15'));        self::assertSame(['kind' => 'image', 'n' => 3, 'source' => '@03'], Smileys::parse('@03 and #-1')[0]);        self::assertSame(' and #-1', Smileys::parse('@03 and #-1')[1], '"#-" is never a smiley');        self::assertSame(['@60'], Smileys::parse('@60'), 'smiley.gif has 60 pictures');    }    /** @return iterable<string, array{0: string, 1: string}> */    public static function references(): iterable    {        yield 'heart' => ['#13', 'big_13.png'];        yield 'tree' => ['#05', 'big_05.png'];        yield 'the mask of the $ nose' => [':$)', 'nose_09.png'];        yield 'the $ mouth' => [':-$', 'mouth_18.png'];        yield 'two hands' => [':-)#dd', 'acc_05.png'];        yield 'an orange face' => [':-)3', 'color_03.png'];        yield 'a wink' => [';-)', 'eyes_01.png'];    }    #[\PHPUnit\Framework\Attributes\DataProvider('references')]    public function testASmileyIsDrawnLikeTheApplet(string $code, string $reference): void    {        if (!function_exists('imagecreatetruecolor')) {            self::markTestSkipped('no GD');        }        $smiley = Smileys::parse($code)[0];        self::assertIsArray($smiley);        $mine = SmileyPainter::image($smiley);        $ref = imagecreatefrompng(__DIR__ . '/fixtures/' . $reference);        $different = 0;        for ($y = 0; $y < imagesy($ref); $y++) {            for ($x = 0; $x < imagesx($ref); $x++) {                $a = imagecolorsforindex($ref, imagecolorat($ref, $x, $y));                $b = ($x < imagesx($mine) && $y < imagesy($mine)) ? imagecolorsforindex($mine, imagecolorat($mine, $x, $y)) : ['red' => 0, 'green' => 0, 'blue' => 0, 'alpha' => 127];                $seenA = $a['alpha'] < 64;                $seenB = $b['alpha'] < 64;                if ($seenA !== $seenB || ($seenA && [$a['red'], $a['green'], $a['blue']] !== [$b['red'], $b['green'], $b['blue']])) {                    $different++;                }            }        }        self::assertSame(0, $different, "$code differs from $reference by $different pixels");    }}