HtmlGeneratorTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Symfony\Tests\Components\Form;
  3. require_once __DIR__ . '/../../../../bootstrap.php';
  4. use Symfony\Components\Form\HtmlGenerator;
  5. class HtmlGeneratorTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $generator;
  8. public function setUp()
  9. {
  10. HtmlGenerator::setXhtml(true);
  11. $this->generator = new HtmlGenerator();
  12. }
  13. public function testEscape()
  14. {
  15. $this->assertEquals('&lt;&amp;abcd', $this->generator->escape('<&abcd'));
  16. }
  17. public function testEscapeOnlyOnce()
  18. {
  19. $this->assertEquals('&lt;&amp;abcd', $this->generator->escape('<&amp;abcd'));
  20. }
  21. public function testAttribute()
  22. {
  23. $this->assertEquals('foo="bar"', $this->generator->attribute('foo', 'bar'));
  24. }
  25. public function testEscapeAttribute()
  26. {
  27. $this->assertEquals('foo="&lt;&gt;"', $this->generator->attribute('foo', '<>'));
  28. }
  29. public function testXhtmlAttribute()
  30. {
  31. HtmlGenerator::setXhtml(true);
  32. $this->assertEquals('foo="foo"', $this->generator->attribute('foo', true));
  33. }
  34. public function testNonXhtmlAttribute()
  35. {
  36. HtmlGenerator::setXhtml(false);
  37. $this->assertEquals('foo', $this->generator->attribute('foo', true));
  38. }
  39. public function testAttributes()
  40. {
  41. $html = $this->generator->attributes(array(
  42. 'foo' => 'bar',
  43. 'bar' => 'baz',
  44. ));
  45. $this->assertEquals(' foo="bar" bar="baz"', $html);
  46. }
  47. public function testXhtmlTag()
  48. {
  49. HtmlGenerator::setXhtml(true);
  50. $html = $this->generator->tag('input', array(
  51. 'type' => 'text',
  52. ));
  53. $this->assertEquals('<input type="text" />', $html);
  54. }
  55. public function testNonXhtmlTag()
  56. {
  57. HtmlGenerator::setXhtml(false);
  58. $html = $this->generator->tag('input', array(
  59. 'type' => 'text',
  60. ));
  61. $this->assertEquals('<input type="text">', $html);
  62. }
  63. public function testContentTag()
  64. {
  65. $html = $this->generator->contentTag('p', 'asdf', array(
  66. 'class' => 'foo',
  67. ));
  68. $this->assertEquals('<p class="foo">asdf</p>', $html);
  69. }
  70. // it should be possible to pass the output of the tag() method as body
  71. // of the content tag
  72. public function testDontEscapeContentTag()
  73. {
  74. $this->assertEquals('<p><&</p>', $this->generator->contentTag('p', '<&'));
  75. }
  76. }