FileTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Validator\Constraints\File;
  13. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  14. class FileTest extends TestCase
  15. {
  16. /**
  17. * @dataProvider provideValidSizes
  18. */
  19. public function testMaxSize($maxSize, $bytes, $binaryFormat)
  20. {
  21. $file = new File(array('maxSize' => $maxSize));
  22. $this->assertSame($bytes, $file->maxSize);
  23. $this->assertSame($binaryFormat, $file->binaryFormat);
  24. $this->assertTrue($file->__isset('maxSize'));
  25. }
  26. public function testMagicIsset()
  27. {
  28. $file = new File(array('maxSize' => 1));
  29. $this->assertTrue($file->__isset('maxSize'));
  30. $this->assertTrue($file->__isset('groups'));
  31. $this->assertFalse($file->__isset('toto'));
  32. }
  33. /**
  34. * @dataProvider provideValidSizes
  35. */
  36. public function testMaxSizeCanBeSetAfterInitialization($maxSize, $bytes, $binaryFormat)
  37. {
  38. $file = new File();
  39. $file->maxSize = $maxSize;
  40. $this->assertSame($bytes, $file->maxSize);
  41. $this->assertSame($binaryFormat, $file->binaryFormat);
  42. }
  43. /**
  44. * @dataProvider provideInvalidSizes
  45. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  46. */
  47. public function testInvalidValueForMaxSizeThrowsExceptionAfterInitialization($maxSize)
  48. {
  49. $file = new File(array('maxSize' => 1000));
  50. $file->maxSize = $maxSize;
  51. }
  52. /**
  53. * @dataProvider provideInvalidSizes
  54. */
  55. public function testMaxSizeCannotBeSetToInvalidValueAfterInitialization($maxSize)
  56. {
  57. $file = new File(array('maxSize' => 1000));
  58. try {
  59. $file->maxSize = $maxSize;
  60. } catch (ConstraintDefinitionException $e) {
  61. }
  62. $this->assertSame(1000, $file->maxSize);
  63. }
  64. /**
  65. * @dataProvider provideInValidSizes
  66. * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
  67. */
  68. public function testInvalidMaxSize($maxSize)
  69. {
  70. new File(array('maxSize' => $maxSize));
  71. }
  72. public function provideValidSizes()
  73. {
  74. return array(
  75. array('500', 500, false),
  76. array(12300, 12300, false),
  77. array('1ki', 1024, true),
  78. array('1KI', 1024, true),
  79. array('2k', 2000, false),
  80. array('2K', 2000, false),
  81. array('1mi', 1048576, true),
  82. array('1MI', 1048576, true),
  83. array('3m', 3000000, false),
  84. array('3M', 3000000, false),
  85. );
  86. }
  87. public function provideInvalidSizes()
  88. {
  89. return array(
  90. array('+100'),
  91. array('foo'),
  92. array('1Ko'),
  93. array('1kio'),
  94. array('1G'),
  95. array('1Gi'),
  96. );
  97. }
  98. /**
  99. * @dataProvider provideFormats
  100. */
  101. public function testBinaryFormat($maxSize, $guessedFormat, $binaryFormat)
  102. {
  103. $file = new File(array('maxSize' => $maxSize, 'binaryFormat' => $guessedFormat));
  104. $this->assertSame($binaryFormat, $file->binaryFormat);
  105. }
  106. public function provideFormats()
  107. {
  108. return array(
  109. array(100, null, false),
  110. array(100, true, true),
  111. array(100, false, false),
  112. array('100K', null, false),
  113. array('100K', true, true),
  114. array('100K', false, false),
  115. array('100Ki', null, true),
  116. array('100Ki', true, true),
  117. array('100Ki', false, false),
  118. );
  119. }
  120. }