|
13 | 13 | use PhpLlm\LlmChain\ToolBox\Metadata; |
14 | 14 | use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer; |
15 | 15 | use PHPUnit\Framework\Attributes\CoversClass; |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
16 | 17 | use PHPUnit\Framework\Attributes\Test; |
17 | 18 | use PHPUnit\Framework\Attributes\UsesClass; |
18 | 19 | use PHPUnit\Framework\TestCase; |
@@ -163,4 +164,83 @@ public function detectParameterDefinitionNone(): void |
163 | 164 |
|
164 | 165 | self::assertNull($actual); |
165 | 166 | } |
| 167 | + |
| 168 | + #[Test] |
| 169 | + public function getParameterDescriptionWithoutDocBlock(): void |
| 170 | + { |
| 171 | + $targetMethod = self::createStub(\ReflectionMethod::class); |
| 172 | + $targetMethod->method('getDocComment')->willReturn(false); |
| 173 | + |
| 174 | + $methodToTest = new \ReflectionMethod(ParameterAnalyzer::class, 'getParameterDescription'); |
| 175 | + |
| 176 | + self::assertSame( |
| 177 | + '', |
| 178 | + $methodToTest->invoke( |
| 179 | + $this->analyzer, |
| 180 | + $targetMethod, |
| 181 | + 'myParam', |
| 182 | + ) |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + #[Test] |
| 187 | + #[DataProvider('provideGetParameterDescriptionCases')] |
| 188 | + public function getParameterDescriptionWithDocs(string $docComment, string $expectedResult): void |
| 189 | + { |
| 190 | + $targetMethod = self::createStub(\ReflectionMethod::class); |
| 191 | + $targetMethod->method('getDocComment')->willReturn($docComment); |
| 192 | + |
| 193 | + $methodToTest = new \ReflectionMethod(ParameterAnalyzer::class, 'getParameterDescription'); |
| 194 | + |
| 195 | + self::assertSame( |
| 196 | + $expectedResult, |
| 197 | + $methodToTest->invoke( |
| 198 | + $this->analyzer, |
| 199 | + $targetMethod, |
| 200 | + 'myParam', |
| 201 | + ) |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + public static function provideGetParameterDescriptionCases(): \Generator |
| 206 | + { |
| 207 | + yield 'empty doc block' => [ |
| 208 | + 'docComment' => '', |
| 209 | + 'expectedResult' => '', |
| 210 | + ]; |
| 211 | + |
| 212 | + yield 'single line doc block with description' => [ |
| 213 | + 'docComment' => '/** @param string $myParam The description */', |
| 214 | + 'expectedResult' => 'The description', |
| 215 | + ]; |
| 216 | + |
| 217 | + yield 'multi line doc block with description and other tags' => [ |
| 218 | + 'docComment' => <<<'TEXT' |
| 219 | + /** |
| 220 | + * @param string $myParam The description |
| 221 | + * @return void |
| 222 | + */ |
| 223 | + TEXT, |
| 224 | + 'expectedResult' => 'The description', |
| 225 | + ]; |
| 226 | + |
| 227 | + yield 'multi line doc block with multiple parameters' => [ |
| 228 | + 'docComment' => <<<'TEXT' |
| 229 | + /** |
| 230 | + * @param string $myParam The description |
| 231 | + * @param string $anotherParam The wrong description |
| 232 | + */ |
| 233 | + TEXT, |
| 234 | + 'expectedResult' => 'The description', |
| 235 | + ]; |
| 236 | + |
| 237 | + yield 'multi line doc block with parameter that is not searched for' => [ |
| 238 | + 'docComment' => <<<'TEXT' |
| 239 | + /** |
| 240 | + * @param string $unknownParam The description |
| 241 | + */ |
| 242 | + TEXT, |
| 243 | + 'expectedResult' => '', |
| 244 | + ]; |
| 245 | + } |
166 | 246 | } |
0 commit comments