Improve test coverage

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;
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 = [
'gif',
'jpeg',
@ -82,22 +66,34 @@ final class Picture {
$ext = array_pop($urlParts);
$path = implode('.', $urlParts);
$mime = array_key_exists($ext, static::MIME_MAP)
? static::MIME_MAP[$ext]
: 'image/jpeg';
$mime = match ($ext) {
'avif' => 'image/avif',
'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)
? static::MIME_MAP[$fallbackExt]
: 'image/jpeg';
$fallbackMime = match ($fallbackExt) {
'gif' => 'image/gif',
'png' => 'image/png',
default => 'image/jpeg',
};
// For image types that are well-established, just return a
// simple <img /> element instead
if (
$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
: $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 {
/**
* @dataProvider dataPictureCase
* @param array $params
*/
public function testPictureHelper($params, $expected = NULL)
public function testPictureHelper(array $params): void
{
$helper = new PictureHelper();
$helper->setContainer($this->container);
$actual = $helper(...$params);
if ($expected === NULL)
{
$this->assertMatchesSnapshot($actual);
}
else
{
$this->assertEquals($expected, $actual);
}
}
/**
* @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->setContainer($this->container);
$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);
}
public function testSimpleImageByFallback()
public function testSimpleImageByFallback(): void
{
$helper = new PictureHelper();
$helper->setContainer($this->container);
$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 [
'Full AVIF URL' => [
'params' => [
'https://www.example.com/image.avif',
],
],
'Full webp URL' => [
'params' => [
'https://www.example.com/image.webp',
@ -112,16 +114,21 @@ class PictureHelperTest extends AnimeClientTestCase {
'params' => [
'images/foo.jpg',
'jpg',
[ 'x' => 1, 'y' => 1 ],
[],
['width' => 200, 'height' => 200, 'alt' => 'should exist'],
]
]
];
}
public function dataSimpleImageCase()
public function dataSimpleImageCase(): array
{
return [
'avif' => [
'ext' => 'avif',
'isSimple' => FALSE,
'fallback' => 'jpf'
],
'apng' => [
'ext' => 'apng',
'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));
}
public function testParseStreamingLinksNoHost(): void
{
$nodes = [[
'url' => '/link-fragment',
'dubs' => [],
'subs' => [],
]];
$this->assertEquals([], Kitsu::parseStreamingLinks($nodes));
}
public function testGetAiringStatusEmptyArguments(): void
{
$this->assertEquals(AnimeAiringStatus::NOT_YET_AIRED, Kitsu::getAiringStatus());
@ -123,7 +134,7 @@ class KitsuTest extends TestCase {
$this->assertEquals($expected, $actual);
}
public function testFilterLocalizedTitles()
public function testFilterLocalizedTitles(): void
{
$input = [
'canonical' => 'foo',
@ -140,7 +151,7 @@ class KitsuTest extends TestCase {
$this->assertEquals(['Foo the Movie'], $actual);
}
public function testGetFilteredTitles()
public function testGetFilteredTitles(): void
{
$input = [
'canonical' => 'foo',