-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathValidationTest.php
126 lines (107 loc) · 3.63 KB
/
ValidationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use LaravelEnso\DataImport\Models\Import;
use LaravelEnso\Users\Models\User;
use Tests\TestCase;
class ValidationTest extends TestCase
{
use RefreshDatabase;
private const ImportType = 'userGroups';
private const Path = __DIR__.DIRECTORY_SEPARATOR.'testFiles'.DIRECTORY_SEPARATOR;
private const InvalidSheetsFile = 'invalid_sheets.xlsx';
private const InvalidColumnsFile = 'invalid_columns.xlsx';
private const TestFile = 'test.xlsx';
protected function setUp(): void
{
parent::setUp();
$this->seed()
->actingAs(User::first());
}
public function tearDown(): void
{
$this->cleanUp();
parent::tearDown();
}
/** @test */
public function stops_on_invalid_sheets()
{
config(['enso.imports.configs.userGroups' => [
'label' => 'User Groups',
'template' => $this->template('userGroups'),
]]);
$this->post(route('import.store', [], false), [
'import' => $this->file(self::InvalidSheetsFile),
'type' => self::ImportType,
])->assertStatus(200)
->assertJsonFragment([
'errors' => [
'Extra Sheets' => ['invalid_sheet'],
'Missing Sheets' => ['groups'],
],
'filename' => self::TestFile,
]);
$this->assertNull(Import::whereName(self::TestFile)->first());
}
/** @test */
public function stops_on_invalid_columns()
{
config(['enso.imports.configs.userGroups' => [
'label' => 'User Groups',
'template' => $this->template('userGroups'),
]]);
$this->post(route('import.store', [], false), [
'import' => $this->file(self::InvalidColumnsFile),
'type' => self::ImportType,
])->assertStatus(200)
->assertJsonFragment([
'errors' => [
'Extra Columns' => ['Sheet "groups", column "invalid_column"'],
'Missing Columns' => ['Sheet "groups", column "description"'],
],
'filename' => self::TestFile,
]);
$this->assertNull(Import::whereName(self::TestFile)->first());
}
/** @test */
public function cannot_import_invalid_params()
{
Config::set(['enso.imports.configs.userGroups' => [
'label' => 'User Groups',
'template' => $this->template('paramsValidation'),
]]);
$exception = $this->post(route('import.store', [], false), [
'import' => $this->file('invalid_sheets.xlsx'),
'type' => self::ImportType,
])->exception;
$this->assertInstanceOf(ValidationException::class, $exception);
$this->assertArrayHasKey('name', $exception->errors());
}
private function file($file)
{
File::copy(
self::Path.$file,
self::Path.self::TestFile
);
return new UploadedFile(
self::Path.self::TestFile,
self::TestFile,
null,
null,
true
);
}
private function cleanUp()
{
File::delete(self::Path.self::TestFile);
}
protected function template($template): string
{
return Str::replaceFirst(base_path(), '', __DIR__.DIRECTORY_SEPARATOR.'templates'
.DIRECTORY_SEPARATOR."{$template}.json");
}
}