From a49ee453ab8201a2668c21a8e9548530e88fbe5f Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 2 May 2021 21:02:43 +0000 Subject: [PATCH] [TESTS] Raise App\Util\HTML test coverage to 100% --- src/Util/HTML.php | 16 +++++++++------- tests/Util/HTMLTest.php | 7 +++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Util/HTML.php b/src/Util/HTML.php index d481aea067..94c7a98e6a 100644 --- a/src/Util/HTML.php +++ b/src/Util/HTML.php @@ -28,6 +28,7 @@ namespace App\Util; use Functional as F; +use InvalidArgumentException; abstract class HTML { @@ -37,12 +38,13 @@ abstract class HTML * @param string $tag * @param array|string $content * @param bool $empty_tag + * @param null|mixed $attrs * * @return array */ - private static function tag(string $tag, $content = '', bool $empty_tag = false): array + public static function tag(string $tag, $attrs = null, $content = null, bool $empty_tag = false): array { - return self::attr_tag($tag, '', $content); + return self::attr_tag($tag, $attrs ?? '', $content ?? '', $empty_tag); } /** @@ -58,11 +60,11 @@ abstract class HTML private static function attr_tag(string $tag, $attrs, $content = '', bool $empty_tag = false): array { $html = '<' . $tag . (is_string($attrs) ? ($attrs ? ' ' : '') . $attrs : self::attr($attrs)); - if (empty($content) || $empty_tag) { - $html .= '>'; + if ($empty_tag) { + $html .= '/>'; } else { $inner = Formatting::indent($content); - $html .= ">\n" . ($inner == '' ? '' : $inner . "\n") . Formatting::indent(""); + $html .= ">\n" . ($inner == '' ? '' : $inner . "\n") . ""; } return explode("\n", $html); } @@ -77,7 +79,7 @@ abstract class HTML private static function attr(array $attrs): string { return ' ' . implode(' ', F\map($attrs, function ($val, $key, $_) { - return "{$key} = '{$val}'"; + return "{$key}=\"{$val}\""; })); } @@ -105,7 +107,7 @@ abstract class HTML } return $out; } else { - return ''; + throw new InvalidArgumentException('HTML::html argument must be of type string or array'); } } } diff --git a/tests/Util/HTMLTest.php b/tests/Util/HTMLTest.php index 8558e09789..30e81cf76f 100644 --- a/tests/Util/HTMLTest.php +++ b/tests/Util/HTMLTest.php @@ -20,15 +20,22 @@ namespace App\Tests\Util; use App\Util\HTML; +use Jchook\AssertThrows\AssertThrows; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class HTMLTest extends WebTestCase { + use AssertThrows; + public function testHTML() { static::assertSame('', HTML::html('')); static::assertSame("\n\n\n", HTML::html(['a' => ''])); static::assertSame("\n

\n

\n
\n", HTML::html(['a' => ['p' => '']])); + static::assertSame("\n

\n

\n
\n", HTML::html(['a' => ['attrs' => ['href' => 'test'], 'p' => '']])); static::assertSame("\n

\n foo\n

\n
\n
\n", HTML::html(['a' => ['p' => 'foo', 'br' => 'empty']])); + static::assertThrows(\InvalidArgumentException::class, fn () => HTML::html(1)); + static::assertSame("\n foo\n", implode("\n", HTML::tag('a', ['href' => 'test'], content: 'foo', empty_tag: false))); + static::assertSame('
', implode("\n", HTML::tag('br', attrs: null, content: null, empty_tag: true))); } }