2
0

FormMultiCheckboxTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. // Call Zend_FormMultiCheckboxTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_FormMultiCheckboxTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. require_once 'Zend/View/Helper/FormMultiCheckbox.php';
  8. require_once 'Zend/View.php';
  9. require_once 'Zend/Registry.php';
  10. /**
  11. * Test class for Zend_View_Helper_FormMultiCheckbox
  12. */
  13. class Zend_View_Helper_FormMultiCheckboxTest extends PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * Runs the test methods of this class.
  17. *
  18. * @return void
  19. */
  20. public static function main()
  21. {
  22. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormMultiCheckboxTest");
  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. if (Zend_Registry::isRegistered('Zend_View_Helper_Doctype')) {
  34. $registry = Zend_Registry::getInstance();
  35. unset($registry['Zend_View_Helper_Doctype']);
  36. }
  37. $this->view = new Zend_View();
  38. $this->helper = new Zend_View_Helper_FormMultiCheckbox();
  39. $this->helper->setView($this->view);
  40. ob_start();
  41. }
  42. /**
  43. * Tears down the fixture, for example, close a network connection.
  44. * This method is called after a test is executed.
  45. *
  46. * @return void
  47. */
  48. public function tearDown()
  49. {
  50. ob_end_clean();
  51. }
  52. public function testMultiCheckboxHelperRendersLabelledCheckboxesForEachOption()
  53. {
  54. $options = array(
  55. 'foo' => 'Foo',
  56. 'bar' => 'Bar',
  57. 'baz' => 'Baz'
  58. );
  59. $html = $this->helper->formMultiCheckbox(array(
  60. 'name' => 'foo',
  61. 'value' => 'bar',
  62. 'options' => $options,
  63. ));
  64. foreach ($options as $key => $value) {
  65. $pattern = '#((<label[^>]*>.*?)(<input[^>]*?("' . $key . '").*?>)(.*?</label>))#';
  66. if (!preg_match($pattern, $html, $matches)) {
  67. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  68. }
  69. $this->assertContains($value, $matches[5], var_export($matches, 1));
  70. $this->assertContains('type="checkbox"', $matches[3], var_export($matches, 1));
  71. $this->assertContains('name="foo[]"', $matches[3], var_export($matches, 1));
  72. $this->assertContains('value="' . $key . '"', $matches[3], var_export($matches, 1));
  73. }
  74. }
  75. public function testRendersAsHtmlByDefault()
  76. {
  77. $options = array(
  78. 'foo' => 'Foo',
  79. 'bar' => 'Bar',
  80. 'baz' => 'Baz'
  81. );
  82. $html = $this->helper->formMultiCheckbox(array(
  83. 'name' => 'foo',
  84. 'value' => 'bar',
  85. 'options' => $options,
  86. ));
  87. foreach ($options as $key => $value) {
  88. $pattern = '#(<input[^>]*?("' . $key . '").*?>)#';
  89. if (!preg_match($pattern, $html, $matches)) {
  90. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  91. }
  92. $this->assertNotContains(' />', $matches[1]);
  93. }
  94. }
  95. public function testCanRendersAsXHtml()
  96. {
  97. $this->view->doctype('XHTML1_STRICT');
  98. $options = array(
  99. 'foo' => 'Foo',
  100. 'bar' => 'Bar',
  101. 'baz' => 'Baz'
  102. );
  103. $html = $this->helper->formMultiCheckbox(array(
  104. 'name' => 'foo',
  105. 'value' => 'bar',
  106. 'options' => $options,
  107. ));
  108. foreach ($options as $key => $value) {
  109. $pattern = '#(<input[^>]*?("' . $key . '").*?>)#';
  110. if (!preg_match($pattern, $html, $matches)) {
  111. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  112. }
  113. $this->assertContains(' />', $matches[1]);
  114. }
  115. }
  116. }
  117. // Call Zend_View_Helper_FormMultiCheckboxTest::main() if this source file is executed directly.
  118. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormMultiCheckboxTest::main") {
  119. Zend_View_Helper_FormMultiCheckboxTest::main();
  120. }