HummingBirdAnimeClient/tests/AnimeClient/Helper/PictureHelperTest.php

153 lines
3.2 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu to manage anime and manga watch lists
*
2023-07-13 11:08:05 -04:00
* PHP version 8.1
*
2023-07-13 11:08:05 -04:00
* @copyright 2015 - 2023 Timothy J. Warren <tim@timshome.page>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
2023-07-13 11:08:05 -04:00
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Tests\Helper;
use Aviat\AnimeClient\Helper\Picture as PictureHelper;
use Aviat\AnimeClient\Tests\AnimeClientTestCase;
2022-03-04 12:19:47 -05:00
/**
* @internal
*/
final class PictureHelperTest extends AnimeClientTestCase
{
2023-05-09 12:46:52 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('dataPictureCase')]
2021-02-23 12:00:22 -05:00
public function testPictureHelper(array $params): void
{
$helper = new PictureHelper();
$helper->setContainer($this->container);
$actual = $helper(...$params);
2021-02-23 12:00:22 -05:00
$this->assertMatchesSnapshot($actual);
}
2023-05-09 12:46:52 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('dataSimpleImageCase')]
2021-02-23 12:00:22 -05:00
public function testSimpleImage(string $ext, bool $isSimple, string $fallbackExt = 'jpg'): void
{
$helper = new PictureHelper();
$helper->setContainer($this->container);
$url = "https://example.com/image.{$ext}";
2021-02-23 12:00:22 -05:00
$actual = $helper($url, $fallbackExt);
2021-02-23 12:00:22 -05:00
$actuallySimple = ! str_contains($actual, '<picture');
2022-03-04 12:19:47 -05:00
$this->assertSame($isSimple, $actuallySimple);
}
2021-02-23 12:00:22 -05:00
public function testSimpleImageByFallback(): void
{
$helper = new PictureHelper();
$helper->setContainer($this->container);
2022-03-04 12:19:47 -05:00
$actual = $helper('foo.svg', 'svg');
2022-03-04 12:19:47 -05:00
$this->assertTrue( ! str_contains($actual, '<picture'));
}
2023-05-09 12:46:52 -04:00
public static function dataPictureCase(): array
{
return [
2021-02-23 12:00:22 -05:00
'Full AVIF URL' => [
'params' => [
'https://www.example.com/image.avif',
],
],
'Full webp URL' => [
'params' => [
'https://www.example.com/image.webp',
],
],
'Partial webp URL' => [
'params' => [
2022-03-04 12:19:47 -05:00
'images/anime/15424.webp',
],
],
'bmp with gif fallback' => [
'params' => [
'images/avatar/25.bmp',
'gif',
],
],
'webp placeholder image' => [
'params' => [
'images/placeholder.webp',
2022-03-04 12:19:47 -05:00
],
],
'png placeholder image' => [
'params' => [
'images/placeholder.png',
2022-03-04 12:19:47 -05:00
],
],
'jpeg2000' => [
'params' => [
'images/foo.jpf',
2022-03-04 12:19:47 -05:00
],
],
'svg with png fallback and lots of attributes' => [
'params' => [
'images/example.svg',
'png',
2022-03-04 12:19:47 -05:00
['width' => 200, 'height' => 300],
['alt' => 'Example text'],
],
],
'simple image with attributes' => [
'params' => [
'images/foo.jpg',
'jpg',
2021-02-23 12:00:22 -05:00
[],
['width' => 200, 'height' => 200, 'alt' => 'should exist'],
2022-03-04 12:19:47 -05:00
],
],
];
}
2023-05-09 12:46:52 -04:00
public static function dataSimpleImageCase(): array
{
return [
2021-02-23 12:00:22 -05:00
'avif' => [
'ext' => 'avif',
'isSimple' => FALSE,
2022-03-04 12:19:47 -05:00
'fallback' => 'jpf',
2021-02-23 12:00:22 -05:00
],
'apng' => [
'ext' => 'apng',
'isSimple' => FALSE,
],
'gif' => [
'ext' => 'gif',
'isSimple' => TRUE,
],
'jpg' => [
'ext' => 'jpg',
'isSimple' => TRUE,
],
'jpeg' => [
'ext' => 'jpeg',
'isSimple' => TRUE,
],
'png' => [
'ext' => 'png',
'isSimple' => TRUE,
],
'webp' => [
'ext' => 'webp',
'isSimple' => FALSE,
],
];
}
2022-03-04 12:19:47 -05:00
}