From 0a87f817688e067ae8cefe76e0adde9eb6d42a56 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 23 Feb 2021 12:00:22 -0500 Subject: [PATCH] Improve test coverage --- src/AnimeClient/Helper/Picture.php | 44 +++++++++---------- tests/AnimeClient/Helper/FormHelperTest.php | 37 ++++++++++++++++ .../AnimeClient/Helper/PictureHelperTest.php | 41 ++++++++++------- .../FormHelperTest__testFormHelper__1.txt | 1 + ...eHelper with data set Full AVIF URL__1.txt | 1 + tests/AnimeClient/KitsuTest.php | 15 ++++++- 6 files changed, 96 insertions(+), 43 deletions(-) create mode 100644 tests/AnimeClient/Helper/FormHelperTest.php create mode 100644 tests/AnimeClient/Helper/__snapshots__/FormHelperTest__testFormHelper__1.txt create mode 100644 tests/AnimeClient/Helper/__snapshots__/PictureHelperTest__testPictureHelper with data set Full AVIF URL__1.txt diff --git a/src/AnimeClient/Helper/Picture.php b/src/AnimeClient/Helper/Picture.php index 32334ebd..0c36abd0 100644 --- a/src/AnimeClient/Helper/Picture.php +++ b/src/AnimeClient/Helper/Picture.php @@ -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 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; diff --git a/tests/AnimeClient/Helper/FormHelperTest.php b/tests/AnimeClient/Helper/FormHelperTest.php new file mode 100644 index 00000000..7db90484 --- /dev/null +++ b/tests/AnimeClient/Helper/FormHelperTest.php @@ -0,0 +1,37 @@ + + * @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); + } +} \ No newline at end of file diff --git a/tests/AnimeClient/Helper/PictureHelperTest.php b/tests/AnimeClient/Helper/PictureHelperTest.php index 9df0f61c..48a35c4d 100644 --- a/tests/AnimeClient/Helper/PictureHelperTest.php +++ b/tests/AnimeClient/Helper/PictureHelperTest.php @@ -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); - } + $this->assertMatchesSnapshot($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, '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, 'assertTrue(! str_contains($actual, ' [ + '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, diff --git a/tests/AnimeClient/Helper/__snapshots__/FormHelperTest__testFormHelper__1.txt b/tests/AnimeClient/Helper/__snapshots__/FormHelperTest__testFormHelper__1.txt new file mode 100644 index 00000000..bc40489d --- /dev/null +++ b/tests/AnimeClient/Helper/__snapshots__/FormHelperTest__testFormHelper__1.txt @@ -0,0 +1 @@ + diff --git a/tests/AnimeClient/Helper/__snapshots__/PictureHelperTest__testPictureHelper with data set Full AVIF URL__1.txt b/tests/AnimeClient/Helper/__snapshots__/PictureHelperTest__testPictureHelper with data set Full AVIF URL__1.txt new file mode 100644 index 00000000..d0856346 --- /dev/null +++ b/tests/AnimeClient/Helper/__snapshots__/PictureHelperTest__testPictureHelper with data set Full AVIF URL__1.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/tests/AnimeClient/KitsuTest.php b/tests/AnimeClient/KitsuTest.php index 7a099a6f..c644db09 100644 --- a/tests/AnimeClient/KitsuTest.php +++ b/tests/AnimeClient/KitsuTest.php @@ -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',