15
15
use MongoDB \Model \BSONDocument ;
16
16
use MongoDB \Model \IndexInfo ;
17
17
use MongoDB \Operation \ListIndexes ;
18
+ use MongoDB \Tests \Fixtures \Codec \TestDocumentCodec ;
19
+ use MongoDB \Tests \Fixtures \Codec \TestFileCodec ;
20
+ use MongoDB \Tests \Fixtures \Document \TestFile ;
21
+ use stdClass ;
18
22
19
23
use function array_merge ;
20
24
use function call_user_func ;
@@ -68,6 +72,7 @@ public function provideInvalidConstructorOptions()
68
72
return $ this ->createOptionDataProvider ([
69
73
'bucketName ' => $ this ->getInvalidStringValues (true ),
70
74
'chunkSizeBytes ' => $ this ->getInvalidIntegerValues (true ),
75
+ 'codec ' => $ this ->getInvalidDocumentCodecValues (),
71
76
'disableMD5 ' => $ this ->getInvalidBooleanValues (true ),
72
77
'readConcern ' => $ this ->getInvalidReadConcernValues (),
73
78
'readPreference ' => $ this ->getInvalidReadPreferenceValues (),
@@ -83,6 +88,17 @@ public function testConstructorShouldRequireChunkSizeBytesOptionToBePositive():
83
88
new Bucket ($ this ->manager , $ this ->getDatabaseName (), ['chunkSizeBytes ' => 0 ]);
84
89
}
85
90
91
+ public function testConstructorWithCodecAndTypeMapOptions (): void
92
+ {
93
+ $ options = [
94
+ 'codec ' => new TestDocumentCodec (),
95
+ 'typeMap ' => ['root ' => 'array ' , 'document ' => 'array ' ],
96
+ ];
97
+
98
+ $ this ->expectExceptionObject (InvalidArgumentException::cannotCombineCodecAndTypeMap ());
99
+ new Bucket ($ this ->manager , $ this ->getDatabaseName (), $ options );
100
+ }
101
+
86
102
/** @dataProvider provideInputDataAndExpectedChunks */
87
103
public function testDelete ($ input , $ expectedChunks ): void
88
104
{
@@ -317,6 +333,41 @@ public function testFindUsesTypeMap(): void
317
333
$ this ->assertInstanceOf (BSONDocument::class, $ fileDocument );
318
334
}
319
335
336
+ public function testFindUsesCodec (): void
337
+ {
338
+ $ this ->bucket ->uploadFromStream ('a ' , $ this ->createStream ('foo ' ));
339
+
340
+ $ cursor = $ this ->bucket ->find ([], ['codec ' => new TestFileCodec ()]);
341
+ $ fileDocument = current ($ cursor ->toArray ());
342
+
343
+ $ this ->assertInstanceOf (TestFile::class, $ fileDocument );
344
+ $ this ->assertSame ('a ' , $ fileDocument ->filename );
345
+ }
346
+
347
+ public function testFindInheritsBucketCodec (): void
348
+ {
349
+ $ bucket = new Bucket ($ this ->manager , $ this ->getDatabaseName (), ['codec ' => new TestFileCodec ()]);
350
+ $ bucket ->uploadFromStream ('a ' , $ this ->createStream ('foo ' ));
351
+
352
+ $ cursor = $ bucket ->find ();
353
+ $ fileDocument = current ($ cursor ->toArray ());
354
+
355
+ $ this ->assertInstanceOf (TestFile::class, $ fileDocument );
356
+ $ this ->assertSame ('a ' , $ fileDocument ->filename );
357
+ }
358
+
359
+ public function testFindResetsInheritedBucketCodec (): void
360
+ {
361
+ $ bucket = new Bucket ($ this ->manager , $ this ->getDatabaseName (), ['codec ' => new TestFileCodec ()]);
362
+ $ bucket ->uploadFromStream ('a ' , $ this ->createStream ('foo ' ));
363
+
364
+ $ cursor = $ bucket ->find ([], ['codec ' => null ]);
365
+ $ fileDocument = current ($ cursor ->toArray ());
366
+
367
+ $ this ->assertInstanceOf (BSONDocument::class, $ fileDocument );
368
+ $ this ->assertSame ('a ' , $ fileDocument ->filename );
369
+ }
370
+
320
371
public function testFindOne (): void
321
372
{
322
373
$ this ->bucket ->uploadFromStream ('a ' , $ this ->createStream ('foo ' ));
@@ -339,6 +390,64 @@ public function testFindOne(): void
339
390
$ this ->assertSameDocument (['filename ' => 'b ' , 'length ' => 6 ], $ fileDocument );
340
391
}
341
392
393
+ public function testFindOneUsesCodec (): void
394
+ {
395
+ $ this ->bucket ->uploadFromStream ('a ' , $ this ->createStream ('foo ' ));
396
+ $ this ->bucket ->uploadFromStream ('b ' , $ this ->createStream ('foobar ' ));
397
+ $ this ->bucket ->uploadFromStream ('c ' , $ this ->createStream ('foobarbaz ' ));
398
+
399
+ $ fileDocument = $ this ->bucket ->findOne (
400
+ ['length ' => ['$lte ' => 6 ]],
401
+ [
402
+ 'sort ' => ['length ' => -1 ],
403
+ 'codec ' => new TestFileCodec (),
404
+ ],
405
+ );
406
+
407
+ $ this ->assertInstanceOf (TestFile::class, $ fileDocument );
408
+ $ this ->assertSame ('b ' , $ fileDocument ->filename );
409
+ $ this ->assertSame (6 , $ fileDocument ->length );
410
+ }
411
+
412
+ public function testFindOneInheritsBucketCodec (): void
413
+ {
414
+ $ bucket = new Bucket ($ this ->manager , $ this ->getDatabaseName (), ['codec ' => new TestFileCodec ()]);
415
+
416
+ $ bucket ->uploadFromStream ('a ' , $ this ->createStream ('foo ' ));
417
+ $ bucket ->uploadFromStream ('b ' , $ this ->createStream ('foobar ' ));
418
+ $ bucket ->uploadFromStream ('c ' , $ this ->createStream ('foobarbaz ' ));
419
+
420
+ $ fileDocument = $ bucket ->findOne (
421
+ ['length ' => ['$lte ' => 6 ]],
422
+ ['sort ' => ['length ' => -1 ]],
423
+ );
424
+
425
+ $ this ->assertInstanceOf (TestFile::class, $ fileDocument );
426
+ $ this ->assertSame ('b ' , $ fileDocument ->filename );
427
+ $ this ->assertSame (6 , $ fileDocument ->length );
428
+ }
429
+
430
+ public function testFindOneResetsInheritedBucketCodec (): void
431
+ {
432
+ $ bucket = new Bucket ($ this ->manager , $ this ->getDatabaseName (), ['codec ' => new TestFileCodec ()]);
433
+
434
+ $ bucket ->uploadFromStream ('a ' , $ this ->createStream ('foo ' ));
435
+ $ bucket ->uploadFromStream ('b ' , $ this ->createStream ('foobar ' ));
436
+ $ bucket ->uploadFromStream ('c ' , $ this ->createStream ('foobarbaz ' ));
437
+
438
+ $ fileDocument = $ bucket ->findOne (
439
+ ['length ' => ['$lte ' => 6 ]],
440
+ [
441
+ 'sort ' => ['length ' => -1 ],
442
+ 'codec ' => null ,
443
+ ],
444
+ );
445
+
446
+ $ this ->assertInstanceOf (BSONDocument::class, $ fileDocument );
447
+ $ this ->assertSame ('b ' , $ fileDocument ->filename );
448
+ $ this ->assertSame (6 , $ fileDocument ->length );
449
+ }
450
+
342
451
public function testGetBucketNameWithCustomValue (): void
343
452
{
344
453
$ bucket = new Bucket ($ this ->manager , $ this ->getDatabaseName (), ['bucketName ' => 'custom_fs ' ]);
@@ -388,6 +497,22 @@ public function testGetFileDocumentForStreamUsesTypeMap(): void
388
497
$ this ->assertSame (['foo ' => 'bar ' ], $ fileDocument ['metadata ' ]->getArrayCopy ());
389
498
}
390
499
500
+ public function testGetFileDocumentForStreamUsesCodec (): void
501
+ {
502
+ $ bucket = new Bucket ($ this ->manager , $ this ->getDatabaseName (), ['codec ' => new TestFileCodec ()]);
503
+
504
+ $ metadata = ['foo ' => 'bar ' ];
505
+ $ stream = $ bucket ->openUploadStream ('filename ' , ['_id ' => 1 , 'metadata ' => $ metadata ]);
506
+
507
+ $ fileDocument = $ bucket ->getFileDocumentForStream ($ stream );
508
+
509
+ $ this ->assertInstanceOf (TestFile::class, $ fileDocument );
510
+
511
+ $ this ->assertSame ('filename ' , $ fileDocument ->filename );
512
+ $ this ->assertInstanceOf (stdClass::class, $ fileDocument ->metadata );
513
+ $ this ->assertSame ($ metadata , (array ) $ fileDocument ->metadata );
514
+ }
515
+
391
516
public function testGetFileDocumentForStreamWithReadableStream (): void
392
517
{
393
518
$ metadata = ['foo ' => 'bar ' ];
0 commit comments