HummingBirdAnimeClient/tests/AnimeClient/Helper/PictureHelperTest.php

159 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
*
2021-02-04 11:57:01 -05:00
* PHP version 8
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
2021-01-13 01:52:03 -05:00
* @copyright 2015 - 2021 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
* @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
{
/**
* @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);
}
/**
* @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'));
}
2021-02-23 12:00:22 -05:00
public 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
],
],
];
}
2021-02-23 12:00:22 -05:00
public 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
}