HtmlGeneratorTest.php 2.3 KB

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