NoteTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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-2015 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_Form_Element_NoteTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Form_Element_NoteTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  27. require_once 'Zend/Form/Element/Note.php';
  28. /**
  29. * Test class for Zend_Form_Element_Text
  30. *
  31. * @category Zend
  32. * @package Zend_Form
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Form
  37. */
  38. class Zend_Form_Element_NoteTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * Runs the test methods of this class.
  42. *
  43. * @return void
  44. */
  45. public static function main()
  46. {
  47. $suite = new PHPUnit_Framework_TestSuite("Zend_Form_Element_NoteTest");
  48. $result = PHPUnit_TextUI_TestRunner::run($suite);
  49. }
  50. /**
  51. * Sets up the fixture, for example, open a network connection.
  52. * This method is called before a test is executed.
  53. *
  54. * @return void
  55. */
  56. public function setUp()
  57. {
  58. $this->element = new Zend_Form_Element_Note('foo');
  59. }
  60. /**
  61. * Tears down the fixture, for example, close a network connection.
  62. * This method is called after a test is executed.
  63. *
  64. * @return void
  65. */
  66. public function tearDown()
  67. {
  68. }
  69. public function testNoteElementSubclassesXhtmlElement()
  70. {
  71. $this->assertTrue($this->element instanceof Zend_Form_Element_Xhtml);
  72. }
  73. public function testNoteElementInstanceOfBaseElement()
  74. {
  75. $this->assertTrue($this->element instanceof Zend_Form_Element);
  76. }
  77. public function testNoteElementUsesNoteHelperInViewHelperDecoratorByDefault()
  78. {
  79. $this->_checkZf2794();
  80. $decorator = $this->element->getDecorator('viewHelper');
  81. $this->assertTrue($decorator instanceof Zend_Form_Decorator_ViewHelper);
  82. $decorator->setElement($this->element);
  83. $helper = $decorator->getHelper();
  84. $this->assertEquals('formNote', $helper);
  85. }
  86. public function testNoteElementValidationIsAlwaysTrue()
  87. {
  88. // Solo
  89. $this->assertTrue($this->element->isValid('foo'));
  90. // Set required
  91. $this->element->setRequired(true);
  92. $this->assertTrue($this->element->isValid(''));
  93. // Reset
  94. $this->element->setRequired(false);
  95. // Examining various validators
  96. $validators = array(
  97. array(
  98. 'options' => array('Alnum'),
  99. 'value' => 'aa11?? ',
  100. ),
  101. array(
  102. 'options' => array('Alpha'),
  103. 'value' => 'aabb11',
  104. ),
  105. array(
  106. 'options' => array(
  107. 'Between',
  108. false,
  109. array(
  110. 'min' => 0,
  111. 'max' => 10,
  112. )
  113. ),
  114. 'value' => '11',
  115. ),
  116. array(
  117. 'options' => array('Date'),
  118. 'value' => '10.10.2000',
  119. ),
  120. array(
  121. 'options' => array('Digits'),
  122. 'value' => '1122aa',
  123. ),
  124. array(
  125. 'options' => array('EmailAddress'),
  126. 'value' => 'foo',
  127. ),
  128. array(
  129. 'options' => array('Float'),
  130. 'value' => '10a01',
  131. ),
  132. array(
  133. 'options' => array(
  134. 'GreaterThan',
  135. false,
  136. array('min' => 10),
  137. ),
  138. 'value' => '9',
  139. ),
  140. array(
  141. 'options' => array('Hex'),
  142. 'value' => '123ABCDEFGH',
  143. ),
  144. array(
  145. 'options' => array(
  146. 'InArray',
  147. false,
  148. array(
  149. 'key' => 'value',
  150. 'otherkey' => 'othervalue',
  151. )
  152. ),
  153. 'value' => 'foo',
  154. ),
  155. array(
  156. 'options' => array('Int'),
  157. 'value' => '1234.5',
  158. ),
  159. array(
  160. 'options' => array(
  161. 'LessThan',
  162. false,
  163. array('max' => 10),
  164. ),
  165. 'value' => '11',
  166. ),
  167. array(
  168. 'options' => array('NotEmpty'),
  169. 'value' => '',
  170. ),
  171. array(
  172. 'options' => array(
  173. 'Regex',
  174. false,
  175. array('pattern' => '/^Test/'),
  176. ),
  177. 'value' => 'Pest',
  178. ),
  179. array(
  180. 'options' => array(
  181. 'StringLength',
  182. false,
  183. array(
  184. 6,
  185. 20,
  186. )
  187. ),
  188. 'value' => 'foo',
  189. ),
  190. );
  191. foreach ($validators as $validator) {
  192. // Add validator
  193. $this->element->addValidators(array($validator['options']));
  194. // Testing
  195. $this->assertTrue($this->element->isValid($validator['value']));
  196. // Remove validator
  197. $this->element->removeValidator($validator['options'][0]);
  198. }
  199. }
  200. /**
  201. * Used by test methods susceptible to ZF-2794, marks a test as incomplete
  202. *
  203. * @link http://framework.zend.com/issues/browse/ZF-2794
  204. * @return void
  205. */
  206. protected function _checkZf2794()
  207. {
  208. if (strtolower(substr(PHP_OS, 0, 3)) == 'win'
  209. && version_compare(PHP_VERSION, '5.1.4', '=')
  210. ) {
  211. $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
  212. }
  213. }
  214. }
  215. // Call Zend_Form_Element_NoteTest::main() if this source file is executed directly.
  216. if (PHPUnit_MAIN_METHOD == "Zend_Form_Element_NoteTest::main") {
  217. Zend_Form_Element_NoteTest::main();
  218. }