SubFormTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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-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. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Form_SubFormTest::main');
  24. }
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. require_once 'PHPUnit/Framework/TestSuite.php';
  27. require_once 'PHPUnit/TextUI/TestRunner.php';
  28. // error_reporting(E_ALL);
  29. require_once 'Zend/Form/SubForm.php';
  30. require_once 'Zend/View.php';
  31. require_once 'Zend/Version.php';
  32. /**
  33. * @category Zend
  34. * @package Zend_Form
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Form
  39. */
  40. class Zend_Form_SubFormTest extends PHPUnit_Framework_TestCase
  41. {
  42. public static function main()
  43. {
  44. require_once "PHPUnit/TextUI/TestRunner.php";
  45. $suite = new PHPUnit_Framework_TestSuite('Zend_Form_SubFormTest');
  46. $result = PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. public function setUp()
  49. {
  50. Zend_Form::setDefaultTranslator(null);
  51. $this->form = new Zend_Form_SubForm();
  52. }
  53. public function tearDown()
  54. {
  55. }
  56. // General
  57. public function testSubFormUtilizesDefaultDecorators()
  58. {
  59. $decorators = $this->form->getDecorators();
  60. $this->assertTrue(array_key_exists('Zend_Form_Decorator_FormElements', $decorators));
  61. $this->assertTrue(array_key_exists('Zend_Form_Decorator_HtmlTag', $decorators));
  62. $this->assertTrue(array_key_exists('Zend_Form_Decorator_Fieldset', $decorators));
  63. $this->assertTrue(array_key_exists('Zend_Form_Decorator_DtDdWrapper', $decorators));
  64. $htmlTag = $decorators['Zend_Form_Decorator_HtmlTag'];
  65. $tag = $htmlTag->getOption('tag');
  66. $this->assertEquals('dl', $tag);
  67. }
  68. public function testSubFormIsArrayByDefault()
  69. {
  70. $this->assertTrue($this->form->isArray());
  71. }
  72. public function testElementsBelongToSubFormNameByDefault()
  73. {
  74. $this->testSubFormIsArrayByDefault();
  75. $this->form->setName('foo');
  76. $this->assertEquals($this->form->getName(), $this->form->getElementsBelongTo());
  77. }
  78. // Extensions
  79. public function testInitCalledBeforeLoadDecorators()
  80. {
  81. $form = new Zend_Form_SubFormTest_SubForm();
  82. $decorators = $form->getDecorators();
  83. $this->assertTrue(empty($decorators));
  84. }
  85. // Bugfixes
  86. /**
  87. * @see ZF-2883
  88. */
  89. public function testDisplayGroupsShouldInheritSubFormNamespace()
  90. {
  91. $this->form->addElement('text', 'foo')
  92. ->addElement('text', 'bar')
  93. ->addDisplayGroup(array('foo', 'bar'), 'foobar');
  94. $form = new Zend_Form();
  95. $form->addSubForm($this->form, 'attributes');
  96. $html = $form->render(new Zend_View());
  97. $this->assertContains('name="attributes[foo]"', $html);
  98. $this->assertContains('name="attributes[bar]"', $html);
  99. }
  100. /**
  101. * @see ZF-3272
  102. */
  103. public function testRenderedSubFormDtShouldContainNoBreakSpace()
  104. {
  105. $subForm = new Zend_Form_SubForm(array(
  106. 'elements' => array(
  107. 'foo' => 'text',
  108. 'bar' => 'text',
  109. ),
  110. ));
  111. $form = new Zend_Form();
  112. $form->addSubForm($subForm, 'foobar')
  113. ->setView(new Zend_View);
  114. $html = $form->render();
  115. $this->assertContains('>&#160;</dt>', $html );
  116. }
  117. /**
  118. * Prove the fluent interface on Zend_Form_Subform::loadDefaultDecorators
  119. *
  120. * @link http://framework.zend.com/issues/browse/ZF-9913
  121. * @return void
  122. */
  123. public function testFluentInterfaceOnLoadDefaultDecorators()
  124. {
  125. $this->assertSame($this->form, $this->form->loadDefaultDecorators());
  126. }
  127. }
  128. class Zend_Form_SubFormTest_SubForm extends Zend_Form_SubForm
  129. {
  130. public function init()
  131. {
  132. $this->setDisableLoadDefaultDecorators(true);
  133. }
  134. }
  135. if (PHPUnit_MAIN_METHOD == 'Zend_Form_SubFormTest::main') {
  136. Zend_Form_SubFormTest::main();
  137. }