PageRenderTime 33ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/test/unit/validator/sfValidatorBaseTest.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 246 lines | 185 code | 35 blank | 26 comment | 0 complexity | 9608f9c9929eed1c1a1722c13bca81a0 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(47);
  11. class ValidatorIdentity extends sfValidatorBase
  12. {
  13. protected function configure($options = array(), $messages = array())
  14. {
  15. $this->addOption('foo', 'bar');
  16. $this->addMessage('foo', 'bar');
  17. }
  18. public function testIsEmpty($value)
  19. {
  20. return $this->isEmpty($value);
  21. }
  22. protected function doClean($value)
  23. {
  24. return $value;
  25. }
  26. }
  27. class ValidatorIdentityWithRequired extends sfValidatorBase
  28. {
  29. protected function configure($options = array(), $messages = array())
  30. {
  31. $this->addRequiredOption('foo');
  32. }
  33. protected function doClean($value)
  34. {
  35. return $value;
  36. }
  37. }
  38. // ->configure()
  39. $t->diag('->configure()');
  40. $v = new ValidatorIdentity();
  41. $t->is($v->getOption('foo'), 'bar', '->configure() can add some options');
  42. $v = new ValidatorIdentity(array('foo' => 'foobar'));
  43. $t->is($v->getOption('foo'), 'foobar', '->configure() takes an options array as its first argument and values override default option values');
  44. $v = new ValidatorIdentity();
  45. $t->is($v->getMessage('foo'), 'bar', '->configure() can add some message');
  46. $v = new ValidatorIdentity(array(), array('foo' => 'foobar'));
  47. $t->is($v->getMessage('foo'), 'foobar', '->configure() takes a messages array as its second argument and values override default message values');
  48. try
  49. {
  50. new ValidatorIdentity(array('nonexistant' => false, 'foo' => 'foobar', 'anothernonexistant' => 'bar', 'required' => true));
  51. $t->fail('__construct() throws an InvalidArgumentException if you pass some non existant options');
  52. $t->skip();
  53. }
  54. catch (InvalidArgumentException $e)
  55. {
  56. $t->pass('__construct() throws an InvalidArgumentException if you pass some non existant options');
  57. $t->like($e->getMessage(), '/ \'nonexistant\', \'anothernonexistant\'/', 'The exception contains the non existant option names');
  58. }
  59. try
  60. {
  61. new ValidatorIdentity(array(), array('required' => 'This is required.', 'nonexistant' => 'foo', 'anothernonexistant' => false));
  62. $t->fail('__construct() throws an InvalidArgumentException if you pass some non existant error codes');
  63. $t->skip();
  64. }
  65. catch (InvalidArgumentException $e)
  66. {
  67. $t->pass('__construct() throws an InvalidArgumentException if you pass some non existant error codes');
  68. $t->like($e->getMessage(), '/ \'nonexistant\', \'anothernonexistant\'/', 'The exception contains the non existant error codes');
  69. }
  70. // ->getRequiredOptions()
  71. $t->diag('getRequiredOptions');
  72. $v = new ValidatorIdentityWithRequired(array('foo' => 'bar'));
  73. $t->is($v->getRequiredOptions(), array('foo'), '->getRequiredOptions() returns an array of required option names');
  74. try
  75. {
  76. new ValidatorIdentityWithRequired();
  77. $t->fail('__construct() throws an RuntimeException if you don\'t pass a required option');
  78. }
  79. catch (RuntimeException $e)
  80. {
  81. $t->pass('__construct() throws an RuntimeException if you don\'t pass a required option');
  82. }
  83. $v = new ValidatorIdentity();
  84. // ->clean()
  85. $t->diag('->clean()');
  86. $t->is($v->clean('foo'), 'foo', '->clean() returns a cleanup version of the data to validate');
  87. try
  88. {
  89. $t->is($v->clean(''), '');
  90. $t->fail('->clean() throws a sfValidatorError exception if the data does not validate');
  91. $t->skip('', 1);
  92. }
  93. catch (sfValidatorError $e)
  94. {
  95. $t->pass('->clean() throws a sfValidatorError exception if the data does not validate');
  96. $t->is($e->getCode(), 'required', '->clean() throws a sfValidatorError');
  97. }
  98. $t->is($v->clean(' foo '), ' foo ', '->clean() does not trim whitespaces by default');
  99. // ->isEmpty()
  100. $t->diag('->isEmpty()');
  101. $t->is($v->testIsEmpty(null), true, 'null value isEmpty()');
  102. $t->is($v->testIsEmpty(''), true, 'empty string value isEmpty()');
  103. $t->is($v->testIsEmpty(array()), true, 'empty array value isEmpty()');
  104. $t->is($v->testIsEmpty(false), false, 'false value not isEmpty()');
  105. // ->getEmptyValue()
  106. $t->diag('->getEmptyValue()');
  107. $v->setOption('required', false);
  108. $v->setOption('empty_value', 'defaultnullvalue');
  109. $t->is($v->clean(''), 'defaultnullvalue', '->getEmptyValue() returns the representation of an empty value for this validator');
  110. $v->setOption('empty_value', null);
  111. // ->setOption()
  112. $t->diag('->setOption()');
  113. $v->setOption('required', false);
  114. $t->is($v->clean(''), null, '->setOption() changes options (required for example)');
  115. $v->setOption('trim', true);
  116. $t->is($v->clean(' foo '), 'foo', '->setOption() can turn on whitespace trimming');
  117. try
  118. {
  119. $v->setOption('foobar', 'foo');
  120. $t->fail('->setOption() throws an InvalidArgumentException if the option is not registered');
  121. }
  122. catch (InvalidArgumentException $e)
  123. {
  124. $t->pass('->setOption() throws an InvalidArgumentException if the option is not registered');
  125. }
  126. // ->hasOption()
  127. $t->diag('->hasOption()');
  128. $t->ok($v->hasOption('required'), '->hasOption() returns true if the validator has the option');
  129. $t->ok(!$v->hasOption('nonexistant'), '->hasOption() returns false if the validator does not have the option');
  130. // ->getOption()
  131. $t->diag('->getOption()');
  132. $t->is($v->getOption('required'), false, '->getOption() returns the value of an option');
  133. $t->is($v->getOption('nonexistant'), null, '->getOption() returns null if the option does not exist');
  134. // ->addOption()
  135. $t->diag('->addOption()');
  136. $v->addOption('foobar');
  137. $v->setOption('foobar', 'foo');
  138. $t->is($v->getOption('foobar'), 'foo', '->addOption() adds a new option to a validator');
  139. // ->getOptions() ->setOptions()
  140. $t->diag('->getOptions() ->setOptions()');
  141. $v->setOptions(array('required' => true, 'trim' => false));
  142. $t->is($v->getOptions(), array('required' => true, 'trim' => false, 'empty_value' => null), '->setOptions() changes all options');
  143. // ->getMessages()
  144. $t->diag('->getMessages()');
  145. $t->is($v->getMessages(), array('required' => 'Required.', 'invalid' => 'Invalid.', 'foo' => 'bar'), '->getMessages() returns an array of all error messages');
  146. // ->getMessage()
  147. $t->diag('->getMessage()');
  148. $t->is($v->getMessage('required'), 'Required.', '->getMessage() returns an error message string');
  149. $t->is($v->getMessage('nonexistant'), '', '->getMessage() returns an empty string if the message does not exist');
  150. // ->setMessage()
  151. $t->diag('->setMessage()');
  152. $v->setMessage('required', 'The field is required.');
  153. try
  154. {
  155. $v->clean('');
  156. $t->isnt($e->getMessage(), 'The field is required.', '->setMessage() changes the default error message string');
  157. }
  158. catch (sfValidatorError $e)
  159. {
  160. $t->is($e->getMessage(), 'The field is required.', '->setMessage() changes the default error message string');
  161. }
  162. try
  163. {
  164. $v->setMessage('foobar', 'foo');
  165. $t->fail('->setMessage() throws an InvalidArgumentException if the message is not registered');
  166. }
  167. catch (InvalidArgumentException $e)
  168. {
  169. $t->pass('->setMessage() throws an InvalidArgumentException if the message is not registered');
  170. }
  171. // ->setMessages()
  172. $t->diag('->setMessages()');
  173. $v->setMessages(array('required' => 'This is required!'));
  174. $t->is($v->getMessages(), array('invalid' => 'Invalid.', 'required' => 'This is required!'), '->setMessages() changes all error messages');
  175. // ->addMessage()
  176. $t->diag('->addMessage()');
  177. $v->addMessage('foobar', 'foo');
  178. $v->setMessage('foobar', 'bar');
  179. $t->is($v->getMessage('foobar'), 'bar', '->addMessage() adds a new error code');
  180. // ->getErrorCodes()
  181. $t->diag('->getErrorCodes()');
  182. $t->is($v->getErrorCodes(), array('required', 'invalid', 'foo'), '->getErrorCodes() returns an array of error codes the validator can use');
  183. // ::getCharset() ::setCharset()
  184. $t->diag('::getCharset() ::setCharset()');
  185. $t->is(sfValidatorBase::getCharset(), 'UTF-8', '::getCharset() returns the charset to use for validators');
  186. sfValidatorBase::setCharset('ISO-8859-1');
  187. $t->is(sfValidatorBase::getCharset(), 'ISO-8859-1', '::setCharset() changes the charset to use for validators');
  188. // ->asString()
  189. $t->diag('->asString()');
  190. $v = new ValidatorIdentity();
  191. $t->is($v->asString(), 'ValidatorIdentity()', '->asString() returns a string representation of the validator');
  192. $v->setOption('required', false);
  193. $v->setOption('foo', 'foo');
  194. $t->is($v->asString(), 'ValidatorIdentity({ required: false, foo: foo })', '->asString() returns a string representation of the validator');
  195. $v->setMessage('required', 'This is required.');
  196. $t->is($v->asString(), 'ValidatorIdentity({ required: false, foo: foo }, { required: \'This is required.\' })', '->asString() returns a string representation of the validator');
  197. $v = new ValidatorIdentity();
  198. $v->setMessage('required', 'This is required.');
  199. $t->is($v->asString(), 'ValidatorIdentity({}, { required: \'This is required.\' })', '->asString() returns a string representation of the validator');
  200. // ::setDefaultMessage()
  201. $t->diag('::setDefaultMessage()');
  202. ValidatorIdentity::setDefaultMessage('required', 'This field is required.');
  203. ValidatorIdentity::setDefaultMessage('invalid', 'This field is invalid.');
  204. ValidatorIdentity::setDefaultMessage('foo', 'Foo bar.');
  205. $v = new ValidatorIdentity();
  206. $t->is($v->getMessage('required'), 'This field is required.', '::setDefaultMessage() sets the default message for an error');
  207. $t->is($v->getMessage('invalid'), 'This field is invalid.', '::setDefaultMessage() sets the default message for an error');
  208. $t->is($v->getMessage('foo'), 'Foo bar.', '::setDefaultMessage() sets the default message for an error');
  209. $v = new ValidatorIdentity(array(), array('required' => 'Yep, this is required!', 'foo' => 'Yep, this is a foo error!'));
  210. $t->is($v->getMessage('required'), 'Yep, this is required!', '::setDefaultMessage() is ignored if the validator explicitly overrides the message');
  211. $t->is($v->getMessage('foo'), 'Yep, this is a foo error!', '::setDefaultMessage() is ignored if the validator explicitly overrides the message');