/lib/vendor/symfony-1.4.14/test/unit/validator/sfValidatorSchemaTest.php

https://github.com/yuya-takeyama/symfony-hackathon-20110924 · PHP · 398 lines · 341 code · 43 blank · 14 comment · 10 complexity · 7bd93dc5003016c4eda079ad41724c80 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
  10. $t = new lime_test(84);
  11. class PreValidator extends sfValidatorBase
  12. {
  13. protected function doClean($values)
  14. {
  15. if (isset($values['s1']) && isset($values['s2']))
  16. {
  17. throw new sfValidatorError($this, 's1_or_s2', array('value' => $values));
  18. }
  19. }
  20. }
  21. class PostValidator extends sfValidatorBase
  22. {
  23. protected function doClean($values)
  24. {
  25. foreach ($values as $key => $value)
  26. {
  27. $values[$key] = "*$value*";
  28. }
  29. return $values;
  30. }
  31. }
  32. class Post1Validator extends sfValidatorBase
  33. {
  34. protected function doClean($values)
  35. {
  36. if ($values['s1'] == $values['s2'])
  37. throw new sfValidatorError($this, 's1_not_equal_s2', array('value' => $values));
  38. }
  39. }
  40. $v1 = new sfValidatorString(array('max_length' => 3));
  41. $v2 = new sfValidatorString(array('min_length' => 3));
  42. // __construct()
  43. $t->diag('__construct()');
  44. $v = new sfValidatorSchema();
  45. $t->is($v->getFields(), array(), '->__construct() can take no argument');
  46. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2));
  47. $t->is($v->getFields(), array('s1' => $v1, 's2' => $v2), '->__construct() can take an array of named sfValidator objects');
  48. try
  49. {
  50. $v = new sfValidatorSchema('string');
  51. $t->fail('__construct() throws an InvalidArgumentException when passing a non supported first argument');
  52. }
  53. catch (InvalidArgumentException $e)
  54. {
  55. $t->pass('__construct() throws an InvalidArgumentException when passing a non supported first argument');
  56. }
  57. // implements ArrayAccess
  58. $t->diag('implements ArrayAccess');
  59. $v = new sfValidatorSchema();
  60. $v['s1'] = $v1;
  61. $v['s2'] = $v2;
  62. $t->is($v->getFields(), array('s1' => $v1, 's2' => $v2), 'sfValidatorSchema implements the ArrayAccess interface for the fields');
  63. try
  64. {
  65. $v['v1'] = 'string';
  66. $t->fail('sfValidatorSchema implements the ArrayAccess interface for the fields');
  67. }
  68. catch (InvalidArgumentException $e)
  69. {
  70. $t->pass('sfValidatorSchema implements the ArrayAccess interface for the fields');
  71. }
  72. $v = new sfValidatorSchema(array('s1' => $v1));
  73. $t->is(isset($v['s1']), true, 'sfValidatorSchema implements the ArrayAccess interface for the fields');
  74. $t->is(isset($v['s2']), false, 'sfValidatorSchema implements the ArrayAccess interface for the fields');
  75. $v = new sfValidatorSchema(array('s1' => $v1));
  76. $t->ok($v['s1'] == $v1, 'sfValidatorSchema implements the ArrayAccess interface for the fields');
  77. $t->is($v['s2'], null, 'sfValidatorSchema implements the ArrayAccess interface for the fields');
  78. $v = new sfValidatorSchema(array('v1' => $v1));
  79. unset($v['s1']);
  80. $t->is($v['s1'], null, 'sfValidatorSchema implements the ArrayAccess interface for the fields');
  81. // ->configure()
  82. $t->diag('->configure()');
  83. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2));
  84. $t->is($v->getOption('allow_extra_fields'), false, '->configure() sets "allow_extra_fields" option to false by default');
  85. $t->is($v->getOption('filter_extra_fields'), true, '->configure() sets "filter_extra_fields" option to true by default');
  86. $t->is($v->getMessage('extra_fields'), 'Unexpected extra form field named "%field%".', '->configure() has a default error message for the "extra_fields" error');
  87. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2), array('allow_extra_fields' => true, 'filter_extra_fields' => false), array('extra_fields' => 'Extra fields'));
  88. $t->is($v->getOption('allow_extra_fields'), true, '->__construct() can override the default value for the "allow_extra_fields" option');
  89. $t->is($v->getOption('filter_extra_fields'), false, '->__construct() can override the default value for the "filter_extra_fields" option');
  90. $t->is($v->getMessage('extra_fields'), 'Extra fields', '->__construct() can override the default message for the "extra_fields" error message');
  91. // ->clean()
  92. $t->diag('->clean()');
  93. $v = new sfValidatorSchema();
  94. $t->is($v->clean(null), array(), '->clean() converts null to empty array before validation');
  95. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2));
  96. try
  97. {
  98. $v->clean('foo');
  99. $t->fail('->clean() throws an InvalidArgumentException exception if the first argument is not an array of value');
  100. }
  101. catch (InvalidArgumentException $e)
  102. {
  103. $t->pass('->clean() throws an InvalidArgumentException exception if the first argument is not an array of value');
  104. }
  105. $t->is($v->clean(array('s1' => 'foo', 's2' => 'bar')), array('s1' => 'foo', 's2' => 'bar'), '->clean() returns the string unmodified');
  106. try
  107. {
  108. $v->clean(array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'));
  109. $t->fail('->clean() throws an sfValidatorErrorSchema exception if a you give a non existant field');
  110. $t->skip('', 2);
  111. }
  112. catch (sfValidatorErrorSchema $e)
  113. {
  114. $t->pass('->clean() throws an sfValidatorErrorSchema exception if a you give a non existant field');
  115. $t->is(count($e), 1, '->clean() throws an exception with all error messages');
  116. $t->is($e[0]->getCode(), 'extra_fields', '->clean() throws an exception with all error messages');
  117. }
  118. $t->diag('required fields');
  119. try
  120. {
  121. $v->clean(array('s1' => 'foo'));
  122. $t->fail('->clean() throws an sfValidatorErrorSchema exception if a required field is not provided');
  123. $t->skip('', 2);
  124. }
  125. catch (sfValidatorErrorSchema $e)
  126. {
  127. $t->pass('->clean() throws an sfValidatorErrorSchema exception if a required field is not provided');
  128. $t->is(count($e), 1, '->clean() throws an exception with all error messages');
  129. $t->is($e['s2']->getCode(), 'required', '->clean() throws an exception with all error messages');
  130. }
  131. // ->getPreValidator() ->setPreValidator()
  132. $t->diag('->getPreValidator() ->setPreValidator()');
  133. $v1 = new sfValidatorString(array('max_length' => 3, 'required' => false));
  134. $v2 = new sfValidatorString(array('min_length' => 3, 'required' => false));
  135. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2));
  136. $v->setPreValidator($preValidator = new PreValidator());
  137. $t->ok($v->getPreValidator() == $preValidator, '->getPreValidator() returns the current pre validator');
  138. try
  139. {
  140. $v->clean(array('s1' => 'foo', 's2' => 'bar'));
  141. $t->fail('->clean() throws an sfValidatorErrorSchema exception if a pre-validator fails');
  142. $t->skip('', 2);
  143. }
  144. catch (sfValidatorErrorSchema $e)
  145. {
  146. $t->pass('->clean() throws an sfValidatorErrorSchema exception if a pre-validator fails');
  147. $t->is(count($e), 1, '->clean() throws an exception with all error messages');
  148. $t->is($e[0]->getCode(), 's1_or_s2', '->clean() throws an exception with all error messages');
  149. }
  150. // ->getPostValidator() ->setPostValidator()
  151. $t->diag('->getPostValidator() ->setPostValidator()');
  152. $v1 = new sfValidatorString(array('max_length' => 3, 'required' => false));
  153. $v2 = new sfValidatorString(array('min_length' => 3, 'required' => false));
  154. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2));
  155. $v->setPostValidator($postValidator = new PostValidator());
  156. $t->ok($v->getPostValidator() == $postValidator, '->getPostValidator() returns the current post validator');
  157. $t->is($v->clean(array('s1' => 'foo', 's2' => 'bar')), array('s1' => '*foo*', 's2' => '*bar*'), '->clean() executes post validators');
  158. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2));
  159. $v->setPostValidator(new Post1Validator());
  160. try
  161. {
  162. $v->clean(array('s1' => 'foo', 's2' => 'foo'));
  163. $t->fail('->clean() throws an sfValidatorErrorSchema exception if a post-validator fails');
  164. $t->skip('', 2);
  165. }
  166. catch (sfValidatorErrorSchema $e)
  167. {
  168. $t->pass('->clean() throws an sfValidatorErrorSchema exception if a post-validator fails');
  169. $t->is(count($e), 1, '->clean() throws an exception with all error messages');
  170. $t->is($e[0]->getCode(), 's1_not_equal_s2', '->clean() throws an exception with all error messages');
  171. }
  172. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2));
  173. $t->is($v->clean(array('s1' => 'foo')), array('s1' => 'foo', 's2' => null), '->clean() returns null values for fields not present in the input array');
  174. $t->diag('extra fields');
  175. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2));
  176. $v->setOption('allow_extra_fields', true);
  177. $ret = $v->clean(array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'));
  178. $t->is($ret, array('s1' => 'foo', 's2' => 'bar'), '->clean() filters non existant fields if "allow_extra_fields" is true');
  179. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2), array('allow_extra_fields' => true));
  180. $ret = $v->clean(array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'));
  181. $t->is($ret, array('s1' => 'foo', 's2' => 'bar'), '->clean() filters non existant fields if "allow_extra_fields" is true');
  182. $v = new sfValidatorSchema(array('s1' => $v1, 's2' => $v2), array('allow_extra_fields' => true, 'filter_extra_fields' => false));
  183. $ret = $v->clean(array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'));
  184. $t->is($ret, array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'), '->clean() do not filter non existant fields if "filter_extra_fields" is false');
  185. $v->setOption('filter_extra_fields', false);
  186. $ret = $v->clean(array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'));
  187. $t->is($ret, array('s1' => 'foo', 's2' => 'bar', 'foo' => 'bar'), '->clean() do not filter non existant fields if "filter_extra_fields" is false');
  188. $t->diag('one validator fails');
  189. $v['s2']->setOption('max_length', 2);
  190. try
  191. {
  192. $v->clean(array('s1' => 'foo', 's2' => 'bar'));
  193. $t->fail('->clean() throws an sfValidatorErrorSchema exception if one of the validators fails');
  194. $t->skip('', 2);
  195. }
  196. catch (sfValidatorErrorSchema $e)
  197. {
  198. $t->pass('->clean() throws an sfValidatorErrorSchema exception if one of the validators fails');
  199. $t->is(count($e), 1, '->clean() throws an exception with all error messages');
  200. $t->is($e['s2']->getCode(), 'max_length', '->clean() throws an exception with all error messages');
  201. }
  202. $t->diag('several validators fail');
  203. $v['s1']->setOption('max_length', 2);
  204. $v['s2']->setOption('max_length', 2);
  205. try
  206. {
  207. $v->clean(array('s1' => 'foo', 's2' => 'bar'));
  208. $t->fail('->clean() throws an sfValidatorErrorSchema exception if one of the validators fails');
  209. $t->skip('', 3);
  210. }
  211. catch (sfValidatorErrorSchema $e)
  212. {
  213. $t->pass('->clean() throws an sfValidatorErrorSchema exception if one of the validators fails');
  214. $t->is(count($e), 2, '->clean() throws an exception with all error messages');
  215. $t->is($e['s2']->getCode(), 'max_length', '->clean() throws an exception with all error messages');
  216. $t->is($e['s1']->getCode(), 'max_length', '->clean() throws an exception with all error messages');
  217. }
  218. $t->diag('postValidator can throw named errors or global errors');
  219. $comparator = new sfValidatorSchemaCompare('left', sfValidatorSchemaCompare::EQUAL, 'right');
  220. $userValidator = new sfValidatorSchema(array(
  221. 'test' => new sfValidatorString(array('min_length' => 10)),
  222. 'left' => new sfValidatorString(array('min_length' => 2)),
  223. 'right' => new sfValidatorString(array('min_length' => 2)),
  224. ));
  225. $userValidator->setPostValidator($comparator);
  226. $v = new sfValidatorSchema(array(
  227. 'test' => new sfValidatorString(array('min_length' => 10)),
  228. 'left' => new sfValidatorString(array('min_length' => 2)),
  229. 'right' => new sfValidatorString(array('min_length' => 2)),
  230. 'embedded' => $userValidator,
  231. ));
  232. $v->setPostValidator($comparator);
  233. $t->diag('postValidator throws global errors');
  234. foreach (array($userValidator->getPostValidator(), $v->getPostValidator(), $v['embedded']->getPostValidator()) as $validator)
  235. {
  236. $validator->setOption('throw_global_error', true);
  237. }
  238. try
  239. {
  240. $v->clean(array('test' => 'fabien', 'right' => 'bar', 'embedded' => array('test' => 'fabien', 'left' => 'oof', 'right' => 'rab')));
  241. $t->skip('', 7);
  242. }
  243. catch (sfValidatorErrorSchema $e)
  244. {
  245. $t->is(count($e->getNamedErrors()), 3, '->clean() throws an exception with all error messages');
  246. $t->is(count($e->getGlobalErrors()), 1, '->clean() throws an exception with all error messages');
  247. $t->is(count($e['embedded']->getNamedErrors()), 1, '->clean() throws an exception with all error messages');
  248. $t->is(count($e['embedded']->getGlobalErrors()), 1, '->clean() throws an exception with all error messages');
  249. $t->is(isset($e['left']) ? $e['left']->getCode() : '', 'required', '->clean() throws an exception with all error messages');
  250. $t->is(isset($e['embedded']['left']) ? $e['embedded']['left']->getCode() : '', '', '->clean() throws an exception with all error messages');
  251. $t->is($e->getCode(), 'invalid test [min_length] embedded [invalid test [min_length]] left [required]', '->clean() throws an exception with all error messages');
  252. }
  253. $t->diag('postValidator throws named errors');
  254. foreach (array($userValidator->getPostValidator(), $v->getPostValidator(), $v['embedded']->getPostValidator()) as $validator)
  255. {
  256. $validator->setOption('throw_global_error', false);
  257. }
  258. try
  259. {
  260. $v->clean(array('test' => 'fabien', 'right' => 'bar', 'embedded' => array('test' => 'fabien', 'left' => 'oof', 'right' => 'rab')));
  261. $t->skip('', 7);
  262. }
  263. catch (sfValidatorErrorSchema $e)
  264. {
  265. $t->is(count($e->getNamedErrors()), 3, '->clean() throws an exception with all error messages');
  266. $t->is(count($e->getGlobalErrors()), 0, '->clean() throws an exception with all error messages');
  267. $t->is(count($e['embedded']->getNamedErrors()), 2, '->clean() throws an exception with all error messages');
  268. $t->is(count($e['embedded']->getGlobalErrors()), 0, '->clean() throws an exception with all error messages');
  269. $t->is(isset($e['left']) ? $e['left']->getCode() : '', 'required invalid', '->clean() throws an exception with all error messages');
  270. $t->is(isset($e['embedded']['left']) ? $e['embedded']['left']->getCode() : '', 'invalid', '->clean() throws an exception with all error messages');
  271. $t->is($e->getCode(), 'test [min_length] embedded [test [min_length] left [invalid]] left [required invalid]', '->clean() throws an exception with all error messages');
  272. }
  273. $t->diag('complex postValidator');
  274. $comparator1 = new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password_bis');
  275. $v = new sfValidatorSchema(array(
  276. 'left' => new sfValidatorString(array('min_length' => 2)),
  277. 'right' => new sfValidatorString(array('min_length' => 2)),
  278. 'password' => new sfValidatorString(array('min_length' => 2)),
  279. 'password_bis' => new sfValidatorString(array('min_length' => 2)),
  280. ));
  281. $v->setPostValidator(new sfValidatorAnd(array($comparator, $comparator1)));
  282. try
  283. {
  284. $v->clean(array('left' => 'foo', 'right' => 'bar', 'password' => 'oof', 'password_bis' => 'rab'));
  285. $t->skip('', 3);
  286. }
  287. catch (sfValidatorErrorSchema $e)
  288. {
  289. $t->is(count($e->getNamedErrors()), 2, '->clean() throws an exception with all error messages');
  290. $t->is(count($e->getGlobalErrors()), 0, '->clean() throws an exception with all error messages');
  291. $t->is($e->getCode(), 'left [invalid] password [invalid]', '->clean() throws an exception with all error messages');
  292. }
  293. $comparator->setOption('throw_global_error', true);
  294. try
  295. {
  296. $v->clean(array('left' => 'foo', 'right' => 'bar', 'password' => 'oof', 'password_bis' => 'rab'));
  297. $t->skip('', 3);
  298. }
  299. catch (sfValidatorErrorSchema $e)
  300. {
  301. $t->is(count($e->getNamedErrors()), 1, '->clean() throws an exception with all error messages');
  302. $t->is(count($e->getGlobalErrors()), 1, '->clean() throws an exception with all error messages');
  303. $t->is($e->getCode(), 'invalid password [invalid]', '->clean() throws an exception with all error messages');
  304. }
  305. $userValidator = new sfValidatorSchema(array(
  306. 'left' => new sfValidatorString(array('min_length' => 2)),
  307. 'right' => new sfValidatorString(array('min_length' => 2)),
  308. 'password' => new sfValidatorString(array('min_length' => 2)),
  309. 'password_bis' => new sfValidatorString(array('min_length' => 2)),
  310. ));
  311. $userValidator->setPostValidator(new sfValidatorAnd(array($comparator, $comparator1)));
  312. $v = new sfValidatorSchema(array(
  313. 'left' => new sfValidatorString(array('min_length' => 2)),
  314. 'right' => new sfValidatorString(array('min_length' => 2)),
  315. 'password' => new sfValidatorString(array('min_length' => 2)),
  316. 'password_bis' => new sfValidatorString(array('min_length' => 2)),
  317. 'user' => $userValidator,
  318. ));
  319. $v->setPostValidator(new sfValidatorAnd(array($comparator, $comparator1)));
  320. try
  321. {
  322. $v->clean(array('left' => 'foo', 'right' => 'bar', 'password' => 'oof', 'password_bis' => 'rab', 'user' => array('left' => 'foo', 'right' => 'bar', 'password' => 'oof', 'password_bis' => 'rab')));
  323. $t->skip('', 7);
  324. }
  325. catch (sfValidatorErrorSchema $e)
  326. {
  327. $t->is(count($e->getNamedErrors()), 2, '->clean() throws an exception with all error messages');
  328. $t->is(count($e->getGlobalErrors()), 1, '->clean() throws an exception with all error messages');
  329. $t->is(count($e['user']->getNamedErrors()), 1, '->clean() throws an exception with all error messages');
  330. $t->is(count($e['user']->getGlobalErrors()), 1, '->clean() throws an exception with all error messages');
  331. $t->is(isset($e['user']) ? $e['user']->getCode() : '', 'invalid password [invalid]', '->clean() throws an exception with all error messages');
  332. $t->is(isset($e['user']['password']) ? $e['user']['password']->getCode() : '', 'invalid', '->clean() throws an exception with all error messages');
  333. $t->is($e->getCode(), 'invalid user [invalid password [invalid]] password [invalid]', '->clean() throws an exception with all error messages');
  334. }
  335. // __clone()
  336. $t->diag('__clone()');
  337. $v = new sfValidatorSchema(array('v1' => $v1, 'v2' => $v2));
  338. $v1 = clone $v;
  339. $f1 = $v1->getFields();
  340. $f = $v->getFields();
  341. $t->is(array_keys($f1), array_keys($f), '__clone() clones embedded validators');
  342. foreach ($f1 as $name => $validator)
  343. {
  344. $t->ok($validator !== $f[$name], '__clone() clones embedded validators');
  345. $t->ok($validator == $f[$name], '__clone() clones embedded validators');
  346. }
  347. $t->is($v1->getPreValidator(), null, '__clone() clones the pre validator');
  348. $t->is($v1->getPostValidator(), null, '__clone() clones the post validator');
  349. $v->setPreValidator(new sfValidatorString(array('min_length' => 4)));
  350. $v->setPostValidator(new sfValidatorString(array('min_length' => 4)));
  351. $v1 = clone $v;
  352. $t->ok($v1->getPreValidator() !== $v->getPreValidator(), '__clone() clones the pre validator');
  353. $t->ok($v1->getPreValidator() == $v->getPreValidator(), '__clone() clones the pre validator');
  354. $t->ok($v1->getPostValidator() !== $v->getPostValidator(), '__clone() clones the post validator');
  355. $t->ok($v1->getPostValidator() == $v->getPostValidator(), '__clone() clones the post validator');