HtmlHelperTest.php 8.2 KB

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