UploadedFileTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Tests\Component\HttpFoundation\File;
  11. use Symfony\Component\HttpFoundation\File\UploadedFile;
  12. class UploadedFileTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected function setUp()
  15. {
  16. if (!ini_get('file_uploads')) {
  17. $this->markTestSkipped('file_uploads is disabled in php.ini');
  18. }
  19. }
  20. public function testFileUploadsWithNoMimeType()
  21. {
  22. $file = new UploadedFile(
  23. __DIR__.'/Fixtures/test.gif',
  24. 'original.gif',
  25. null,
  26. filesize(__DIR__.'/Fixtures/test.gif'),
  27. UPLOAD_ERR_OK
  28. );
  29. $this->assertAttributeEquals('application/octet-stream', 'mimeType', $file);
  30. if (extension_loaded('fileinfo')) {
  31. $this->assertEquals('image/gif', $file->getMimeType());
  32. } else {
  33. $this->assertEquals('application/octet-stream', $file->getMimeType());
  34. }
  35. }
  36. public function testFileUploadsWithUnknownMimeType()
  37. {
  38. $file = new UploadedFile(
  39. __DIR__.'/Fixtures/.unknownextension',
  40. 'original.gif',
  41. null,
  42. filesize(__DIR__.'/Fixtures/.unknownextension'),
  43. UPLOAD_ERR_OK
  44. );
  45. $this->assertAttributeEquals('application/octet-stream', 'mimeType', $file);
  46. $this->assertEquals('application/octet-stream', $file->getMimeType());
  47. }
  48. public function testErrorIsOkByDefault()
  49. {
  50. $file = new UploadedFile(
  51. __DIR__.'/Fixtures/test.gif',
  52. 'original.gif',
  53. 'image/gif',
  54. filesize(__DIR__.'/Fixtures/test.gif'),
  55. null
  56. );
  57. $this->assertEquals(UPLOAD_ERR_OK, $file->getError());
  58. }
  59. public function testGetOriginalName()
  60. {
  61. $file = new UploadedFile(
  62. __DIR__.'/Fixtures/test.gif',
  63. 'original.gif',
  64. 'image/gif',
  65. filesize(__DIR__.'/Fixtures/test.gif'),
  66. null
  67. );
  68. $this->assertEquals('original.gif', $file->getOriginalName());
  69. }
  70. public function testGetOriginalNameSanitizeFilename()
  71. {
  72. $file = new UploadedFile(
  73. __DIR__.'/Fixtures/test.gif',
  74. '../../original.gif',
  75. 'image/gif',
  76. filesize(__DIR__.'/Fixtures/test.gif'),
  77. null
  78. );
  79. $this->assertEquals('original.gif', $file->getOriginalName());
  80. }
  81. }