Skip to content

Commit f704f40

Browse files
committed
Fix UploadedFile unit tests, add phpdoc
1 parent 97b32f9 commit f704f40

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

tests/MessageTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function getMessage()
3636
/**
3737
* Get a \Psr\Http\Message\StreamInterface mock.
3838
*
39+
* @suppress PhanTypeMismatchReturn
3940
* @return StreamInterface
4041
*/
4142
protected function getMockStream(): StreamInterface

tests/RequestTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public function getRequest(): RequestInterface
3434
* @param array $methods (optional) Methods to mock.
3535
*
3636
* @return UriInterface
37+
* @suppress PhanAccessMethodInternal
38+
* @suppress PhanTypeMismatchReturn
3739
*/
3840
protected function getMockUri($methods = []): UriInterface
3941
{

tests/StreamTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function getStream()
6666
/**
6767
* Get a generic StreamInterface object with a specific stream.
6868
*
69-
* @param resource $handle {@see \fopen}.
69+
* @param resource|mixed $handle {@see \fopen}.
7070
* @return StreamInterface
7171
*/
7272
protected function getStreamWithHandle($handle)

tests/UploadedFileTest.php

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class UploadedFileTest extends TestCase
2222
public static $is_resource = null;
2323

2424
/**
25-
* Before each test, reset functions overrides.
25+
* After each test, reset functions overrides.
2626
*/
27-
protected function setUp(): void
27+
protected function tearDown(): void
2828
{
2929
self::$rename = null;
3030
self::$is_uploaded_file = null;
@@ -58,13 +58,16 @@ protected function getTempFilePath(): string
5858
return tempnam(sys_get_temp_dir(), 'UploadedFileTest');
5959
}
6060

61+
/**
62+
* @suppress PhanAccessMethodInternal
63+
* @suppress PhanTypeMismatchReturn
64+
*/
6165
protected function getStreamMock(?string $path = null, $methods = []): StreamInterface
6266
{
6367
if ($path === null) {
6468
$path = $this->getTempFilePath();
6569
}
6670

67-
/** @var StreamInterface */
6871
$streamMock = $this->createMock(StreamInterface::class);
6972

7073
foreach ($methods as $name => $returnValue) {
@@ -89,21 +92,22 @@ protected function getStreamMock(?string $path = null, $methods = []): StreamInt
8992
*/
9093
protected function getUploadedFile(?string $path = null, ?int $size = null, ?int $error = null, ?string $clientFilename = null, ?string $clientMediaType = null)
9194
{
92-
$mockStream = $this->getStreamMock($path);
93-
$mockStream
94-
->method('getContents')
95-
->willReturn('foo bar baz !');
95+
$mockStream = $this->getStreamMock($path, [
96+
'getContents' => 'foo bar baz !',
97+
]);
98+
99+
$errorToUse = $error ?? \UPLOAD_ERR_OK;
96100

97101
if ($clientMediaType !== null) {
98-
return new \IngeniozIT\Http\Message\UploadedFile($mockStream, $size, $error, $clientFilename, $clientMediaType);
102+
return new \IngeniozIT\Http\Message\UploadedFile($mockStream, $size, $errorToUse, $clientFilename, $clientMediaType);
99103
}
100104

101105
if ($clientFilename !== null) {
102-
return new \IngeniozIT\Http\Message\UploadedFile($mockStream, $size, $error, $clientFilename);
106+
return new \IngeniozIT\Http\Message\UploadedFile($mockStream, $size, $errorToUse, $clientFilename);
103107
}
104108

105109
if ($error !== null) {
106-
return new \IngeniozIT\Http\Message\UploadedFile($mockStream, $size, $error);
110+
return new \IngeniozIT\Http\Message\UploadedFile($mockStream, $size, $errorToUse);
107111
}
108112

109113
if ($size !== null) {
@@ -225,39 +229,58 @@ public function testMoveToExistingTargetPath()
225229
* throws \RuntimeException on any error during the move operation.
226230
* @dataProvider providerFsErrorCases
227231
*/
228-
public function testMoveToThrowsExceptionOnFilesystemError(bool $isCli, bool $streamWithUri)
232+
public function testMoveToThrowsExceptionOnFilesystemError(bool $isCli, bool $streamWithUri, bool $overrideFopen)
229233
{
230234
$uploadedFile = $this->getUploadedFile();
231235
$path = $this->getEmptyPath();
232236
$mockStream = $this->getStreamMock($path, [
233237
'getContents' => 'foo bar baz !',
234-
'getMetadata' => 'test_uri',
238+
'getMetadata' => $streamWithUri ? 'test_uri' : null,
235239
]);
236-
237240
$uploadedFile = new \IngeniozIT\Http\Message\UploadedFile($mockStream);
238241
$path = $this->getEmptyPath();
242+
239243
self::$rename = false;
240244
self::$move_uploaded_file = false;
241245
self::$file_put_contents = false;
242246
self::$fopen = false;
243247
self::$php_sapi_name = $isCli ? 'cli' : 'not_cli';
248+
if ($overrideFopen) {
249+
self::$fopen = false;
250+
}
244251

245252
$this->expectException(\RuntimeException::class);
246253
$uploadedFile->moveTo($path);
247254
}
248255

256+
/**
257+
* Provider. Gives filesystem error cases based on
258+
* - cli / not cli environment
259+
* - StreamInterface object with / without uri, metadata, fopen fail
260+
* @return array
261+
*/
262+
public function providerFsErrorCases(): array
263+
{
264+
return [
265+
'cli + uri' => [true, true, false],
266+
'cli + no uri' => [true, false, false],
267+
'not cli + uri' => [false, true, false],
268+
'not cli + no uri' => [false, false, false],
269+
'cli + no uri + fopen fail' => [false, false, true],
270+
];
271+
}
272+
249273
/**
250274
* Move the uploaded file to a new location.
251-
* throws \RuntimeException on any error during the move operation.
252-
* @dataProvider providerFsErrorCases
275+
* @dataProvider providerFsWorkingCases
253276
*/
254277
public function testMoveToWorksWithAllEnvs(bool $isCli, bool $streamWithUri)
255278
{
256279
$uploadedFile = $this->getUploadedFile();
257280
$path = $this->getEmptyPath();
258281
$mockStream = $this->getStreamMock($path, [
259282
'getContents' => 'foo bar baz !',
260-
'getMetadata' => 'test_uri',
283+
'getMetadata' => $streamWithUri ? 'test_uri' : null,
261284
]);
262285
$uploadedFile = new \IngeniozIT\Http\Message\UploadedFile($mockStream);
263286
$path = $this->getEmptyPath();
@@ -278,13 +301,14 @@ public function testMoveToWorksWithAllEnvs(bool $isCli, bool $streamWithUri)
278301
* - StreamInterface object with / without uri metadata
279302
* @return array
280303
*/
281-
public function providerFsErrorCases(): array
304+
public function providerFsWorkingCases(): array
282305
{
283306
return [
284307
'cli + uri' => [true, true],
285308
'cli + no uri' => [true, false],
286309
'not cli + uri' => [false, true],
287310
'not cli + no uri' => [false, false],
311+
'cli + no uri + fopen fail' => [false, false],
288312
];
289313
}
290314

tests/UriTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,8 @@ public function providerFullComponents()
10291029
* Return the string representation as a URI reference.
10301030
*
10311031
* @dataProvider providerStringUri
1032-
* @param string $str Expected uri.
1032+
* @param string $str Expected uri.
1033+
* @suppress PhanTypeSuspiciousStringExpression
10331034
*/
10341035
public function testReturnsExpectedStringWhenPassingUriInConstructor(string $str)
10351036
{
@@ -1058,7 +1059,8 @@ public function providerStringUri()
10581059
* Return the string representation as a URI reference.
10591060
*
10601061
* @dataProvider providerInvalidStringUri
1061-
* @param string $str Invalid uri.
1062+
* @param string $str Invalid uri.
1063+
* @suppress PhanNoopNew
10621064
*/
10631065
public function testThrowsInvalidargumentExceptionOnInvalidString(string $str)
10641066
{

0 commit comments

Comments
 (0)