/
FileUploaderTest.php
159 lines (125 loc) · 4.75 KB
/
FileUploaderTest.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
* @copyright 2015 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Mautic\CoreBundle\Tests\Helper;
use Mautic\CoreBundle\Exception\FilePathException;
use Mautic\CoreBundle\Exception\FileUploadException;
use Mautic\CoreBundle\Helper\FilePathResolver;
use Mautic\CoreBundle\Helper\FileUploader;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUploaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @testdox Uploader uploads files correctly
*
* @covers \Mautic\CoreBundle\Helper\FileUploader::upload
*/
public function testSuccessfulUpload()
{
$uploadDir = 'my/upload/dir';
$fileName = 'MyfileName';
$filePathResolverMock = $this->getMockBuilder(FilePathResolver::class)
->disableOriginalConstructor()
->getMock();
$fileMock = $this->getMockBuilder(UploadedFile::class)
->disableOriginalConstructor()
->getMock();
$fileMock->expects($this->once())
->method('move')
->with($uploadDir, $fileName);
$filePathResolverMock->expects($this->once())
->method('getUniqueFileName')
->with($uploadDir, $fileMock)
->willReturn($fileName);
$filePathResolverMock->expects($this->once())
->method('createDirectory')
->with($uploadDir);
$fileUploader = new FileUploader($filePathResolverMock);
$fileUploader->upload($uploadDir, $fileMock);
}
/**
* @testdox Throw an Exception if Uploader could not create directory
*
* @covers \Mautic\CoreBundle\Helper\FileUploader::upload
*/
public function testCouldNotCreateDirectory()
{
$uploadDir = 'my/upload/dir';
$fileName = 'MyfileName';
$filePathResolverMock = $this->getMockBuilder(FilePathResolver::class)
->disableOriginalConstructor()
->getMock();
$fileMock = $this->getMockBuilder(UploadedFile::class)
->disableOriginalConstructor()
->getMock();
$fileMock->expects($this->never())
->method('move');
$filePathResolverMock->expects($this->once())
->method('getUniqueFileName')
->with($uploadDir, $fileMock)
->willReturn($fileName);
$filePathResolverMock->expects($this->once())
->method('createDirectory')
->with($uploadDir)
->willThrowException(new FilePathException('Could not create directory'));
$fileUploader = new FileUploader($filePathResolverMock);
$this->expectException(FileUploadException::class);
$this->expectExceptionMessage('Could not create directory');
$fileUploader->upload($uploadDir, $fileMock);
}
/**
* @testdox Throw an Exception if Uploader could not move file to givven directory
*
* @covers \Mautic\CoreBundle\Helper\FileUploader::upload
*/
public function testCouldNotMoveFile()
{
$uploadDir = 'my/upload/dir';
$fileName = 'MyfileName';
$filePathResolverMock = $this->getMockBuilder(FilePathResolver::class)
->disableOriginalConstructor()
->getMock();
$fileMock = $this->getMockBuilder(UploadedFile::class)
->disableOriginalConstructor()
->getMock();
$fileMock->expects($this->once())
->method('move')
->with($uploadDir, $fileName)
->willThrowException(new FileException());
$filePathResolverMock->expects($this->once())
->method('getUniqueFileName')
->with($uploadDir, $fileMock)
->willReturn($fileName);
$filePathResolverMock->expects($this->once())
->method('createDirectory')
->with($uploadDir);
$fileUploader = new FileUploader($filePathResolverMock);
$this->expectException(FileUploadException::class);
$this->expectExceptionMessage('Could not upload file');
$fileUploader->upload($uploadDir, $fileMock);
}
/**
* @testdox Test for file delete
*
* @covers \Mautic\CoreBundle\Helper\FileUploader::delete
*/
public function testDeleteFile()
{
$file = 'MyfileName';
$filePathResolverMock = $this->getMockBuilder(FilePathResolver::class)
->disableOriginalConstructor()
->getMock();
$filePathResolverMock->expects($this->once())
->method('delete')
->with($file);
$fileUploader = new FileUploader($filePathResolverMock);
$fileUploader->delete($file);
}
}