ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
FileUploadImplTestTBD.php
Go to the documentation of this file.
1<?php
2
19namespace ILIAS\FileUpload;
20
21use PHPUnit\Framework\Attributes\BackupGlobals;
22use PHPUnit\Framework\Attributes\BackupStaticProperties;
23use PHPUnit\Framework\Attributes\PreserveGlobalState;
24use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
25use PHPUnit\Framework\Attributes\Test;
26use PHPUnit\Framework\Attributes\Small;
27
28require_once('./vendor/composer/vendor/autoload.php');
29
37use Mockery;
38use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
39use Mockery\MockInterface;
40use PHPUnit\Framework\TestCase;
41use Psr\Http\Message\UploadedFileInterface;
42
48#[BackupGlobals(false)]
49#[BackupStaticProperties(false)]
50#[PreserveGlobalState(false)]
51#[RunTestsInSeparateProcesses]
52class FileUploadImplTest extends TestCase
53{
54 use MockeryPHPUnitIntegration;
55
68
69
70 #[Test]
71
72 public function testRegisterWhichShouldSucceed(): void
73 {
74 $processorMock = \Mockery::mock(PreProcessor::class);
75 $this->prePorcessorManagerMock->shouldReceive('with')
76 ->once()
77 ->with($processorMock);
78
79 $this->subject->register($processorMock);
80 }
81
82 #[Test]
83
85 {
86 $processorMock = \Mockery::mock(PreProcessor::class);
87
88 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
89 ->once()
90 ->andReturn([]);
91
92 $this->expectException(IllegalStateException::class);
93 $this->expectExceptionMessage('Can not register processor after the upload was processed.');
94
95 $this->subject->process();
96 $this->subject->register($processorMock);
97 }
98
99 #[Test]
100
101 public function testProcessWhichShouldSucceed(): void
102 {
103 $processingResult = new ProcessingStatus(ProcessingStatus::OK, 'All green!');
104 $uploadedFile = Mockery::mock(UploadedFileInterface::class);
105 $uploadedFile
106 ->shouldReceive('getClientFilename')
107 ->once()
108 ->andReturn('hello.txt')
109 ->getMock()
110 ->shouldReceive('getSize')
111 ->once()
112 ->andReturn(10)
113 ->getMock()
114 ->shouldReceive('getClientMediaType')
115 ->once()
116 ->andReturn('text/plain')
117 ->getMock()
118 ->shouldReceive('getError')
119 ->once()
120 ->andReturn(UPLOAD_ERR_OK)
121 ->getMock()
122 ->shouldReceive('getStream')
123 ->twice()
124 ->andReturn(Streams::ofString("Text file content."));
125
126 $uploadedFiles = [
127 $uploadedFile
128 ];
129
130 $this->prePorcessorManagerMock->shouldReceive('process')
131 ->withAnyArgs()
132 ->once()
133 ->andReturn($processingResult);
134
135 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
136 ->once()
137 ->andReturn($uploadedFiles);
138
139 $this->subject->process();
140 }
141
142 #[Test]
143
145 {
146 $uploadedFile = Mockery::mock(UploadedFileInterface::class);
147 $uploadedFile
148 ->shouldReceive('getClientFilename')
149 ->once()
150 ->andReturn('hello.txt')
151 ->getMock()
152 ->shouldReceive('getSize')
153 ->once()
154 ->andReturn(10)
155 ->getMock()
156 ->shouldReceive('getClientMediaType')
157 ->once()
158 ->andReturn('text/plain')
159 ->getMock()
160 ->shouldReceive('getError')
161 ->once()
162 ->andReturn(UPLOAD_ERR_PARTIAL)
163 ->getMock()
164 ->shouldReceive('getStream')
165 ->twice()
166 ->andReturn(Streams::ofString("Text file content."));
167
168 $uploadedFiles = [
169 $uploadedFile
170 ];
171
172 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
173 ->once()
174 ->andReturn($uploadedFiles);
175
176 $this->subject->process();
177
178 $result = $this->subject->getResults()[0];
179 $this->assertSame(ProcessingStatus::REJECTED, $result->getStatus()->getCode());
180 }
181
182
183 #[Test]
184
186 {
187 // No File-Upload Element
188 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
189 ->once()
190 ->andReturn([]);
191 $this->assertFalse($this->subject->hasUploads());
192 }
193
194 #[Test]
195
197 {
198 $uploadedFile = Mockery::mock(UploadedFileInterface::class);
199
200 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
201 ->once()
202 ->andReturn([ $uploadedFile ]);
203
204 $this->assertTrue($this->subject->hasUploads());
205 }
206
207 #[Test]
208
210 {
211 $files = [];
212 for ($i = 0; $i < 10; $i++) {
213 $files[] = Mockery::mock(UploadedFileInterface::class);
214 }
215
216 $this->globalHttpStateMock->shouldReceive('request->getUploadedFiles')
217 ->once()
218 ->andReturn($files);
219
220 $this->assertTrue($this->subject->hasUploads());
221 }
222
223
227 protected function setUp(): void
228 {
229 parent::setUp();
230
231 $this->prePorcessorManagerMock = \Mockery::mock(PreProcessorManager::class);
232 $filesystemsMock = \Mockery::mock(Filesystems::class);
233 $this->globalHttpStateMock = \Mockery::mock(GlobalHttpState::class);
234
235 $this->subject = new FileUploadImpl($this->prePorcessorManagerMock, $filesystemsMock, $this->globalHttpStateMock);
236 }
237}
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
The Filesystems interface defines the access methods which can be used to fetch the different filesys...
Definition: Filesystems.php:30
Interface GlobalHttpState.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...