NoteTest.php 7.0 KB

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