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