PageRenderTime 28ms CodeModel.GetById 62ms RepoModel.GetById 12ms app.codeStats 1ms

/tests/ValidatorTest.php

https://github.com/solo-framework/solo-framework
PHP | 380 lines | 264 code | 77 blank | 39 comment | 0 complexity | 48a561997ac496f08bf6c6f31f314cf9 MD5 | raw file
  1. <?php
  2. require_once "../Solo/Lib/Validator/IValidatorRule.php";
  3. require_once "../Solo/Lib/Validator/BaseValidatorRule.php";
  4. require_once "../Solo/Lib/Validator/Validator.php";
  5. require_once "../Solo/Lib/Validator/DateTimeValidator.php";
  6. require_once "../Solo/Lib/Validator/IdValidatorRule.php";
  7. use Solo\Lib\Validator\IdValidatorRule;
  8. use Solo\Lib\Validator\Validator;
  9. use Solo\Lib\Validator\DateTimeValidator;
  10. class ValidatorTest extends PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * Экземпляр валидатора
  14. *
  15. * @var Validator
  16. */
  17. private $val = null;
  18. /**
  19. * Prepares the environment before running a test.
  20. */
  21. protected function setUp()
  22. {
  23. parent::setUp();
  24. $this->val = new Validator();
  25. }
  26. /**
  27. * Cleans up the environment after running a test.
  28. */
  29. protected function tearDown()
  30. {
  31. parent::tearDown();
  32. // Сбросим валидатор, чтобы не было влияния одних тестов на другие
  33. $this->val->reset();
  34. }
  35. public function test_required()
  36. {
  37. $this->val->check("value")->required(false, "not required");
  38. $this->val->check(null)->required(false, "not required");
  39. $this->val->check(null)->required(true, "required");
  40. $this->assertFalse($this->val->isValid());
  41. }
  42. public function test_inArray()
  43. {
  44. $arr = array(0, 1, 2);
  45. $this->val->check("0", "not found")->inArray($arr);
  46. $this->assertTrue($this->val->isValid());
  47. $this->val->reset();
  48. $this->val->check(2, "not found")->inArray($arr);
  49. $this->assertTrue($this->val->isValid());
  50. $this->val->reset();
  51. $this->val->check("string", "not found")->inArray($arr, "in");
  52. $this->assertFalse($this->val->isValid());
  53. $this->val->reset();
  54. $this->val->check("string", "not found")->inArray(array(), "in");
  55. $this->assertFalse($this->val->isValid());
  56. $this->val->reset();
  57. }
  58. /**
  59. * Проверяет значение на принадлежность к
  60. * числовым типам int, float
  61. *
  62. * @return
  63. */
  64. public function test_isNumeric()
  65. {
  66. $this->val->check("1")->isNumeric("Не число");
  67. $this->assertTrue($this->val->isValid());
  68. $this->val->reset();
  69. $this->val->check("1.2")->isNumeric("Не число");
  70. $this->assertTrue($this->val->isValid());
  71. $this->val->reset();
  72. $this->val->check("12e2")->isNumeric("Не число");
  73. $this->assertTrue($this->val->isValid());
  74. $this->val->reset();
  75. $this->val->check(".2")->isNumeric("Не число");
  76. $this->assertTrue($this->val->isValid());
  77. $this->val->reset();
  78. $this->val->check("123174235892350983498324698723489713478123987514987532489714239875124785324987")->isNumeric("Не число");
  79. $this->assertTrue($this->val->isValid());
  80. $this->val->reset();
  81. $this->val->check(1)->isNumeric("Не число");
  82. $this->assertTrue($this->val->isValid());
  83. $this->val->reset();
  84. $this->val->check(2.32)->isNumeric("Не число");
  85. $this->assertTrue($this->val->isValid());
  86. $this->val->reset();
  87. $this->val->check(12e4)->isNumeric("Не число");
  88. $this->assertTrue($this->val->isValid());
  89. $this->val->reset();
  90. // not valid
  91. $this->val->check("1,2")->isNumeric("Не число");
  92. $this->assertFalse($this->val->isValid());
  93. $this->val->reset();
  94. $this->val->check("12w4")->isNumeric("Не число");
  95. $this->assertFalse($this->val->isValid());
  96. $this->val->reset();
  97. }
  98. public function test_isArray()
  99. {
  100. $this->val->check(array(1))->isArray("Не список");
  101. $this->assertTrue($this->val->isValid());
  102. $this->val->reset();
  103. $this->val->check(array(1, 2, "item"))->isArray("Не список");
  104. $this->assertTrue($this->val->isValid());
  105. $this->val->reset();
  106. // not valid
  107. $this->val->check("value")->isArray("Не список");
  108. $this->assertFalse($this->val->isValid());
  109. $this->val->reset();
  110. }
  111. public function test_matchRegex()
  112. {
  113. $this->val->check(123)->matchRegex('/[\d]+/', "regex ok");
  114. $this->assertTrue($this->val->isValid());
  115. $this->val->check("32123")->matchRegex('/[\d]+/', "regex ok");
  116. $this->assertTrue($this->val->isValid());
  117. }
  118. public function test_matchRegex_fail()
  119. {
  120. $this->val->check(123)->matchRegex('/[a-z]+/', "regex ok");
  121. $this->assertFalse($this->val->isValid());
  122. $this->val->reset();
  123. $this->val->check("32123")->matchRegex('/[a-z]+/', "regex ok");
  124. $this->assertFalse($this->val->isValid());
  125. }
  126. public function test_addMessage()
  127. {
  128. // добавление сообщения в валидатор делает его невалидным
  129. $this->val->addMessage("something wrong!");
  130. $this->assertFalse($this->val->isValid());
  131. }
  132. public function test_lessThen()
  133. {
  134. $this->val->check(20)->lessThen(21);
  135. $this->assertTrue($this->val->isValid());
  136. $this->val->reset();
  137. $this->val->check(20)->lessThen(19);
  138. $this->assertFalse($this->val->isValid());
  139. }
  140. public function test_greateThen()
  141. {
  142. $this->val->check(20)->greateThen(19);
  143. $this->assertTrue($this->val->isValid());
  144. $this->val->reset();
  145. $this->val->check(20)->greateThen(22);
  146. $this->assertFalse($this->val->isValid());
  147. }
  148. public function test_equalTo()
  149. {
  150. $this->val->check(20)->equalTo(20);
  151. $this->assertTrue($this->val->isValid());
  152. $this->val->reset();
  153. $this->val->check(20)->equalTo(22);
  154. $this->assertFalse($this->val->isValid());
  155. $this->val->reset();
  156. $this->val->check("value")->equalTo("other_value");
  157. $this->assertFalse($this->val->isValid());
  158. }
  159. public function test_minLength()
  160. {
  161. $this->val->check(20)->minLength(2);
  162. $this->assertTrue($this->val->isValid());
  163. $this->val->reset();
  164. $this->val->check(2)->minLength(2);
  165. $this->assertFalse($this->val->isValid());
  166. $this->val->reset();
  167. $this->val->check("value")->minLength(3);
  168. $this->assertTrue($this->val->isValid());
  169. $this->val->reset();
  170. $this->val->check("value")->minLength(30);
  171. $this->assertFalse($this->val->isValid());
  172. $this->val->reset();
  173. $this->val->check("строка")->minLength(6);
  174. $this->assertTrue($this->val->isValid());
  175. }
  176. public function test_maxLength()
  177. {
  178. $this->val->check(20)->maxLength(2);
  179. $this->assertTrue($this->val->isValid());
  180. $this->val->reset();
  181. $this->val->check(222)->maxLength(2);
  182. $this->assertFalse($this->val->isValid());
  183. $this->val->reset();
  184. $this->val->check("value")->maxLength(3);
  185. $this->assertFalse($this->val->isValid());
  186. $this->val->reset();
  187. $this->val->check("value")->maxLength(30);
  188. $this->assertTrue($this->val->isValid());
  189. $this->val->reset();
  190. $this->val->check("стро")->maxLength(6);
  191. $this->assertTrue($this->val->isValid());
  192. }
  193. public function test_range()
  194. {
  195. $this->val->check(10)->range(8, 20);
  196. $this->assertTrue($this->val->isValid());
  197. $this->val->reset();
  198. $this->val->check(10)->range(10, 20);
  199. $this->assertTrue($this->val->isValid());
  200. $this->val->reset();
  201. $this->val->check(30)->range(10, 20);
  202. $this->assertFalse($this->val->isValid());
  203. }
  204. public function test_rangeLenght()
  205. {
  206. $this->val->check(103)->rangeLenght(3, 10);
  207. $this->assertTrue($this->val->isValid());
  208. $this->val->reset();
  209. $this->val->check("value")->rangeLenght(4, 10);
  210. $this->assertTrue($this->val->isValid());
  211. $this->val->reset();
  212. $this->val->check(30)->rangeLenght(10, 20);
  213. $this->assertFalse($this->val->isValid());
  214. $this->val->reset();
  215. $this->val->check("hello")->rangeLenght(10, 20);
  216. $this->assertFalse($this->val->isValid());
  217. }
  218. /**
  219. *
  220. *
  221. * @return
  222. */
  223. public function test_DateTimeValidator()
  224. {
  225. $this->val->check("2005-08-09")->addValidator(
  226. new DateTimeValidator("comment", DateTimeValidator::FORMAT_ISO_8601)
  227. );
  228. $this->assertTrue($this->val->isValid());
  229. $this->val->reset();
  230. $this->val->check("2005-08-09 12:33:45")->addValidator(
  231. new DateTimeValidator("comment", DateTimeValidator::FORMAT_ISO_8601)
  232. );
  233. $this->assertTrue($this->val->isValid());
  234. $this->val->reset();
  235. }
  236. /**
  237. * Проверка получения значения по умолчанию
  238. *
  239. * @return
  240. */
  241. public function test_get_default_value()
  242. {
  243. $val = $this->val->check("val")->value();
  244. $this->assertEquals("val", $val);
  245. $this->val->reset();
  246. $val = $this->val->check(null)->value("default");
  247. $this->assertEquals("default", $val);
  248. $this->val->reset();
  249. $val = $this->val->check("some")->value("default");
  250. $this->assertEquals("some", $val);
  251. $this->val->reset();
  252. }
  253. /**
  254. * Точное совпадение длины значения
  255. *
  256. * @return void
  257. */
  258. public function test_matchLenght()
  259. {
  260. $this->val->check("тестовая строка")->matchLenght(15);
  261. $this->assertTrue($this->val->isValid());
  262. $this->val->reset();
  263. $this->val->check(222)->matchLenght(3);
  264. $this->assertTrue($this->val->isValid());
  265. $this->val->reset();
  266. $this->val->check("тест")->matchLenght(3);
  267. $this->assertFalse($this->val->isValid());
  268. $this->val->reset();
  269. }
  270. public function test_id_validator()
  271. {
  272. $this->val->check("ddd")
  273. ->addValidator(new IdValidatorRule());
  274. // только числовые значения
  275. $this->assertFalse($this->val->isValid());
  276. $this->val->reset();
  277. // длина ID от 1 - 11 цифр
  278. $this->val->check(1111111111111111111)
  279. ->addValidator(new IdValidatorRule());
  280. $this->assertFalse($this->val->isValid());
  281. $this->val->reset();
  282. // идентификатор может и не быть указан
  283. $this->val->check(null)
  284. ->addValidator(new IdValidatorRule());
  285. $this->assertTrue($this->val->isValid());
  286. $this->val->reset();
  287. }
  288. public function test_contains()
  289. {
  290. $this->val->check("some_string")->contains("_", "Doesn't contain a string!");
  291. $this->assertTrue($this->val->isValid());
  292. $this->val->reset();
  293. $this->val->check("somestring")->contains("_", "Doesn't contain a string!");
  294. $this->assertFalse($this->val->isValid());
  295. $this->val->reset();
  296. }
  297. public function test_post()
  298. {
  299. $_POST["subId"] = 1;
  300. $_POST["boxId"] = "boxId";
  301. }
  302. }