File tree Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Expand file tree Collapse file tree 2 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -861,15 +861,21 @@ public function fileWrite($resource, $data)
861861 */
862862 public function fileClose ($ resource ): bool
863863 {
864+ if (!is_resource ($ resource )) {
865+ return false ;
866+ }
864867 //phpcs:disable
865- $ resourcePath = stream_get_meta_data ($ resource )[ ' uri ' ] ;
868+ $ meta = stream_get_meta_data ($ resource );
866869 //phpcs:enable
867870
868871 foreach ($ this ->streams as $ path => $ stream ) {
869872 // phpcs:ignore
870- if (stream_get_meta_data ($ stream )['uri ' ] === $ resourcePath ) {
873+ if (stream_get_meta_data ($ stream )['uri ' ] === $ meta ['uri ' ]) {
874+ if (isset ($ meta ['seekable ' ]) && $ meta ['seekable ' ]) {
875+ // rewind the file pointer to make sure the full content of the file is saved
876+ $ this ->fileSeek ($ resource , 0 );
877+ }
871878 $ this ->adapter ->writeStream ($ path , $ resource , new Config (self ::CONFIG ));
872-
873879 // Remove path from streams after
874880 unset($ this ->streams [$ path ]);
875881
Original file line number Diff line number Diff line change @@ -513,4 +513,28 @@ public function testRenameSameDestination(): void
513513
514514 self ::assertTrue ($ this ->driver ->rename ('test/path ' , 'test/path ' ));
515515 }
516+
517+ public function testFileShouldBeRewindBeforeSave (): void
518+ {
519+ $ resource = $ this ->driver ->fileOpen ('test/path ' , 'w ' );
520+ $ this ->driver ->fileWrite ($ resource , 'abc ' );
521+ $ this ->adapterMock ->method ('fileExists ' )->willReturn (false );
522+ $ this ->adapterMock ->expects ($ this ->once ())
523+ ->method ('writeStream ' )
524+ ->with (
525+ 'test/path ' ,
526+ $ this ->callback (
527+ // assert that the file pointer is at the beginning of the file before saving it in aws
528+ fn ($ stream ) => $ stream === $ resource && is_resource ($ stream ) && ftell ($ stream ) === 0
529+ )
530+ );
531+ $ this ->driver ->fileClose ($ resource );
532+ }
533+
534+ public function testFileCloseShouldReturnFalseIfTheArgumentIsNotAResource (): void
535+ {
536+ $ this ->assertEquals (false , $ this ->driver ->fileClose ('' ));
537+ $ this ->assertEquals (false , $ this ->driver ->fileClose (null ));
538+ $ this ->assertEquals (false , $ this ->driver ->fileClose (false ));
539+ }
516540}
You can’t perform that action at this time.
0 commit comments