-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathTestCase.php
151 lines (131 loc) · 4.28 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?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);
}
}
}