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.

281 lines
7.4 KiB

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. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. require_once dirname(__FILE__) . '/../../form/Form.php';
  7. require_once dirname(__FILE__) . '/../../form/FormField.php';
  8. require_once dirname(__FILE__) . '/../../session/Session.php';
  9. class FormTest extends PHPUnit_Framework_TestCase
  10. {
  11. public function testIsValidWithNotArray()
  12. {
  13. $form = new NotEmptyForm();
  14. $this->setExpectedException('Exception');
  15. $form->isValid('');
  16. }
  17. public function testIsValidCorrectFields()
  18. {
  19. $form = new NotEmptyForm();
  20. $test_array = array(
  21. 'login'=> 'login',
  22. 'password'=> 'password'
  23. );
  24. $this->assertSame(1, $form->isValid($test_array));
  25. }
  26. public function testIsValidMissingField()
  27. {
  28. $form = new NotEmptyForm();
  29. $test_array = array(
  30. 'password'=> 'password'
  31. );
  32. $this->assertSame(0, $form->isValid($test_array));
  33. }
  34. public function testGetValues()
  35. {
  36. $form = new NotEmptyForm();
  37. $fields = array(
  38. 'login'=> 'login',
  39. 'password'=>'password'
  40. );
  41. $obj_fields = $form->getProp('fields');
  42. $this->assertTrue(!$obj_fields['login']->isIgnored());
  43. $this->assertTrue(!$obj_fields['password']->isIgnored());
  44. $this->assertSame(1, $form->isValid($fields));
  45. $this->assertEquals($fields, $form->getValues());
  46. }
  47. public function testGetValueCorrect()
  48. {
  49. $form = new NotEmptyForm();
  50. $fields = array(
  51. 'login'=> 'login',
  52. 'password'=>'password'
  53. );
  54. $this->assertSame(1, $form->isValid($fields));
  55. $this->assertEquals($fields['login'], $form->getValue('login'));
  56. $this->assertEquals($fields['password'], $form->getValue('password'));
  57. }
  58. public function testGetValueMissing()
  59. {
  60. $form = new NotEmptyForm();
  61. $fields = array(
  62. 'login'=> ''
  63. );
  64. $this->assertSame(0, $form->isValid($fields));
  65. $this->assertEquals('', $form->getValue('login'));
  66. $this->assertEquals('', $form->getValue('password'));
  67. }
  68. public function testGetValueMissingFieldLogin()
  69. {
  70. $form = new NotEmptyForm();
  71. $fields = array(
  72. 'login'=> ''
  73. );
  74. $this->assertSame(0, $form->isValid($fields));
  75. $this->assertFalse($form->getValue('loginw'));
  76. $this->assertEquals('', $form->getValue('passwordw'));
  77. }
  78. public function testGetValueMissingFieldPassword()
  79. {
  80. $form = new NotEmptyForm();
  81. $fields = array(
  82. 'login'=> ''
  83. );
  84. $this->assertSame(0, $form->isValid($fields));
  85. $this->assertFalse($form->getValue('loginw'));
  86. $this->assertEquals('', $form->getValue('password'));
  87. }
  88. public function testGetMessageTypeCorrect()
  89. {
  90. $form = new NotEmptyForm();
  91. $fields = array(
  92. 'login'=> 'login',
  93. 'password'=>'password'
  94. );
  95. $this->assertEquals(1, $form->isValid($fields));
  96. $this->assertEquals('success', Form::SUCCESS);
  97. $this->assertEquals($form->getMessageType(), Form::SUCCESS);
  98. }
  99. public function testGetMessageTypeMissing()
  100. {
  101. $form = new NotEmptyForm();
  102. $fields = array(
  103. 'login'=> '',
  104. 'password'=>''
  105. );
  106. $this->assertSame(0, $form->isValid($fields));
  107. $this->assertSame(0, $form->getProp('valid'));
  108. $this->assertEquals('error', Form::ERROR);
  109. $this->assertEquals($form->getMessageType(), Form::ERROR);
  110. }
  111. public function testGetMessageCorrect()
  112. {
  113. $form = new NotEmptyForm();
  114. $fields = array(
  115. 'login'=> 'login',
  116. 'password'=>'password'
  117. );
  118. $this->assertSame(1, $form->isValid($fields));
  119. $messages = $form->getProp('messages');
  120. $this->assertEquals($messages[Form::SUCCESS], $form->getMessage());
  121. }
  122. public function testGetMessageMissing()
  123. {
  124. $form = new NotEmptyForm();
  125. $fields = array(
  126. 'login'=> ''
  127. );
  128. $this->assertSame(0, $form->isValid($fields));
  129. $messages = $form->getProp('messages');//attributeEq ?
  130. $this->assertEquals($messages[Form::ERROR], $form->getMessage());
  131. }
  132. public function testSetSuccessMessage()
  133. {
  134. $form = new NotEmptyForm();
  135. $fields = array(
  136. 'login'=> 'login',
  137. 'password'=>'password'
  138. );
  139. $this->assertSame(1, $form->isValid($fields));
  140. $message = 'Form data valid';
  141. $form->setSuccessMessage($message);
  142. $this->assertNotNull($form->getMessage());
  143. }
  144. public function testSetErrorMessage()
  145. {
  146. $form = new NotEmptyForm();
  147. $fields = array(
  148. 'login'=> ''
  149. );
  150. $this->assertSame(0, $form->isValid($fields));
  151. $message = 'Form data invalid';
  152. $form->setErrorMessage($message);
  153. $this->assertNotNull($form->getMessage());
  154. }
  155. }
  156. require_once dirname(__FILE__) . '/../../validator/iValidator.php';
  157. require_once dirname(__FILE__) . '/../../validator/Validator.php';
  158. require_once dirname(__FILE__) . '/../../validator/NotEmptyValidator.php';
  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. require_once dirname(__FILE__) . '/../../validator/RegexValidator.php';
  178. require_once dirname(__FILE__) . '/../../validator/EmailValidator.php';
  179. class DiffForm extends Form
  180. {
  181. public function init()
  182. {
  183. ///*
  184. $validator = new EmailValidator();
  185. $validator->setMessage('Enter login name.');
  186. $this->addField('login')->addValidator($validator);
  187. // User password
  188. $validator = new NotEmptyValidator();
  189. $validator->setMessage('Enter your password.');
  190. $this->addField('password')->addValidator($validator);
  191. }
  192. public function getProp($name)
  193. {
  194. return $this->$name;
  195. }
  196. }