|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Vich\UploaderBundle\Tests\Naming; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 6 | +use Vich\UploaderBundle\Naming\ChainDirectoryNamer; |
| 7 | +use Vich\UploaderBundle\Naming\DirectoryNamerInterface; |
| 8 | +use Vich\UploaderBundle\Tests\DummyEntity; |
| 9 | +use Vich\UploaderBundle\Tests\TestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author Guillaume Loulier |
| 13 | + */ |
| 14 | +final class ChainDirectoryNamerTest extends TestCase |
| 15 | +{ |
| 16 | + public static function chainDataProvider(): array |
| 17 | + { |
| 18 | + return [ |
| 19 | + 'two namers' => [['dir1', 'dir2'], '/', 'dir1/dir2'], |
| 20 | + 'three namers' => [['a', 'b', 'c'], '/', 'a/b/c'], |
| 21 | + 'custom separator' => [['a', 'b'], '-', 'a-b'], |
| 22 | + 'with empty values' => [['a', '', 'c'], '/', 'a/c'], |
| 23 | + 'single namer' => [['only'], '/', 'only'], |
| 24 | + 'no namers' => [[], '/', ''], |
| 25 | + ]; |
| 26 | + } |
| 27 | + |
| 28 | + #[DataProvider('chainDataProvider')] |
| 29 | + public function testDirectoryNameChainsNamers(array $namerResults, string $separator, string $expected): void |
| 30 | + { |
| 31 | + $entity = new DummyEntity(); |
| 32 | + $mapping = $this->getPropertyMappingMock(); |
| 33 | + |
| 34 | + $namers = []; |
| 35 | + foreach ($namerResults as $result) { |
| 36 | + $namer = $this->createMock(DirectoryNamerInterface::class); |
| 37 | + $namer->expects(self::once()) |
| 38 | + ->method('directoryName') |
| 39 | + ->with($entity, $mapping) |
| 40 | + ->willReturn($result); |
| 41 | + $namers[] = $namer; |
| 42 | + } |
| 43 | + |
| 44 | + $chainNamer = new ChainDirectoryNamer(); |
| 45 | + $chainNamer->setNamers($namers); |
| 46 | + $chainNamer->configure(['separator' => $separator]); |
| 47 | + |
| 48 | + self::assertSame($expected, $chainNamer->directoryName($entity, $mapping)); |
| 49 | + } |
| 50 | + |
| 51 | + public function testDefaultSeparatorIsSlash(): void |
| 52 | + { |
| 53 | + $entity = new DummyEntity(); |
| 54 | + $mapping = $this->getPropertyMappingMock(); |
| 55 | + |
| 56 | + $namer1 = $this->createMock(DirectoryNamerInterface::class); |
| 57 | + $namer1->method('directoryName')->willReturn('a'); |
| 58 | + |
| 59 | + $namer2 = $this->createMock(DirectoryNamerInterface::class); |
| 60 | + $namer2->method('directoryName')->willReturn('b'); |
| 61 | + |
| 62 | + $chainNamer = new ChainDirectoryNamer(); |
| 63 | + $chainNamer->setNamers([$namer1, $namer2]); |
| 64 | + |
| 65 | + self::assertSame('a/b', $chainNamer->directoryName($entity, $mapping)); |
| 66 | + } |
| 67 | + |
| 68 | + public function testConfigureWithoutSeparatorKeepsDefault(): void |
| 69 | + { |
| 70 | + $entity = new DummyEntity(); |
| 71 | + $mapping = $this->getPropertyMappingMock(); |
| 72 | + |
| 73 | + $namer1 = $this->createMock(DirectoryNamerInterface::class); |
| 74 | + $namer1->method('directoryName')->willReturn('a'); |
| 75 | + |
| 76 | + $namer2 = $this->createMock(DirectoryNamerInterface::class); |
| 77 | + $namer2->method('directoryName')->willReturn('b'); |
| 78 | + |
| 79 | + $chainNamer = new ChainDirectoryNamer(); |
| 80 | + $chainNamer->setNamers([$namer1, $namer2]); |
| 81 | + $chainNamer->configure([]); // Empty options should not change the default separator |
| 82 | + |
| 83 | + self::assertSame('a/b', $chainNamer->directoryName($entity, $mapping)); |
| 84 | + } |
| 85 | + |
| 86 | + public function testEmptyStringFromNamerIsFiltered(): void |
| 87 | + { |
| 88 | + $entity = new DummyEntity(); |
| 89 | + $mapping = $this->getPropertyMappingMock(); |
| 90 | + |
| 91 | + $namer1 = $this->createMock(DirectoryNamerInterface::class); |
| 92 | + $namer1->method('directoryName')->willReturn('start'); |
| 93 | + |
| 94 | + $namer2 = $this->createMock(DirectoryNamerInterface::class); |
| 95 | + $namer2->method('directoryName')->willReturn(''); |
| 96 | + |
| 97 | + $namer3 = $this->createMock(DirectoryNamerInterface::class); |
| 98 | + $namer3->method('directoryName')->willReturn('end'); |
| 99 | + |
| 100 | + $chainNamer = new ChainDirectoryNamer(); |
| 101 | + $chainNamer->setNamers([$namer1, $namer2, $namer3]); |
| 102 | + |
| 103 | + self::assertSame('start/end', $chainNamer->directoryName($entity, $mapping)); |
| 104 | + } |
| 105 | +} |
0 commit comments