CurrencyValidatorTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\Currency;
  13. use Symfony\Component\Validator\Constraints\CurrencyValidator;
  14. use Symfony\Component\Validator\Validation;
  15. class CurrencyValidatorTest extends AbstractConstraintValidatorTest
  16. {
  17. protected function getApiVersion()
  18. {
  19. return Validation::API_VERSION_2_5;
  20. }
  21. protected function createValidator()
  22. {
  23. return new CurrencyValidator();
  24. }
  25. public function testNullIsValid()
  26. {
  27. $this->validator->validate(null, new Currency());
  28. $this->assertNoViolation();
  29. }
  30. public function testEmptyStringIsValid()
  31. {
  32. $this->validator->validate('', new Currency());
  33. $this->assertNoViolation();
  34. }
  35. /**
  36. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  37. */
  38. public function testExpectsStringCompatibleType()
  39. {
  40. $this->validator->validate(new \stdClass(), new Currency());
  41. }
  42. /**
  43. * @dataProvider getValidCurrencies
  44. */
  45. public function testValidCurrencies($currency)
  46. {
  47. $this->validator->validate($currency, new Currency());
  48. $this->assertNoViolation();
  49. }
  50. /**
  51. * @dataProvider getValidCurrencies
  52. **/
  53. public function testValidCurrenciesWithCountrySpecificLocale($currency)
  54. {
  55. IntlTestHelper::requireFullIntl($this, false);
  56. \Locale::setDefault('en_GB');
  57. $this->validator->validate($currency, new Currency());
  58. $this->assertNoViolation();
  59. }
  60. public function getValidCurrencies()
  61. {
  62. return array(
  63. array('EUR'),
  64. array('USD'),
  65. array('SIT'),
  66. array('AUD'),
  67. array('CAD'),
  68. );
  69. }
  70. /**
  71. * @dataProvider getInvalidCurrencies
  72. */
  73. public function testInvalidCurrencies($currency)
  74. {
  75. $constraint = new Currency(array(
  76. 'message' => 'myMessage',
  77. ));
  78. $this->validator->validate($currency, $constraint);
  79. $this->buildViolation('myMessage')
  80. ->setParameter('{{ value }}', '"'.$currency.'"')
  81. ->setCode(Currency::NO_SUCH_CURRENCY_ERROR)
  82. ->assertRaised();
  83. }
  84. public function getInvalidCurrencies()
  85. {
  86. return array(
  87. array('EN'),
  88. array('foobar'),
  89. );
  90. }
  91. }