ButtonTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_Form
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 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_Form_Element_ButtonTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_ButtonTest::main");
  25. }
  26. require_once 'Zend/Form/Element/Button.php';
  27. require_once 'Zend/Translate.php';
  28. /**
  29. * Test class for Zend_Form_Element_Button
  30. *
  31. * @category Zend
  32. * @package Zend_Form
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Form
  37. */
  38. class Zend_Form_Element_ButtonTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Runs the test methods of this class.
  42. *
  43. * @return void
  44. */
  45. public static function main()
  46. {
  47. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_ButtonTest");
  48. $result = PHPUnit_TextUI_TestRunner::run($suite);
  49. }
  50. /**
  51. * Sets up the fixture, for example, open a network connection.
  52. * This method is called before a test is executed.
  53. *
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. $this->element = new Zend_Form_Element_Button('foo');
  59. }
  60. /**
  61. * Tears down the fixture, for example, close a network connection.
  62. * This method is called after a test is executed.
  63. *
  64. * @return void
  65. */
  66. public function tearDown()
  67. {
  68. }
  69. public function getView()
  70. {
  71. require_once 'Zend/View.php';
  72. $view = new Zend_View();
  73. return $view;
  74. }
  75. public function testButtonElementSubclassesSubmitElement()
  76. {
  77. $this->assertTrue($this->element instanceof Zend_Form_Element_Submit);
  78. }
  79. public function testButtonElementSubclassesXhtmlElement()
  80. {
  81. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  82. }
  83. public function testButtonElementInstanceOfBaseElement()
  84. {
  85. $this->assertTrue($this->element instanceof Zend_Form_Element);
  86. }
  87. public function testHelperAttributeSetToFormButtonByDefault()
  88. {
  89. $this->assertEquals('formButton', $this->element->getAttrib('helper'));
  90. }
  91. public function testButtonElementUsesButtonHelperInViewHelperDecoratorByDefault()
  92. {
  93. $this->_checkZf2794();
  94. $decorator = $this->element->getDecorator('viewHelper');
  95. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  96. $decorator->setElement($this->element);
  97. $helper = $decorator->getHelper();
  98. $this->assertEquals('formButton', $helper);
  99. }
  100. public function testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered()
  101. {
  102. $translations = include dirname(__FILE__) . '/../_files/locale/array.php';
  103. $translate = new Zend_Translate('array', $translations, 'en');
  104. $this->element->setTranslator($translate)
  105. ->setLabel('submit');
  106. $test = $this->element->getLabel();
  107. $this->assertEquals($translations['submit'], $test);
  108. }
  109. public function testTranslatedLabelIsRendered()
  110. {
  111. $this->_checkZf2794();
  112. $this->testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered();
  113. $this->element->setView($this->getView());
  114. $decorator = $this->element->getDecorator('ViewHelper');
  115. $decorator->setElement($this->element);
  116. $html = $decorator->render('');
  117. $this->assertRegexp('/<(input|button)[^>]*?>Submit Button/', $html, $html);
  118. }
  119. /**
  120. * @group ZF-3961
  121. */
  122. public function testValuePropertyShouldNotBeRendered()
  123. {
  124. $this->element->setLabel('Button Label')
  125. ->setView($this->getView());
  126. $html = $this->element->render();
  127. $this->assertContains('Button Label', $html, $html);
  128. $this->assertNotContains('value="', $html);
  129. }
  130. public function testSetDefaultIgnoredToTrueWhenNotDefined()
  131. {
  132. $this->assertTrue($this->element->getIgnore());
  133. }
  134. /**
  135. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  136. *
  137. * @link http://framework.zend.com/issues/browse/ZF-2794
  138. * @return void
  139. */
  140. protected function _checkZf2794()
  141. {
  142. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  143. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  144. }
  145. }
  146. }
  147. // Call Zend_Form_Element_ButtonTest::main() if this source file is executed directly.
  148. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_ButtonTest::main") {
  149. Zend_Form_Element_ButtonTest::main();
  150. }