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.

194 lines
5.8 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
  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__) . '/../../validator/iValidator.php';
  9. require_once dirname(__FILE__) . '/../../validator/Validator.php';
  10. require_once dirname(__FILE__) . '/../../validator/RegexValidator.php';
  11. require_once dirname(__FILE__) . '/../../validator/NotEmptyValidator.php';
  12. require_once dirname(__FILE__) . '/../../validator/EmailValidator.php';
  13. class FormTest extends PHPUnit_Framework_TestCase
  14. {
  15. public function testIsValidWithNotArray()
  16. {
  17. $form = new NotEmptyForm();
  18. $this->setExpectedException('Exception');
  19. $form->isValid('');
  20. }
  21. public function testIsValidCorrectFields()
  22. {
  23. $form = new NotEmptyForm();
  24. $test_array = array(
  25. 'login'=> 'login',
  26. 'password'=> 'password'
  27. );
  28. $this->assertSame(1, $form->isValid($test_array));
  29. }
  30. public function testIsValidMissingField()
  31. {
  32. $form = new NotEmptyForm();
  33. $test_array = array(
  34. 'password'=> 'password'
  35. );
  36. $this->assertSame(0, $form->isValid($test_array));
  37. }
  38. public function testGetValues()
  39. {
  40. $form = new NotEmptyForm();
  41. $fields = array(
  42. 'login'=> 'login',
  43. 'password'=>'password'
  44. );
  45. $this->assertSame(1, $form->isValid($fields));
  46. $this->assertEquals($fields, $form->getValues());
  47. }
  48. public function testGetValueCorrect()
  49. {
  50. $form = new NotEmptyForm();
  51. $fields = array(
  52. 'login'=> 'login',
  53. 'password'=>'password'
  54. );
  55. $this->assertSame(1, $form->isValid($fields));
  56. $this->assertEquals($fields['login'], $form->getValue('login'));
  57. $this->assertEquals($fields['password'], $form->getValue('password'));
  58. }
  59. public function testGetValueMissing()
  60. {
  61. $form = new NotEmptyForm();
  62. $fields = array(
  63. 'login'=> ''
  64. );
  65. $this->assertSame(0, $form->isValid($fields));
  66. $this->assertEquals('', $form->getValue('login'));
  67. $this->assertEquals('', $form->getValue('password'));
  68. }
  69. public function testGetValueMissingFieldLogin()
  70. {
  71. $form = new NotEmptyForm();
  72. $fields = array(
  73. 'login'=> ''
  74. );
  75. $this->assertSame(0, $form->isValid($fields));
  76. $this->assertFalse($form->getValue('loginw'));
  77. $this->assertEquals('', $form->getValue('passwordw'));
  78. }
  79. public function testGetValueMissingFieldPassword()
  80. {
  81. $form = new NotEmptyForm();
  82. $fields = array(
  83. 'login'=> ''
  84. );
  85. $this->assertSame(0, $form->isValid($fields));
  86. $this->assertFalse($form->getValue('loginw'));
  87. $this->assertEquals('', $form->getValue('password'));
  88. }
  89. public function testGetMessageTypeCorrect()
  90. {
  91. $form = new NotEmptyForm();
  92. $fields = array(
  93. 'login'=> 'login',
  94. 'password'=>'password'
  95. );
  96. $this->assertEquals(1, $form->isValid($fields));
  97. $this->assertEquals('success', Form::SUCCESS);
  98. $this->assertEquals($form->getMessageType(), Form::SUCCESS);
  99. }
  100. public function testGetMessageTypeMissing()
  101. {
  102. $form = new NotEmptyForm();
  103. $fields = array(
  104. 'login'=> '',
  105. 'password'=>''
  106. );
  107. $this->assertSame(0, $form->isValid($fields));
  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. $form->setSuccessMessage('message');
  120. $this->assertEquals('message', $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. $form->setErrorMessage('message');
  130. $this->assertEquals('message', $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. class NotEmptyForm extends Form
  157. {
  158. public function init()
  159. {
  160. ///*
  161. $validator = new NotEmptyValidator();
  162. $validator->setMessage('Enter login name.');
  163. $this->addField('login')->addValidator($validator);
  164. // User password
  165. $validator = new NotEmptyValidator();
  166. $validator->setMessage('Enter your password.');
  167. $this->addField('password')->addValidator($validator);
  168. }
  169. }