Completed
Push — master ( dab270...a45a25 )
by Abhishek Kumar
08:02
created

ExportTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
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 Ladybirdweb\ImportExport\Facades\Export;
11
use Ladybirdweb\ImportExport\Jobs\ExportJob;
12
use Illuminate\Foundation\Testing\RefreshDatabase;
13
use Ladybirdweb\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
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 ['Ladybirdweb\ImportExport\ImportExportServiceProvider'];
24
    }
25
26
    protected function getEnvironmentSetUp($app)
27
    {
28
        $app['config']->set('database.default', 'testing');
29
    }
30
31
    protected function setUp()
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' => 'Ladybirdweb\ImportExport\Export@showExportStatus']);
39
40
            Route::get('/export/{id}/download', ['as' => 'ladybirdweb.export.download', 'uses' => 'Ladybirdweb\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 Ladybirdweb\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
75
        $response->assertJsonFragment(['status' => 200]);
76
77
        $response->assertJsonFragment(['progress']);
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function try_download_exported_file()
84
    {
85
        $response = $this->get('/export/'.$this->export->id.'/download');
86
87
        $response->assertHeader('content-disposition', 'attachment; filename=test.xls');
88
89
        $response->assertStatus(200);
90
    }
91
92
    /**
93
     * @test
94
     */
95
    public function fail_download_exported_file()
96
    {
97
        $response = $this->get('/export/987654321/download');
98
99
        $response->assertStatus(404);
100
    }
101
}
102