RangeValidatorTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Constraints;
  11. use Symfony\Component\Intl\Util\IntlTestHelper;
  12. use Symfony\Component\Validator\Constraints\Range;
  13. use Symfony\Component\Validator\Constraints\RangeValidator;
  14. use Symfony\Component\Validator\Validation;
  15. class RangeValidatorTest extends AbstractConstraintValidatorTest
  16. {
  17. protected function getApiVersion()
  18. {
  19. return Validation::API_VERSION_2_5;
  20. }
  21. protected function createValidator()
  22. {
  23. return new RangeValidator();
  24. }
  25. public function testNullIsValid()
  26. {
  27. $this->validator->validate(null, new Range(array('min' => 10, 'max' => 20)));
  28. $this->assertNoViolation();
  29. }
  30. public function getTenToTwenty()
  31. {
  32. return array(
  33. array(10.00001),
  34. array(19.99999),
  35. array('10.00001'),
  36. array('19.99999'),
  37. array(10),
  38. array(20),
  39. array(10.0),
  40. array(20.0),
  41. );
  42. }
  43. public function getLessThanTen()
  44. {
  45. return array(
  46. array(9.99999, '9.99999'),
  47. array('9.99999', '"9.99999"'),
  48. array(5, '5'),
  49. array(1.0, '1.0'),
  50. );
  51. }
  52. public function getMoreThanTwenty()
  53. {
  54. return array(
  55. array(20.000001, '20.000001'),
  56. array('20.000001', '"20.000001"'),
  57. array(21, '21'),
  58. array(30.0, '30.0'),
  59. );
  60. }
  61. /**
  62. * @dataProvider getTenToTwenty
  63. */
  64. public function testValidValuesMin($value)
  65. {
  66. $constraint = new Range(array('min' => 10));
  67. $this->validator->validate($value, $constraint);
  68. $this->assertNoViolation();
  69. }
  70. /**
  71. * @dataProvider getTenToTwenty
  72. */
  73. public function testValidValuesMax($value)
  74. {
  75. $constraint = new Range(array('max' => 20));
  76. $this->validator->validate($value, $constraint);
  77. $this->assertNoViolation();
  78. }
  79. /**
  80. * @dataProvider getTenToTwenty
  81. */
  82. public function testValidValuesMinMax($value)
  83. {
  84. $constraint = new Range(array('min' => 10, 'max' => 20));
  85. $this->validator->validate($value, $constraint);
  86. $this->assertNoViolation();
  87. }
  88. /**
  89. * @dataProvider getLessThanTen
  90. */
  91. public function testInvalidValuesMin($value, $formattedValue)
  92. {
  93. $constraint = new Range(array(
  94. 'min' => 10,
  95. 'minMessage' => 'myMessage',
  96. ));
  97. $this->validator->validate($value, $constraint);
  98. $this->buildViolation('myMessage')
  99. ->setParameter('{{ value }}', $formattedValue)
  100. ->setParameter('{{ limit }}', 10)
  101. ->setCode(Range::TOO_LOW_ERROR)
  102. ->assertRaised();
  103. }
  104. /**
  105. * @dataProvider getMoreThanTwenty
  106. */
  107. public function testInvalidValuesMax($value, $formattedValue)
  108. {
  109. $constraint = new Range(array(
  110. 'max' => 20,
  111. 'maxMessage' => 'myMessage',
  112. ));
  113. $this->validator->validate($value, $constraint);
  114. $this->buildViolation('myMessage')
  115. ->setParameter('{{ value }}', $formattedValue)
  116. ->setParameter('{{ limit }}', 20)
  117. ->setCode(Range::TOO_HIGH_ERROR)
  118. ->assertRaised();
  119. }
  120. /**
  121. * @dataProvider getMoreThanTwenty
  122. */
  123. public function testInvalidValuesCombinedMax($value, $formattedValue)
  124. {
  125. $constraint = new Range(array(
  126. 'min' => 10,
  127. 'max' => 20,
  128. 'minMessage' => 'myMinMessage',
  129. 'maxMessage' => 'myMaxMessage',
  130. ));
  131. $this->validator->validate($value, $constraint);
  132. $this->buildViolation('myMaxMessage')
  133. ->setParameter('{{ value }}', $formattedValue)
  134. ->setParameter('{{ limit }}', 20)
  135. ->setCode(Range::TOO_HIGH_ERROR)
  136. ->assertRaised();
  137. }
  138. /**
  139. * @dataProvider getLessThanTen
  140. */
  141. public function testInvalidValuesCombinedMin($value, $formattedValue)
  142. {
  143. $constraint = new Range(array(
  144. 'min' => 10,
  145. 'max' => 20,
  146. 'minMessage' => 'myMinMessage',
  147. 'maxMessage' => 'myMaxMessage',
  148. ));
  149. $this->validator->validate($value, $constraint);
  150. $this->buildViolation('myMinMessage')
  151. ->setParameter('{{ value }}', $formattedValue)
  152. ->setParameter('{{ limit }}', 10)
  153. ->setCode(Range::TOO_LOW_ERROR)
  154. ->assertRaised();
  155. }
  156. public function getTenthToTwentiethMarch2014()
  157. {
  158. // The provider runs before setUp(), so we need to manually fix
  159. // the default timezone
  160. $this->setDefaultTimezone('UTC');
  161. $tests = array(
  162. array(new \DateTime('March 10, 2014')),
  163. array(new \DateTime('March 15, 2014')),
  164. array(new \DateTime('March 20, 2014')),
  165. );
  166. if (\PHP_VERSION_ID >= 50500) {
  167. $tests[] = array(new \DateTimeImmutable('March 10, 2014'));
  168. $tests[] = array(new \DateTimeImmutable('March 15, 2014'));
  169. $tests[] = array(new \DateTimeImmutable('March 20, 2014'));
  170. }
  171. $this->restoreDefaultTimezone();
  172. return $tests;
  173. }
  174. public function getSoonerThanTenthMarch2014()
  175. {
  176. // The provider runs before setUp(), so we need to manually fix
  177. // the default timezone
  178. $this->setDefaultTimezone('UTC');
  179. $tests = array(
  180. array(new \DateTime('March 20, 2013'), 'Mar 20, 2013, 12:00 AM'),
  181. array(new \DateTime('March 9, 2014'), 'Mar 9, 2014, 12:00 AM'),
  182. );
  183. if (\PHP_VERSION_ID >= 50500) {
  184. $tests[] = array(new \DateTimeImmutable('March 20, 2013'), 'Mar 20, 2013, 12:00 AM');
  185. $tests[] = array(new \DateTimeImmutable('March 9, 2014'), 'Mar 9, 2014, 12:00 AM');
  186. }
  187. $this->restoreDefaultTimezone();
  188. return $tests;
  189. }
  190. public function getLaterThanTwentiethMarch2014()
  191. {
  192. // The provider runs before setUp(), so we need to manually fix
  193. // the default timezone
  194. $this->setDefaultTimezone('UTC');
  195. $tests = array(
  196. array(new \DateTime('March 21, 2014'), 'Mar 21, 2014, 12:00 AM'),
  197. array(new \DateTime('March 9, 2015'), 'Mar 9, 2015, 12:00 AM'),
  198. );
  199. if (\PHP_VERSION_ID >= 50500) {
  200. $tests[] = array(new \DateTimeImmutable('March 21, 2014'), 'Mar 21, 2014, 12:00 AM');
  201. $tests[] = array(new \DateTimeImmutable('March 9, 2015'), 'Mar 9, 2015, 12:00 AM');
  202. }
  203. $this->restoreDefaultTimezone();
  204. return $tests;
  205. }
  206. /**
  207. * @dataProvider getTenthToTwentiethMarch2014
  208. */
  209. public function testValidDatesMin($value)
  210. {
  211. $constraint = new Range(array('min' => 'March 10, 2014'));
  212. $this->validator->validate($value, $constraint);
  213. $this->assertNoViolation();
  214. }
  215. /**
  216. * @dataProvider getTenthToTwentiethMarch2014
  217. */
  218. public function testValidDatesMax($value)
  219. {
  220. $constraint = new Range(array('max' => 'March 20, 2014'));
  221. $this->validator->validate($value, $constraint);
  222. $this->assertNoViolation();
  223. }
  224. /**
  225. * @dataProvider getTenthToTwentiethMarch2014
  226. */
  227. public function testValidDatesMinMax($value)
  228. {
  229. $constraint = new Range(array('min' => 'March 10, 2014', 'max' => 'March 20, 2014'));
  230. $this->validator->validate($value, $constraint);
  231. $this->assertNoViolation();
  232. }
  233. /**
  234. * @dataProvider getSoonerThanTenthMarch2014
  235. */
  236. public function testInvalidDatesMin($value, $dateTimeAsString)
  237. {
  238. // Conversion of dates to string differs between ICU versions
  239. // Make sure we have the correct version loaded
  240. IntlTestHelper::requireIntl($this, '57.1');
  241. $constraint = new Range(array(
  242. 'min' => 'March 10, 2014',
  243. 'minMessage' => 'myMessage',
  244. ));
  245. $this->validator->validate($value, $constraint);
  246. $this->buildViolation('myMessage')
  247. ->setParameter('{{ value }}', $dateTimeAsString)
  248. ->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
  249. ->setCode(Range::TOO_LOW_ERROR)
  250. ->assertRaised();
  251. }
  252. /**
  253. * @dataProvider getLaterThanTwentiethMarch2014
  254. */
  255. public function testInvalidDatesMax($value, $dateTimeAsString)
  256. {
  257. // Conversion of dates to string differs between ICU versions
  258. // Make sure we have the correct version loaded
  259. IntlTestHelper::requireIntl($this, '57.1');
  260. $constraint = new Range(array(
  261. 'max' => 'March 20, 2014',
  262. 'maxMessage' => 'myMessage',
  263. ));
  264. $this->validator->validate($value, $constraint);
  265. $this->buildViolation('myMessage')
  266. ->setParameter('{{ value }}', $dateTimeAsString)
  267. ->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
  268. ->setCode(Range::TOO_HIGH_ERROR)
  269. ->assertRaised();
  270. }
  271. /**
  272. * @dataProvider getLaterThanTwentiethMarch2014
  273. */
  274. public function testInvalidDatesCombinedMax($value, $dateTimeAsString)
  275. {
  276. // Conversion of dates to string differs between ICU versions
  277. // Make sure we have the correct version loaded
  278. IntlTestHelper::requireIntl($this, '57.1');
  279. $constraint = new Range(array(
  280. 'min' => 'March 10, 2014',
  281. 'max' => 'March 20, 2014',
  282. 'minMessage' => 'myMinMessage',
  283. 'maxMessage' => 'myMaxMessage',
  284. ));
  285. $this->validator->validate($value, $constraint);
  286. $this->buildViolation('myMaxMessage')
  287. ->setParameter('{{ value }}', $dateTimeAsString)
  288. ->setParameter('{{ limit }}', 'Mar 20, 2014, 12:00 AM')
  289. ->setCode(Range::TOO_HIGH_ERROR)
  290. ->assertRaised();
  291. }
  292. /**
  293. * @dataProvider getSoonerThanTenthMarch2014
  294. */
  295. public function testInvalidDatesCombinedMin($value, $dateTimeAsString)
  296. {
  297. // Conversion of dates to string differs between ICU versions
  298. // Make sure we have the correct version loaded
  299. IntlTestHelper::requireIntl($this, '57.1');
  300. $constraint = new Range(array(
  301. 'min' => 'March 10, 2014',
  302. 'max' => 'March 20, 2014',
  303. 'minMessage' => 'myMinMessage',
  304. 'maxMessage' => 'myMaxMessage',
  305. ));
  306. $this->validator->validate($value, $constraint);
  307. $this->buildViolation('myMinMessage')
  308. ->setParameter('{{ value }}', $dateTimeAsString)
  309. ->setParameter('{{ limit }}', 'Mar 10, 2014, 12:00 AM')
  310. ->setCode(Range::TOO_LOW_ERROR)
  311. ->assertRaised();
  312. }
  313. public function getInvalidValues()
  314. {
  315. return array(
  316. array(9.999999),
  317. array(20.000001),
  318. array('9.999999'),
  319. array('20.000001'),
  320. array(new \stdClass()),
  321. );
  322. }
  323. public function testNonNumeric()
  324. {
  325. $this->validator->validate('abcd', new Range(array(
  326. 'min' => 10,
  327. 'max' => 20,
  328. 'invalidMessage' => 'myMessage',
  329. )));
  330. $this->buildViolation('myMessage')
  331. ->setParameter('{{ value }}', '"abcd"')
  332. ->setCode(Range::INVALID_CHARACTERS_ERROR)
  333. ->assertRaised();
  334. }
  335. }