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.

322 lines
8.5 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
  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/RegexValidator.php';
  15. require_once dirname(__FILE__) . '/../../validator/EmailValidator.php';
  16. require_once dirname(__FILE__) . '/../../form/Form.php';
  17. require_once dirname(__FILE__) . '/../../form/FormField.php';
  18. class FormFieldTest extends PHPUnit_Framework_TestCase
  19. {
  20. public function setUp()
  21. {
  22. $this->ff = new TestFormForFormField;
  23. $this->obj_fields = $this->ff->getProp('fields');
  24. }
  25. public function testSetRequired()
  26. {
  27. $form_field = new FormField;
  28. $this->assertNotNull($form_field->setRequired(1));
  29. }
  30. public function testSetIgnored()
  31. {
  32. $form_field = new FormField;
  33. $this->assertNotNull($form_field->setIgnored(1));
  34. }
  35. public function testIsIgnored()
  36. {
  37. $form_field = new FormField;
  38. $this->assertNotNull($form_field->isIgnored());
  39. }
  40. public function testIsRequired()
  41. {
  42. $form_field = new FormField;
  43. $this->assertNotNull($form_field->isRequired());
  44. }
  45. public function testAddValidators()
  46. {
  47. $fields = array(
  48. 'NotEmpty' => new NotEmptyValidator(),
  49. 'Email' => new EmailValidator()
  50. );
  51. $this->assertInstanceOf('iValidator', $fields['NotEmpty']);
  52. $this->assertInstanceOf('iValidator', $fields['Email']);
  53. $tmp_form_field = new TmpFormField;
  54. $tmp_form_field->addValidators($fields);
  55. $array = $tmp_form_field->getProp('validators');
  56. $this->assertArrayHasKey('NotEmptyValidator', $array);
  57. $this->assertArrayHasKey('EmailValidator', $array);
  58. }
  59. public function testAddValidatorObject()
  60. {
  61. $validator = new NotEmptyValidator;
  62. $this->assertInstanceOf('iValidator', new NotEmptyValidator);
  63. $tmp_form_field = new TmpFormField;
  64. $tmp_form_field->addValidator($validator);
  65. $array = $tmp_form_field->getProp('validators');
  66. $this->assertArrayHasKey('NotEmptyValidator', $array);
  67. }
  68. public function testAddValidatorString()
  69. {
  70. $validator = 'NotEmpty';
  71. $tmp_form_field = new TmpFormField;
  72. $tmp_form_field->addValidator($validator);
  73. $array = $tmp_form_field->getProp('validators');
  74. $this->assertArrayHasKey('NotEmptyValidator', $array);
  75. }
  76. public function testAddValidatorElse()
  77. {
  78. $validator = true;
  79. $this->setExpectedException('Exception');
  80. $tmp_form_field = new TmpFormField;
  81. $tmp_form_field->addValidator($validator);
  82. $array = $tmp_form_field->getProp('validators');
  83. $this->assertArrayHasKey('NotEmptyValidator', $array);
  84. }
  85. public function testAddFilters()
  86. {
  87. $fields = array(
  88. 'fff2' => new loginFilter(),
  89. 'fff1' => 'login'
  90. );
  91. $this->assertInstanceOf('iFilter', $fields['fff2']);
  92. $tmp_form_field = new TmpFormField;
  93. $tmp_form_field->addFilters($fields);
  94. $array = $tmp_form_field->getProp('filters');
  95. $this->assertArrayHasKey('loginFilter', $array);
  96. }
  97. public function testAddFilterObject()
  98. {
  99. $filter = new loginFilter;
  100. $this->assertInstanceOf('iFilter', $filter);
  101. $tmp_form_field = new TmpFormField;
  102. $tmp_form_field->addFilter($filter);
  103. $array = $tmp_form_field->getProp('filters');
  104. $this->assertArrayHasKey('loginFilter', $array);
  105. }
  106. public function testAddFilterString()
  107. {
  108. $tmp_form_field = new TmpFormField;
  109. $tmp_form_field->addFilter('login');
  110. $array = $tmp_form_field->getProp('filters');
  111. $this->assertArrayHasKey('loginFilter', $array);
  112. }
  113. public function testAddFilterElse()
  114. {
  115. $this->setExpectedException('Exception');
  116. $filter = new NotEmptyValidator;
  117. $tmp_form_field = new TmpFormField;
  118. $tmp_form_field->addFilter($filter);
  119. $array = $tmp_form_field->getProp('filters');
  120. $this->assertArrayHasKey('NotEmptyValidator', $array);
  121. }
  122. public function testGetValueArray()
  123. {
  124. $form = new TestFormForFormField();
  125. $test_array = array(
  126. 'login'=> 'login',
  127. 'password'=> 'password'
  128. );
  129. $this->assertSame(1, $form->isValid($test_array));
  130. $this->assertTrue($this->obj_fields['login']->isValid($test_array));
  131. $this->assertAttributeInternalType('array', 'value', $this->obj_fields['login']);
  132. $this->assertArrayHasKey('login', $this->obj_fields['login']->getSourceValue());
  133. }
  134. public function testGetValueString()
  135. {
  136. $test_array = array(
  137. 'login'=> 'login',
  138. 'password'=> 'password'
  139. );
  140. $this->assertTrue($this->obj_fields['login']->isValid($test_array['login']));
  141. $this->assertAttributeNotInternalType('array', 'value', $this->obj_fields['login']);
  142. $this->assertEquals('login',$this->obj_fields['login']->getSourceValue());
  143. }
  144. public function testGetMessage()
  145. {
  146. $this->assertNotNull($this->obj_fields['login']->getMessage());
  147. }
  148. public function testIsValidMissing1()
  149. {
  150. $test_array = array(
  151. 'login'=> '',
  152. 'password'=> ''
  153. );
  154. $this->setExpectedException('Exception');
  155. $this->assertSame(1, $this->obj_fields['login']->isValid($test_array));
  156. }
  157. public function testIsValidMissing11()
  158. {
  159. $test_array = array(
  160. 'login'=> '',
  161. 'password'=> ''
  162. );
  163. $this->obj_fields['login']->__construct('ssssssss');
  164. $this->assertFalse($this->obj_fields['login']->isValid($test_array));
  165. }
  166. public function testIsValidMissing2()
  167. {
  168. $this->obj_fields2 = $this->ff->getProp('fields');
  169. $this->assertFalse($this->obj_fields2['login']->isValid(''));
  170. }
  171. public function testIsValidMissing3()
  172. {
  173. $this->obj_fields2 = $this->ff->getProp('fields');
  174. $this->obj_fields2['login']->setRequired(false);
  175. $this->obj_fields2['login']->isValid('');
  176. $this->assertFalse($this->obj_fields2['login']->isRequired());
  177. }
  178. public function testFilterValue()
  179. {
  180. $this->obj_fields['login']->isValid('login');
  181. $this->obj_fields['login']->addFilter('login');
  182. $lf = new loginFilter;
  183. $this->assertSame($this->obj_fields['login']->getValue(), $lf->filter('login'));
  184. }
  185. }
  186. class TmpFormField extends FormField
  187. {
  188. public function getProp($name)
  189. {
  190. return $this->$name;
  191. }
  192. }
  193. class TestFormForFormField extends Form
  194. {
  195. public function init()
  196. {
  197. ///*
  198. $validator = new NotEmptyValidator();
  199. $validator->setMessage('Enter login name.');
  200. $this->addField('login')->addValidator($validator);
  201. // User password
  202. $validator = new NotEmptyValidator();
  203. $validator->setMessage('Enter your password.');
  204. $this->addField('password')->addValidator($validator);
  205. }
  206. public function getProp($name)
  207. {
  208. return $this->$name;
  209. }
  210. }
  211. interface iFilter
  212. {
  213. public function isValid($value, $context = null);
  214. public function getMessage();
  215. }
  216. Class loginFilter implements iFilter
  217. {
  218. public function filter($value)
  219. {
  220. return $value;
  221. }
  222. public function isValid($value, $context = null)
  223. {
  224. }
  225. public function getMessage()
  226. {
  227. }
  228. }