FormButtonTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_View_Helper_FormButtonTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormButtonTest::main");
  25. }
  26. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
  27. require_once 'Zend/View.php';
  28. require_once 'Zend/View/Helper/FormButton.php';
  29. /**
  30. * Test class for Zend_View_Helper_FormButton.
  31. *
  32. * @category Zend
  33. * @package Zend_View
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_View
  38. * @group Zend_View_Helper
  39. */
  40. class Zend_View_Helper_FormButtonTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @access public
  46. * @static
  47. */
  48. public static function main()
  49. {
  50. require_once "PHPUnit/TextUI/TestRunner.php";
  51. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormButtonTest");
  52. $result = PHPUnit_TextUI_TestRunner::run($suite);
  53. }
  54. /**
  55. * Sets up the fixture, for example, open a network connection.
  56. * This method is called before a test is executed.
  57. *
  58. * @access protected
  59. */
  60. protected function setUp()
  61. {
  62. $this->view = new Zend_View();
  63. $this->helper = new Zend_View_Helper_FormButton();
  64. $this->helper->setView($this->view);
  65. }
  66. /**
  67. * Tears down the fixture, for example, close a network connection.
  68. * This method is called after a test is executed.
  69. *
  70. * @access protected
  71. */
  72. protected function tearDown()
  73. {
  74. }
  75. public function testFormButtonRendersButtonXhtml()
  76. {
  77. $button = $this->helper->formButton('foo', 'bar');
  78. $this->assertRegexp('/<button[^>]*?value="bar"/', $button);
  79. $this->assertRegexp('/<button[^>]*?name="foo"/', $button);
  80. $this->assertRegexp('/<button[^>]*?id="foo"/', $button);
  81. $this->assertContains('</button>', $button);
  82. }
  83. public function testCanPassContentViaContentAttribKey()
  84. {
  85. $button = $this->helper->formButton('foo', 'bar', array('content' => 'Display this'));
  86. $this->assertContains('>Display this<', $button);
  87. $this->assertContains('<button', $button);
  88. $this->assertContains('</button>', $button);
  89. }
  90. public function testCanDisableContentEscaping()
  91. {
  92. $button = $this->helper->formButton('foo', 'bar', array('content' => '<b>Display this</b>', 'escape' => false));
  93. $this->assertContains('><b>Display this</b><', $button);
  94. $button = $this->helper->formButton(array('name' => 'foo', 'value' => 'bar', 'attribs' => array('content' => '<b>Display this</b>', 'escape' => false)));
  95. $this->assertContains('><b>Display this</b><', $button);
  96. $button = $this->helper->formButton(array('name' => 'foo', 'value' => 'bar', 'escape' => false, 'attribs' => array('content' => '<b>Display this</b>')));
  97. $this->assertContains('><b>Display this</b><', $button);
  98. $this->assertContains('<button', $button);
  99. $this->assertContains('</button>', $button);
  100. }
  101. public function testValueUsedForContentWhenNoContentProvided()
  102. {
  103. $button = $this->helper->formButton(array('name' => 'foo', 'value' => 'bar'));
  104. $this->assertRegexp('#<button[^>]*?value="bar"[^>]*>bar</button>#', $button);
  105. }
  106. public function testButtonTypeIsButtonByDefault()
  107. {
  108. $button = $this->helper->formButton(array('name' => 'foo', 'value' => 'bar'));
  109. $this->assertContains('type="button"', $button);
  110. }
  111. public function testButtonTypeMayOnlyBeValidXhtmlButtonType()
  112. {
  113. $button = $this->helper->formButton(array('name' => 'foo', 'value' => 'bar', 'attribs' => array('type' => 'submit')));
  114. $this->assertContains('type="submit"', $button);
  115. $button = $this->helper->formButton(array('name' => 'foo', 'value' => 'bar', 'attribs' => array('type' => 'reset')));
  116. $this->assertContains('type="reset"', $button);
  117. $button = $this->helper->formButton(array('name' => 'foo', 'value' => 'bar', 'attribs' => array('type' => 'button')));
  118. $this->assertContains('type="button"', $button);
  119. $button = $this->helper->formButton(array('name' => 'foo', 'value' => 'bar', 'attribs' => array('type' => 'bogus')));
  120. $this->assertContains('type="button"', $button);
  121. }
  122. }
  123. // Call Zend_View_Helper_FormButtonTest::main() if this source file is executed directly.
  124. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormButtonTest::main") {
  125. Zend_View_Helper_FormButtonTest::main();
  126. }