2
0

ButtonTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // Call Zend_Form_Element_ButtonTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_ButtonTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/Form/Element/Button.php';
  8. require_once 'Zend/Translate.php';
  9. /**
  10. * Test class for Zend_Form_Element_Button
  11. */
  12. class Zend_Form_Element_ButtonTest extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * Runs the test methods of this class.
  16. *
  17. * @return void
  18. */
  19. public static function main()
  20. {
  21. require_once "PHPUnit/TextUI/TestRunner.php";
  22. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_ButtonTest");
  23. $result = PHPUnit_TextUI_TestRunner::run($suite);
  24. }
  25. /**
  26. * Sets up the fixture, for example, open a network connection.
  27. * This method is called before a test is executed.
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. $this->element = new Zend_Form_Element_Button('foo');
  34. }
  35. /**
  36. * Tears down the fixture, for example, close a network connection.
  37. * This method is called after a test is executed.
  38. *
  39. * @return void
  40. */
  41. public function tearDown()
  42. {
  43. }
  44. public function getView()
  45. {
  46. require_once 'Zend/View.php';
  47. $view = new Zend_View();
  48. return $view;
  49. }
  50. public function testButtonElementSubclassesSubmitElement()
  51. {
  52. $this->assertTrue($this->element instanceof Zend_Form_Element_Submit);
  53. }
  54. public function testButtonElementSubclassesXhtmlElement()
  55. {
  56. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  57. }
  58. public function testButtonElementInstanceOfBaseElement()
  59. {
  60. $this->assertTrue($this->element instanceof Zend_Form_Element);
  61. }
  62. public function testHelperAttributeSetToFormButtonByDefault()
  63. {
  64. $this->assertEquals('formButton', $this->element->getAttrib('helper'));
  65. }
  66. public function testButtonElementUsesButtonHelperInViewHelperDecoratorByDefault()
  67. {
  68. $this->_checkZf2794();
  69. $decorator = $this->element->getDecorator('viewHelper');
  70. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  71. $decorator->setElement($this->element);
  72. $helper = $decorator->getHelper();
  73. $this->assertEquals('formButton', $helper);
  74. }
  75. public function testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered()
  76. {
  77. $translations = include dirname(__FILE__) . '/../_files/locale/array.php';
  78. $translate = new Zend_Translate('array', $translations, 'en');
  79. $this->element->setTranslator($translate)
  80. ->setLabel('submit');
  81. $test = $this->element->getLabel();
  82. $this->assertEquals($translations['submit'], $test);
  83. }
  84. public function testTranslatedLabelIsRendered()
  85. {
  86. $this->_checkZf2794();
  87. $this->testGetLabelReturnsTranslatedLabelIfTranslatorIsRegistered();
  88. $this->element->setView($this->getView());
  89. $decorator = $this->element->getDecorator('ViewHelper');
  90. $decorator->setElement($this->element);
  91. $html = $decorator->render('');
  92. $this->assertRegexp('/<(input|button)[^>]*?>Submit Button/', $html, $html);
  93. }
  94. /**
  95. * @group ZF-3961
  96. */
  97. public function testValuePropertyShouldNotBeRendered()
  98. {
  99. $this->element->setLabel('Button Label')
  100. ->setView($this->getView());
  101. $html = $this->element->render();
  102. $this->assertContains('Button Label', $html, $html);
  103. $this->assertNotContains('value="', $html);
  104. }
  105. /**
  106. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  107. *
  108. * @link http://framework.zend.com/issues/browse/ZF-2794
  109. * @return void
  110. */
  111. protected function _checkZf2794()
  112. {
  113. if (strtolower(substr(PHP_OS, 0, 3)) == 'win' && version_compare(PHP_VERSION, '5.1.4', '=')) {
  114. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  115. }
  116. }
  117. }
  118. // Call Zend_Form_Element_ButtonTest::main() if this source file is executed directly.
  119. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_ButtonTest::main") {
  120. Zend_Form_Element_ButtonTest::main();
  121. }