-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathFileUploadTest.php
More file actions
121 lines (104 loc) · 3.06 KB
/
Copy pathFileUploadTest.php
File metadata and controls
121 lines (104 loc) · 3.06 KB
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
<?php declare(strict_types=1);
namespace hollodotme\FastCGI\Tests\Integration\FileUpload;
use hollodotme\FastCGI\Client;
use hollodotme\FastCGI\Exceptions\ConnectException;
use hollodotme\FastCGI\Exceptions\TimedoutException;
use hollodotme\FastCGI\Exceptions\WriteFailedException;
use hollodotme\FastCGI\RequestContents\MultipartFormData;
use hollodotme\FastCGI\Requests\PostRequest;
use hollodotme\FastCGI\SocketConnections\NetworkSocket;
use hollodotme\FastCGI\Tests\Traits\SocketDataProviding;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use Throwable;
use function basename;
use function dirname;
use function filesize;
use function sys_get_temp_dir;
use function unlink;
final class FileUploadTest extends TestCase
{
use SocketDataProviding;
/** @var NetworkSocket */
private $connection;
/** @var Client */
private $client;
protected function setUp() : void
{
$this->connection = new NetworkSocket( $this->getNetworkSocketHost(), $this->getNetworkSocketPort() );
$this->client = new Client();
}
protected function tearDown() : void
{
}
/**
* @param array<string, string> $files
*
* @throws ConnectException
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws Throwable
* @throws TimedoutException
* @throws WriteFailedException
* @throws \InvalidArgumentException
*
* @dataProvider filesProvider
*/
public function testCanUploadFiles( array $files ) : void
{
$formData = [
'testKey1' => 'value1',
'testKey2' => 'value2',
];
$multipartFormData = new MultipartFormData( $formData, $files );
$postRequest = new PostRequest(
dirname( __DIR__ ) . '/Workers/fileUploadWorker.php',
$multipartFormData
);
$response = $this->client->sendRequest( $this->connection, $postRequest );
$expectedBody = "POST data:\n"
. "KEY: testKey1\n"
. "VALUE: value1\n\n"
. "KEY: testKey2\n"
. "VALUE: value2\n\n"
. "Uploaded files:\n";
foreach ( $files as $key => $filePath )
{
$fileName = basename( $filePath );
$fileSize = filesize( $filePath );
$targetPath = sys_get_temp_dir() . '/' . $fileName;
$expectedBody .= "KEY: {$key}\n"
. "FILENAME: {$fileName}\n"
. "SIZE: {$fileSize}\n"
. "Moved to {$targetPath}\n\n";
self::assertFileEquals( $targetPath, $filePath );
@unlink( $targetPath );
}
self::assertSame( $expectedBody, $response->getBody() );
}
/**
* @return array<array<string,array<string, string>>>
*/
public function filesProvider() : array
{
return [
[
'files' => [
'textFile' => dirname( __DIR__ ) . '/_files/TestFile.txt',
],
],
[
'files' => [
'image' => dirname( __DIR__ ) . '/_files/php-logo.png',
],
],
[
'files' => [
'textFile' => dirname( __DIR__ ) . '/_files/TestFile.txt',
'image' => dirname( __DIR__ ) . '/_files/php-logo.png',
],
],
];
}
}