UploadedFileTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Tests\Component\HttpFoundation\File;
  11. use Symfony\Component\HttpFoundation\File\UploadedFile;
  12. class UploadedFileTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testFileUploadsMustBeEnabled()
  15. {
  16. // we can't change this setting without modifying php.ini :(
  17. if (!ini_get('file_uploads')) {
  18. $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
  19. new UploadedFile(
  20. __DIR__.'/Fixtures/test.gif',
  21. 'original.gif',
  22. 'image/gif',
  23. filesize(__DIR__.'/Fixtures/test.gif'),
  24. UPLOAD_ERR_OK
  25. );
  26. }
  27. }
  28. public function testFileUploadsWithNoMimeType()
  29. {
  30. // we can't change this setting without modifying php.ini :(
  31. if (ini_get('file_uploads')) {
  32. $file = new UploadedFile(
  33. __DIR__.'/Fixtures/test.gif',
  34. 'original.gif',
  35. null,
  36. filesize(__DIR__.'/Fixtures/test.gif'),
  37. UPLOAD_ERR_OK
  38. );
  39. $this->assertAttributeEquals('application/octet-stream', 'mimeType', $file);
  40. $this->assertEquals('image/gif', $file->getMimeType());
  41. }
  42. }
  43. public function testFileUploadsWithUnknownMimeType()
  44. {
  45. // we can't change this setting without modifying php.ini :(
  46. if (ini_get('file_uploads')) {
  47. $file = new UploadedFile(
  48. __DIR__.'/Fixtures/.unknownextension',
  49. 'original.gif',
  50. null,
  51. filesize(__DIR__.'/Fixtures/.unknownextension'),
  52. UPLOAD_ERR_OK
  53. );
  54. $this->assertAttributeEquals('application/octet-stream', 'mimeType', $file);
  55. $this->assertEquals('application/octet-stream', $file->getMimeType());
  56. }
  57. }
  58. public function testErrorIsOkByDefault()
  59. {
  60. // we can't change this setting without modifying php.ini :(
  61. if (ini_get('file_uploads')) {
  62. $file = new UploadedFile(
  63. __DIR__.'/Fixtures/test.gif',
  64. 'original.gif',
  65. 'image/gif',
  66. filesize(__DIR__.'/Fixtures/test.gif'),
  67. null
  68. );
  69. $this->assertEquals(UPLOAD_ERR_OK, $file->getError());
  70. }
  71. }
  72. public function testGetOriginalName()
  73. {
  74. // we can't change this setting without modifying php.ini :(
  75. if (ini_get('file_uploads')) {
  76. $file = new UploadedFile(
  77. __DIR__.'/Fixtures/test.gif',
  78. 'original.gif',
  79. 'image/gif',
  80. filesize(__DIR__.'/Fixtures/test.gif'),
  81. null
  82. );
  83. $this->assertEquals('original.gif', $file->getOriginalName());
  84. }
  85. }
  86. }