diff --git a/src/GridFS/GridFSAdapter.php b/src/GridFS/GridFSAdapter.php index 1035fdc2a..cff68b188 100644 --- a/src/GridFS/GridFSAdapter.php +++ b/src/GridFS/GridFSAdapter.php @@ -345,17 +345,15 @@ public function listContents(string $path, bool $deep): iterable public function move(string $source, string $destination, Config $config): void { - $file = $this->findFile($source); - - if ($file === null) { - throw UnableToMoveFile::because('file does not exist', $source, $destination); - } - try { - $this->bucket->getFilesCollection()->updateMany( - ['filename' => $file['filename']], + $result = $this->bucket->getFilesCollection()->updateMany( + ['filename' => $this->prefixer->prefixPath($source)], ['$set' => ['filename' => $this->prefixer->prefixPath($destination)]], ); + + if ($result->getModifiedCount() === 0) { + throw UnableToMoveFile::because('file does not exist', $source, $destination); + } } catch (Exception $exception) { throw UnableToMoveFile::fromLocationTo($source, $destination, $exception); } diff --git a/src/GridFS/GridFSAdapterTest.php b/src/GridFS/GridFSAdapterTest.php index 147a9971c..fee6c566e 100644 --- a/src/GridFS/GridFSAdapterTest.php +++ b/src/GridFS/GridFSAdapterTest.php @@ -146,6 +146,7 @@ public function reading_last_revision(): void $this->runScenario( function () { $this->givenWeHaveAnExistingFile('file.txt', 'version 1'); + usleep(10); $this->givenWeHaveAnExistingFile('file.txt', 'version 2'); $this->assertSame('version 2', $this->adapter()->read('file.txt')); @@ -164,6 +165,7 @@ public function listing_contents_last_revision(bool $deep): void $this->runScenario( function () use ($deep) { $this->givenWeHaveAnExistingFile('file.txt', 'version 1'); + usleep(10); $this->givenWeHaveAnExistingFile('file.txt', 'version 2'); $files = $this->adapter()->listContents('', $deep); @@ -225,13 +227,15 @@ public function move_all_revisions(): void $this->runScenario( function () { $this->givenWeHaveAnExistingFile('file.txt', 'version 1'); + usleep(10); $this->givenWeHaveAnExistingFile('file.txt', 'version 2'); + usleep(10); $this->givenWeHaveAnExistingFile('file.txt', 'version 3'); $this->adapter()->move('file.txt', 'destination.txt', new Config()); - $this->assertSame($this->adapter()->read('destination.txt'), 'version 3'); $this->assertFalse($this->adapter()->fileExists('file.txt')); + $this->assertSame($this->adapter()->read('destination.txt'), 'version 3'); } );