|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the flysystem-bundle project. |
| 5 | + * |
| 6 | + * (c) Titouan Galopin <galopintitouan@gmail.com> |
| 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\Lazy; |
| 13 | + |
| 14 | +use League\Flysystem\InMemory\InMemoryFilesystemAdapter; |
| 15 | +use League\FlysystemBundle\Lazy\LazyFactory; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 18 | + |
| 19 | +class LazyFactoryTest extends TestCase |
| 20 | +{ |
| 21 | + public function testItPreventsInfiniteRecursion(): void |
| 22 | + { |
| 23 | + $containerBuilder = new ContainerBuilder(); |
| 24 | + $factory = new LazyFactory($containerBuilder); |
| 25 | + |
| 26 | + $this->expectException(\InvalidArgumentException::class); |
| 27 | + $this->expectExceptionMessage('The "lazy" adapter source is referring to itself as "lazy_storage", which would lead to infinite recursion.'); |
| 28 | + $factory->createStorage('lazy_storage', 'lazy_storage'); |
| 29 | + } |
| 30 | + |
| 31 | + public function testItErrorsWhenServiceIsNotAvailable(): void |
| 32 | + { |
| 33 | + $containerBuilder = new ContainerBuilder(); |
| 34 | + $factory = new LazyFactory($containerBuilder); |
| 35 | + |
| 36 | + $this->expectException(\InvalidArgumentException::class); |
| 37 | + $this->expectExceptionMessage('You have requested a non-existent source storage "source_storage" in lazy storage "lazy_storage".'); |
| 38 | + $factory->createStorage('source_storage', 'lazy_storage'); |
| 39 | + } |
| 40 | + |
| 41 | + public function testItReturnsTheRequestedStorageService(): void |
| 42 | + { |
| 43 | + $actual = new InMemoryFilesystemAdapter(); |
| 44 | + $lazy = new InMemoryFilesystemAdapter(); |
| 45 | + |
| 46 | + $containerBuilder = new ContainerBuilder(); |
| 47 | + $containerBuilder->set('source_storage', $actual); |
| 48 | + $containerBuilder->set('lazy_storage', $lazy); |
| 49 | + |
| 50 | + $factory = new LazyFactory($containerBuilder); |
| 51 | + |
| 52 | + $adapter = $factory->createStorage('source_storage', 'lazy_storage'); |
| 53 | + self::assertSame($actual, $adapter); |
| 54 | + } |
| 55 | +} |
0 commit comments