UploadedFileTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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('test.gif', $file->getName());
  69. }
  70. }