Skip to content

Commit

Permalink
Ensure files are created in a different millisecond in test to keep r…
Browse files Browse the repository at this point in the history
…evisions order
  • Loading branch information
GromNaN committed May 21, 2024
1 parent a351269 commit 7d0538f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 6 additions & 8 deletions GridFSAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 5 additions & 1 deletion GridFSAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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);
Expand Down Expand Up @@ -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');

}
);
Expand Down

0 comments on commit 7d0538f

Please sign in to comment.