|
3 | 3 | namespace Matthias\SymfonyServiceDefinitionValidator\Tests;
|
4 | 4 |
|
5 | 5 | use Matthias\SymfonyServiceDefinitionValidator\ArgumentValidator;
|
| 6 | +use Matthias\SymfonyServiceDefinitionValidator\Exception\TypeHintMismatchException; |
6 | 7 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
7 | 8 | use Symfony\Component\DependencyInjection\Definition;
|
8 | 9 | use Symfony\Component\DependencyInjection\Reference;
|
| 10 | +use Symfony\Component\ExpressionLanguage\Expression; |
9 | 11 |
|
10 | 12 | class ArgumentValidatorTest extends \PHPUnit_Framework_TestCase
|
11 | 13 | {
|
@@ -82,6 +84,81 @@ public function testFailsWhenParameterHasArrayTypeHintButArgumentIsNotArray()
|
82 | 84 | $validator->validate($parameter, $argument);
|
83 | 85 | }
|
84 | 86 |
|
| 87 | + public function testFailsWhenResultOfExpressionIsNotAnObjectOfTheExpectedClass() |
| 88 | + { |
| 89 | + $class = 'Matthias\SymfonyServiceDefinitionValidator\Tests\Fixtures\ClassWithTypeHintedConstructorArgument'; |
| 90 | + |
| 91 | + $parameter = new \ReflectionParameter(array($class, '__construct'), 'expected'); |
| 92 | + $argument = new Expression('service("service_wrong_class")'); |
| 93 | + |
| 94 | + $containerBuilder = new ContainerBuilder(); |
| 95 | + $containerBuilder->setDefinition('service_wrong_class', new Definition('stdClass')); |
| 96 | + |
| 97 | + $validator = new ArgumentValidator($containerBuilder, $this->createMockResultingClassResolver()); |
| 98 | + |
| 99 | + $this->setExpectedException('Matthias\SymfonyServiceDefinitionValidator\Exception\TypeHintMismatchException', 'ExpectedClass'); |
| 100 | + |
| 101 | + $validator->validate($parameter, $argument); |
| 102 | + } |
| 103 | + |
| 104 | + public function testFailsWhenResultOfExpressionIsNotAnObject() |
| 105 | + { |
| 106 | + $class = 'Matthias\SymfonyServiceDefinitionValidator\Tests\Fixtures\ClassWithTypeHintedConstructorArgument'; |
| 107 | + |
| 108 | + $parameter = new \ReflectionParameter(array($class, '__construct'), 'expected'); |
| 109 | + $argument = new Expression('"a string"'); |
| 110 | + |
| 111 | + $validator = new ArgumentValidator(new ContainerBuilder(), $this->createMockResultingClassResolver()); |
| 112 | + |
| 113 | + $this->setExpectedException('Matthias\SymfonyServiceDefinitionValidator\Exception\TypeHintMismatchException', 'ExpectedClass'); |
| 114 | + |
| 115 | + $validator->validate($parameter, $argument); |
| 116 | + } |
| 117 | + |
| 118 | + public function testFailsWhenResultOfExpressionIsNullButNullIsNotAllowed() |
| 119 | + { |
| 120 | + $class = 'Matthias\SymfonyServiceDefinitionValidator\Tests\Fixtures\ClassWithTypeHintedOptionalConstructorArgument'; |
| 121 | + |
| 122 | + $parameter = new \ReflectionParameter(array($class, '__construct'), 'expected'); |
| 123 | + $argument = new Expression('null'); |
| 124 | + |
| 125 | + $validator = new ArgumentValidator(new ContainerBuilder(), $this->createMockResultingClassResolver()); |
| 126 | + |
| 127 | + try { |
| 128 | + $validator->validate($parameter, $argument); |
| 129 | + } catch (TypeHintMismatchException $exception) { |
| 130 | + $this->fail('null argument should be allowed'); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public function testFailsIfSyntaxOfExpressionIsInvalid() |
| 135 | + { |
| 136 | + $class = 'Matthias\SymfonyServiceDefinitionValidator\Tests\Fixtures\ClassWithTypeHintedConstructorArgument'; |
| 137 | + |
| 138 | + $parameter = new \ReflectionParameter(array($class, '__construct'), 'expected'); |
| 139 | + $argument = new Expression('*invalid expression'); |
| 140 | + |
| 141 | + $validator = new ArgumentValidator(new ContainerBuilder(), $this->createMockResultingClassResolver()); |
| 142 | + |
| 143 | + $this->setExpectedException('Matthias\SymfonyServiceDefinitionValidator\Exception\InvalidExpressionSyntaxException'); |
| 144 | + |
| 145 | + $validator->validate($parameter, $argument); |
| 146 | + } |
| 147 | + |
| 148 | + public function testFailsIfExpressionCouldNotBeEvaluated() |
| 149 | + { |
| 150 | + $class = 'Matthias\SymfonyServiceDefinitionValidator\Tests\Fixtures\ClassWithTypeHintedConstructorArgument'; |
| 151 | + |
| 152 | + $parameter = new \ReflectionParameter(array($class, '__construct'), 'expected'); |
| 153 | + $argument = new Expression('service("invalid service")'); |
| 154 | + |
| 155 | + $validator = new ArgumentValidator(new ContainerBuilder(), $this->createMockResultingClassResolver()); |
| 156 | + |
| 157 | + $this->setExpectedException('Matthias\SymfonyServiceDefinitionValidator\Exception\InvalidExpressionException'); |
| 158 | + |
| 159 | + $validator->validate($parameter, $argument); |
| 160 | + } |
| 161 | + |
85 | 162 | private function createMockResultingClassResolver()
|
86 | 163 | {
|
87 | 164 | return $this->getMock('Matthias\SymfonyServiceDefinitionValidator\ResultingClassResolverInterface');
|
|
0 commit comments