PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/View/Helper/FormMultiCheckboxTest.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 156 lines | 97 code | 9 blank | 50 comment | 7 complexity | 4db76d7ad45b3eeea558e925ffd1a2d0 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: FormMultiCheckboxTest.php 24593 2012-01-05 20:35:02Z matthew $
  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 'Zend/View/Helper/FormMultiCheckbox.php';
  27. require_once 'Zend/View.php';
  28. require_once 'Zend/Registry.php';
  29. /**
  30. * Test class for Zend_View_Helper_FormMultiCheckbox
  31. *
  32. * @category Zend
  33. * @package Zend_View
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_View
  38. * @group Zend_View_Helper
  39. */
  40. class Zend_View_Helper_FormMultiCheckboxTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @return void
  46. */
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_FormMultiCheckboxTest");
  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. if (Zend_Registry::isRegistered('Zend_View_Helper_Doctype')) {
  61. $registry = Zend_Registry::getInstance();
  62. unset($registry['Zend_View_Helper_Doctype']);
  63. }
  64. $this->view = new Zend_View();
  65. $this->helper = new Zend_View_Helper_FormMultiCheckbox();
  66. $this->helper->setView($this->view);
  67. ob_start();
  68. }
  69. /**
  70. * Tears down the fixture, for example, close a network connection.
  71. * This method is called after a test is executed.
  72. *
  73. * @return void
  74. */
  75. public function tearDown()
  76. {
  77. ob_end_clean();
  78. }
  79. public function testMultiCheckboxHelperRendersLabelledCheckboxesForEachOption()
  80. {
  81. $options = array(
  82. 'foo' => 'Foo',
  83. 'bar' => 'Bar',
  84. 'baz' => 'Baz'
  85. );
  86. $html = $this->helper->formMultiCheckbox(array(
  87. 'name' => 'foo',
  88. 'value' => 'bar',
  89. 'options' => $options,
  90. ));
  91. foreach ($options as $key => $value) {
  92. $pattern = '#((<label[^>]*>.*?)(<input[^>]*?("' . $key . '").*?>)(.*?</label>))#';
  93. if (!preg_match($pattern, $html, $matches)) {
  94. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  95. }
  96. $this->assertContains($value, $matches[5], var_export($matches, 1));
  97. $this->assertContains('type="checkbox"', $matches[3], var_export($matches, 1));
  98. $this->assertContains('name="foo[]"', $matches[3], var_export($matches, 1));
  99. $this->assertContains('value="' . $key . '"', $matches[3], var_export($matches, 1));
  100. }
  101. }
  102. public function testRendersAsHtmlByDefault()
  103. {
  104. $options = array(
  105. 'foo' => 'Foo',
  106. 'bar' => 'Bar',
  107. 'baz' => 'Baz'
  108. );
  109. $html = $this->helper->formMultiCheckbox(array(
  110. 'name' => 'foo',
  111. 'value' => 'bar',
  112. 'options' => $options,
  113. ));
  114. foreach ($options as $key => $value) {
  115. $pattern = '#(<input[^>]*?("' . $key . '").*?>)#';
  116. if (!preg_match($pattern, $html, $matches)) {
  117. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  118. }
  119. $this->assertNotContains(' />', $matches[1]);
  120. }
  121. }
  122. public function testCanRendersAsXHtml()
  123. {
  124. $this->view->doctype('XHTML1_STRICT');
  125. $options = array(
  126. 'foo' => 'Foo',
  127. 'bar' => 'Bar',
  128. 'baz' => 'Baz'
  129. );
  130. $html = $this->helper->formMultiCheckbox(array(
  131. 'name' => 'foo',
  132. 'value' => 'bar',
  133. 'options' => $options,
  134. ));
  135. foreach ($options as $key => $value) {
  136. $pattern = '#(<input[^>]*?("' . $key . '").*?>)#';
  137. if (!preg_match($pattern, $html, $matches)) {
  138. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  139. }
  140. $this->assertContains(' />', $matches[1]);
  141. }
  142. }
  143. }
  144. // Call Zend_View_Helper_FormMultiCheckboxTest::main() if this source file is executed directly.
  145. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_FormMultiCheckboxTest::main") {
  146. Zend_View_Helper_FormMultiCheckboxTest::main();
  147. }