|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 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 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sylius\Resource\Tests\Symfony\DependencyInjection\Compiler; |
| 15 | + |
| 16 | +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; |
| 17 | +use Sylius\Resource\Symfony\DependencyInjection\Compiler\MetadataMutatorPass; |
| 18 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 19 | +use Symfony\Component\DependencyInjection\Definition; |
| 20 | +use Symfony\Component\DependencyInjection\Reference; |
| 21 | + |
| 22 | +final class MetadataMutatorPassTest extends AbstractCompilerPassTestCase |
| 23 | +{ |
| 24 | + public function testItDoesNothingWhenResourceMutatorCollectionDoesNotExist(): void |
| 25 | + { |
| 26 | + $this->compile(); |
| 27 | + |
| 28 | + $this->assertContainerBuilderNotHasService('sylius.metadata.mutator_collection.resource'); |
| 29 | + } |
| 30 | + |
| 31 | + public function testItDoesNothingWhenOperationMutatorCollectionDoesNotExist(): void |
| 32 | + { |
| 33 | + $this->compile(); |
| 34 | + |
| 35 | + $this->assertContainerBuilderNotHasService('sylius.metadata.mutator_collection.operation'); |
| 36 | + } |
| 37 | + |
| 38 | + public function testItAddsResourceMutatorsToCollection(): void |
| 39 | + { |
| 40 | + $mutatorCollectionDefinition = new Definition(); |
| 41 | + $this->setDefinition('sylius.metadata.mutator_collection.resource', $mutatorCollectionDefinition); |
| 42 | + |
| 43 | + $mutatorDefinition = new Definition(); |
| 44 | + $mutatorDefinition->addTag('sylius.resource_mutator', ['resourceClass' => 'App\Entity\Product']); |
| 45 | + $this->setDefinition('app.resource_mutator.product', $mutatorDefinition); |
| 46 | + |
| 47 | + $this->compile(); |
| 48 | + |
| 49 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 50 | + 'sylius.metadata.mutator_collection.resource', |
| 51 | + 'add', |
| 52 | + [ |
| 53 | + 'App\Entity\Product', |
| 54 | + new Reference('app.resource_mutator.product'), |
| 55 | + ], |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public function testItAddsMultipleResourceMutatorsToCollection(): void |
| 60 | + { |
| 61 | + $mutatorCollectionDefinition = new Definition(); |
| 62 | + $this->setDefinition('sylius.metadata.mutator_collection.resource', $mutatorCollectionDefinition); |
| 63 | + |
| 64 | + $productMutatorDefinition = new Definition(); |
| 65 | + $productMutatorDefinition->addTag('sylius.resource_mutator', ['resourceClass' => 'App\Entity\Product']); |
| 66 | + $this->setDefinition('app.resource_mutator.product', $productMutatorDefinition); |
| 67 | + |
| 68 | + $customerMutatorDefinition = new Definition(); |
| 69 | + $customerMutatorDefinition->addTag('sylius.resource_mutator', ['resourceClass' => 'App\Entity\Customer']); |
| 70 | + $this->setDefinition('app.resource_mutator.customer', $customerMutatorDefinition); |
| 71 | + |
| 72 | + $this->compile(); |
| 73 | + |
| 74 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 75 | + 'sylius.metadata.mutator_collection.resource', |
| 76 | + 'add', |
| 77 | + [ |
| 78 | + 'App\Entity\Product', |
| 79 | + new Reference('app.resource_mutator.product'), |
| 80 | + ], |
| 81 | + ); |
| 82 | + |
| 83 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 84 | + 'sylius.metadata.mutator_collection.resource', |
| 85 | + 'add', |
| 86 | + [ |
| 87 | + 'App\Entity\Customer', |
| 88 | + new Reference('app.resource_mutator.customer'), |
| 89 | + ], |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + public function testItAddsResourceMutatorWithMultipleTags(): void |
| 94 | + { |
| 95 | + $mutatorCollectionDefinition = new Definition(); |
| 96 | + $this->setDefinition('sylius.metadata.mutator_collection.resource', $mutatorCollectionDefinition); |
| 97 | + |
| 98 | + $mutatorDefinition = new Definition(); |
| 99 | + $mutatorDefinition->addTag('sylius.resource_mutator', ['resourceClass' => 'App\Entity\Product']); |
| 100 | + $mutatorDefinition->addTag('sylius.resource_mutator', ['resourceClass' => 'App\Entity\Customer']); |
| 101 | + $this->setDefinition('app.resource_mutator.multi', $mutatorDefinition); |
| 102 | + |
| 103 | + $this->compile(); |
| 104 | + |
| 105 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 106 | + 'sylius.metadata.mutator_collection.resource', |
| 107 | + 'add', |
| 108 | + [ |
| 109 | + 'App\Entity\Product', |
| 110 | + new Reference('app.resource_mutator.multi'), |
| 111 | + ], |
| 112 | + ); |
| 113 | + |
| 114 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 115 | + 'sylius.metadata.mutator_collection.resource', |
| 116 | + 'add', |
| 117 | + [ |
| 118 | + 'App\Entity\Customer', |
| 119 | + new Reference('app.resource_mutator.multi'), |
| 120 | + ], |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + public function testItAddsOperationMutatorsToCollection(): void |
| 125 | + { |
| 126 | + $mutatorCollectionDefinition = new Definition(); |
| 127 | + $this->setDefinition('sylius.metadata.mutator_collection.operation', $mutatorCollectionDefinition); |
| 128 | + |
| 129 | + $mutatorDefinition = new Definition(); |
| 130 | + $mutatorDefinition->addTag('sylius.operation_mutator', ['operationName' => 'app_product_create']); |
| 131 | + $this->setDefinition('app.operation_mutator.product_create', $mutatorDefinition); |
| 132 | + |
| 133 | + $this->compile(); |
| 134 | + |
| 135 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 136 | + 'sylius.metadata.mutator_collection.operation', |
| 137 | + 'add', |
| 138 | + [ |
| 139 | + 'app_product_create', |
| 140 | + new Reference('app.operation_mutator.product_create'), |
| 141 | + ], |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + public function testItAddsMultipleOperationMutatorsToCollection(): void |
| 146 | + { |
| 147 | + $mutatorCollectionDefinition = new Definition(); |
| 148 | + $this->setDefinition('sylius.metadata.mutator_collection.operation', $mutatorCollectionDefinition); |
| 149 | + |
| 150 | + $createMutatorDefinition = new Definition(); |
| 151 | + $createMutatorDefinition->addTag('sylius.operation_mutator', ['operationName' => 'app_product_create']); |
| 152 | + $this->setDefinition('app.operation_mutator.product_create', $createMutatorDefinition); |
| 153 | + |
| 154 | + $updateMutatorDefinition = new Definition(); |
| 155 | + $updateMutatorDefinition->addTag('sylius.operation_mutator', ['operationName' => 'app_product_update']); |
| 156 | + $this->setDefinition('app.operation_mutator.product_update', $updateMutatorDefinition); |
| 157 | + |
| 158 | + $this->compile(); |
| 159 | + |
| 160 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 161 | + 'sylius.metadata.mutator_collection.operation', |
| 162 | + 'add', |
| 163 | + [ |
| 164 | + 'app_product_create', |
| 165 | + new Reference('app.operation_mutator.product_create'), |
| 166 | + ], |
| 167 | + ); |
| 168 | + |
| 169 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 170 | + 'sylius.metadata.mutator_collection.operation', |
| 171 | + 'add', |
| 172 | + [ |
| 173 | + 'app_product_update', |
| 174 | + new Reference('app.operation_mutator.product_update'), |
| 175 | + ], |
| 176 | + ); |
| 177 | + } |
| 178 | + |
| 179 | + public function testItAddsOperationMutatorWithMultipleTags(): void |
| 180 | + { |
| 181 | + $mutatorCollectionDefinition = new Definition(); |
| 182 | + $this->setDefinition('sylius.metadata.mutator_collection.operation', $mutatorCollectionDefinition); |
| 183 | + |
| 184 | + $mutatorDefinition = new Definition(); |
| 185 | + $mutatorDefinition->addTag('sylius.operation_mutator', ['operationName' => 'app_product_create']); |
| 186 | + $mutatorDefinition->addTag('sylius.operation_mutator', ['operationName' => 'app_product_update']); |
| 187 | + $this->setDefinition('app.operation_mutator.multi', $mutatorDefinition); |
| 188 | + |
| 189 | + $this->compile(); |
| 190 | + |
| 191 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 192 | + 'sylius.metadata.mutator_collection.operation', |
| 193 | + 'add', |
| 194 | + [ |
| 195 | + 'app_product_create', |
| 196 | + new Reference('app.operation_mutator.multi'), |
| 197 | + ], |
| 198 | + ); |
| 199 | + |
| 200 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 201 | + 'sylius.metadata.mutator_collection.operation', |
| 202 | + 'add', |
| 203 | + [ |
| 204 | + 'app_product_update', |
| 205 | + new Reference('app.operation_mutator.multi'), |
| 206 | + ], |
| 207 | + ); |
| 208 | + } |
| 209 | + |
| 210 | + public function testItProcessesBothResourceAndOperationMutators(): void |
| 211 | + { |
| 212 | + $resourceMutatorCollectionDefinition = new Definition(); |
| 213 | + $this->setDefinition('sylius.metadata.mutator_collection.resource', $resourceMutatorCollectionDefinition); |
| 214 | + |
| 215 | + $operationMutatorCollectionDefinition = new Definition(); |
| 216 | + $this->setDefinition('sylius.metadata.mutator_collection.operation', $operationMutatorCollectionDefinition); |
| 217 | + |
| 218 | + $resourceMutatorDefinition = new Definition(); |
| 219 | + $resourceMutatorDefinition->addTag('sylius.resource_mutator', ['resourceClass' => 'App\Entity\Product']); |
| 220 | + $this->setDefinition('app.resource_mutator', $resourceMutatorDefinition); |
| 221 | + |
| 222 | + $operationMutatorDefinition = new Definition(); |
| 223 | + $operationMutatorDefinition->addTag('sylius.operation_mutator', ['operationName' => 'app_product_create']); |
| 224 | + $this->setDefinition('app.operation_mutator', $operationMutatorDefinition); |
| 225 | + |
| 226 | + $this->compile(); |
| 227 | + |
| 228 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 229 | + 'sylius.metadata.mutator_collection.resource', |
| 230 | + 'add', |
| 231 | + [ |
| 232 | + 'App\Entity\Product', |
| 233 | + new Reference('app.resource_mutator'), |
| 234 | + ], |
| 235 | + ); |
| 236 | + |
| 237 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 238 | + 'sylius.metadata.mutator_collection.operation', |
| 239 | + 'add', |
| 240 | + [ |
| 241 | + 'app_product_create', |
| 242 | + new Reference('app.operation_mutator'), |
| 243 | + ], |
| 244 | + ); |
| 245 | + } |
| 246 | + |
| 247 | + protected function registerCompilerPass(ContainerBuilder $container): void |
| 248 | + { |
| 249 | + $container->addCompilerPass(new MetadataMutatorPass()); |
| 250 | + } |
| 251 | +} |
0 commit comments