Completed
Pull Request — master (#4037)
by Muhlis
02:52
created

FileUploadTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Encore\Admin\Auth\Database\Administrator;
4
use Illuminate\Support\Facades\File;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, File.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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