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.

267 lines
7.9 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
  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-07
  8. *
  9. * Unit tests for Form class
  10. */
  11. require_once dirname(__FILE__) . '/../../form/Form.php';
  12. require_once dirname(__FILE__) . '/../../form/FormField.php';
  13. require_once dirname(__FILE__) . '/../../validator/iValidator.php';
  14. require_once dirname(__FILE__) . '/../../validator/Validator.php';
  15. require_once dirname(__FILE__) . '/../../validator/RegexValidator.php';
  16. require_once dirname(__FILE__) . '/../../validator/NotEmptyValidator.php';
  17. require_once dirname(__FILE__) . '/../../validator/EmailValidator.php';
  18. require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
  19. class FormTest extends PHPUnit_Framework_TestCase
  20. {
  21. public function testAddFieldEmptyMessage()
  22. {
  23. $form_field = new FormField();
  24. $stub = $this->getMockForAbstractClass('Form');
  25. $method = new ReflectionMethod('Form', 'addField');
  26. $method->setAccessible(true);
  27. $return_object = $method->invokeArgs($stub, array('login'));
  28. $this->assertInstanceOf('FormField', $return_object);
  29. $this->assertEquals($form_field, $return_object);
  30. }
  31. public function testAddFieldWithMessage()
  32. {
  33. $message = 'Test message';
  34. $form_field = new FormField($message);
  35. $stub = $this->getMockForAbstractClass('Form');
  36. $method = new ReflectionMethod('Form', 'addField');
  37. $method->setAccessible(true);
  38. $return_object = $method->invokeArgs($stub, array('login', $message));
  39. $this->assertInstanceOf('FormField', $return_object);
  40. $this->assertEquals($form_field, $return_object);
  41. $this->assertAttributeEquals($message, 'default_message', $return_object);
  42. }
  43. public function testIsValidWithNotArray()
  44. {
  45. $form = new NotEmptyForm();
  46. // @TODO Fix exception type
  47. $this->setExpectedException('InitializationException', 'Form::Form::isValid expects an array');
  48. $form->isValid('');
  49. }
  50. public function testIsValidCorrectFields()
  51. {
  52. $form = new NotEmptyForm();
  53. $test_array = array(
  54. 'login' => 'login',
  55. 'password' => 'password'
  56. );
  57. $this->assertSame(1, $form->isValid($test_array));
  58. }
  59. public function testIsValidMissingField()
  60. {
  61. $form = new NotEmptyForm();
  62. $test_array = array(
  63. 'password' => 'password'
  64. );
  65. $this->assertSame(0, $form->isValid($test_array));
  66. $data['messages'] = $form->getMessages();
  67. $data['values'] = $form->getSourceValues();
  68. $this->assertSame($_SESSION['NotEmptyForm'], $data);
  69. }
  70. public function testGetValues()
  71. {
  72. $form = new NotEmptyForm();
  73. $test_array = array(
  74. 'login' => 'login',
  75. 'password' => 'password'
  76. );
  77. $this->assertSame(1, $form->isValid($test_array));
  78. $this->assertSame($test_array, $form->getValues());
  79. }
  80. public function testGetValuesWithFieldIsIgnored()
  81. {
  82. $form = new WithIgnoredFieldForm();
  83. $test_array = array(
  84. 'login' => 'login',
  85. 'password' => 'password',
  86. 'remember' => true
  87. );
  88. $this->assertSame(1, $form->isValid($test_array));
  89. $this->assertSame(array(
  90. 'login' => 'login',
  91. 'password' => 'password'), $form->getValues());
  92. $this->assertSame($test_array, $form->getSourceValues());
  93. }
  94. public function testGetValueCorrect()
  95. {
  96. $form = new NotEmptyForm();
  97. $test_array = array(
  98. 'login' => 'login',
  99. 'password' => 'password'
  100. );
  101. $this->assertSame(1, $form->isValid($test_array));
  102. $this->assertSame($test_array['login'], $form->getValue('login'));
  103. $this->assertSame($test_array['password'], $form->getValue('password'));
  104. }
  105. public function testGetValueEmpty()
  106. {
  107. $form = new NotEmptyForm();
  108. $test_array = array(
  109. 'login' => 'login'
  110. );
  111. $this->assertSame(0, $form->isValid($test_array));
  112. $this->assertSame('login', $form->getValue('login'));
  113. $this->assertNull($form->getValue('password'));
  114. }
  115. public function testGetValueMissing()
  116. {
  117. $form = new NotEmptyForm();
  118. $test_array = array(
  119. 'login' => 'login',
  120. 'password' => 'password'
  121. );
  122. $this->assertSame(1, $form->isValid($test_array));
  123. $this->assertSame('login', $form->getValue('login'));
  124. $this->assertSame('password', $form->getValue('password'));
  125. $this->assertFalse($form->getValue('missing'));
  126. }
  127. public function testGetMessageTypeCorrect()
  128. {
  129. $form = new NotEmptyForm();
  130. $test_array = array(
  131. 'login' => 'login',
  132. 'password' => 'password'
  133. );
  134. $this->assertSame(1, $form->isValid($test_array));
  135. $this->assertSame('success', Form::SUCCESS);
  136. $this->assertSame($form->getMessageType(), Form::SUCCESS);
  137. }
  138. public function testGetMessageTypeMissing()
  139. {
  140. $form = new NotEmptyForm();
  141. $test_array = array(
  142. 'login' => '',
  143. 'password' => ''
  144. );
  145. $this->assertSame(0, $form->isValid($test_array));
  146. $this->assertSame('error', Form::ERROR);
  147. $this->assertSame($form->getMessageType(), Form::ERROR);
  148. }
  149. public function testGetMessageCorrect()
  150. {
  151. $form = new NotEmptyForm();
  152. $message = 'Test message';
  153. $test_array = array(
  154. 'login' => 'login',
  155. 'password' => 'password'
  156. );
  157. $this->assertSame(1, $form->isValid($test_array));
  158. $form->setSuccessMessage($message);
  159. $this->assertSame($message, $form->getMessage());
  160. }
  161. public function testGetMessageMissing()
  162. {
  163. $form = new NotEmptyForm();
  164. $message = 'Test message';
  165. $test_array = array(
  166. 'login' => ''
  167. );
  168. $this->assertSame(0, $form->isValid($test_array));
  169. $form->setErrorMessage($message);
  170. $this->assertSame($message, $form->getMessage());
  171. }
  172. public function testSetSuccessMessage()
  173. {
  174. $form = new NotEmptyForm();
  175. $test_array = array(
  176. 'login' => 'login',
  177. 'password' => 'password'
  178. );
  179. $this->assertSame(1, $form->isValid($test_array));
  180. $message = 'Form data valid';
  181. $form->setSuccessMessage($message);
  182. $this->assertSame($message, $form->getMessage());
  183. }
  184. public function testSetErrorMessage()
  185. {
  186. $form = new NotEmptyForm();
  187. $test_array = array(
  188. 'login' => ''
  189. );
  190. $this->assertSame(0, $form->isValid($test_array));
  191. $message = 'Form data invalid';
  192. $form->setErrorMessage($message);
  193. $this->assertSame($message, $form->getMessage());
  194. }
  195. }
  196. class NotEmptyForm extends Form
  197. {
  198. public function init()
  199. {
  200. $validator = new NotEmptyValidator();
  201. $validator->setMessage('Enter login name.');
  202. $this->addField('login')->addValidator($validator);
  203. $validator = new NotEmptyValidator();
  204. $validator->setMessage('Enter your password.');
  205. $this->addField('password')->addValidator($validator);
  206. }
  207. }
  208. class WithIgnoredFieldForm extends Form
  209. {
  210. public function init()
  211. {
  212. $validator = new NotEmptyValidator();
  213. $validator->setMessage('Enter login name.');
  214. $this->addField('login')->addValidator($validator);
  215. $validator = new NotEmptyValidator();
  216. $validator->setMessage('Enter your password.');
  217. $this->addField('password')->addValidator($validator);
  218. $validator = new NotEmptyValidator();
  219. $validator->setMessage('Remember');
  220. $this->addField('remember')->addValidator($validator)->setIgnored(true);
  221. }
  222. }