File.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Illuminate\Http\Testing;
  3. use Illuminate\Http\UploadedFile;
  4. class File extends UploadedFile
  5. {
  6. /**
  7. * The name of the file.
  8. *
  9. * @var string
  10. */
  11. public $name;
  12. /**
  13. * The temporary file resource.
  14. *
  15. * @var resource
  16. */
  17. public $tempFile;
  18. /**
  19. * The "size" to report.
  20. *
  21. * @var int
  22. */
  23. public $sizeToReport;
  24. /**
  25. * Create a new file instance.
  26. *
  27. * @param string $name
  28. * @param resource $tempFile
  29. * @return void
  30. */
  31. public function __construct($name, $tempFile)
  32. {
  33. $this->name = $name;
  34. $this->tempFile = $tempFile;
  35. parent::__construct(
  36. $this->tempFilePath(), $name, $this->getMimeType(),
  37. filesize($this->tempFilePath()), null, true
  38. );
  39. }
  40. /**
  41. * Create a new fake file.
  42. *
  43. * @param string $name
  44. * @param int $kilobytes
  45. * @return \Illuminate\Http\Testing\File
  46. */
  47. public static function create($name, $kilobytes = 0)
  48. {
  49. return (new FileFactory)->create($name, $kilobytes);
  50. }
  51. /**
  52. * Create a new fake image.
  53. *
  54. * @param string $name
  55. * @param int $width
  56. * @param int $height
  57. * @return \Illuminate\Http\Testing\File
  58. */
  59. public static function image($name, $width = 10, $height = 10)
  60. {
  61. return (new FileFactory)->image($name, $width, $height);
  62. }
  63. /**
  64. * Set the "size" of the file in kilobytes.
  65. *
  66. * @param int $kilobytes
  67. * @return $this
  68. */
  69. public function size($kilobytes)
  70. {
  71. $this->sizeToReport = $kilobytes * 1024;
  72. return $this;
  73. }
  74. /**
  75. * Get the size of the file.
  76. *
  77. * @return int
  78. */
  79. public function getSize()
  80. {
  81. return $this->sizeToReport ?: parent::getSize();
  82. }
  83. /**
  84. * Get the MIME type for the file.
  85. *
  86. * @return string
  87. */
  88. public function getMimeType()
  89. {
  90. return MimeType::from($this->name);
  91. }
  92. /**
  93. * Get the path to the temporary file.
  94. *
  95. * @return string
  96. */
  97. protected function tempFilePath()
  98. {
  99. return stream_get_meta_data($this->tempFile)['uri'];
  100. }
  101. }