ExportTest::check_export_progress_ajax()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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;
0 ignored issues
show
introduced by
The trait Illuminate\Foundation\Testing\RefreshDatabase requires some properties which are not provided by Tests\ExportTest: $connectionsToTransact, $dropViews, $dropTypes
Loading history...
18
19
    protected $export;
20
21
    protected function getPackageProviders($app)
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

21
    protected function getPackageProviders(/** @scrutinizer ignore-unused */ $app)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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');
0 ignored issues
show
Bug introduced by
The method export() does not exist on LWS\ImportExport\Facades\Export. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
        /** @scrutinizer ignore-call */ 
60
        $export = Export::export(User::select(['name', 'email', 'created_at']), 'xls');
Loading history...
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