|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit; |
| 4 | + |
| 5 | +use PHPUnit\Framework\Constraint\Constraint; |
| 6 | +use PHPUnit\Framework\Constraint\IsEqual; |
| 7 | +use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; |
| 8 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 9 | +use Symfony\Component\DependencyInjection\Definition; |
| 10 | +use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException; |
| 11 | +use Symfony\Component\DependencyInjection\Reference; |
| 12 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
| 13 | + |
| 14 | +class DefinitionArgumentEqualsServiceLocatorConstraint extends Constraint |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var int|string |
| 18 | + */ |
| 19 | + private $argumentIndex; |
| 20 | + private $expectedValue; |
| 21 | + private $serviceId; |
| 22 | + |
| 23 | + public function __construct($serviceId, $argumentIndex, array $expectedValue) |
| 24 | + { |
| 25 | + parent::__construct(); |
| 26 | + |
| 27 | + if (!(is_string($argumentIndex) || (is_int($argumentIndex) && $argumentIndex >= 0))) { |
| 28 | + throw new \InvalidArgumentException('Expected either a string or a positive integer for $argumentIndex.'); |
| 29 | + } |
| 30 | + |
| 31 | + if (is_string($argumentIndex)) { |
| 32 | + if ('' === $argumentIndex) { |
| 33 | + throw new \InvalidArgumentException('A named argument must begin with a "$".'); |
| 34 | + } |
| 35 | + |
| 36 | + if ('$' !== $argumentIndex[0]) { |
| 37 | + throw new \InvalidArgumentException( |
| 38 | + sprintf('Unknown argument "%s". Did you mean "$%s"?', $argumentIndex, $argumentIndex) |
| 39 | + ); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + $this->serviceId = $serviceId; |
| 44 | + $this->argumentIndex = $argumentIndex; |
| 45 | + $this->expectedValue = array_map( |
| 46 | + function ($serviceId) { |
| 47 | + if (is_string($serviceId)) { |
| 48 | + return new ServiceClosureArgument(new Reference($serviceId)); |
| 49 | + } |
| 50 | + |
| 51 | + if (!$serviceId instanceof ServiceClosureArgument) { |
| 52 | + return new ServiceClosureArgument($serviceId); |
| 53 | + } |
| 54 | + |
| 55 | + return $serviceId; |
| 56 | + }, |
| 57 | + $expectedValue |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public function toString(): string |
| 62 | + { |
| 63 | + if (is_string($this->argumentIndex)) { |
| 64 | + return sprintf( |
| 65 | + 'has an argument named "%s" with the ServiceLocator', |
| 66 | + $this->argumentIndex |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + return sprintf( |
| 71 | + 'has an argument with index %d with the ServiceLocator', |
| 72 | + $this->argumentIndex |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + public function evaluate($other, $description = '', $returnResult = false) |
| 77 | + { |
| 78 | + if (!($other instanceof ContainerBuilder)) { |
| 79 | + throw new \InvalidArgumentException( |
| 80 | + 'Expected an instance of Symfony\Component\DependencyInjection\ContainerBuilder' |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + if (!$this->evaluateArgumentIndex($other->findDefinition($this->serviceId), $returnResult)) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + if (!$this->evaluateArgumentValue($other, $returnResult)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + private function evaluateArgumentIndex(Definition $definition, $returnResult) |
| 96 | + { |
| 97 | + try { |
| 98 | + $definition->getArgument($this->argumentIndex); |
| 99 | + } catch (\Exception $exception) { |
| 100 | + // Older versions of Symfony throw \OutOfBoundsException |
| 101 | + // Newer versions throw Symfony\Component\DependencyInjection\Exception\OutOfBoundsException |
| 102 | + if (!($exception instanceof \OutOfBoundsException || $exception instanceof OutOfBoundsException)) { |
| 103 | + // this was not the expected exception |
| 104 | + throw $exception; |
| 105 | + } |
| 106 | + |
| 107 | + if ($returnResult) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + $this->fail( |
| 112 | + $definition, |
| 113 | + sprintf('The definition has no argument with index %s', $this->argumentIndex) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + private function evaluateArgumentValue(ContainerBuilder $container, $returnResult) |
| 121 | + { |
| 122 | + $definition = $container->findDefinition($this->serviceId); |
| 123 | + $actualValue = $definition->getArgument($this->argumentIndex); |
| 124 | + $serviceLocatorDef = $actualValue; |
| 125 | + |
| 126 | + if ($actualValue instanceof Reference) { |
| 127 | + $serviceLocatorDef = $container->findDefinition((string) $actualValue); |
| 128 | + } elseif (!($actualValue instanceof Definition)) { |
| 129 | + if ($returnResult) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + $this->fail( |
| 134 | + $definition, |
| 135 | + sprintf( |
| 136 | + 'The value of argument with index %s (%s) was expected to an instance of Symfony\Component\DependencyInjection\Reference or \Symfony\Component\DependencyInjection\Definition', |
| 137 | + $this->argumentIndex, |
| 138 | + $this->exporter->export($actualValue) |
| 139 | + ) |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + if (!is_a($serviceLocatorDef->getClass(), ServiceLocator::class, true)) { |
| 144 | + if ($returnResult) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + $this->fail( |
| 149 | + $definition, |
| 150 | + sprintf( |
| 151 | + 'The referenced service class of argument with index %s (%s) was expected to be an instance of Symfony\Component\DependencyInjection\ServiceLocator', |
| 152 | + $this->argumentIndex, |
| 153 | + $this->exporter->export($serviceLocatorDef->getClass()) |
| 154 | + ) |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + // Service locator was provided as context (and therefor a factory) |
| 159 | + if (isset($serviceLocatorDef->getFactory()[1])) { |
| 160 | + $serviceLocatorDef = $container->findDefinition((string) $serviceLocatorDef->getFactory()[0]); |
| 161 | + } |
| 162 | + |
| 163 | + return $this->evaluateServiceDefinition($serviceLocatorDef, $definition, $returnResult); |
| 164 | + } |
| 165 | + |
| 166 | + private function evaluateServiceDefinition(Definition $serviceLocatorDef, Definition $definition, $returnResult): bool |
| 167 | + { |
| 168 | + $actualValue = $serviceLocatorDef->getArgument(0); |
| 169 | + $constraint = new IsEqual($this->expectedValue); |
| 170 | + |
| 171 | + if (!$constraint->evaluate($actualValue, '', true)) { |
| 172 | + if ($returnResult) { |
| 173 | + return false; |
| 174 | + } |
| 175 | + |
| 176 | + $this->fail( |
| 177 | + $definition, |
| 178 | + sprintf( |
| 179 | + 'The value of argument with index %s (%s) does not equal to the expected ServiceLocator service-map (%s)', |
| 180 | + $this->argumentIndex, |
| 181 | + $this->exporter->export($actualValue), |
| 182 | + $this->exporter->export($this->expectedValue) |
| 183 | + ) |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + return true; |
| 188 | + } |
| 189 | +} |
0 commit comments