diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 6915125cc7..1b7e44bc95 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -12,7 +12,7 @@ env: jobs: phpunit: name: "PHPUnit" - runs-on: "ubuntu-18.04" + runs-on: "ubuntu-20.04" strategy: matrix: @@ -25,7 +25,6 @@ jobs: - "6.0" - "5.0" - "4.4" - - "4.2" driver-version: - "stable" topology: @@ -35,8 +34,8 @@ jobs: include: - dependencies: "lowest" php-version: "7.4" - mongodb-version: "4.2" - driver-version: "1.5.0" + mongodb-version: "4.4" + driver-version: "1.11.0" topology: "server" - topology: "sharded_cluster" php-version: "8.2" diff --git a/composer.json b/composer.json index a1d51b1d5d..b4b868986d 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ ], "require": { "php": "^7.4 || ^8.0", - "ext-mongodb": "^1.5", + "ext-mongodb": "^1.11", "doctrine/annotations": "^1.12 || ^2.0", "doctrine/cache": "^1.11 || ^2.0", "doctrine/collections": "^1.5 || ^2.0", @@ -31,7 +31,7 @@ "doctrine/persistence": "^2.4 || ^3.0", "friendsofphp/proxy-manager-lts": "^1.0", "jean85/pretty-package-versions": "^1.3.0 || ^2.0.1", - "mongodb/mongodb": "^1.2.0", + "mongodb/mongodb": "^1.10.0", "psr/cache": "^1.0 || ^2.0 || ^3.0", "symfony/console": "^3.4 || ^4.1 || ^5.0 || ^6.0", "symfony/deprecation-contracts": "^2.2 || ^3.0", diff --git a/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php b/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php index e8bb26ab81..1486f15879 100644 --- a/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php +++ b/lib/Doctrine/ODM/MongoDB/Repository/DefaultGridFSRepository.php @@ -87,6 +87,7 @@ private function getDocumentBucket(): Bucket /** * @psalm-return array{ + * _id?: mixed, * chunkSizeBytes?: int, * metadata?: object * } @@ -100,6 +101,10 @@ private function prepareOptions(?UploadOptions $uploadOptions = null): array $chunkSizeBytes = $uploadOptions->chunkSizeBytes ?: $this->class->getChunkSizeBytes(); $options = []; + if ($uploadOptions->id !== null) { + $options['_id'] = $uploadOptions->id; + } + if ($chunkSizeBytes !== null) { $options['chunkSizeBytes'] = $chunkSizeBytes; } diff --git a/lib/Doctrine/ODM/MongoDB/Repository/UploadOptions.php b/lib/Doctrine/ODM/MongoDB/Repository/UploadOptions.php index 97e20ecefd..6d022389a5 100644 --- a/lib/Doctrine/ODM/MongoDB/Repository/UploadOptions.php +++ b/lib/Doctrine/ODM/MongoDB/Repository/UploadOptions.php @@ -6,9 +6,12 @@ final class UploadOptions { - /** @var object|null */ - public $metadata; + /** @var mixed */ + public $id; /** @var int|null */ public $chunkSizeBytes; + + /** @var object|null */ + public $metadata; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php index 63a8e57ab8..bf51be9da0 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Repository/DefaultGridFSRepositoryTest.php @@ -14,6 +14,7 @@ use Documents\FileWithoutChunkSize; use Documents\FileWithoutMetadata; use Documents\User; +use MongoDB\BSON\ObjectId; use function assert; use function fclose; @@ -46,6 +47,29 @@ public function testOpenUploadStreamReturnsWritableResource(): void self::assertNull($file->getMetadata()); } + public function testOpenUploadStreamUsesIdFromOptions(): void + { + $uploadOptions = new UploadOptions(); + $uploadOptions->id = new ObjectId('1234567890abcdef12345678'); + + $uploadStream = $this->getRepository()->openUploadStream('somefile.txt', $uploadOptions); + self::assertIsResource($uploadStream); + + fwrite($uploadStream, 'contents'); + fclose($uploadStream); + + $file = $this->getRepository()->findOneBy(['filename' => 'somefile.txt']); + assert($file instanceof File); + self::assertInstanceOf(File::class, $file); + + self::assertSame('somefile.txt', $file->getFilename()); + self::assertSame('1234567890abcdef12345678', $file->getId()); + self::assertSame(8, $file->getLength()); + self::assertSame(12345, $file->getChunkSize()); + self::assertEqualsWithDelta(new DateTime(), $file->getUploadDate(), 1); + self::assertNull($file->getMetadata()); + } + public function testOpenUploadStreamUsesChunkSizeFromOptions(): void { $uploadOptions = new UploadOptions();