Skip to content

Commit 6660c19

Browse files
Merge v1.x into v2.x (#1422)
2 parents 74e90aa + b27a61d commit 6660c19

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

tests/GridFS/BucketFunctionalTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,22 @@ public function testUploadingFirstFileCreatesIndexes(): void
782782

783783
public function testExistingIndexIsReused(): void
784784
{
785+
// The collections may exist from other tests, ensure they are removed
786+
// before and after to avoid potential conflicts.
787+
$this->dropCollection($this->getDatabaseName(), 'fs.chunks');
788+
$this->dropCollection($this->getDatabaseName(), 'fs.files');
789+
790+
// Create indexes with different numeric types before interacting with
791+
// GridFS to assert that the library respects the existing indexes and
792+
// does not attempt to create its own.
785793
$this->filesCollection->createIndex(['filename' => 1.0, 'uploadDate' => 1], ['name' => 'test']);
786794
$this->chunksCollection->createIndex(['files_id' => 1.0, 'n' => 1], ['name' => 'test', 'unique' => true]);
787795

796+
$this->assertIndexExists('fs.files', 'test');
797+
$this->assertIndexExists('fs.chunks', 'test', function (IndexInfo $info): void {
798+
$this->assertTrue($info->isUnique());
799+
});
800+
788801
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
789802

790803
$this->assertIndexNotExists($this->filesCollection->getCollectionName(), 'filename_1_uploadDate_1');

tests/GridFS/FunctionalTestCase.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use MongoDB\Collection;
66
use MongoDB\GridFS\Bucket;
7+
use MongoDB\Operation\DropCollection;
78
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
89

910
use function fopen;
@@ -28,10 +29,33 @@ public function setUp(): void
2829
parent::setUp();
2930

3031
$this->bucket = new Bucket($this->manager, $this->getDatabaseName());
31-
$this->bucket->drop();
3232

33-
$this->chunksCollection = $this->createCollection($this->getDatabaseName(), 'fs.chunks');
34-
$this->filesCollection = $this->createCollection($this->getDatabaseName(), 'fs.files');
33+
$this->chunksCollection = new Collection($this->manager, $this->getDatabaseName(), 'fs.chunks');
34+
$this->filesCollection = new Collection($this->manager, $this->getDatabaseName(), 'fs.files');
35+
}
36+
37+
public function tearDown(): void
38+
{
39+
$this->chunksCollection->deleteMany([]);
40+
$this->filesCollection->deleteMany([]);
41+
42+
parent::tearDown();
43+
}
44+
45+
/**
46+
* The bucket's collections are created by the first test that runs and
47+
* kept for all subsequent tests. This is done to avoid creating the
48+
* collections and their indexes for each test, which would be slow.
49+
*
50+
* @beforeClass
51+
* @afterClass
52+
*/
53+
public static function dropCollectionsBeforeAfterClass(): void
54+
{
55+
$manager = static::createTestManager();
56+
57+
(new DropCollection(self::getDatabaseName(), 'fs.chunks'))->execute($manager->selectServer());
58+
(new DropCollection(self::getDatabaseName(), 'fs.files'))->execute($manager->selectServer());
3559
}
3660

3761
/**

0 commit comments

Comments
 (0)