FormMultiCheckboxTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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-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_FormMultiCheckboxTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormMultiCheckboxTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/View/Helper/FormMultiCheckbox.php';
  28. require_once 'Zend/View.php';
  29. require_once 'Zend/Registry.php';
  30. /**
  31. * Test class for Zend_View_Helper_FormMultiCheckbox
  32. *
  33. * @category Zend
  34. * @package Zend_View
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_View
  39. * @group Zend_View_Helper
  40. */
  41. class Zend_View_Helper_FormMultiCheckboxTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Runs the test methods of this class.
  45. *
  46. * @return void
  47. */
  48. public static function main()
  49. {
  50. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormMultiCheckboxTest");
  51. $result = PHPUnit_TextUI_TestRunner::run($suite);
  52. }
  53. /**
  54. * Sets up the fixture, for example, open a network connection.
  55. * This method is called before a test is executed.
  56. *
  57. * @return void
  58. */
  59. public function setUp()
  60. {
  61. if (Zend_Registry::isRegistered('Zend_View_Helper_Doctype')) {
  62. $registry = Zend_Registry::getInstance();
  63. unset($registry['Zend_View_Helper_Doctype']);
  64. }
  65. $this->view = new Zend_View();
  66. $this->helper = new Zend_View_Helper_FormMultiCheckbox();
  67. $this->helper->setView($this->view);
  68. ob_start();
  69. }
  70. /**
  71. * Tears down the fixture, for example, close a network connection.
  72. * This method is called after a test is executed.
  73. *
  74. * @return void
  75. */
  76. public function tearDown()
  77. {
  78. ob_end_clean();
  79. }
  80. public function testMultiCheckboxHelperRendersLabelledCheckboxesForEachOption()
  81. {
  82. $options = array(
  83. 'foo' => 'Foo',
  84. 'bar' => 'Bar',
  85. 'baz' => 'Baz'
  86. );
  87. $html = $this->helper->formMultiCheckbox(array(
  88. 'name' => 'foo',
  89. 'value' => 'bar',
  90. 'options' => $options,
  91. ));
  92. foreach ($options as $key => $value) {
  93. $pattern = '#((<label[^>]*>.*?)(<input[^>]*?("' . $key . '").*?>)(.*?</label>))#';
  94. if (!preg_match($pattern, $html, $matches)) {
  95. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  96. }
  97. $this->assertContains($value, $matches[5], var_export($matches, 1));
  98. $this->assertContains('type="checkbox"', $matches[3], var_export($matches, 1));
  99. $this->assertContains('name="foo[]"', $matches[3], var_export($matches, 1));
  100. $this->assertContains('value="' . $key . '"', $matches[3], var_export($matches, 1));
  101. }
  102. }
  103. public function testRendersAsHtmlByDefault()
  104. {
  105. $options = array(
  106. 'foo' => 'Foo',
  107. 'bar' => 'Bar',
  108. 'baz' => 'Baz'
  109. );
  110. $html = $this->helper->formMultiCheckbox(array(
  111. 'name' => 'foo',
  112. 'value' => 'bar',
  113. 'options' => $options,
  114. ));
  115. foreach ($options as $key => $value) {
  116. $pattern = '#(<input[^>]*?("' . $key . '").*?>)#';
  117. if (!preg_match($pattern, $html, $matches)) {
  118. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  119. }
  120. $this->assertNotContains(' />', $matches[1]);
  121. }
  122. }
  123. public function testCanRendersAsXHtml()
  124. {
  125. $this->view->doctype('XHTML1_STRICT');
  126. $options = array(
  127. 'foo' => 'Foo',
  128. 'bar' => 'Bar',
  129. 'baz' => 'Baz'
  130. );
  131. $html = $this->helper->formMultiCheckbox(array(
  132. 'name' => 'foo',
  133. 'value' => 'bar',
  134. 'options' => $options,
  135. ));
  136. foreach ($options as $key => $value) {
  137. $pattern = '#(<input[^>]*?("' . $key . '").*?>)#';
  138. if (!preg_match($pattern, $html, $matches)) {
  139. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  140. }
  141. $this->assertContains(' />', $matches[1]);
  142. }
  143. }
  144. }
  145. // Call Zend_View_Helper_FormMultiCheckboxTest::main() if this source file is executed directly.
  146. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormMultiCheckboxTest::main") {
  147. Zend_View_Helper_FormMultiCheckboxTest::main();
  148. }