Skip to content

Commit ed91f36

Browse files
committed
Add tests and remove Client instanciation from Configuration
1 parent 6919fb1 commit ed91f36

File tree

3 files changed

+136
-13
lines changed

3 files changed

+136
-13
lines changed

src/MongoDBServiceProvider.php

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

55
namespace MongoDB\Laravel;
66

7+
use Closure;
78
use Illuminate\Cache\CacheManager;
89
use Illuminate\Cache\Repository;
910
use Illuminate\Filesystem\FilesystemAdapter;
@@ -14,7 +15,6 @@
1415
use League\Flysystem\Filesystem;
1516
use League\Flysystem\GridFS\GridFSAdapter;
1617
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
17-
use MongoDB\Client;
1818
use MongoDB\GridFS\Bucket;
1919
use MongoDB\Laravel\Cache\MongoStore;
2020
use MongoDB\Laravel\Eloquent\Model;
@@ -106,23 +106,15 @@ private function registerFlysystemAdapter(): void
106106

107107
$bucket = $connection->getMongoClient()
108108
->selectDatabase($config['database'] ?? $connection->getDatabaseName())
109-
->selectGridFSBucket(['bucketName' => $config['bucket'] ?? 'fs']);
110-
} elseif (isset($config['mongodb_uri'])) {
111-
// Create a new MongoDB client from the config
112-
if (! isset($config['database'])) {
113-
throw new InvalidArgumentException('MongoDB "database" name is required for filesystem GridFS configuration');
114-
}
109+
->selectGridFSBucket(['bucketName' => $config['bucket'] ?? 'fs', 'disableMD5' => true]);
115110

116-
$bucket = (new Client($config['mongodb_uri'], $config['mongodb_uri_options'] ?? [], $config['mongodb_driver_options'] ?? []))
117-
->selectDatabase($config['database'])
118-
->selectGridFSBucket(['bucketName' => $config['bucket'] ?? 'fs']);
119-
} elseif (isset($config['bucket'])) {
120111
// Allows setting the bucket instance directly
112+
} elseif (isset($config['bucket'])) {
121113
$bucket = $config['bucket'];
114+
// Resolves the "bucket" service
122115
if (is_string($bucket)) {
123-
// Resolves the "bucket" service
124116
$bucket = $app->get($bucket);
125-
} elseif ($bucket instanceof \Closure) {
117+
} elseif ($bucket instanceof Closure) {
126118
$bucket = $bucket($app, $config);
127119
}
128120

tests/FilesystemsTest.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests;
4+
5+
use Generator;
6+
use Illuminate\Foundation\Application;
7+
use Illuminate\Support\Facades\DB;
8+
use Illuminate\Support\Facades\Storage;
9+
use League\Flysystem\UnableToWriteFile;
10+
use PHPUnit\Framework\Attributes\DataProvider;
11+
12+
use function env;
13+
use function microtime;
14+
15+
class FilesystemsTest extends TestCase
16+
{
17+
public function tearDown(): void
18+
{
19+
DB::connection('mongodb')->getMongoDB()->drop();
20+
21+
parent::tearDown();
22+
}
23+
24+
public static function provideDiskConfigurations(): Generator
25+
{
26+
yield [
27+
'gridfs-connection-minimal',
28+
[
29+
'driver' => 'gridfs',
30+
'connection' => 'mongodb',
31+
],
32+
];
33+
34+
yield [
35+
'gridfs-connection-full',
36+
[
37+
'driver' => 'gridfs',
38+
'connection' => 'mongodb',
39+
'database' => env('MONGODB_DATABASE', 'unittest'),
40+
'bucket' => 'fs',
41+
'prefix' => 'foo/',
42+
],
43+
];
44+
45+
yield [
46+
'gridfs-bucket-service',
47+
[
48+
'driver' => 'gridfs',
49+
'bucket' => 'bucket',
50+
],
51+
];
52+
53+
yield [
54+
'gridfs-bucket-factory',
55+
[
56+
'driver' => 'gridfs',
57+
'bucket' => static fn (Application $app) => $app['db']
58+
->connection('mongodb')
59+
->getMongoDB()
60+
->selectGridFSBucket(),
61+
],
62+
];
63+
}
64+
65+
#[DataProvider('provideDiskConfigurations')]
66+
public function testConfig(string $name, array $options)
67+
{
68+
// Service used by "gridfs-bucket-service"
69+
$this->app->singleton('bucket', static fn (Application $app) => $app['db']
70+
->connection('mongodb')
71+
->getMongoDB()
72+
->selectGridFSBucket());
73+
74+
$this->app['config']->set('filesystems.disks.' . $name, $options);
75+
76+
$disk = Storage::disk($name);
77+
$disk->put($filename = $name . '.txt', $value = microtime());
78+
$this->assertEquals($value, $disk->get($filename), 'File saved');
79+
}
80+
81+
public function testReadOnlyAndThrowOption()
82+
{
83+
$this->app['config']->set('filesystems.disks.gridfs-readonly', [
84+
'driver' => 'gridfs',
85+
'connection' => 'mongodb',
86+
// Use ReadOnlyAdapter
87+
'read-only' => true,
88+
// Throw exceptions
89+
'throw' => true,
90+
]);
91+
92+
$disk = Storage::disk('gridfs-readonly');
93+
94+
$this->expectException(UnableToWriteFile::class);
95+
$this->expectExceptionMessage('This is a readonly adapter.');
96+
97+
$disk->put('file.txt', '');
98+
}
99+
}

tests/config/filesystems.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Foundation\Application;
6+
7+
return [
8+
'disks' => [
9+
'gridfs-connection-minimal' => [
10+
'driver' => 'gridfs',
11+
'connection' => 'mongodb',
12+
],
13+
14+
'gridfs-connection-full' => [
15+
'driver' => 'gridfs',
16+
'connection' => 'mongodb',
17+
],
18+
19+
'gridfs-bucket-service' => [
20+
'driver' => 'gridfs',
21+
'bucket' => 'bucket',
22+
],
23+
24+
'gridfs-bucket-factory' => [
25+
'driver' => 'gridfs',
26+
'bucket' => static fn (Application $app) => $app['db']
27+
->connection('mongodb')
28+
->getMongoDB()
29+
->selectGridFSBucket(),
30+
],
31+
],
32+
];

0 commit comments

Comments
 (0)