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.

261 lines
7.0 KiB

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