123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace Maatwebsite\Excel\Tests;
- use Illuminate\Contracts\Queue\Job;
- use Illuminate\Http\Testing\File;
- use Maatwebsite\Excel\ExcelServiceProvider;
- use Orchestra\Testbench\TestCase as OrchestraTestCase;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PHPUnit\Framework\Constraint\StringContains;
- class TestCase extends OrchestraTestCase
- {
- /**
- * @param string $filePath
- * @param string $writerType
- * @return \PhpOffice\PhpSpreadsheet\Spreadsheet
- *
- * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
- */
- public function read(string $filePath, string $writerType)
- {
- $reader = IOFactory::createReader($writerType);
- return $reader->load($filePath);
- }
- /**
- * @param string $filePath
- * @param string|null $filename
- * @return File
- */
- public function givenUploadedFile(string $filePath, string $filename = null): File
- {
- $filename = $filename ?? basename($filePath);
- // Create temporary file.
- $newFilePath = tempnam(sys_get_temp_dir(), 'import-');
- // Copy the existing file to a temporary file.
- copy($filePath, $newFilePath);
- return new File($filename, fopen($newFilePath, 'r'));
- }
- /**
- * @param string $filePath
- * @param string $writerType
- * @param int|null $sheetIndex
- * @return array
- *
- * @throws \PhpOffice\PhpSpreadsheet\Exception
- */
- protected function readAsArray(string $filePath, string $writerType, int $sheetIndex = null)
- {
- $spreadsheet = $this->read($filePath, $writerType);
- if (null === $sheetIndex) {
- $sheet = $spreadsheet->getActiveSheet();
- } else {
- $sheet = $spreadsheet->getSheet($sheetIndex);
- }
- return $sheet->toArray();
- }
- /**
- * @param \Illuminate\Foundation\Application $app
- * @return array
- */
- protected function getPackageProviders($app)
- {
- return [
- ExcelServiceProvider::class,
- ];
- }
- /**
- * @param \Illuminate\Foundation\Application $app
- */
- protected function getEnvironmentSetUp($app)
- {
- $app['config']->set('filesystems.disks.local.root', __DIR__ . '/Data/Disks/Local');
- $app['config']->set('filesystems.disks.test', [
- 'driver' => 'local',
- 'root' => __DIR__ . '/Data/Disks/Test',
- ]);
- $app['config']->set('database.default', 'testing');
- $app['config']->set('database.connections.testing', [
- 'driver' => 'mysql',
- 'host' => env('DB_HOST'),
- 'port' => env('DB_PORT'),
- 'database' => env('DB_DATABASE'),
- 'username' => env('DB_USERNAME'),
- 'password' => env('DB_PASSWORD'),
- ]);
- $app['config']->set('view.paths', [
- __DIR__ . '/Data/Stubs/Views',
- ]);
- }
- /**
- * @param Job $job
- * @param string $property
- * @return mixed
- */
- protected function inspectJobProperty(Job $job, string $property)
- {
- $dict = (array) unserialize($job->payload()['data']['command']);
- $class = $job->resolveName();
- return $dict[$property] ?? $dict["\0*\0$property"] ?? $dict["\0$class\0$property"];
- }
- /**
- * @param string $needle
- * @param string $haystack
- * @param string $message
- */
- protected function assertStringContains(string $needle, string $haystack, string $message = '')
- {
- if (method_exists($this, 'assertStringContainsString')) {
- $this->assertStringContainsString($needle, $haystack, $message);
- } else {
- static::assertThat($haystack, new StringContains($needle, false), $message);
- }
- }
- /**
- * @param string $path
- */
- protected function assertFileMissing(string $path)
- {
- if (method_exists($this, 'assertFileDoesNotExist')) {
- $this->assertFileDoesNotExist($path);
- } else {
- $this->assertFileNotExists($path);
- }
- }
- protected function assertRegex(string $pattern, string $string)
- {
- if (method_exists($this, 'assertMatchesRegularExpression')) {
- $this->assertMatchesRegularExpression($pattern, $string);
- } else {
- $this->assertRegExp($pattern, $string);
- }
- }
- }
|