Browse Source

added HtmlHelper::endTag()

Fabian Peter Hammerle 7 years ago
parent
commit
e229a1c0ed
2 changed files with 47 additions and 0 deletions
  1. 13 0
      HtmlHelper.php
  2. 34 0
      tests/HtmlHelperTest.php

+ 13 - 0
HtmlHelper.php

@@ -8,4 +8,17 @@ class HtmlHelper
     {
         return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE);
     }
+
+    public static function endTag($name)
+    {
+        if($name === null) {
+            return null;
+        } elseif(!is_string($name)) {
+            throw new \TypeError(
+                sprintf('expected string or null as name, %s given', gettype($name))
+                );
+        } else {
+            return '</' . $name . '>';
+        }
+    }
 }

+ 34 - 0
tests/HtmlHelperTest.php

@@ -25,4 +25,38 @@ class HtmlHelperTest extends \PHPUnit_Framework_TestCase
     {
         $this->assertSame($expected, HtmlHelper::encode($string));
     }
+
+    public function endTagTypeErrorProvider()
+    {
+        return [
+            [1],
+            [false],
+            ];
+    }
+
+    /**
+     * @dataProvider endTagTypeErrorProvider
+     * @expectedException \TypeError
+     */
+    public function testEndTagTypeError($name)
+    {
+        HtmlHelper::endTag($name);
+    }
+
+    public function endTagProvider()
+    {
+        return [
+            ['tag', '</tag>'],
+            ['end', '</end>'],
+            [null, null],
+            ];
+    }
+
+    /**
+     * @dataProvider endTagProvider
+     */
+    public function testEndTag($name, $expected_tag)
+    {
+        $this->assertSame($expected_tag, HtmlHelper::endTag($name));
+    }
 }