Completed
Push — 2.0 ( 60b181...751b46 )
by Nicolas
03:03
created

it_can_create_file_from_uploadedfile()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace Modules\Media\Tests;
4
5
use Mockery;
6
use Modules\Media\Entities\File;
7
use Modules\Media\Repositories\FileRepository;
8
use Symfony\Component\Finder\SplFileInfo;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
11
class FileRepositoryTest extends MediaTestCase
12
{
13
    /**
14
     * @var FileRepository
15
     */
16
    private $file;
17
18
    public function setUp()
19
    {
20
        parent::setUp();
21
22
        $this->resetDatabase();
23
24
        $this->file = app(FileRepository::class);
25
    }
26
27
    /** @test */
28
    public function it_can_update_file_info()
29
    {
30
        $file = $this->createFile();
31
32
        $this->file->update($file, [
33
            'en' => [
34
                'description' => 'My description',
35
                'alt_attribute' => 'My alt attribute',
36
                'keywords' => 'keyword1, keyword2',
37
            ],
38
        ]);
39
40
        $file = $this->file->find(1);
41
42
        $this->assertEquals('My description', $file->description);
43
        $this->assertEquals('My alt attribute', $file->alt_attribute);
44
        $this->assertEquals('keyword1, keyword2', $file->keywords);
45
    }
46
47
    /** @test */
48
    public function it_can_create_file_from_uploadedfile()
49
    {
50
        $uploadedFile = Mockery::mock(UploadedFile::class);
51
        $fileInfo = Mockery::mock(SplFileInfo::class);
52
53
        $fileInfo->shouldReceive('getSize')
54
            ->andReturn(1024)
55
            ->once();
56
57
        $uploadedFile->shouldReceive('getClientOriginalName')
58
            ->andReturn('my-file.jpg')
59
            ->once();
60
        $uploadedFile->shouldReceive('getClientMimeType')
61
            ->andReturn('image/jpg')
62
            ->once();
63
        $uploadedFile->shouldReceive('getFileInfo')
64
            ->andReturn($fileInfo)
65
            ->once();
66
67
        $this->file->createFromFile($uploadedFile);
68
69
        $file = $this->file->find(1);
70
71
        $this->assertCount(1, $this->file->all());
72
        $this->assertEquals('my-file.jpg', $file->filename);
73
        $this->assertEquals('jpg', $file->extension);
74
        $this->assertEquals('image/jpg', $file->mimetype);
75
        $this->assertEquals('1024', $file->filesize);
76
    }
77
78
    /** @test */
79
    public function it_can_increment_file_name_version_if_name_exists()
80
    {
81
        $this->createFile('my-file.jpg');
82
83
        $uploadedFile = Mockery::mock(UploadedFile::class);
84
        $fileInfo = Mockery::mock(SplFileInfo::class);
85
86
        $fileInfo->shouldReceive('getSize')
87
            ->andReturn(1024)
88
            ->once();
89
90
        $uploadedFile->shouldReceive('getClientOriginalName')
91
            ->andReturn('my-file.jpg')
92
            ->once();
93
        $uploadedFile->shouldReceive('getClientMimeType')
94
            ->andReturn('image/jpg')
95
            ->once();
96
        $uploadedFile->shouldReceive('getFileInfo')
97
            ->andReturn($fileInfo)
98
            ->once();
99
100
        $this->file->createFromFile($uploadedFile);
101
102
        $file = $this->file->find(2);
103
104
        $this->assertEquals('my-file_1.jpg', $file->filename);
105
    }
106
107
    /** @test */
108
    public function it_can_delete_a_file()
109
    {
110
        $file = $this->createFile();
111
112
        $this->file->destroy($file);
113
114
        $this->assertCount(0, $this->file->all());
115
    }
116
117 View Code Duplication
    private function createFile($fileName = 'random/name.jpg')
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...
118
    {
119
        return File::create([
120
            'filename' => $fileName,
121
            'path' => config('asgard.media.config.files-path') . $fileName,
122
            'extension' => substr(strrchr($fileName, "."), 1),
123
            'mimetype' => 'image/jpg',
124
            'filesize' => '1024',
125
            'folder_id' => 0,
126
        ]);
127
    }
128
129
    private function resetDatabase()
130
    {
131
        // Relative to the testbench app folder: vendors/orchestra/testbench/src/fixture
132
        $migrationsPath = 'Database/Migrations';
133
        // Makes sure the migrations table is created
134
        $this->artisan('migrate', [
135
            '--database' => 'sqlite',
136
            '--path'     => $migrationsPath,
137
        ]);
138
        // We empty all tables
139
        $this->artisan('migrate:reset', [
140
            '--database' => 'sqlite',
141
        ]);
142
        // Migrate
143
        $this->artisan('migrate', [
144
            '--database' => 'sqlite',
145
            '--path'     => $migrationsPath,
146
        ]);
147
148
        $this->artisan('migrate', [
149
            '--database' => 'sqlite',
150
            '--path'     => 'Modules/Tag/Database/Migrations',
151
        ]);
152
    }
153
}
154