Test Failed
Branch develop (5056e3)
by Abhishek Kumar
05:17
created

ImportTest::fail_file_upload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Tests;
4
5
use Tests\TestCase;
0 ignored issues
show
Bug introduced by
The type Tests\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Http\File;
7
use Illuminate\Http\UploadedFile;
8
use Illuminate\Foundation\Testing\RefreshDatabase;
9
use Illuminate\Foundation\Testing\WithoutMiddleware;
10
use Illuminate\Support\Facades\Queue;
11
use Illuminate\Support\Facades\Route;
12
use Illuminate\Support\Facades\Storage;
13
use Ladybirdweb\ImportExport\Facades\Import;
14
use Ladybirdweb\ImportExport\Models\Import as ModelImport;
15
16
class ImportTest extends TestCase
17
{
18
	use RefreshDatabase;
19
20
	protected $import;
21
22
	protected function setUp ()
23
	{
24
	    parent::setUp();
25
26
	    Route::middleware('web')->group(function() {
27
28
		    Route::get('ticket/import', ['as' => 'ticket.import', 'uses' => 'Ladybirdweb\ImportExport\Import@showImportForm'] );
29
		    Route::post('ticket/import', ['as' => 'ticket.import', 'uses' => 'Ladybirdweb\ImportExport\Import@uploadImportFile'] );
30
31
			Route::get('/ticket/import/{id}/map', [ 'as' => 'ticket.import.show.map', 'uses' => 'Ladybirdweb\ImportExport\Import@showColumnsMapForm']);
32
			Route::post('/ticket/import/{id}', [ 'as' => 'ticket.import.map', 'uses' => 'Ladybirdweb\ImportExport\Import@storeColumnsMap']);
33
34
			Route::get( '/import/{id}/progress', [ 'as' => 'ladybirdweb.import.ajax.progress', 'uses' => 'Ladybirdweb\ImportExport\Import@returnImportProgress']);
35
36
		});
37
38
		Storage::putFileAs('imports', new File( storage_path( 'test/test.csv' ) ), 'test.csv');
39
40
		$this->import = ModelImport::create([
41
			'file' => 'imports/test.csv',
42
			'file_rows' => 104,
43
			'db_cols' => [ 'name', 'email', 'password'],
44
			'model_map' => ['email', 'name', 'password']
45
		]);
46
	}
47
48
	/**
49
	* @test
50
	*/
51
	public function see_import_form()
52
	{
53
		Import::setUploadRoute('ticket.import');
0 ignored issues
show
Bug introduced by
The method setUploadRoute() does not exist on Ladybirdweb\ImportExport\Facades\Import. 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

53
		Import::/** @scrutinizer ignore-call */ 
54
          setUploadRoute('ticket.import');
Loading history...
54
55
		$response = $this->get('ticket/import');
56
57
		$response->assertSee( 'importer' );
58
	}
59
60
	/**
61
	* @test
62
	*/
63
	public function try_file_upload()
64
	{
65
		Storage::fake('file');
66
67
        $response = $this->json( 'POST', '/ticket/import', [
68
            'file' => UploadedFile::fake()->create('file.csv', 100)
69
        ]);
70
71
        $response->assertStatus(200);
72
73
        $response->assertJsonFragment( ['status' => 'ready'] );
74
	}
75
76
	/**
77
	* @test
78
	*/
79
	public function fail_file_upload()
80
	{
81
		Storage::fake('file');
82
83
        $response = $this->json( 'POST', '/ticket/import', [
84
            'file' => UploadedFile::fake()->image('file.jpg')
85
        ]);
86
87
        $response->assertStatus(200);
88
89
        $response->assertJsonFragment( ['status' => 'failed'] );
90
91
        $this->assertArrayHasKey( 'errors', Import::getImportErrors() );
0 ignored issues
show
Bug introduced by
The method getImportErrors() does not exist on Ladybirdweb\ImportExport\Facades\Import. 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

91
        $this->assertArrayHasKey( 'errors', Import::/** @scrutinizer ignore-call */ getImportErrors() );
Loading history...
92
	}
93
94
	/**
95
	* @test
96
	*/
97
	public function map_data_with_csv_cols()
98
	{
99
		Import::setImportMapRoute('ticket.import.map');
0 ignored issues
show
Bug introduced by
The method setImportMapRoute() does not exist on Ladybirdweb\ImportExport\Facades\Import. 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

99
		Import::/** @scrutinizer ignore-call */ 
100
          setImportMapRoute('ticket.import.map');
Loading history...
100
101
		$id = $this->import->id;
102
103
		$response = $this->get('/ticket/import/' . $id . '/map');
104
105
		$response->assertStatus(200);
106
107
		$response->assertSee( 'Confirm Import' );
108
	}
109
110
	/**
111
	* @test
112
	*/
113
	public function store_data_map_with_csv_cols()
114
	{
115
		$id = $this->import->id;
116
117
		$response = $this->post( '/ticket/import/' . $id, [
118
			'db_column' => ['email', 'name', 'password']
119
		] );
120
121
		$response->assertStatus(200);
122
123
		$response->assertSessionMissing( 'errors' );
124
	}
125
126
	/**
127
	* @test
128
	*/
129
	public function store_data_map_with_csv_cols_failed_validation()
130
	{
131
		$id = $this->import->id;
132
133
		$response = $this->post( '/ticket/import/' . $id, [
134
			'db_column' => ['name', 'name', 'name']
135
		] );
136
137
		$response->assertStatus(302);
138
139
		$response->assertSessionHas( 'errors' );
140
	}
141
142
	/**
143
	* @test
144
	*/
145
	public function sucess_to_dispatch_given_job_class()
146
	{
147
		$import = $id = $this->import;
0 ignored issues
show
Unused Code introduced by
The assignment to $id is dead and can be removed.
Loading history...
148
149
		Queue::fake();
150
151
		Import::dispatchImportJob( FakeJob::class, $import );
0 ignored issues
show
Bug introduced by
The method dispatchImportJob() does not exist on Ladybirdweb\ImportExport\Facades\Import. 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

151
		Import::/** @scrutinizer ignore-call */ 
152
          dispatchImportJob( FakeJob::class, $import );
Loading history...
152
153
		Queue::assertPushedOn('importing', FakeJob::class);
154
	}
155
156
	/**
157
	* @test
158
	*/
159
	public function check_import_progress()
160
	{
161
		$response = $this->json( 'GET', '/import/1/progress' );
162
163
		$response->assertStatus(200);
164
165
		$response->assertJsonFragment( ['status' => 200] );
166
167
		$response->assertJsonFragment( ['progress'] );
168
	}
169
}
170