@@ -461,7 +461,7 @@ public function uploadAttachmentForImage(): void
461
461
$ this ->clientMock ->method ('request ' )->willReturn (['url ' => $ uploadUrl ]);
462
462
$ this ->modelFactoryMock ->method ('createUploadEndpoint ' )->willReturn (new UploadEndpoint ($ uploadUrl ));
463
463
464
- $ this ->clientMock ->method ('upload ' )->willReturn ($ uploadResponseJson );
464
+ $ this ->clientMock ->method ('multipartUpload ' )->willReturn ($ uploadResponseJson );
465
465
466
466
$ result = $ this ->api ->uploadAttachment (UploadType::Image, $ filePath );
467
467
@@ -480,7 +480,7 @@ public function uploadAttachmentForFile(): void
480
480
$ this ->clientMock ->method ('request ' )->willReturn (['url ' => $ uploadUrl ]);
481
481
$ this ->modelFactoryMock ->method ('createUploadEndpoint ' )->willReturn (new UploadEndpoint ($ uploadUrl ));
482
482
483
- $ this ->clientMock ->method ('upload ' )->willReturn ($ uploadResponseJson );
483
+ $ this ->clientMock ->method ('multipartUpload ' )->willReturn ($ uploadResponseJson );
484
484
485
485
$ result = $ this ->api ->uploadAttachment (UploadType::File, $ filePath );
486
486
@@ -514,7 +514,7 @@ public function uploadAttachmentForAudio(): void
514
514
515
515
$ this ->clientMock
516
516
->expects ($ this ->once ())
517
- ->method ('upload ' )
517
+ ->method ('multipartUpload ' )
518
518
->with ($ uploadUrl , $ this ->isResource (), basename ($ filePath ))
519
519
->willReturn ($ uploadResponse );
520
520
@@ -551,7 +551,7 @@ public function uploadAttachmentForVideo(): void
551
551
552
552
$ this ->clientMock
553
553
->expects ($ this ->once ())
554
- ->method ('upload ' )
554
+ ->method ('multipartUpload ' )
555
555
->with ($ uploadUrl , $ this ->isResource (), basename ($ filePath ))
556
556
->willReturn ($ uploadResponse );
557
557
@@ -726,7 +726,7 @@ public function uploadAttachmentThrowsSerializationExceptionForInvalidUploadResp
726
726
727
727
$ this ->clientMock
728
728
->expects ($ this ->once ())
729
- ->method ('upload ' )
729
+ ->method ('multipartUpload ' )
730
730
->with ($ uploadUrl , $ this ->isResource (), basename ($ filePath ))
731
731
->willReturn (json_encode ($ invalidUploadResponse ));
732
732
@@ -769,7 +769,7 @@ public function uploadAttachmentSuccessfullyUploadsFileAndReturnsAttachment(): v
769
769
770
770
$ this ->clientMock
771
771
->expects ($ this ->once ())
772
- ->method ('upload ' )
772
+ ->method ('multipartUpload ' )
773
773
->with ($ uploadUrl , $ this ->isResource (), basename ($ filePath ))
774
774
->willReturn (json_encode ($ uploadResponse ));
775
775
@@ -1983,7 +1983,7 @@ public function uploadAttachmentThrowsSerializationExceptionOnInvalidUploadRespo
1983
1983
1984
1984
$ this ->clientMock
1985
1985
->expects ($ this ->once ())
1986
- ->method ('upload ' )
1986
+ ->method ('multipartUpload ' )
1987
1987
->willReturn ($ invalidJsonResponse );
1988
1988
1989
1989
try {
@@ -2017,7 +2017,7 @@ public function uploadAttachmentForVideoThrowsExceptionOnMissingPreUploadToken()
2017
2017
->with ($ getUploadUrlResponse )
2018
2018
->willReturn ($ expectedEndpoint );
2019
2019
2020
- $ this ->clientMock ->expects ($ this ->never ())->method ('upload ' );
2020
+ $ this ->clientMock ->expects ($ this ->never ())->method ('multipartUpload ' );
2021
2021
2022
2022
try {
2023
2023
$ this ->api ->uploadAttachment (UploadType::Video, $ filePath );
@@ -2042,7 +2042,7 @@ public function uploadAttachmentForFileThrowsExceptionOnMissingPostUploadToken()
2042
2042
2043
2043
$ this ->clientMock
2044
2044
->expects ($ this ->once ())
2045
- ->method ('upload ' )
2045
+ ->method ('multipartUpload ' )
2046
2046
->willReturn ($ invalidUploadResponse );
2047
2047
2048
2048
try {
@@ -2051,4 +2051,87 @@ public function uploadAttachmentForFileThrowsExceptionOnMissingPostUploadToken()
2051
2051
unlink ($ filePath );
2052
2052
}
2053
2053
}
2054
+
2055
+ #[Test]
2056
+ #[RunInSeparateProcess]
2057
+ #[PreserveGlobalState(false )]
2058
+ public function uploadFileUsesMultipartForSmallFiles (): void
2059
+ {
2060
+ $ uploadUrl = 'https://upload.server/path ' ;
2061
+ $ fileName = 'small.txt ' ;
2062
+ $ fileContents = 'content ' ;
2063
+ $ fileHandle = fopen ('php://memory ' , 'w+ ' );
2064
+ fwrite ($ fileHandle , $ fileContents );
2065
+ rewind ($ fileHandle );
2066
+
2067
+ $ smallFileSize = strlen ($ fileContents );
2068
+ $ expectedResponse = 'multipart-response ' ;
2069
+
2070
+ $ fstatMock = $ this ->getFunctionMock ('BushlanovDev\MaxMessengerBot ' , 'fstat ' );
2071
+ $ fstatMock ->expects ($ this ->once ())->with ($ fileHandle )->willReturn (['size ' => $ smallFileSize ]);
2072
+
2073
+ $ this ->clientMock
2074
+ ->expects ($ this ->once ())
2075
+ ->method ('multipartUpload ' )
2076
+ ->with ($ uploadUrl , $ fileHandle , $ fileName )
2077
+ ->willReturn ($ expectedResponse );
2078
+
2079
+ $ this ->clientMock
2080
+ ->expects ($ this ->never ())
2081
+ ->method ('resumableUpload ' );
2082
+
2083
+ $ result = $ this ->api ->uploadFile ($ uploadUrl , $ fileHandle , $ fileName );
2084
+
2085
+ $ this ->assertSame ($ expectedResponse , $ result );
2086
+ fclose ($ fileHandle );
2087
+ }
2088
+
2089
+ #[Test]
2090
+ #[RunInSeparateProcess]
2091
+ #[PreserveGlobalState(false )]
2092
+ public function uploadFileUsesResumableForLargeFiles (): void
2093
+ {
2094
+ $ uploadUrl = 'https://upload.server/path ' ;
2095
+ $ fileName = 'large.zip ' ;
2096
+ $ fileHandle = fopen ('php://memory ' , 'w+ ' );
2097
+
2098
+ rewind ($ fileHandle );
2099
+
2100
+ $ largeFileSize = 10 * 1024 * 1024 ;
2101
+ $ expectedResponse = 'resumable-response ' ;
2102
+
2103
+ $ fstatMock = $ this ->getFunctionMock ('BushlanovDev\MaxMessengerBot ' , 'fstat ' );
2104
+ $ fstatMock ->expects ($ this ->once ())->with ($ fileHandle )->willReturn (['size ' => $ largeFileSize ]);
2105
+
2106
+ $ this ->clientMock
2107
+ ->expects ($ this ->once ())
2108
+ ->method ('resumableUpload ' )
2109
+ ->with ($ uploadUrl , $ fileHandle , $ fileName , $ largeFileSize )
2110
+ ->willReturn ($ expectedResponse );
2111
+
2112
+ $ this ->clientMock
2113
+ ->expects ($ this ->never ())
2114
+ ->method ('multipartUpload ' );
2115
+
2116
+ $ result = $ this ->api ->uploadFile ($ uploadUrl , $ fileHandle , $ fileName );
2117
+
2118
+ $ this ->assertSame ($ expectedResponse , $ result );
2119
+ fclose ($ fileHandle );
2120
+ }
2121
+
2122
+ #[Test]
2123
+ #[RunInSeparateProcess]
2124
+ #[PreserveGlobalState(false )]
2125
+ public function uploadFileThrowsExceptionWhenFstatFails (): void
2126
+ {
2127
+ $ this ->expectException (RuntimeException::class);
2128
+ $ this ->expectExceptionMessage ('File handle is not a valid resource. ' );
2129
+
2130
+ $ fileHandle = fopen ('php://memory ' , 'r ' );
2131
+
2132
+ $ fstatMock = $ this ->getFunctionMock ('BushlanovDev\MaxMessengerBot ' , 'fstat ' );
2133
+ $ fstatMock ->expects ($ this ->once ())->with ($ fileHandle )->willReturn (false );
2134
+
2135
+ $ this ->api ->uploadFile ('http://a.b ' , $ fileHandle , 'file.txt ' );
2136
+ }
2054
2137
}
0 commit comments