Skip to content

Commit 2496d16

Browse files
google-labs-jules[bot]james2037
authored andcommitted
Fix Tool.php coverage and PHPStan errors in fixture
This commit addresses feedback on the previous submission: 1. **Cover `\$value === null` branch in `Tool::validateType`**: - I added a new test case (`testValidateTypeWithNullForNonAnyTypes`) to `tests/Tool/ToolTest.php`. - This test specifically verifies that `validateType` returns `false` when a non-'any' type parameter (e.g., 'string', 'integer') is given a `null` value. 2. **Resolve PHPStan errors in `tests/Tool/Fixture/ValidationTestTool.php`**: - I removed the `#[Parameter]` attributes from the `doExecute` method in `ValidationTestTool.php`. - These attributes were causing PHPStan errors as they were placed on a method instead of a parameter, and were not functionally required for the existing tests (which use reflection to set parameter metadata for `Tool::validateArguments` testing). All PHPUnit tests, PHPCS, and PHPStan checks now pass.
1 parent 1b36f8f commit 2496d16

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

tests/Tool/Fixture/ValidationTestTool.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010

1111
class ValidationTestTool extends Tool
1212
{
13-
#[Parameter(name: 'pInt', type: 'integer', description: 'An integer parameter')]
14-
#[Parameter(name: 'pObj', type: 'object', description: 'An object parameter')]
15-
#[Parameter(name: 'pAny', type: 'any', description: 'A parameter of any type')]
16-
#[Parameter(name: 'pOptInt', type: 'integer', description: 'An optional integer parameter', required: false)]
17-
#[Parameter(name: 'pOptObj', type: 'object', description: 'An optional object parameter', required: false)]
18-
#[Parameter(name: 'pOptAny', type: 'any', description: 'An optional parameter of any type', required: false)]
1913
protected function doExecute(array $arguments): ContentItemInterface
2014
{
2115
$results = [];

tests/Tool/ToolTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,4 +626,29 @@ public function testValidateArgumentsOptionalAnyTypeCorrect(): void
626626
$result = $tool->execute(['pInt' => 1, 'pObj' => new \stdClass(), 'pAny' => 'a']);
627627
$this->assertStringNotContainsString("pOptAny type", $result[0]['text']);
628628
}
629+
630+
public function testValidateTypeWithNullForNonAnyTypes(): void
631+
{
632+
$tool = new TestTool(); // Using TestTool, or any concrete Tool implementation
633+
$reflectionMethod = new \ReflectionMethod(Tool::class, 'validateType');
634+
$reflectionMethod->setAccessible(true);
635+
636+
$typesToTest = [
637+
'string',
638+
'number',
639+
'integer',
640+
'boolean',
641+
'array',
642+
'object',
643+
];
644+
645+
foreach ($typesToTest as $type) {
646+
$isValid = $reflectionMethod->invoke($tool, null, $type);
647+
$this->assertFalse($isValid, "validateType(null, '{$type}') should return false.");
648+
}
649+
650+
// Also confirm 'any' type still accepts null
651+
$isValidForAny = $reflectionMethod->invoke($tool, null, 'any');
652+
$this->assertTrue($isValidForAny, "validateType(null, 'any') should return true.");
653+
}
629654
}

0 commit comments

Comments
 (0)