|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\DependencyInjection\Tests\Argument; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
| 16 | + |
| 17 | +class TaggedIteratorArgumentTest extends TestCase |
| 18 | +{ |
| 19 | + public function testWithTagOnly() |
| 20 | + { |
| 21 | + $taggedIteratorArgument = new TaggedIteratorArgument('foo'); |
| 22 | + |
| 23 | + $this->assertSame('foo', $taggedIteratorArgument->getTag()); |
| 24 | + $this->assertNull($taggedIteratorArgument->getIndexAttribute()); |
| 25 | + $this->assertNull($taggedIteratorArgument->getDefaultIndexMethod()); |
| 26 | + $this->assertFalse($taggedIteratorArgument->needsIndexes()); |
| 27 | + $this->assertNull($taggedIteratorArgument->getDefaultPriorityMethod()); |
| 28 | + } |
| 29 | + |
| 30 | + public function testOnlyTagWithNeedsIndexes() |
| 31 | + { |
| 32 | + $taggedIteratorArgument = new TaggedIteratorArgument('foo', null, null, true); |
| 33 | + |
| 34 | + $this->assertSame('foo', $taggedIteratorArgument->getTag()); |
| 35 | + $this->assertSame('foo', $taggedIteratorArgument->getIndexAttribute()); |
| 36 | + $this->assertSame('getDefaultFooName', $taggedIteratorArgument->getDefaultIndexMethod()); |
| 37 | + $this->assertSame('getDefaultFooPriority', $taggedIteratorArgument->getDefaultPriorityMethod()); |
| 38 | + } |
| 39 | + |
| 40 | + public function testOnlyTagWithNeedsIndexesAndDotTag() |
| 41 | + { |
| 42 | + $taggedIteratorArgument = new TaggedIteratorArgument('foo.bar', null, null, true); |
| 43 | + |
| 44 | + $this->assertSame('foo.bar', $taggedIteratorArgument->getTag()); |
| 45 | + $this->assertSame('bar', $taggedIteratorArgument->getIndexAttribute()); |
| 46 | + $this->assertSame('getDefaultBarName', $taggedIteratorArgument->getDefaultIndexMethod()); |
| 47 | + $this->assertSame('getDefaultBarPriority', $taggedIteratorArgument->getDefaultPriorityMethod()); |
| 48 | + } |
| 49 | + |
| 50 | + public function testOnlyTagWithNeedsIndexesAndDotsTag() |
| 51 | + { |
| 52 | + $taggedIteratorArgument = new TaggedIteratorArgument('foo.bar.baz.qux', null, null, true); |
| 53 | + |
| 54 | + $this->assertSame('foo.bar.baz.qux', $taggedIteratorArgument->getTag()); |
| 55 | + $this->assertSame('qux', $taggedIteratorArgument->getIndexAttribute()); |
| 56 | + $this->assertSame('getDefaultQuxName', $taggedIteratorArgument->getDefaultIndexMethod()); |
| 57 | + $this->assertSame('getDefaultQuxPriority', $taggedIteratorArgument->getDefaultPriorityMethod()); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dataProvider defaultIndexMethodProvider |
| 62 | + */ |
| 63 | + public function testDefaultIndexMethod(?string $indexAttribute, ?string $defaultIndexMethod, ?string $expectedDefaultIndexMethod) |
| 64 | + { |
| 65 | + $taggedIteratorArgument = new TaggedIteratorArgument('foo', $indexAttribute, $defaultIndexMethod); |
| 66 | + |
| 67 | + $this->assertSame($expectedDefaultIndexMethod, $taggedIteratorArgument->getDefaultIndexMethod()); |
| 68 | + } |
| 69 | + |
| 70 | + public function defaultIndexMethodProvider() |
| 71 | + { |
| 72 | + yield 'No indexAttribute and no defaultIndexMethod' => [ |
| 73 | + null, |
| 74 | + null, |
| 75 | + null, |
| 76 | + ]; |
| 77 | + |
| 78 | + yield 'Only indexAttribute' => [ |
| 79 | + 'bar', |
| 80 | + null, |
| 81 | + 'getDefaultBarName', |
| 82 | + ]; |
| 83 | + |
| 84 | + yield 'Only defaultIndexMethod' => [ |
| 85 | + null, |
| 86 | + 'getBaz', |
| 87 | + 'getBaz', |
| 88 | + ]; |
| 89 | + |
| 90 | + yield 'DefaultIndexMethod and indexAttribute' => [ |
| 91 | + 'bar', |
| 92 | + 'getBaz', |
| 93 | + 'getBaz', |
| 94 | + ]; |
| 95 | + |
| 96 | + yield 'Transform to getter with one special char' => [ |
| 97 | + 'bar_baz', |
| 98 | + null, |
| 99 | + 'getDefaultBarBazName', |
| 100 | + ]; |
| 101 | + |
| 102 | + yield 'Transform to getter with multiple special char' => [ |
| 103 | + 'bar-baz-qux', |
| 104 | + null, |
| 105 | + 'getDefaultBarBazQuxName', |
| 106 | + ]; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @dataProvider defaultPriorityMethodProvider |
| 111 | + */ |
| 112 | + public function testDefaultPriorityIndexMethod(?string $indexAttribute, ?string $defaultPriorityMethod, ?string $expectedDefaultPriorityMethod) |
| 113 | + { |
| 114 | + $taggedIteratorArgument = new TaggedIteratorArgument('foo', $indexAttribute, null, false, $defaultPriorityMethod); |
| 115 | + |
| 116 | + $this->assertSame($expectedDefaultPriorityMethod, $taggedIteratorArgument->getDefaultPriorityMethod()); |
| 117 | + } |
| 118 | + |
| 119 | + public function defaultPriorityMethodProvider() |
| 120 | + { |
| 121 | + yield 'No indexAttribute and no defaultPriorityMethod' => [ |
| 122 | + null, |
| 123 | + null, |
| 124 | + null, |
| 125 | + ]; |
| 126 | + |
| 127 | + yield 'Only indexAttribute' => [ |
| 128 | + 'bar', |
| 129 | + null, |
| 130 | + 'getDefaultBarPriority', |
| 131 | + ]; |
| 132 | + |
| 133 | + yield 'Only defaultPriorityMethod' => [ |
| 134 | + null, |
| 135 | + 'getBaz', |
| 136 | + 'getBaz', |
| 137 | + ]; |
| 138 | + |
| 139 | + yield 'DefaultPriorityMethod and indexAttribute' => [ |
| 140 | + 'bar', |
| 141 | + 'getBaz', |
| 142 | + 'getBaz', |
| 143 | + ]; |
| 144 | + |
| 145 | + yield 'Transform to getter with one special char' => [ |
| 146 | + 'bar_baz', |
| 147 | + null, |
| 148 | + 'getDefaultBarBazPriority', |
| 149 | + ]; |
| 150 | + |
| 151 | + yield 'Transform to getter with multiple special char' => [ |
| 152 | + 'bar-baz-qux', |
| 153 | + null, |
| 154 | + 'getDefaultBarBazQuxPriority', |
| 155 | + ]; |
| 156 | + } |
| 157 | +} |
0 commit comments