ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
FileUploadImplTest.php
Go to the documentation of this file.
1<?php
2
3namespace ILIAS\FileUpload;
4
5require_once('./libs/composer/vendor/autoload.php');
6
14use Mockery;
15use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
16use Mockery\MockInterface;
17use PHPUnit\Framework\TestCase;
18use Psr\Http\Message\UploadedFileInterface;
19
30class FileUploadImplTest extends TestCase
31{
32 use MockeryPHPUnitIntegration;
33
37 private $subject;
50
51
57 {
58 $processorMock = \Mockery::mock(PreProcessor::class);
59 $this->prePorcessorManagerMock->shouldReceive('with')
60 ->once()
61 ->with($processorMock);
62
63 $this->subject->register($processorMock);
64 }
65
71 {
72 $processorMock = \Mockery::mock(PreProcessor::class);
73
74 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
75 ->once()
76 ->andReturn([]);
77
78 $this->expectException(IllegalStateException::class);
79 $this->expectExceptionMessage('Can not register processor after the upload was processed.');
80
81 $this->subject->process();
82 $this->subject->register($processorMock);
83 }
84
90 {
91 $processingResult = new ProcessingStatus(ProcessingStatus::OK, 'All green!');
92 $uploadedFile = Mockery::mock(UploadedFileInterface::class);
94 ->shouldReceive('getClientFilename')
95 ->once()
96 ->andReturn('hello.txt')
97 ->getMock()
98 ->shouldReceive('getSize')
99 ->once()
100 ->andReturn(10)
101 ->getMock()
102 ->shouldReceive('getClientMediaType')
103 ->once()
104 ->andReturn('text/plain')
105 ->getMock()
106 ->shouldReceive('getError')
107 ->once()
108 ->andReturn(UPLOAD_ERR_OK)
109 ->getMock()
110 ->shouldReceive('getStream')
111 ->twice()
112 ->andReturn(Streams::ofString("Text file content."));
113
114 $uploadedFiles = [
116 ];
117
118 $this->prePorcessorManagerMock->shouldReceive('process')
119 ->withAnyArgs()
120 ->once()
121 ->andReturn($processingResult);
122
123 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
124 ->once()
125 ->andReturn($uploadedFiles);
126
127 $this->subject->process();
128 }
129
135 {
136 $uploadedFile = Mockery::mock(UploadedFileInterface::class);
138 ->shouldReceive('getClientFilename')
139 ->once()
140 ->andReturn('hello.txt')
141 ->getMock()
142 ->shouldReceive('getSize')
143 ->once()
144 ->andReturn(10)
145 ->getMock()
146 ->shouldReceive('getClientMediaType')
147 ->once()
148 ->andReturn('text/plain')
149 ->getMock()
150 ->shouldReceive('getError')
151 ->once()
152 ->andReturn(UPLOAD_ERR_PARTIAL)
153 ->getMock()
154 ->shouldReceive('getStream')
155 ->twice()
156 ->andReturn(Streams::ofString("Text file content."));
157
158 $uploadedFiles = [
160 ];
161
162 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
163 ->once()
164 ->andReturn($uploadedFiles);
165
166 $this->subject->process();
167
168 $result = $this->subject->getResults()[0];
169 $this->assertSame(ProcessingStatus::REJECTED, $result->getStatus()->getCode());
170 }
171
172
178 {
179 // No File-Upload Element
180 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
181 ->once()
182 ->andReturn([]);
183 $this->assertFalse($this->subject->hasUploads());
184 }
185
191 {
192 $uploadedFile = Mockery::mock(UploadedFileInterface::class);
193
194 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
195 ->once()
196 ->andReturn([ $uploadedFile ]);
197
198 $this->assertTrue($this->subject->hasUploads());
199 }
200
206 {
207 $files = [];
208 for ($i = 0; $i < 10; $i++) {
209 $files[] = Mockery::mock(UploadedFileInterface::class);
210 }
211
212 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
213 ->once()
214 ->andReturn($files);
215
216 $this->assertTrue($this->subject->hasUploads());
217 }
218
219
223 protected function setUp() : void
224 {
225 parent::setUp();
226
227 $this->prePorcessorManagerMock = \Mockery::mock(PreProcessorManager::class);
228 $this->filesystemsMock = \Mockery::mock(Filesystems::class);
229 $this->globalHttpStateMock = \Mockery::mock(GlobalHttpState::class);
230
231 $this->subject = new FileUploadImpl($this->prePorcessorManagerMock, $this->filesystemsMock, $this->globalHttpStateMock);
232 }
233}
$result
An exception for terminatinating execution or to throw for unit testing.
const REJECTED
Upload got rejected by a processor.
testRegisterWithProcessedFilesWhichShouldFail()
@Test @small
testProcessWithFailedUploadWhichShouldGetRejected()
@Test @small
static ofString($string)
Creates a new stream with an initial value.
Definition: Streams.php:30
Interface GlobalHttpState.
$i
Definition: metadata.php:24
$uploadedFile
Definition: imgupload.php:61