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

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

https://github.com/mfairchild365/zf2
PHP | 140 lines | 87 code | 8 blank | 45 comment | 4 complexity | b320d804f7f08f3d45ab9069321ecc1c MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace ZendTest\View\Helper;
  25. use Zend\Registry,
  26. Zend\View\PhpRenderer as View,
  27. Zend\View\Helper\FormMultiCheckbox;
  28. /**
  29. * Test class for Zend_View_Helper_FormMultiCheckbox
  30. *
  31. * @category Zend
  32. * @package Zend_View
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_View
  37. * @group Zend_View_Helper
  38. */
  39. class FormMultiCheckboxTest extends \PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Sets up the fixture, for example, open a network connection.
  43. * This method is called before a test is executed.
  44. *
  45. * @return void
  46. */
  47. public function setUp()
  48. {
  49. if (Registry::isRegistered('Zend_View_Helper_Doctype')) {
  50. $registry = Registry::getInstance();
  51. unset($registry['Zend_View_Helper_Doctype']);
  52. }
  53. $this->view = new View();
  54. $this->helper = new FormMultiCheckbox();
  55. $this->helper->setView($this->view);
  56. ob_start();
  57. }
  58. /**
  59. * Tears down the fixture, for example, close a network connection.
  60. * This method is called after a test is executed.
  61. *
  62. * @return void
  63. */
  64. public function tearDown()
  65. {
  66. ob_end_clean();
  67. }
  68. public function testMultiCheckboxHelperRendersLabelledCheckboxesForEachOption()
  69. {
  70. $options = array(
  71. 'foo' => 'Foo',
  72. 'bar' => 'Bar',
  73. 'baz' => 'Baz'
  74. );
  75. $html = $this->helper->direct(array(
  76. 'name' => 'foo',
  77. 'value' => 'bar',
  78. 'options' => $options,
  79. ));
  80. foreach ($options as $key => $value) {
  81. $pattern = '#((<label[^>]*>.*?)(<input[^>]*?("' . $key . '").*?>)(.*?</label>))#';
  82. if (!preg_match($pattern, $html, $matches)) {
  83. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  84. }
  85. $this->assertContains($value, $matches[5], var_export($matches, 1));
  86. $this->assertContains('type="checkbox"', $matches[3], var_export($matches, 1));
  87. $this->assertContains('name="foo[]"', $matches[3], var_export($matches, 1));
  88. $this->assertContains('value="' . $key . '"', $matches[3], var_export($matches, 1));
  89. }
  90. }
  91. public function testRendersAsHtmlByDefault()
  92. {
  93. $options = array(
  94. 'foo' => 'Foo',
  95. 'bar' => 'Bar',
  96. 'baz' => 'Baz'
  97. );
  98. $html = $this->helper->direct(array(
  99. 'name' => 'foo',
  100. 'value' => 'bar',
  101. 'options' => $options,
  102. ));
  103. foreach ($options as $key => $value) {
  104. $pattern = '#(<input[^>]*?("' . $key . '").*?>)#';
  105. if (!preg_match($pattern, $html, $matches)) {
  106. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  107. }
  108. $this->assertNotContains(' />', $matches[1]);
  109. }
  110. }
  111. public function testCanRendersAsXHtml()
  112. {
  113. $this->view->plugin('doctype')->direct('XHTML1_STRICT');
  114. $options = array(
  115. 'foo' => 'Foo',
  116. 'bar' => 'Bar',
  117. 'baz' => 'Baz'
  118. );
  119. $html = $this->helper->direct(array(
  120. 'name' => 'foo',
  121. 'value' => 'bar',
  122. 'options' => $options,
  123. ));
  124. foreach ($options as $key => $value) {
  125. $pattern = '#(<input[^>]*?("' . $key . '").*?>)#';
  126. if (!preg_match($pattern, $html, $matches)) {
  127. $this->fail('Failed to match ' . $pattern . ': ' . $html);
  128. }
  129. $this->assertContains(' />', $matches[1]);
  130. }
  131. }
  132. }