|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Encore\Admin\Auth\Database\Administrator; |
|
4
|
|
|
use Illuminate\Support\Facades\File; |
|
|
|
|
|
|
5
|
|
|
use Tests\Models\File as FileModel; |
|
6
|
|
|
|
|
7
|
|
|
class FileUploadTest extends TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
public function setUp() |
|
10
|
|
|
{ |
|
11
|
|
|
parent::setUp(); |
|
12
|
|
|
|
|
13
|
|
|
$this->be(Administrator::first(), 'admin'); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
View Code Duplication |
public function testFileUploadPage() |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
$this->visit('admin/files/create') |
|
19
|
|
|
->see('Upload file') |
|
20
|
|
|
->seeInElement('h3[class=box-title]', 'Create') |
|
21
|
|
|
->seeElement('input[name=file1]') |
|
22
|
|
|
->seeElement('input[name=file2]') |
|
23
|
|
|
->seeElement('input[name=file3]') |
|
24
|
|
|
->seeElement('input[name=file4]') |
|
25
|
|
|
->seeElement('input[name=file5]') |
|
26
|
|
|
->seeElement('input[name=file6]') |
|
27
|
|
|
// ->seeInElement('a[href="/admin/files"]', 'List') |
|
28
|
|
|
->seeInElement('button[type=reset]', 'Reset') |
|
29
|
|
|
->seeInElement('button[type=submit]', 'Submit'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function uploadFiles() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->visit('admin/files/create') |
|
35
|
|
|
->attach(__DIR__.'/AuthTest.php', 'file1') |
|
36
|
|
|
->attach(__DIR__.'/InstallTest.php', 'file2') |
|
37
|
|
|
->attach(__DIR__.'/IndexTest.php', 'file3') |
|
38
|
|
|
->attach(__DIR__.'/LaravelTest.php', 'file4') |
|
39
|
|
|
->attach(__DIR__.'/routes.php', 'file5') |
|
40
|
|
|
->attach(__DIR__.'/migrations/2016_11_22_093148_create_test_tables.php', 'file6') |
|
41
|
|
|
->press('Submit'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testUploadFile() |
|
45
|
|
|
{ |
|
46
|
|
|
File::cleanDirectory(public_path('uploads/files')); |
|
47
|
|
|
|
|
48
|
|
|
$this->uploadFiles() |
|
49
|
|
|
->seePageIs('admin/files'); |
|
50
|
|
|
|
|
51
|
|
|
$this->assertEquals(FileModel::count(), 1); |
|
52
|
|
|
|
|
53
|
|
|
$where = [ |
|
54
|
|
|
'file1' => 'files/AuthTest.php', |
|
55
|
|
|
'file2' => 'files/InstallTest.php', |
|
56
|
|
|
'file3' => 'files/IndexTest.php', |
|
57
|
|
|
'file4' => 'files/LaravelTest.php', |
|
58
|
|
|
'file5' => 'files/routes.php', |
|
59
|
|
|
'file6' => 'files/2016_11_22_093148_create_test_tables.php', |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
$this->seeInDatabase('test_files', $where); |
|
63
|
|
|
|
|
64
|
|
|
$files = FileModel::first()->toArray(); |
|
65
|
|
|
|
|
66
|
|
|
foreach (range(1, 6) as $index) { |
|
67
|
|
|
$this->assertFileExists(public_path('uploads/'.$files['file'.$index])); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
File::cleanDirectory(public_path('uploads/files')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
View Code Duplication |
public function testUpdateFile() |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
File::cleanDirectory(public_path('uploads/files')); |
|
76
|
|
|
|
|
77
|
|
|
$this->uploadFiles(); |
|
78
|
|
|
|
|
79
|
|
|
$old = FileModel::first(); |
|
80
|
|
|
|
|
81
|
|
|
$this->visit('admin/files/1/edit') |
|
82
|
|
|
->see('ID') |
|
83
|
|
|
->see('Created At') |
|
84
|
|
|
->see('Updated At') |
|
85
|
|
|
->seeElement('input[name=file1]') |
|
86
|
|
|
->seeElement('input[name=file2]') |
|
87
|
|
|
->seeElement('input[name=file3]') |
|
88
|
|
|
->seeElement('input[name=file4]') |
|
89
|
|
|
->seeElement('input[name=file5]') |
|
90
|
|
|
->seeElement('input[name=file6]') |
|
91
|
|
|
// ->seeInElement('a[href="/admin/files"]', 'List') |
|
92
|
|
|
->seeInElement('button[type=reset]', 'Reset') |
|
93
|
|
|
->seeInElement('button[type=submit]', 'Submit'); |
|
94
|
|
|
|
|
95
|
|
|
$this->attach(__DIR__.'/RolesTest.php', 'file3') |
|
96
|
|
|
->attach(__DIR__.'/MenuTest.php', 'file4') |
|
97
|
|
|
->attach(__DIR__.'/TestCase.php', 'file5') |
|
98
|
|
|
->press('Submit'); |
|
99
|
|
|
|
|
100
|
|
|
$new = FileModel::first(); |
|
101
|
|
|
|
|
102
|
|
|
$this->assertEquals($old->id, $new->id); |
|
103
|
|
|
$this->assertEquals($old->file1, $new->file1); |
|
104
|
|
|
$this->assertEquals($old->file2, $new->file2); |
|
105
|
|
|
$this->assertEquals($old->file6, $new->file6); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertNotEquals($old->file3, $new->file3); |
|
108
|
|
|
$this->assertNotEquals($old->file4, $new->file4); |
|
109
|
|
|
$this->assertNotEquals($old->file5, $new->file5); |
|
110
|
|
|
|
|
111
|
|
|
File::cleanDirectory(public_path('uploads/files')); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
View Code Duplication |
public function testDeleteFiles() |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
File::cleanDirectory(public_path('uploads/files')); |
|
117
|
|
|
|
|
118
|
|
|
$this->uploadFiles(); |
|
119
|
|
|
|
|
120
|
|
|
$this->visit('admin/files') |
|
121
|
|
|
->seeInElement('td', 1); |
|
122
|
|
|
|
|
123
|
|
|
$files = FileModel::first()->toArray(); |
|
124
|
|
|
|
|
125
|
|
|
$this->delete('admin/files/1') |
|
126
|
|
|
->dontSeeInDatabase('test_files', ['id' => 1]); |
|
127
|
|
|
|
|
128
|
|
|
foreach (range(1, 6) as $index) { |
|
129
|
|
|
$this->assertFileNotExists(public_path('uploads/'.$files['file'.$index])); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$this->visit('admin/files') |
|
133
|
|
|
->seeInElement('td', 'svg'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testBatchDelete() |
|
137
|
|
|
{ |
|
138
|
|
|
File::cleanDirectory(public_path('uploads/files')); |
|
139
|
|
|
|
|
140
|
|
|
$this->uploadFiles(); |
|
141
|
|
|
$this->uploadFiles(); |
|
142
|
|
|
$this->uploadFiles(); |
|
143
|
|
|
|
|
144
|
|
|
$this->visit('admin/files') |
|
145
|
|
|
->seeInElement('td', 1) |
|
146
|
|
|
->seeInElement('td', 2) |
|
147
|
|
|
->seeInElement('td', 3); |
|
148
|
|
|
|
|
149
|
|
|
$fi = new FilesystemIterator(public_path('uploads/files'), FilesystemIterator::SKIP_DOTS); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertEquals(iterator_count($fi), 18); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertEquals(FileModel::count(), 3); |
|
154
|
|
|
|
|
155
|
|
|
$this->delete('admin/files/1,2,3'); |
|
156
|
|
|
|
|
157
|
|
|
$this->assertEquals(FileModel::count(), 0); |
|
158
|
|
|
|
|
159
|
|
|
$this->visit('admin/files') |
|
160
|
|
|
->seeInElement('td', 'svg'); |
|
161
|
|
|
|
|
162
|
|
|
$this->assertEquals(iterator_count($fi), 0); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: