diff --git a/src/Type/Pest/PestTestCaseType.php b/src/Type/Pest/PestTestCaseType.php index c27f643..a08f80a 100644 --- a/src/Type/Pest/PestTestCaseType.php +++ b/src/Type/Pest/PestTestCaseType.php @@ -19,29 +19,16 @@ public function __construct( public function resolve(string $filePath): Type { - $bindings = $this->pestConfigReader->resolveFileBindings($filePath); - - if ($bindings === []) { - $bindings = $this->pestConfigReader->resolveBindings($filePath); - } - - $classNames = []; - $traitNames = []; - - foreach ($bindings as $binding) { - if (! $this->reflectionProvider->hasClass($binding)) { - continue; - } - - $reflection = $this->reflectionProvider->getClass($binding); - - if ($reflection->isTrait()) { - $traitNames[] = $binding; + [$classNames, $traitNames] = $this->partition( + $this->pestConfigReader->resolveFileBindings($filePath), + ); - continue; - } + if ($classNames === []) { + [$classNames, $directoryTraitNames] = $this->partition( + $this->pestConfigReader->resolveBindings($filePath), + ); - $classNames[] = $binding; + $traitNames = array_values(array_unique([...$directoryTraitNames, ...$traitNames])); } if ($classNames === []) { @@ -61,6 +48,32 @@ public function resolve(string $filePath): Type ); } + /** + * @param list $bindings + * @return array{list, list} + */ + private function partition(array $bindings): array + { + $classNames = []; + $traitNames = []; + + foreach ($bindings as $binding) { + if (! $this->reflectionProvider->hasClass($binding)) { + continue; + } + + if ($this->reflectionProvider->getClass($binding)->isTrait()) { + $traitNames[] = $binding; + + continue; + } + + $classNames[] = $binding; + } + + return [$classNames, $traitNames]; + } + /** * @param list $classNames */ diff --git a/tests/Fixtures/CustomTestCaseInference/Feature/local-uses-trait-only-keeps-directory-testcase.php b/tests/Fixtures/CustomTestCaseInference/Feature/local-uses-trait-only-keeps-directory-testcase.php new file mode 100644 index 0000000..0ad9fc3 --- /dev/null +++ b/tests/Fixtures/CustomTestCaseInference/Feature/local-uses-trait-only-keeps-directory-testcase.php @@ -0,0 +1,21 @@ +publicHelper()); + assertType('string', $this->helperMethod()); + }); +} diff --git a/tests/Type/CustomTestCaseTest.php b/tests/Type/CustomTestCaseTest.php index fb31376..a13648b 100644 --- a/tests/Type/CustomTestCaseTest.php +++ b/tests/Type/CustomTestCaseTest.php @@ -33,3 +33,9 @@ })->with(function (): Iterator { yield from CustomTestCaseTestCase::gatherAssertTypes(__DIR__.'/../Fixtures/CustomTestCaseInference/Feature/local-uses-overrides-directory.php'); }); + +test('a trait-only file-level uses() keeps the directory binding', function (string $assertType, string $file, mixed ...$args): void { + $this->assertFileAsserts($assertType, $file, ...$args); +})->with(function (): Iterator { + yield from CustomTestCaseTestCase::gatherAssertTypes(__DIR__.'/../Fixtures/CustomTestCaseInference/Feature/local-uses-trait-only-keeps-directory-testcase.php'); +});