HtmlHelperTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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' => 1], '<void a="1" />'],
  47. ['void', ['a' => '1', 'b' => '2'], '<void a="1" b="2" />'],
  48. ['void', ['b' => '1', 'a' => '2'], '<void b="1" a="2" />'],
  49. ['void', ['a' => null, 'b' => '2'], '<void b="2" />'],
  50. ['void', ['a' => true], '<void a="a" />'],
  51. ['void', ['a' => true, 'b' => '2'], '<void a="a" b="2" />'],
  52. ['void', ['a' => false], '<void />'],
  53. ['void', ['a' => false, 'b' => '2'], '<void b="2" />'],
  54. ['script', ['type' => 'text/javascript'], '<script type="text/javascript" />'],
  55. ['span', ['onclick' => 'alert(":-)")'], '<span onclick="alert(&quot;:-)&quot;)" />'],
  56. ['span', ['onclick' => "alert(':-)')"], '<span onclick="alert(&#039;:-)&#039;)" />'],
  57. [null, [], null],
  58. [null, ['attr' => 'v'], null],
  59. ];
  60. }
  61. /**
  62. * @dataProvider voidTagProvider
  63. */
  64. public function testVoidTag($name, $attributes, $expected_tag)
  65. {
  66. $this->assertSame($expected_tag, HtmlHelper::voidTag($name, $attributes));
  67. }
  68. public function startTagTypeErrorProvider()
  69. {
  70. return [
  71. [1, []],
  72. [false, []],
  73. ['tag', true],
  74. ['tag', 'attr'],
  75. ];
  76. }
  77. /**
  78. * @dataProvider startTagTypeErrorProvider
  79. * @expectedException \TypeError
  80. */
  81. public function testStartTagTypeError($name, $attributes)
  82. {
  83. HtmlHelper::startTag($name, $attributes);
  84. }
  85. public function startTagProvider()
  86. {
  87. return [
  88. ['tag', [], '<tag>'],
  89. ['start', ['a' => '1'], '<start a="1">'],
  90. ['start', ['a' => 1], '<start a="1">'],
  91. ['start', ['a' => '1', 'b' => '2'], '<start a="1" b="2">'],
  92. ['start', ['b' => '1', 'a' => '2'], '<start b="1" a="2">'],
  93. ['start', ['a' => null, 'b' => '2'], '<start b="2">'],
  94. ['start', ['a' => true], '<start a="a">'],
  95. ['start', ['a' => true, 'b' => '2'], '<start a="a" b="2">'],
  96. ['start', ['a' => false], '<start>'],
  97. ['start', ['a' => false, 'b' => '2'], '<start b="2">'],
  98. ['script', ['type' => 'text/javascript'], '<script type="text/javascript">'],
  99. ['span', ['onclick' => 'alert(":-)")'], '<span onclick="alert(&quot;:-)&quot;)">'],
  100. ['span', ['onclick' => "alert(':-)')"], '<span onclick="alert(&#039;:-)&#039;)">'],
  101. [null, [], null],
  102. [null, ['attr' => 'v'], null],
  103. ];
  104. }
  105. /**
  106. * @dataProvider startTagProvider
  107. */
  108. public function testStartTag($name, $attributes, $expected_tag)
  109. {
  110. $this->assertSame($expected_tag, HtmlHelper::startTag($name, $attributes));
  111. }
  112. public function endTagTypeErrorProvider()
  113. {
  114. return [
  115. [1],
  116. [false],
  117. ];
  118. }
  119. /**
  120. * @dataProvider endTagTypeErrorProvider
  121. * @expectedException \TypeError
  122. */
  123. public function testEndTagTypeError($name)
  124. {
  125. HtmlHelper::endTag($name);
  126. }
  127. public function endTagProvider()
  128. {
  129. return [
  130. ['tag', '</tag>'],
  131. ['end', '</end>'],
  132. [null, null],
  133. ];
  134. }
  135. /**
  136. * @dataProvider endTagProvider
  137. */
  138. public function testEndTag($name, $expected_tag)
  139. {
  140. $this->assertSame($expected_tag, HtmlHelper::endTag($name));
  141. }
  142. }