|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony CMF package. |
| 5 | + * |
| 6 | + * (c) 2011-2017 Symfony CMF |
| 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\Cmf\Bundle\RoutingBundle\Tests\Unit\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; |
| 15 | +use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\ValidationPass; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | + |
| 18 | +class ValidationPassTest extends AbstractCompilerPassTestCase |
| 19 | +{ |
| 20 | + protected function registerCompilerPass(ContainerBuilder $container) |
| 21 | + { |
| 22 | + $container->addCompilerPass(new ValidationPass()); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * It should register the PHPCR documents for validation only when: |
| 27 | + * - the PHP backend is enabled AND |
| 28 | + * - the validator service is available. |
| 29 | + * |
| 30 | + * @dataProvider provideDocumentsValidationContext |
| 31 | + */ |
| 32 | + public function testRegisterDocumentsValidation($hasPhpcr, $hasValidator, $shouldBeRegistered) |
| 33 | + { |
| 34 | + if ($hasPhpcr) { |
| 35 | + $this->setParameter('cmf_routing.backend_type_phpcr', null); |
| 36 | + } |
| 37 | + |
| 38 | + if ($hasValidator) { |
| 39 | + $this->registerService('validator.builder', \stdClass::class); |
| 40 | + } |
| 41 | + |
| 42 | + $this->compile(); |
| 43 | + |
| 44 | + if ($shouldBeRegistered) { |
| 45 | + $r = new \ReflectionClass(ValidationPass::class); |
| 46 | + $dir = \dirname($r->getFilename()); |
| 47 | + |
| 48 | + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
| 49 | + 'validator.builder', |
| 50 | + 'addXmlMappings', |
| 51 | + [["{$dir}/../../Resources/config/validation-phpcr.xml"]] |
| 52 | + ); |
| 53 | + } elseif ($hasValidator) { |
| 54 | + $definition = $this->container->findDefinition('validator.builder'); |
| 55 | + |
| 56 | + $this->assertFalse($definition->hasMethodCall('addXmlMappings')); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Provides the contexts in witch the documents validation registering occurs. |
| 62 | + * |
| 63 | + * A context is: |
| 64 | + * |
| 65 | + * `[$hasPhpcr, $hasValidator, $shouldBeRegistered]` |
| 66 | + * |
| 67 | + * where: |
| 68 | + * - _$hasPhpcr: Is the PHPCR backend enabled ? |
| 69 | + * - _$hasValidator: Is the validator available ? |
| 70 | + * - _$shouldBeRegistered_: Should the documents validation be registered ? |
| 71 | + */ |
| 72 | + public function provideDocumentsValidationContext() |
| 73 | + { |
| 74 | + return [ |
| 75 | + [true, true, true], |
| 76 | + [true, false, false], |
| 77 | + [false, true, false], |
| 78 | + [false, false, false], |
| 79 | + ]; |
| 80 | + } |
| 81 | +} |
0 commit comments