TestCase.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace Maatwebsite\Excel\Tests;
  3. use Illuminate\Contracts\Queue\Job;
  4. use Illuminate\Http\Testing\File;
  5. use Maatwebsite\Excel\ExcelServiceProvider;
  6. use Orchestra\Testbench\TestCase as OrchestraTestCase;
  7. use PhpOffice\PhpSpreadsheet\IOFactory;
  8. use PHPUnit\Framework\Constraint\StringContains;
  9. class TestCase extends OrchestraTestCase
  10. {
  11. /**
  12. * @param string $filePath
  13. * @param string $writerType
  14. * @return \PhpOffice\PhpSpreadsheet\Spreadsheet
  15. *
  16. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  17. */
  18. public function read(string $filePath, string $writerType)
  19. {
  20. $reader = IOFactory::createReader($writerType);
  21. return $reader->load($filePath);
  22. }
  23. /**
  24. * @param string $filePath
  25. * @param string|null $filename
  26. * @return File
  27. */
  28. public function givenUploadedFile(string $filePath, string $filename = null): File
  29. {
  30. $filename = $filename ?? basename($filePath);
  31. // Create temporary file.
  32. $newFilePath = tempnam(sys_get_temp_dir(), 'import-');
  33. // Copy the existing file to a temporary file.
  34. copy($filePath, $newFilePath);
  35. return new File($filename, fopen($newFilePath, 'r'));
  36. }
  37. /**
  38. * @param string $filePath
  39. * @param string $writerType
  40. * @param int|null $sheetIndex
  41. * @return array
  42. *
  43. * @throws \PhpOffice\PhpSpreadsheet\Exception
  44. */
  45. protected function readAsArray(string $filePath, string $writerType, int $sheetIndex = null)
  46. {
  47. $spreadsheet = $this->read($filePath, $writerType);
  48. if (null === $sheetIndex) {
  49. $sheet = $spreadsheet->getActiveSheet();
  50. } else {
  51. $sheet = $spreadsheet->getSheet($sheetIndex);
  52. }
  53. return $sheet->toArray();
  54. }
  55. /**
  56. * @param \Illuminate\Foundation\Application $app
  57. * @return array
  58. */
  59. protected function getPackageProviders($app)
  60. {
  61. return [
  62. ExcelServiceProvider::class,
  63. ];
  64. }
  65. /**
  66. * @param \Illuminate\Foundation\Application $app
  67. */
  68. protected function getEnvironmentSetUp($app)
  69. {
  70. $app['config']->set('filesystems.disks.local.root', __DIR__ . '/Data/Disks/Local');
  71. $app['config']->set('filesystems.disks.test', [
  72. 'driver' => 'local',
  73. 'root' => __DIR__ . '/Data/Disks/Test',
  74. ]);
  75. $app['config']->set('database.default', 'testing');
  76. $app['config']->set('database.connections.testing', [
  77. 'driver' => 'mysql',
  78. 'host' => env('DB_HOST'),
  79. 'port' => env('DB_PORT'),
  80. 'database' => env('DB_DATABASE'),
  81. 'username' => env('DB_USERNAME'),
  82. 'password' => env('DB_PASSWORD'),
  83. ]);
  84. $app['config']->set('view.paths', [
  85. __DIR__ . '/Data/Stubs/Views',
  86. ]);
  87. }
  88. /**
  89. * @param Job $job
  90. * @param string $property
  91. * @return mixed
  92. */
  93. protected function inspectJobProperty(Job $job, string $property)
  94. {
  95. $dict = (array) unserialize($job->payload()['data']['command']);
  96. $class = $job->resolveName();
  97. return $dict[$property] ?? $dict["\0*\0$property"] ?? $dict["\0$class\0$property"];
  98. }
  99. /**
  100. * @param string $needle
  101. * @param string $haystack
  102. * @param string $message
  103. */
  104. protected function assertStringContains(string $needle, string $haystack, string $message = '')
  105. {
  106. if (method_exists($this, 'assertStringContainsString')) {
  107. $this->assertStringContainsString($needle, $haystack, $message);
  108. } else {
  109. static::assertThat($haystack, new StringContains($needle, false), $message);
  110. }
  111. }
  112. /**
  113. * @param string $path
  114. */
  115. protected function assertFileMissing(string $path)
  116. {
  117. if (method_exists($this, 'assertFileDoesNotExist')) {
  118. $this->assertFileDoesNotExist($path);
  119. } else {
  120. $this->assertFileNotExists($path);
  121. }
  122. }
  123. protected function assertRegex(string $pattern, string $string)
  124. {
  125. if (method_exists($this, 'assertMatchesRegularExpression')) {
  126. $this->assertMatchesRegularExpression($pattern, $string);
  127. } else {
  128. $this->assertRegExp($pattern, $string);
  129. }
  130. }
  131. }