Improve test coverage
All checks were successful
timw4mail/HummingBirdAnimeClient/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-02-23 12:00:22 -05:00
parent 0f9dd61b6b
commit 0a87f81768
6 changed files with 96 additions and 43 deletions

View File

@ -25,22 +25,6 @@ final class Picture {
use ContainerAware; use ContainerAware;
private const MIME_MAP = [
'apng' => 'image/vnd.mozilla.apng',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ico' => 'image/x-icon',
'jpeg' => 'image/jpeg',
'jpf' => 'image/jpx',
'jpg' => 'image/jpeg',
'jpx' => 'image/jpx',
'png' => 'image/png',
'svg' => 'image/svg+xml',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'webp' => 'image/webp',
];
private const SIMPLE_IMAGE_TYPES = [ private const SIMPLE_IMAGE_TYPES = [
'gif', 'gif',
'jpeg', 'jpeg',
@ -82,22 +66,34 @@ final class Picture {
$ext = array_pop($urlParts); $ext = array_pop($urlParts);
$path = implode('.', $urlParts); $path = implode('.', $urlParts);
$mime = array_key_exists($ext, static::MIME_MAP) $mime = match ($ext) {
? static::MIME_MAP[$ext] 'avif' => 'image/avif',
: 'image/jpeg'; 'apng' => 'image/vnd.mozilla.apng',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ico' => 'image/x-icon',
'jpf', 'jpx' => 'image/jpx',
'png' => 'image/png',
'svg' => 'image/svg+xml',
'tif', 'tiff' => 'image/tiff',
'webp' => 'image/webp',
default => 'image/jpeg',
};
$fallbackMime = array_key_exists($fallbackExt, static::MIME_MAP) $fallbackMime = match ($fallbackExt) {
? static::MIME_MAP[$fallbackExt] 'gif' => 'image/gif',
: 'image/jpeg'; 'png' => 'image/png',
default => 'image/jpeg',
};
// For image types that are well-established, just return a // For image types that are well-established, just return a
// simple <img /> element instead // simple <img /> element instead
if ( if (
$ext === $fallbackExt || $ext === $fallbackExt ||
\in_array($ext, static::SIMPLE_IMAGE_TYPES, TRUE) \in_array($ext, Picture::SIMPLE_IMAGE_TYPES, TRUE)
) )
{ {
$attrs = ( ! empty($imgAttrs)) $attrs = (count($imgAttrs) > 1)
? $imgAttrs ? $imgAttrs
: $picAttrs; : $picAttrs;

View File

@ -0,0 +1,37 @@
<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu to manage anime and manga watch lists
*
* PHP version 8
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren <tim@timshomepage.net>
* @copyright 2015 - 2021 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 5.2
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Tests\Helper;
use Aviat\AnimeClient\Helper\Form as FormHelper;
use Aviat\AnimeClient\Tests\AnimeClientTestCase;
class FormHelperTest extends AnimeClientTestCase {
public function testFormHelper(): void
{
$helper = new FormHelper();
$helper->setContainer($this->container);
$actual = $helper('input', [
'type' => 'text',
'value' => 'foo',
'placeholder' => 'field',
'name' => 'test'
]);
$this->assertMatchesSnapshot($actual);
}
}

View File

@ -22,53 +22,55 @@ use Aviat\AnimeClient\Tests\AnimeClientTestCase;
class PictureHelperTest extends AnimeClientTestCase { class PictureHelperTest extends AnimeClientTestCase {
/** /**
* @dataProvider dataPictureCase * @dataProvider dataPictureCase
* @param array $params
*/ */
public function testPictureHelper($params, $expected = NULL) public function testPictureHelper(array $params): void
{ {
$helper = new PictureHelper(); $helper = new PictureHelper();
$helper->setContainer($this->container); $helper->setContainer($this->container);
$actual = $helper(...$params); $actual = $helper(...$params);
if ($expected === NULL)
{
$this->assertMatchesSnapshot($actual); $this->assertMatchesSnapshot($actual);
} }
else
{
$this->assertEquals($expected, $actual);
}
}
/** /**
* @dataProvider dataSimpleImageCase * @dataProvider dataSimpleImageCase
* @param string $ext
* @param bool $isSimple
* @param string $fallbackExt
*/ */
public function testSimpleImage(string $ext, bool $isSimple) public function testSimpleImage(string $ext, bool $isSimple, string $fallbackExt = 'jpg'): void
{ {
$helper = new PictureHelper(); $helper = new PictureHelper();
$helper->setContainer($this->container); $helper->setContainer($this->container);
$url = "https://example.com/image.{$ext}"; $url = "https://example.com/image.{$ext}";
$actual = $helper($url); $actual = $helper($url, $fallbackExt);
$actuallySimple = strpos($actual, '<picture') === FALSE; $actuallySimple = ! str_contains($actual, '<picture');
$this->assertEquals($isSimple, $actuallySimple); $this->assertEquals($isSimple, $actuallySimple);
} }
public function testSimpleImageByFallback() public function testSimpleImageByFallback(): void
{ {
$helper = new PictureHelper(); $helper = new PictureHelper();
$helper->setContainer($this->container); $helper->setContainer($this->container);
$actual = $helper("foo.svg", 'svg'); $actual = $helper("foo.svg", 'svg');
$this->assertTrue(strpos($actual, '<picture') === FALSE); $this->assertTrue(! str_contains($actual, '<picture'));
} }
public function dataPictureCase() public function dataPictureCase(): array
{ {
return [ return [
'Full AVIF URL' => [
'params' => [
'https://www.example.com/image.avif',
],
],
'Full webp URL' => [ 'Full webp URL' => [
'params' => [ 'params' => [
'https://www.example.com/image.webp', 'https://www.example.com/image.webp',
@ -112,16 +114,21 @@ class PictureHelperTest extends AnimeClientTestCase {
'params' => [ 'params' => [
'images/foo.jpg', 'images/foo.jpg',
'jpg', 'jpg',
[ 'x' => 1, 'y' => 1 ], [],
['width' => 200, 'height' => 200, 'alt' => 'should exist'], ['width' => 200, 'height' => 200, 'alt' => 'should exist'],
] ]
] ]
]; ];
} }
public function dataSimpleImageCase() public function dataSimpleImageCase(): array
{ {
return [ return [
'avif' => [
'ext' => 'avif',
'isSimple' => FALSE,
'fallback' => 'jpf'
],
'apng' => [ 'apng' => [
'ext' => 'apng', 'ext' => 'apng',
'isSimple' => FALSE, 'isSimple' => FALSE,

View File

@ -0,0 +1 @@
<input id="input" type="text" name="input" value="foo" />

View File

@ -0,0 +1 @@
<picture loading="lazy"><source srcset="https://www.example.com/image.avif" type="image/avif" /><source srcset="https://www.example.com/image.jpg" type="image/jpeg" /><img src="https://www.example.com/image.jpg" alt="" loading="lazy" /></picture>

View File

@ -55,6 +55,17 @@ class KitsuTest extends TestCase {
$this->assertEquals($expected, Kitsu::parseStreamingLinks($nodes)); $this->assertEquals($expected, Kitsu::parseStreamingLinks($nodes));
} }
public function testParseStreamingLinksNoHost(): void
{
$nodes = [[
'url' => '/link-fragment',
'dubs' => [],
'subs' => [],
]];
$this->assertEquals([], Kitsu::parseStreamingLinks($nodes));
}
public function testGetAiringStatusEmptyArguments(): void public function testGetAiringStatusEmptyArguments(): void
{ {
$this->assertEquals(AnimeAiringStatus::NOT_YET_AIRED, Kitsu::getAiringStatus()); $this->assertEquals(AnimeAiringStatus::NOT_YET_AIRED, Kitsu::getAiringStatus());
@ -123,7 +134,7 @@ class KitsuTest extends TestCase {
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
public function testFilterLocalizedTitles() public function testFilterLocalizedTitles(): void
{ {
$input = [ $input = [
'canonical' => 'foo', 'canonical' => 'foo',
@ -140,7 +151,7 @@ class KitsuTest extends TestCase {
$this->assertEquals(['Foo the Movie'], $actual); $this->assertEquals(['Foo the Movie'], $actual);
} }
public function testGetFilteredTitles() public function testGetFilteredTitles(): void
{ {
$input = [ $input = [
'canonical' => 'foo', 'canonical' => 'foo',