Skip to content

Commit 1fa8976

Browse files
committed
Fix rename with same file name
1 parent 70c71e8 commit 1fa8976

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/GridFS/CollectionWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public function updateFilenameForFilename(string $filename, string $newFilename)
284284
return $this->filesCollection->updateMany(
285285
['filename' => $filename],
286286
['$set' => ['filename' => $newFilename]],
287-
)->getModifiedCount();
287+
)->getMatchedCount();
288288
}
289289

290290
/**

src/GridFS/StreamWrapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,10 @@ public function url_stat(string $path, int $flags)
330330
return $this->stream_stat();
331331
}
332332

333-
/** @psalm-return ($mode == 'r' ? array{collectionWrapper: CollectionWrapper, file: object} : array{collectionWrapper: CollectionWrapper, filename: string, options: array}) */
333+
/**
334+
* @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array}
335+
* @psalm-return ($mode == 'r' or $mode == 'rb' ? array{collectionWrapper: CollectionWrapper, file: object} : array{collectionWrapper: CollectionWrapper, filename: string, options: array})
336+
*/
334337
private function getContext(string $path, string $mode): array
335338
{
336339
$context = [];

tests/GridFS/StreamWrapperFunctionalTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,24 @@ public function testRenameAllRevisions(): void
386386
rename($path, $path . '.renamed');
387387
}
388388

389+
public function testRenameSameFilename(): void
390+
{
391+
$this->bucket->registerGlobalStreamWrapperAlias('bucket');
392+
$path = 'gridfs://bucket/filename';
393+
394+
$this->assertSame(6, file_put_contents($path, 'foobar'));
395+
396+
$result = rename($path, $path);
397+
$this->assertTrue($result);
398+
$this->assertTrue(file_exists($path));
399+
$this->assertSame('foobar', file_get_contents($path));
400+
401+
$path = 'gridfs://bucket/missing';
402+
$this->expectException(FileNotFoundException::class);
403+
$this->expectExceptionMessage('File with name "gridfs://bucket/missing" not found');
404+
rename($path, $path);
405+
}
406+
389407
public function testRenamePathMismatch(): void
390408
{
391409
$this->expectException(LogicException::class);

0 commit comments

Comments
 (0)