HtmlHelperTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 startTagTypeErrorProvider()
  25. {
  26. return [
  27. [1, []],
  28. [false, []],
  29. ['tag', true],
  30. ['tag', 'attr'],
  31. ];
  32. }
  33. /**
  34. * @dataProvider startTagTypeErrorProvider
  35. * @expectedException \TypeError
  36. */
  37. public function testStartTagTypeError($name, $attributes)
  38. {
  39. HtmlHelper::startTag($name, $attributes);
  40. }
  41. public function startTagProvider()
  42. {
  43. return [
  44. ['tag', [], '<tag>'],
  45. ['start', ['a' => '1'], '<start a="1">'],
  46. ['start', ['a' => 1], '<start a="1">'],
  47. ['start', ['a' => '1', 'b' => '2'], '<start a="1" b="2">'],
  48. ['start', ['b' => '1', 'a' => '2'], '<start b="1" a="2">'],
  49. ['start', ['a' => null, 'b' => '2'], '<start b="2">'],
  50. ['start', ['a' => true], '<start a="a">'],
  51. ['start', ['a' => true, 'b' => '2'], '<start a="a" b="2">'],
  52. ['start', ['a' => false], '<start>'],
  53. ['start', ['a' => false, 'b' => '2'], '<start 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 startTagProvider
  63. */
  64. public function testStartTag($name, $attributes, $expected_tag)
  65. {
  66. $this->assertSame($expected_tag, HtmlHelper::startTag($name, $attributes));
  67. }
  68. public function endTagTypeErrorProvider()
  69. {
  70. return [
  71. [1],
  72. [false],
  73. ];
  74. }
  75. /**
  76. * @dataProvider endTagTypeErrorProvider
  77. * @expectedException \TypeError
  78. */
  79. public function testEndTagTypeError($name)
  80. {
  81. HtmlHelper::endTag($name);
  82. }
  83. public function endTagProvider()
  84. {
  85. return [
  86. ['tag', '</tag>'],
  87. ['end', '</end>'],
  88. [null, null],
  89. ];
  90. }
  91. /**
  92. * @dataProvider endTagProvider
  93. */
  94. public function testEndTag($name, $expected_tag)
  95. {
  96. $this->assertSame($expected_tag, HtmlHelper::endTag($name));
  97. }
  98. }