|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\SpecTests\Crud; |
| 4 | + |
| 5 | +use MongoDB\ClientBulkWrite; |
| 6 | +use MongoDB\Driver\Exception\BulkWriteCommandException; |
| 7 | +use MongoDB\Driver\Exception\InvalidArgumentException; |
| 8 | +use MongoDB\Tests\SpecTests\FunctionalTestCase; |
| 9 | + |
| 10 | +use function str_repeat; |
| 11 | + |
| 12 | +/** |
| 13 | + * Prose test 12: MongoClient.bulkWrite returns an error if no operations can be added to ops |
| 14 | + * |
| 15 | + * @see https://github.com/mongodb/specifications/tree/master/source/crud/tests#12-mongoclientbulkwrite-returns-an-error-if-no-operations-can-be-added-to-ops |
| 16 | + */ |
| 17 | +class Prose12_BulkWriteExceedsMaxMessageSizeBytesTest extends FunctionalTestCase |
| 18 | +{ |
| 19 | + public function setUp(): void |
| 20 | + { |
| 21 | + parent::setUp(); |
| 22 | + |
| 23 | + if ($this->isServerless()) { |
| 24 | + $this->markTestSkipped('bulkWrite command is not supported'); |
| 25 | + } |
| 26 | + |
| 27 | + $this->skipIfServerVersion('<', '8.0', 'bulkWrite command is not supported'); |
| 28 | + } |
| 29 | + |
| 30 | + public function testDocumentTooLarge(): void |
| 31 | + { |
| 32 | + $client = self::createTestClient(); |
| 33 | + |
| 34 | + $maxMessageSizeBytes = $this->getPrimaryServer()->getInfo()['maxMessageSizeBytes'] ?? null; |
| 35 | + self::assertIsInt($maxMessageSizeBytes); |
| 36 | + |
| 37 | + $collection = $client->selectCollection($this->getDatabaseName(), $this->getCollectionName()); |
| 38 | + $bulkWrite = ClientBulkWrite::createWithCollection($collection); |
| 39 | + $bulkWrite->insertOne(['a' => str_repeat('b', $maxMessageSizeBytes)]); |
| 40 | + |
| 41 | + try { |
| 42 | + $client->bulkWrite($bulkWrite); |
| 43 | + self::fail('Exception was not thrown'); |
| 44 | + } catch (BulkWriteCommandException $e) { |
| 45 | + /* Note: although the client-side error occurs on the first operation, libmongoc still populates the partial |
| 46 | + * result (see: CDRIVER-5969). This causes PHPC to proxy the underlying InvalidArgumentException behind |
| 47 | + * BulkWriteCommandException. Until this is addressed, unwrap the error and check the partial result. */ |
| 48 | + self::assertInstanceOf(InvalidArgumentException::class, $e->getPrevious()); |
| 49 | + self::assertSame(0, $e->getPartialResult()->getInsertedCount()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public function testNamespaceTooLarge(): void |
| 54 | + { |
| 55 | + $client = self::createTestClient(); |
| 56 | + |
| 57 | + $maxMessageSizeBytes = $this->getPrimaryServer()->getInfo()['maxMessageSizeBytes'] ?? null; |
| 58 | + self::assertIsInt($maxMessageSizeBytes); |
| 59 | + |
| 60 | + $collectionName = str_repeat('c', $maxMessageSizeBytes); |
| 61 | + $collection = $client->selectCollection($this->getDatabaseName(), $collectionName); |
| 62 | + $bulkWrite = ClientBulkWrite::createWithCollection($collection); |
| 63 | + $bulkWrite->insertOne(['a' => 'b']); |
| 64 | + |
| 65 | + try { |
| 66 | + $client->bulkWrite($bulkWrite); |
| 67 | + self::fail('Exception was not thrown'); |
| 68 | + } catch (BulkWriteCommandException $e) { |
| 69 | + /* Note: although the client-side error occurs on the first operation, libmongoc still populates the partial |
| 70 | + * result (see: CDRIVER-5969). This causes PHPC to proxy the underlying InvalidArgumentException behind |
| 71 | + * BulkWriteCommandException. Until this is addressed, unwrap the error and check the partial result. */ |
| 72 | + self::assertInstanceOf(InvalidArgumentException::class, $e->getPrevious()); |
| 73 | + self::assertSame(0, $e->getPartialResult()->getInsertedCount()); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments