[TESTS] Raise App\Util\HTML test coverage to 100%
This commit is contained in:
parent
fbea08ca9b
commit
a49ee453ab
@ -28,6 +28,7 @@
|
|||||||
namespace App\Util;
|
namespace App\Util;
|
||||||
|
|
||||||
use Functional as F;
|
use Functional as F;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
abstract class HTML
|
abstract class HTML
|
||||||
{
|
{
|
||||||
@ -37,12 +38,13 @@ abstract class HTML
|
|||||||
* @param string $tag
|
* @param string $tag
|
||||||
* @param array|string $content
|
* @param array|string $content
|
||||||
* @param bool $empty_tag
|
* @param bool $empty_tag
|
||||||
|
* @param null|mixed $attrs
|
||||||
*
|
*
|
||||||
* @return array
|
* @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
|
private static function attr_tag(string $tag, $attrs, $content = '', bool $empty_tag = false): array
|
||||||
{
|
{
|
||||||
$html = '<' . $tag . (is_string($attrs) ? ($attrs ? ' ' : '') . $attrs : self::attr($attrs));
|
$html = '<' . $tag . (is_string($attrs) ? ($attrs ? ' ' : '') . $attrs : self::attr($attrs));
|
||||||
if (empty($content) || $empty_tag) {
|
if ($empty_tag) {
|
||||||
$html .= '>';
|
$html .= '/>';
|
||||||
} else {
|
} else {
|
||||||
$inner = Formatting::indent($content);
|
$inner = Formatting::indent($content);
|
||||||
$html .= ">\n" . ($inner == '' ? '' : $inner . "\n") . Formatting::indent("</{$tag}>");
|
$html .= ">\n" . ($inner == '' ? '' : $inner . "\n") . "</{$tag}>";
|
||||||
}
|
}
|
||||||
return explode("\n", $html);
|
return explode("\n", $html);
|
||||||
}
|
}
|
||||||
@ -77,7 +79,7 @@ abstract class HTML
|
|||||||
private static function attr(array $attrs): string
|
private static function attr(array $attrs): string
|
||||||
{
|
{
|
||||||
return ' ' . implode(' ', F\map($attrs, function ($val, $key, $_) {
|
return ' ' . implode(' ', F\map($attrs, function ($val, $key, $_) {
|
||||||
return "{$key} = '{$val}'";
|
return "{$key}=\"{$val}\"";
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +107,7 @@ abstract class HTML
|
|||||||
}
|
}
|
||||||
return $out;
|
return $out;
|
||||||
} else {
|
} else {
|
||||||
return '';
|
throw new InvalidArgumentException('HTML::html argument must be of type string or array');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,15 +20,22 @@
|
|||||||
namespace App\Tests\Util;
|
namespace App\Tests\Util;
|
||||||
|
|
||||||
use App\Util\HTML;
|
use App\Util\HTML;
|
||||||
|
use Jchook\AssertThrows\AssertThrows;
|
||||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||||
|
|
||||||
class HTMLTest extends WebTestCase
|
class HTMLTest extends WebTestCase
|
||||||
{
|
{
|
||||||
|
use AssertThrows;
|
||||||
|
|
||||||
public function testHTML()
|
public function testHTML()
|
||||||
{
|
{
|
||||||
static::assertSame('', HTML::html(''));
|
static::assertSame('', HTML::html(''));
|
||||||
static::assertSame("<a>\n\n</a>\n", HTML::html(['a' => '']));
|
static::assertSame("<a>\n\n</a>\n", HTML::html(['a' => '']));
|
||||||
static::assertSame("<a>\n <p>\n </p>\n</a>\n", HTML::html(['a' => ['p' => '']]));
|
static::assertSame("<a>\n <p>\n </p>\n</a>\n", HTML::html(['a' => ['p' => '']]));
|
||||||
|
static::assertSame("<a href=\"test\">\n <p>\n </p>\n</a>\n", HTML::html(['a' => ['attrs' => ['href' => 'test'], 'p' => '']]));
|
||||||
static::assertSame("<a>\n <p>\n foo\n </p>\n <br/>\n</a>\n", HTML::html(['a' => ['p' => 'foo', 'br' => 'empty']]));
|
static::assertSame("<a>\n <p>\n foo\n </p>\n <br/>\n</a>\n", HTML::html(['a' => ['p' => 'foo', 'br' => 'empty']]));
|
||||||
|
static::assertThrows(\InvalidArgumentException::class, fn () => HTML::html(1));
|
||||||
|
static::assertSame("<a href=\"test\">\n foo\n</a>", implode("\n", HTML::tag('a', ['href' => 'test'], content: 'foo', empty_tag: false)));
|
||||||
|
static::assertSame('<br/>', implode("\n", HTML::tag('br', attrs: null, content: null, empty_tag: true)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user