ButtonTest.php 5.3 KB

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