1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\File; |
6
|
|
|
use Orchestra\Testbench\TestCase; |
7
|
|
|
use Illuminate\Support\Facades\Queue; |
8
|
|
|
use Illuminate\Support\Facades\Route; |
9
|
|
|
use Illuminate\Support\Facades\Storage; |
10
|
|
|
use LWS\ImportExport\Facades\Export; |
11
|
|
|
use LWS\ImportExport\Jobs\ExportJob; |
12
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase; |
13
|
|
|
use LWS\ImportExport\Models\Export as ModelExport; |
14
|
|
|
|
15
|
|
|
class ExportTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
use RefreshDatabase; |
|
|
|
|
18
|
|
|
|
19
|
|
|
protected $export; |
20
|
|
|
|
21
|
|
|
protected function getPackageProviders($app) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
return ['LWS\ImportExport\ImportExportServiceProvider']; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function getEnvironmentSetUp($app) |
27
|
|
|
{ |
28
|
|
|
$app['config']->set('database.default', 'testing'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function setUp():void |
32
|
|
|
{ |
33
|
|
|
parent::setUp(); |
34
|
|
|
|
35
|
|
|
$this->artisan('migrate', ['--database' => 'testing']); |
36
|
|
|
|
37
|
|
|
Route::middleware('web')->group(function () { |
38
|
|
|
Route::get('/ticket/export/{id}', ['as' => 'ticket.export.progress', 'uses' => 'LWS\ImportExport\Export@showExportStatus']); |
39
|
|
|
|
40
|
|
|
Route::get('/export/{id}/download', ['as' => 'ladybirdweb.export.download', 'uses' => 'LWS\ImportExport\Export@downloadExportedFile']); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
Storage::putFileAs('exports', new File(__DIR__.'/storage/test/test.csv'), 'test.xls'); |
44
|
|
|
|
45
|
|
|
$this->export = ModelExport::create([ |
46
|
|
|
'file' => 'test.xls', |
47
|
|
|
'query' => User::select(['name', 'email', 'created_at'])->getModel(), |
48
|
|
|
'type' => 'xls', |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @test |
54
|
|
|
*/ |
55
|
|
|
public function data_export_initiated_and_dispatched() |
56
|
|
|
{ |
57
|
|
|
Queue::fake(); |
58
|
|
|
|
59
|
|
|
$export = Export::export(User::select(['name', 'email', 'created_at']), 'xls'); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->assertInstanceOf(ModelExport::class, $export); |
62
|
|
|
|
63
|
|
|
Queue::assertPushedOn('exporting', ExportJob::class); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
*/ |
69
|
|
|
public function check_export_progress_ajax() |
70
|
|
|
{ |
71
|
|
|
$response = $this->json('GET', '/export/'.$this->export->id.'/progress'); |
72
|
|
|
|
73
|
|
|
$response->assertStatus(200); |
74
|
|
|
$response->assertJsonFragment(['status' => 200]); |
75
|
|
|
|
76
|
|
|
$response->assertJsonFragment(['progress'=>0]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @test |
81
|
|
|
*/ |
82
|
|
|
public function try_download_exported_file() |
83
|
|
|
{ |
84
|
|
|
$response = $this->get('/export/'.$this->export->id.'/download'); |
85
|
|
|
|
86
|
|
|
$response->assertHeader('content-disposition', 'attachment; filename=test.xls'); |
87
|
|
|
|
88
|
|
|
$response->assertStatus(200); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @test |
93
|
|
|
*/ |
94
|
|
|
public function fail_download_exported_file() |
95
|
|
|
{ |
96
|
|
|
$response = $this->get('/export/987654321/download'); |
97
|
|
|
|
98
|
|
|
$response->assertStatus(404); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|