|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Orchestra\Testbench\TestCase; |
|
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 getPackageProviders($app) |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
return ['Ladybirdweb\ImportExport\ImportExportServiceProvider']; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
protected function setUp () |
|
28
|
|
|
{ |
|
29
|
|
|
parent::setUp(); |
|
30
|
|
|
|
|
31
|
|
|
$this->loadMigrationsFrom(__DIR__ . '/database/migrations'); |
|
32
|
|
|
|
|
33
|
|
|
Route::middleware('web')->group(function() { |
|
34
|
|
|
|
|
35
|
|
|
Route::get( '/import/{id}/progress', [ 'as' => 'ladybirdweb.import.ajax.progress', 'uses' => 'Ladybirdweb\ImportExport\Import@returnImportProgress']); |
|
36
|
|
|
|
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
Storage::putFileAs('imports', new File( storage_path( 'test/test.csv' ) ), 'test.csv'); |
|
40
|
|
|
|
|
41
|
|
|
$this->import = ModelImport::create([ |
|
42
|
|
|
'file' => 'imports/test.csv', |
|
43
|
|
|
'file_rows' => 104, |
|
44
|
|
|
'db_cols' => [ 'name', 'email', 'password'], |
|
45
|
|
|
'model_map' => ['email', 'name', 'password'] |
|
46
|
|
|
]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @test |
|
51
|
|
|
*/ |
|
52
|
|
|
public function create_new_import_success() |
|
53
|
|
|
{ |
|
54
|
|
|
$import = Import::createImport('imports/test.csv', [ 'name', 'email', 'password']); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
$this->assertInstanceOf(ModelImport::class, $import); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @test |
|
61
|
|
|
*/ |
|
62
|
|
|
public function fetch_import() |
|
63
|
|
|
{ |
|
64
|
|
|
$import = Import::getImport($this->import->id); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$this->assertInstanceOf(ModelImport::class, $import); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @test |
|
71
|
|
|
*/ |
|
72
|
|
|
public function get_few_rows_from_uploaded_file() |
|
73
|
|
|
{ |
|
74
|
|
|
$csv_data = Import::getImportFileData($this->import->id); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$this->assertInternalType( 'array', $csv_data ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @test |
|
81
|
|
|
*/ |
|
82
|
|
|
public function store_data_map_with_csv_cols() |
|
83
|
|
|
{ |
|
84
|
|
|
$import = Import::setDataMap(['email', 'name', 'password'], $this->import->id); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
$this->assertInstanceOf(ModelImport::class, $import); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @test |
|
91
|
|
|
*/ |
|
92
|
|
|
public function sucess_to_dispatch_given_job_class() |
|
93
|
|
|
{ |
|
94
|
|
|
$import = $id = $this->import; |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
Queue::fake(); |
|
97
|
|
|
|
|
98
|
|
|
Import::dispatchImportJob( FakeJob::class, $import ); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
Queue::assertPushedOn('importing', FakeJob::class); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @test |
|
105
|
|
|
*/ |
|
106
|
|
|
public function check_import_progress() |
|
107
|
|
|
{ |
|
108
|
|
|
$response = $this->json( 'GET', '/import/1/progress' ); |
|
109
|
|
|
|
|
110
|
|
|
$response->assertStatus(200); |
|
111
|
|
|
|
|
112
|
|
|
$response->assertJsonFragment( ['status' => 200] ); |
|
113
|
|
|
|
|
114
|
|
|
$response->assertJsonFragment( ['progress'] ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @test |
|
119
|
|
|
*/ |
|
120
|
|
|
public function remove_import() |
|
121
|
|
|
{ |
|
122
|
|
|
$this->assertTrue(Import::removeImport($this->import->id)); |
|
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|