|
3 | 3 | namespace Matthias\SymfonyDependencyInjectionTest\Tests\PhpUnit\DependencyInjection; |
4 | 4 |
|
5 | 5 | use Matthias\SymfonyDependencyInjectionTest\PhpUnit\DefinitionHasArgumentConstraint; |
| 6 | +use PHPUnit\Framework\ExpectationFailedException; |
6 | 7 | use PHPUnit\Framework\TestCase; |
| 8 | +use stdClass; |
7 | 9 | use Symfony\Component\DependencyInjection\Definition; |
8 | 10 | use Symfony\Component\DependencyInjection\DefinitionDecorator; |
9 | 11 |
|
@@ -45,4 +47,133 @@ public function definitionProvider() |
45 | 47 | array($decoratedDefinitionWithArguments, 1, $rightValue, true), |
46 | 48 | ); |
47 | 49 | } |
| 50 | + |
| 51 | + /** |
| 52 | + * @test |
| 53 | + * @dataProvider invalid_definition_indexes |
| 54 | + * |
| 55 | + * @param mixed $argument |
| 56 | + * @param string $exceptionMessage |
| 57 | + */ |
| 58 | + public function validates_definitionIndex($argument, $exceptionMessage) |
| 59 | + { |
| 60 | + $this->expectException(\InvalidArgumentException::class); |
| 61 | + $this->expectExceptionMessage($exceptionMessage); |
| 62 | + |
| 63 | + new DefinitionHasArgumentConstraint($argument, 0); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @return \Generator |
| 68 | + */ |
| 69 | + public function invalid_definition_indexes() |
| 70 | + { |
| 71 | + yield [ |
| 72 | + new stdClass(), 'Expected either a string or a positive integer for $argumentIndex.' |
| 73 | + ]; |
| 74 | + |
| 75 | + yield [ |
| 76 | + 1.0, 'Expected either a string or a positive integer for $argumentIndex.' |
| 77 | + ]; |
| 78 | + |
| 79 | + yield [ |
| 80 | + '1', 'Unknown argument "1". Did you mean "$1"?', |
| 81 | + ]; |
| 82 | + |
| 83 | + yield [ |
| 84 | + 'a', 'Unknown argument "a". Did you mean "$a"?', |
| 85 | + ]; |
| 86 | + |
| 87 | + yield [ |
| 88 | + '', 'A named argument must begin with a "$".' |
| 89 | + ]; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @test |
| 94 | + * @dataProvider indexed_arguments |
| 95 | + * @param int $argumentIndex |
| 96 | + */ |
| 97 | + public function supports_indexed_arguments($argumentIndex) |
| 98 | + { |
| 99 | + $expectedValue = 'bar'; |
| 100 | + |
| 101 | + $constraint = new DefinitionHasArgumentConstraint($argumentIndex, $expectedValue); |
| 102 | + $definition = new Definition(stdClass::class, array_fill(0, $argumentIndex + 1, $expectedValue)); |
| 103 | + |
| 104 | + self::assertTrue($constraint->evaluate($definition)); |
| 105 | + self::assertSame("has an argument with index $argumentIndex with the given value", $constraint->toString()); |
| 106 | + |
| 107 | + $failingExpectation = $expectedValue . $expectedValue; |
| 108 | + $constraint = new DefinitionHasArgumentConstraint($argumentIndex, $failingExpectation); |
| 109 | + |
| 110 | + try { |
| 111 | + $constraint->evaluate($definition); |
| 112 | + $this->fail('The expression above should throw an exception.'); |
| 113 | + } catch (ExpectationFailedException $e) { |
| 114 | + self::assertStringStartsWith( |
| 115 | + sprintf( |
| 116 | + 'The value of argument with index %d (\'%s\') is not equal to the expected value (\'%s\')', |
| 117 | + $argumentIndex, $expectedValue, $failingExpectation |
| 118 | + ), |
| 119 | + $e->getMessage() |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @return \Generator |
| 126 | + */ |
| 127 | + public function indexed_arguments() |
| 128 | + { |
| 129 | + // yield [0]; |
| 130 | + yield [1]; |
| 131 | + yield [2]; |
| 132 | + yield [3]; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @test |
| 137 | + * @dataProvider named_arguments |
| 138 | + * @param string $argument |
| 139 | + */ |
| 140 | + public function supports_named_arguments($argument) |
| 141 | + { |
| 142 | + $expectedValue = 'bar'; |
| 143 | + |
| 144 | + $constraint = new DefinitionHasArgumentConstraint($argument, $expectedValue); |
| 145 | + $definition = new Definition(stdClass::class, [ |
| 146 | + $argument => $expectedValue, |
| 147 | + ]); |
| 148 | + |
| 149 | + self::assertTrue($constraint->evaluate($definition)); |
| 150 | + self::assertSame(sprintf('has an argument named "%s" with the given value', $argument), $constraint->toString()); |
| 151 | + |
| 152 | + $failingExpectation = $expectedValue . $expectedValue; |
| 153 | + $constraint = new DefinitionHasArgumentConstraint($argument, $failingExpectation); |
| 154 | + |
| 155 | + try { |
| 156 | + $constraint->evaluate($definition); |
| 157 | + $this->fail('The expression above should throw an exception.'); |
| 158 | + } catch (ExpectationFailedException $e) { |
| 159 | + self::assertStringStartsWith( |
| 160 | + sprintf( |
| 161 | + 'The value of argument named "%s" (\'%s\') is not equal to the expected value (\'%s\')', |
| 162 | + $argument, $expectedValue, $failingExpectation |
| 163 | + ), |
| 164 | + $e->getMessage() |
| 165 | + ); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @return \Generator |
| 171 | + */ |
| 172 | + public function named_arguments() |
| 173 | + { |
| 174 | + yield ['$foo']; |
| 175 | + yield ['$bar']; |
| 176 | + yield ['$a']; |
| 177 | + } |
| 178 | + |
48 | 179 | } |
0 commit comments