You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
9.1 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage form
  7. * @since 2010-04-25
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. require_once dirname(__FILE__) . '/../../validator/iValidator.php';
  12. require_once dirname(__FILE__) . '/../../validator/Validator.php';
  13. require_once dirname(__FILE__) . '/../../validator/NotEmptyValidator.php';
  14. require_once dirname(__FILE__) . '/../../validator/EmailValidator.php';
  15. require_once dirname(__FILE__) . '/../../form/FormField.php';
  16. class FormFieldTest extends PHPUnit_Framework_TestCase
  17. {
  18. public function setUp()
  19. {
  20. /**/
  21. }
  22. public function testSetRequired()
  23. {
  24. $form_field = new FormField();
  25. $this->assertTrue($form_field->isRequired());
  26. $return_object = $form_field->setRequired(false);
  27. $this->assertInstanceOf('FormField', $return_object);
  28. $this->assertSame($form_field, $return_object);
  29. $this->assertFalse($form_field->isRequired());
  30. }
  31. public function testSetIgnored()
  32. {
  33. $form_field = new FormField();
  34. $this->assertFalse($form_field->isIgnored());
  35. $return_object = $form_field->setIgnored(true);
  36. $this->assertInstanceOf('FormField', $return_object);
  37. $this->assertSame($form_field, $return_object);
  38. $this->assertTrue($form_field->isIgnored());
  39. }
  40. public function testIsIgnored()
  41. {
  42. $class_name = 'FormField';
  43. $form_field = new $class_name();
  44. $this->assertFalse($form_field->isIgnored());
  45. $form_field->setIgnored(true);
  46. $this->assertTrue($form_field->isIgnored());
  47. }
  48. public function testIsRequired()
  49. {
  50. $class_name = 'FormField';
  51. $form_field = new $class_name();
  52. $this->assertTrue($form_field->isRequired());
  53. $form_field->setRequired(false);
  54. $this->assertFalse($form_field->isRequired());
  55. }
  56. public function testAddValidators()
  57. {
  58. $validator = array(
  59. 'NotEmpty' => new NotEmptyValidator(),
  60. 'Email' => new EmailValidator()
  61. );
  62. $this->assertInstanceOf('iValidator', $validator['NotEmpty']);
  63. $this->assertInstanceOf('iValidator', $validator['Email']);
  64. $tmp_form_field = new FormField();
  65. $return_object = $tmp_form_field->addValidators($validator);
  66. $array = array('NotEmptyValidator'=>new NotEmptyValidator(),'EmailValidator'=>new EmailValidator());
  67. $this->assertAttributeEquals($array, 'validators', $tmp_form_field);
  68. $this->assertSame($tmp_form_field, $return_object);
  69. }
  70. public function testAddValidatorObject()
  71. {
  72. $validator = new NotEmptyValidator();
  73. $array = array('NotEmptyValidator'=>new NotEmptyValidator());
  74. $this->assertInstanceOf('iValidator', new NotEmptyValidator());
  75. $tmp_form_field = new FormField();
  76. $return_object = $tmp_form_field->addValidator($validator);
  77. $this->assertAttributeEquals($array, 'validators', $tmp_form_field);
  78. $this->assertSame($tmp_form_field, $return_object);
  79. }
  80. public function testAddValidatorString()
  81. {
  82. $tmp_form_field = new FormField();
  83. $return_object = $tmp_form_field->addValidator('NotEmpty');
  84. $this->assertInstanceOf('iValidator', new NotEmptyValidator());
  85. $array = array('NotEmptyValidator'=>new NotEmptyValidator());
  86. $this->assertAttributeEquals($array, 'validators', $tmp_form_field);
  87. $this->assertSame($tmp_form_field, $return_object);
  88. }
  89. public function testAddValidatorElse()
  90. {
  91. $validator = true;
  92. $this->setExpectedException('Exception');
  93. $tmp_form_field = new FormField();
  94. $tmp_form_field->addValidator($validator);
  95. }
  96. public function testAddFilters()
  97. {
  98. $array = array('loginFilter' => new loginFilter(),'passwordFilter' => new passwordFilter());
  99. $this->assertInstanceOf('iFilter', $array['loginFilter']);
  100. $this->assertInstanceOf('iFilter', $array['passwordFilter']);
  101. $tmp_form_field = new FormField();
  102. $return_object = $tmp_form_field->addFilters($array);
  103. $this->assertAttributeEquals($array, 'filters', $tmp_form_field);
  104. $this->assertSame($tmp_form_field, $return_object);
  105. }
  106. public function testAddFilterObject()
  107. {
  108. $filter = new loginFilter();
  109. $array = array('loginFilter' => new loginFilter());
  110. $this->assertInstanceOf('iFilter', new loginFilter());
  111. $tmp_form_field = new FormField();
  112. $return_object = $tmp_form_field->addFilter($filter);
  113. $this->assertAttributeEquals($array, 'filters', $tmp_form_field);
  114. $this->assertSame($tmp_form_field, $return_object);
  115. }
  116. public function testAddFilterString()
  117. {
  118. $tmp_form_field = new FormField();
  119. $array = array('loginFilter' => new loginFilter());
  120. $this->assertInstanceOf('iFilter', new loginFilter());
  121. $return_object = $tmp_form_field->addFilter('login');
  122. $this->assertAttributeEquals($array, 'filters', $tmp_form_field);
  123. $this->assertSame($tmp_form_field, $return_object);
  124. }
  125. public function testAddFilterElse()
  126. {
  127. $this->setExpectedException('Exception');
  128. $filter = new NotEmptyValidator();
  129. $tmp_form_field = new FormField();
  130. $tmp_form_field->addFilter($filter);
  131. }
  132. public function testGetValueArray()
  133. {
  134. $form = new FormField();
  135. $test_array = array(
  136. 'login'=> 'login',
  137. 'password'=> 'password'
  138. );
  139. $this->assertTrue($form->isValid($test_array));
  140. $tmp_form_field = new FormField();
  141. $this->assertTrue($tmp_form_field->isValid($test_array));
  142. $this->assertAttributeInternalType('array', 'value', $tmp_form_field);
  143. $this->assertArrayHasKey('login', $tmp_form_field->getSourceValue());
  144. }
  145. public function testGetValueString()
  146. {
  147. $test_array = array(
  148. 'login'=> 'login',
  149. 'password'=> 'password'
  150. );
  151. $test_form_field = new FormField();
  152. $this->assertTrue($test_form_field->isValid($test_array['login']));
  153. $this->assertAttributeNotInternalType('array', 'value', $test_form_field);
  154. $this->assertEquals('login', $test_form_field->getSourceValue());
  155. }
  156. public function testGetMessage()
  157. {
  158. $test_form_field = new FormField();
  159. $this->assertNotNull($test_form_field->getMessage());
  160. }
  161. public function testIsValidMissingException()
  162. {
  163. $test_array = array(
  164. 'login'=> '',
  165. 'password'=> ''
  166. );
  167. $test_form_field = new FormField();
  168. $test_form_field->addValidator('NotEmpty');
  169. $this->setExpectedException('Exception','Define default message for array fields');
  170. $this->assertTrue($test_form_field->isValid($test_array));
  171. }
  172. public function testIsValidMissingDefaultMessage()
  173. {
  174. $test_array = array(
  175. 'login'=> '',
  176. 'password'=> ''
  177. );
  178. $test_form_field = new FormField('ssssssss');
  179. $return_object = $test_form_field->addValidator('NotEmpty');
  180. $this->assertInstanceOf('iValidator', new NotEmptyValidator());
  181. $array = array('NotEmptyValidator'=>new NotEmptyValidator());
  182. $this->assertAttributeEquals($array, 'validators', $test_form_field);
  183. $this->assertSame($test_form_field, $return_object);
  184. $this->assertFalse($test_form_field->isValid($test_array));
  185. }
  186. public function testIsValidMissingRequired()
  187. {
  188. $form_field = new FormField();
  189. $form_field->setRequired(false);
  190. $this->assertTrue($form_field->isValid(''));
  191. }
  192. public function testIsValidMissingValid()
  193. {
  194. $test_array = array(
  195. 'login'=> 'login',
  196. 'password'=> 'password'
  197. );
  198. $validator = new NotEmptyValidator();
  199. $form_field = new FormField();
  200. $return_object = $form_field->addValidator($validator);
  201. $this->assertTrue($form_field->isValid($test_array));
  202. }
  203. public function testFilterValue()
  204. {
  205. $form_field = new FormField();
  206. $form_field->isValid('login');
  207. $form_field->addFilter('login');
  208. $lf = new loginFilter();
  209. $this->assertSame($form_field->getValue(), $lf->filter('login'));
  210. }
  211. }
  212. interface iFilter
  213. {
  214. public function isValid($value, $context = null);
  215. public function getMessage();
  216. }
  217. class loginFilter implements iFilter
  218. {
  219. public function filter($value)
  220. {
  221. return $value;
  222. }
  223. public function isValid($value, $context = null)
  224. {
  225. }
  226. public function getMessage()
  227. {
  228. }
  229. }
  230. class passwordFilter implements iFilter
  231. {
  232. public function filter($value)
  233. {
  234. return $value;
  235. }
  236. public function isValid($value, $context = null)
  237. {
  238. }
  239. public function getMessage()
  240. {
  241. }
  242. }