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

ImageUploadTest::testUpdateImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39

Duplication

Lines 39
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 39
loc 39
rs 9.296
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\Image;
6
use Tests\Models\MultipleImage;
7
8
class ImageUploadTest extends TestCase
9
{
10
    public function setUp()
11
    {
12
        parent::setUp();
13
14
        $this->be(Administrator::first(), 'admin');
15
    }
16
17
    public function testDisableFilter()
18
    {
19
        $this->visit('admin/images')
20
            ->dontSeeElement('input[name=id]');
21
    }
22
23 View Code Duplication
    public function testImageUploadPage()
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...
24
    {
25
        $this->visit('admin/images/create')
26
            ->see('Upload image')
27
            ->seeInElement('h3[class=box-title]', 'Create')
28
            ->seeElement('input[name=image1]')
29
            ->seeElement('input[name=image2]')
30
            ->seeElement('input[name=image3]')
31
            ->seeElement('input[name=image4]')
32
            ->seeElement('input[name=image5]')
33
            ->seeElement('input[name=image6]')
34
            ->seeInElement('button[type=reset]', 'Reset')
35
            ->seeInElement('button[type=submit]', 'Submit');
36
    }
37
38
    protected function uploadImages()
39
    {
40
        return $this->visit('admin/images/create')
41
            ->attach(__DIR__.'/assets/test.jpg', 'image1')
42
            ->attach(__DIR__.'/assets/test.jpg', 'image2')
43
            ->attach(__DIR__.'/assets/test.jpg', 'image3')
44
            ->attach(__DIR__.'/assets/test.jpg', 'image4')
45
            ->attach(__DIR__.'/assets/test.jpg', 'image5')
46
            ->attach(__DIR__.'/assets/test.jpg', 'image6')
47
            ->press('Submit');
48
    }
49
50
    public function testUploadImage()
51
    {
52
        File::cleanDirectory(public_path('uploads/images'));
53
54
        $this->uploadImages()
55
            ->seePageIs('admin/images');
56
57
        $this->assertEquals(Image::count(), 1);
58
59
        $this->seeInDatabase('test_images', ['image4' => 'images/renamed.jpeg']);
60
61
        $images = Image::first()->toArray();
62
63
        foreach (range(1, 6) as $index) {
64
            $this->assertFileExists(public_path('uploads/'.$images['image'.$index]));
65
        }
66
67
        $this->assertFileExists(public_path('uploads/images/asdasdasdasdasd.jpeg'));
68
69
        File::cleanDirectory(public_path('uploads/images'));
70
    }
71
72
    public function testRemoveImage()
73
    {
74
        File::cleanDirectory(public_path('uploads/images'));
75
76
        $this->uploadImages();
77
78
        $this->assertEquals($this->fileCountInImageDir(), 6);
79
    }
80
81 View Code Duplication
    public function testUpdateImage()
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...
82
    {
83
        File::cleanDirectory(public_path('uploads/images'));
84
85
        $this->uploadImages();
86
87
        $old = Image::first();
88
89
        $this->visit('admin/images/1/edit')
90
            ->see('ID')
91
            ->see('Created At')
92
            ->see('Updated At')
93
            ->seeElement('input[name=image1]')
94
            ->seeElement('input[name=image2]')
95
            ->seeElement('input[name=image3]')
96
            ->seeElement('input[name=image4]')
97
            ->seeElement('input[name=image5]')
98
            ->seeElement('input[name=image6]')
99
            ->seeInElement('button[type=reset]', 'Reset')
100
            ->seeInElement('button[type=submit]', 'Submit');
101
102
        $this->attach(__DIR__.'/assets/test.jpg', 'image3')
103
            ->attach(__DIR__.'/assets/test.jpg', 'image4')
104
            ->attach(__DIR__.'/assets/test.jpg', 'image5')
105
            ->press('Submit');
106
107
        $new = Image::first();
108
109
        $this->assertEquals($old->id, $new->id);
110
        $this->assertEquals($old->image1, $new->image1);
111
        $this->assertEquals($old->image2, $new->image2);
112
        $this->assertEquals($old->image6, $new->image6);
113
114
        $this->assertNotEquals($old->image3, $new->image3);
115
        $this->assertNotEquals($old->image4, $new->image4);
116
        $this->assertNotEquals($old->image5, $new->image5);
117
118
        File::cleanDirectory(public_path('uploads/images'));
119
    }
120
121 View Code Duplication
    public function testDeleteImages()
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...
122
    {
123
        File::cleanDirectory(public_path('uploads/images'));
124
125
        $this->uploadImages();
126
127
        $this->visit('admin/images')
128
            ->seeInElement('td', 1);
129
130
        $images = Image::first()->toArray();
131
132
        $this->delete('admin/images/1')
133
            ->dontSeeInDatabase('test_images', ['id' => 1]);
134
135
        foreach (range(1, 6) as $index) {
136
            $this->assertFileNotExists(public_path('uploads/'.$images['image'.$index]));
137
        }
138
139
        $this->visit('admin/images')
140
            ->seeInElement('td', 'svg');
141
    }
142
143
    public function testBatchDelete()
144
    {
145
        File::cleanDirectory(public_path('uploads/images'));
146
147
        $this->uploadImages();
148
        $this->uploadImages();
149
        $this->uploadImages();
150
151
        $this->visit('admin/images')
152
            ->seeInElement('td', 1)
153
            ->seeInElement('td', 2)
154
            ->seeInElement('td', 3);
155
156
        $this->assertEquals($this->fileCountInImageDir(), 18);
157
158
        $this->assertEquals(Image::count(), 3);
159
160
        $this->delete('admin/images/1,2,3');
161
162
        $this->assertEquals(Image::count(), 0);
163
164
        $this->visit('admin/images')
165
            ->seeInElement('td', 'svg');
166
167
        $this->assertEquals($this->fileCountInImageDir(), 0);
168
    }
169
170
    public function testUploadMultipleImage()
171
    {
172
        File::cleanDirectory(public_path('uploads/images'));
173
174
        $this->visit('admin/multiple-images/create')
175
            ->seeElement('input[type=file][name="pictures[]"][multiple=1]');
176
177
        $path = __DIR__.'/assets/test.jpg';
178
179
        $file = new \Illuminate\Http\UploadedFile(
180
            $path, 'test.jpg', 'image/jpeg', filesize($path), null, true
181
        );
182
183
        $size = rand(10, 20);
184
        $files = ['pictures' => array_pad([], $size, $file)];
185
186
        $this->call(
187
            'POST', // $method
188
            '/admin/multiple-images', // $action
189
            [], // $parameters
190
            [],
191
            $files
192
        );
193
194
        $this->assertResponseStatus(302);
195
        $this->assertRedirectedTo('/admin/multiple-images');
196
197
        $this->assertEquals($this->fileCountInImageDir(), $size);
198
199
        $pictures = MultipleImage::first()->pictures;
200
201
        $this->assertCount($size, $pictures);
202
203
        foreach ($pictures as $picture) {
204
            $this->assertFileExists(public_path('uploads/'.$picture));
205
        }
206
    }
207
208
    public function testRemoveMultipleFiles()
209
    {
210
        File::cleanDirectory(public_path('uploads/images'));
211
212
        // upload files
213
        $path = __DIR__.'/assets/test.jpg';
214
215
        $file = new \Illuminate\Http\UploadedFile(
216
            $path, 'test.jpg', 'image/jpeg', filesize($path), null, true
217
        );
218
219
        $size = rand(10, 20);
220
        $files = ['pictures' => array_pad([], $size, $file)];
221
222
        $this->call(
223
            'POST', // $method
224
            '/admin/multiple-images', // $action
225
            [], // $parameters
226
            [],
227
            $files
228
        );
229
230
        $this->assertEquals($this->fileCountInImageDir(), $size);
231
    }
232
233
    protected function fileCountInImageDir($dir = 'uploads/images')
234
    {
235
        $file = new FilesystemIterator(public_path($dir), FilesystemIterator::SKIP_DOTS);
236
237
        return iterator_count($file);
238
    }
239
}
240