HtmlHelperTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace fphammerle\helpers\tests;
  3. use fphammerle\helpers\HtmlHelper;
  4. class HtmlHelperTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function encodeProvider()
  7. {
  8. return [
  9. ['abc', 'abc'],
  10. ['可以', '可以'],
  11. ['⚕', '⚕'],
  12. ['<abc>', '&lt;abc&gt;'],
  13. ['alert(":-)");', 'alert(&quot;:-)&quot;);'],
  14. ['alert(\':-)\');', 'alert(&#039;:-)&#039;);'],
  15. ];
  16. }
  17. /**
  18. * @dataProvider encodeProvider
  19. */
  20. public function testEncode($string, $expected)
  21. {
  22. $this->assertSame($expected, HtmlHelper::encode($string));
  23. }
  24. public function voidTagInvalidArgumentProvider()
  25. {
  26. return [
  27. [1, []],
  28. [false, []],
  29. ];
  30. }
  31. /**
  32. * @dataProvider voidTagInvalidArgumentProvider
  33. * @expectedException \InvalidArgumentException
  34. */
  35. public function testVoidTagInvalidArgument($name, $attributes)
  36. {
  37. HtmlHelper::voidTag($name, $attributes);
  38. }
  39. public function voidTagProvider()
  40. {
  41. return [
  42. ['tag', [], '<tag />'],
  43. ['void', ['a' => '1'], '<void a="1" />'],
  44. ['void', ['a' => ''], '<void a="" />'],
  45. ['void', ['a' => 1], '<void a="1" />'],
  46. ['void', ['a' => '1', 'b' => '2'], '<void a="1" b="2" />'],
  47. ['void', ['b' => '1', 'a' => '2'], '<void b="1" a="2" />'],
  48. ['void', ['a' => null, 'b' => '2'], '<void b="2" />'],
  49. ['void', ['a' => true], '<void a="a" />'],
  50. ['void', ['a' => true, 'b' => '2'], '<void a="a" b="2" />'],
  51. ['void', ['a' => false], '<void />'],
  52. ['void', ['a' => false, 'b' => '2'], '<void b="2" />'],
  53. ['script', ['type' => 'text/javascript'], '<script type="text/javascript" />'],
  54. ['img', ['ondrag' => 'alert(":-)")'], '<img ondrag="alert(&quot;:-)&quot;)" />'],
  55. ['img', ['ondrag' => "alert(':-)')"], '<img ondrag="alert(&#039;:-)&#039;)" />'],
  56. [null, [], null],
  57. [null, ['attr' => 'v'], null],
  58. ];
  59. }
  60. /**
  61. * @dataProvider voidTagProvider
  62. */
  63. public function testVoidTag($name, $attributes, $expected_tag)
  64. {
  65. $this->assertSame($expected_tag, HtmlHelper::voidTag($name, $attributes));
  66. }
  67. public function testVoidTagNoAttributes()
  68. {
  69. $this->assertSame('<tag />', HtmlHelper::voidTag('tag'));
  70. }
  71. public function startTagInvalidArgumentProvider()
  72. {
  73. return [
  74. [1, []],
  75. [false, []],
  76. ];
  77. }
  78. /**
  79. * @dataProvider startTagInvalidArgumentProvider
  80. * @expectedException \InvalidArgumentException
  81. */
  82. public function testStartTagInvalidArgument($name, $attributes)
  83. {
  84. HtmlHelper::startTag($name, $attributes);
  85. }
  86. public function startTagProvider()
  87. {
  88. return [
  89. ['tag', [], '<tag>'],
  90. ['start', ['a' => '1'], '<start a="1">'],
  91. ['start', ['a' => ''], '<start a="">'],
  92. ['start', ['a' => 1], '<start a="1">'],
  93. ['start', ['a' => '1', 'b' => '2'], '<start a="1" b="2">'],
  94. ['start', ['b' => '1', 'a' => '2'], '<start b="1" a="2">'],
  95. ['start', ['a' => null, 'b' => '2'], '<start b="2">'],
  96. ['start', ['a' => true], '<start a="a">'],
  97. ['start', ['a' => true, 'b' => '2'], '<start a="a" b="2">'],
  98. ['start', ['a' => false], '<start>'],
  99. ['start', ['a' => false, 'b' => '2'], '<start b="2">'],
  100. ['script', ['type' => 'text/javascript'], '<script type="text/javascript">'],
  101. ['span', ['onclick' => 'alert(":-)")'], '<span onclick="alert(&quot;:-)&quot;)">'],
  102. ['span', ['onclick' => "alert(':-)')"], '<span onclick="alert(&#039;:-)&#039;)">'],
  103. [null, [], null],
  104. [null, ['attr' => 'v'], null],
  105. ];
  106. }
  107. /**
  108. * @dataProvider startTagProvider
  109. */
  110. public function testStartTag($name, $attributes, $expected_tag)
  111. {
  112. $this->assertSame($expected_tag, HtmlHelper::startTag($name, $attributes));
  113. }
  114. public function testStartTagNoAttributes()
  115. {
  116. $this->assertSame('<tag>', HtmlHelper::startTag('tag'));
  117. }
  118. public function endTagInvalidArgumentProvider()
  119. {
  120. return [
  121. [1],
  122. [false],
  123. ];
  124. }
  125. /**
  126. * @dataProvider endTagInvalidArgumentProvider
  127. * @expectedException \InvalidArgumentException
  128. */
  129. public function testEndTagInvalidArgument($name)
  130. {
  131. HtmlHelper::endTag($name);
  132. }
  133. public function endTagProvider()
  134. {
  135. return [
  136. ['tag', '</tag>'],
  137. ['end', '</end>'],
  138. [null, null],
  139. ];
  140. }
  141. /**
  142. * @dataProvider endTagProvider
  143. */
  144. public function testEndTag($name, $expected_tag)
  145. {
  146. $this->assertSame($expected_tag, HtmlHelper::endTag($name));
  147. }
  148. public function nonVoidTagProvider()
  149. {
  150. return [
  151. [null, null, [], null],
  152. [null, null, ['a' => '1'], null],
  153. [null, 'content', [], 'content'],
  154. [null, 'content', ['a' => '1'], 'content'],
  155. [null, 'qu"ote', [], 'qu"ote'],
  156. [null, '', [], ''],
  157. ['tag', null, [], null],
  158. ['tag', null, ['a' => true, 'b' => 2], null],
  159. ['tag', '', [], '<tag></tag>'],
  160. ['tag', '', ['a' => true, 'b' => 2], '<tag a="a" b="2"></tag>'],
  161. ['tag', 'content', [], '<tag>content</tag>'],
  162. ['tag', 'content', ['a' => true, 'b' => 2], '<tag a="a" b="2">content</tag>'],
  163. ['tag', 'content', ['a' => null], '<tag>content</tag>'],
  164. ['tag', 'content', ['a' => 'qu"ote'], '<tag a="qu&quot;ote">content</tag>'],
  165. ['tag', 'cont"ent', ['a' => ''], '<tag a="">cont"ent</tag>'],
  166. ];
  167. }
  168. /**
  169. * @dataProvider nonVoidTagProvider
  170. */
  171. public function testNonVoidTag($name, $content, $attributes, $expected_tag)
  172. {
  173. $this->assertSame($expected_tag, HtmlHelper::nonVoidTag($name, $content, $attributes));
  174. }
  175. public function timeProvider()
  176. {
  177. return [
  178. [null, null, [], null],
  179. [null, '04.08.2016', [], '<time>04.08.2016</time>'],
  180. [null, function($y) { return is_null($y) ? '2016' : ''; }, [], '<time>2016</time>'],
  181. ['2016-08-04', '04.08.2016', [], '<time datetime="2016-08-04">04.08.2016</time>'],
  182. ['2016-08-04', '', [], '<time datetime="2016-08-04"></time>'],
  183. ['2016', '2016', ['title' => 'year'], '<time datetime="2016" title="year">2016</time>'],
  184. ['2016', '2016', ['datetime' => '2014'], '<time datetime="2014">2016</time>'],
  185. ['2016', function($y) { return strrev($y); }, [], '<time datetime="2016">6102</time>'],
  186. ['2016', function() { return 'year'; }, [], '<time datetime="2016">year</time>'],
  187. [
  188. new \DateTime('2016-08-04 13:54+08:00'),
  189. '2016',
  190. [],
  191. '<time datetime="2016-08-04T13:54:00+08:00">2016</time>',
  192. ],
  193. [
  194. new \DateTime('2016-08-04 13:54:13Z'),
  195. function($dt) { return $dt->format('m.d.Y'); },
  196. [],
  197. '<time datetime="2016-08-04T13:54:13+00:00">08.04.2016</time>',
  198. ],
  199. [
  200. new \DateTime('2016-08-04 13:54:13Z'),
  201. function($dt) { return $dt->format('m.d.Y'); },
  202. ['title' => function($dt) { return $dt->format('H:i'); }],
  203. '<time datetime="2016-08-04T13:54:13+00:00" title="13:54">08.04.2016</time>',
  204. ],
  205. [
  206. new \DateInterval('P1YT15S'),
  207. function($i) { return $i->format('%yy, %ss'); },
  208. ['title' => function($i) { return $i->format('%mm'); }],
  209. '<time datetime="P1Y0M0DT0H0M15S" title="0m">1y, 15s</time>',
  210. ],
  211. ];
  212. }
  213. /**
  214. * @dataProvider timeProvider
  215. */
  216. public function testTime($dt, $content, $attributes, $expected_tag)
  217. {
  218. $this->assertSame($expected_tag, HtmlHelper::time($dt, $content, $attributes));
  219. }
  220. }