|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the flysystem-bundle project. |
| 5 | + * |
| 6 | + * (c) Titouan Galopin <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Tests\League\FlysystemBundle\Adapter\Builder; |
| 13 | + |
| 14 | +use Doctrine\ODM\MongoDB\Configuration; |
| 15 | +use Doctrine\ODM\MongoDB\DocumentManager; |
| 16 | +use League\Flysystem\GridFS\GridFSAdapter; |
| 17 | +use League\FlysystemBundle\Adapter\Builder\GridFSAdapterDefinitionBuilder; |
| 18 | +use MongoDB\Client; |
| 19 | +use MongoDB\GridFS\Bucket; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 22 | + |
| 23 | +class GridFSAdapterDefinitionBuilderTest extends TestCase |
| 24 | +{ |
| 25 | + public function createBuilder(): GridFSAdapterDefinitionBuilder |
| 26 | + { |
| 27 | + return new GridFSAdapterDefinitionBuilder(); |
| 28 | + } |
| 29 | + |
| 30 | + public static function provideValidOptions(): \Generator |
| 31 | + { |
| 32 | + yield 'doctrine_minimal' => [[ |
| 33 | + 'doctrine_connection' => 'default', |
| 34 | + ]]; |
| 35 | + |
| 36 | + yield 'doctrine_full' => [[ |
| 37 | + 'doctrine_connection' => 'custom', |
| 38 | + 'database' => 'testing', |
| 39 | + 'bucket' => 'avatars', |
| 40 | + ]]; |
| 41 | + |
| 42 | + yield 'config_minimal' => [[ |
| 43 | + 'mongodb_uri' => 'mongodb://localhost:27017/', |
| 44 | + 'database' => 'testing', |
| 45 | + ]]; |
| 46 | + |
| 47 | + yield 'config_full' => [[ |
| 48 | + 'mongodb_uri' => 'mongodb://server1:27017,server2:27017/', |
| 49 | + 'mongodb_uri_options' => ['appname' => 'flysystem'], |
| 50 | + 'mongodb_driver_options' => ['disableClientPersistence' => false], |
| 51 | + 'database' => 'testing', |
| 52 | + 'bucket' => 'avatars', |
| 53 | + ]]; |
| 54 | + |
| 55 | + yield 'service' => [[ |
| 56 | + 'bucket' => 'bucket', |
| 57 | + ]]; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dataProvider provideValidOptions |
| 62 | + */ |
| 63 | + public function testCreateDefinition($options) |
| 64 | + { |
| 65 | + $this->assertSame(GridFSAdapter::class, $this->createBuilder()->createDefinition($options, null)->getClass()); |
| 66 | + } |
| 67 | + |
| 68 | + public static function provideInvalidOptions(): \Generator |
| 69 | + { |
| 70 | + yield 'empty' => [ |
| 71 | + [], |
| 72 | + 'Flysystem GridFS configuration requires a "bucket" service name, a "mongodb_uri" or a "doctrine_connection" name', |
| 73 | + ]; |
| 74 | + |
| 75 | + yield 'no database with mongodb_uri' => [ |
| 76 | + ['mongodb_uri' => 'mongodb://127.0.0.1:27017/'], |
| 77 | + 'MongoDB "database" name is required for Flysystem GridFS configuration', |
| 78 | + ]; |
| 79 | + |
| 80 | + yield 'both doctrine_connection and mongodb_uri' => [ |
| 81 | + ['doctrine_connection' => 'default', 'mongodb_uri' => 'mongodb://127.0.0.1:27017/'], |
| 82 | + 'In GridFS configuration, "doctrine_connection" and "mongodb_uri" options cannot be set together.', |
| 83 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @dataProvider provideInvalidOptions |
| 88 | + */ |
| 89 | + public function testInvalidOptions(array $options, string $message) |
| 90 | + { |
| 91 | + $builder = $this->createBuilder(); |
| 92 | + |
| 93 | + $this->expectException(InvalidArgumentException::class); |
| 94 | + $this->expectExceptionMessage($message); |
| 95 | + |
| 96 | + $builder->createDefinition($options, null); |
| 97 | + } |
| 98 | + |
| 99 | + public function testInitializeBucketFromDocumentManager() |
| 100 | + { |
| 101 | + $client = new Client(); |
| 102 | + $config = new Configuration(); |
| 103 | + $config->setDefaultDB('testing'); |
| 104 | + $dm = $this->createMock(DocumentManager::class); |
| 105 | + $dm->expects($this->once())->method('getClient')->willReturn($client); |
| 106 | + $dm->expects($this->once())->method('getConfiguration')->willReturn($config); |
| 107 | + |
| 108 | + $bucket = GridFSAdapterDefinitionBuilder::initializeBucketFromDocumentManager($dm, null, 'avatars'); |
| 109 | + |
| 110 | + $this->assertInstanceOf(Bucket::class, $bucket); |
| 111 | + $this->assertSame('testing', $bucket->getDatabaseName()); |
| 112 | + $this->assertSame('avatars', $bucket->getBucketName()); |
| 113 | + } |
| 114 | + |
| 115 | + public function testInitializeBucketFromConfig() |
| 116 | + { |
| 117 | + $bucket = GridFSAdapterDefinitionBuilder::initializeBucketFromConfig( |
| 118 | + 'mongodb://server:27017/', |
| 119 | + ['appname' => 'flysystem'], |
| 120 | + ['disableClientPersistence' => false], |
| 121 | + 'testing', |
| 122 | + 'avatars' |
| 123 | + ); |
| 124 | + |
| 125 | + $this->assertInstanceOf(Bucket::class, $bucket); |
| 126 | + $this->assertSame('testing', $bucket->getDatabaseName()); |
| 127 | + $this->assertSame('avatars', $bucket->getBucketName()); |
| 128 | + } |
| 129 | +} |
0 commit comments