Skip to content

Commit 0872d4c

Browse files
committed
feat(DataProvider): Support string method names in DataProvider attribute
1 parent 55d94ea commit 0872d4c

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

src/Sample/DataProvider.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
#[FallbackInterceptor(DataProviderInterceptor::class)]
2121
final class DataProvider implements Interceptable
2222
{
23-
public readonly \Closure $provider;
23+
public readonly \Closure|string $provider;
2424

25+
/**
26+
* @param callable(): iterable<array>|non-empty-string $provider The data provider callable or the method name.
27+
*/
2528
public function __construct(
26-
callable $provider,
29+
callable|string $provider,
2730
) {
28-
$this->provider = $provider(...);
31+
$this->provider = \is_callable($provider) ? $provider(...) : $provider;
2932
}
3033
}

src/Sample/Internal/DataProviderInterceptor.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Testo\Sample\Internal;
66

77
use Psr\EventDispatcher\EventDispatcherInterface;
8+
use ReflectionMethod;
89
use Testo\Sample\DataProvider;
910
use Testo\Sample\MultipleResult;
1011
use Testo\Interceptor\TestRunInterceptor;
@@ -32,8 +33,22 @@ public function runTest(TestInfo $info, callable $next): TestResult
3233
// Dispatch batch starting event
3334
$this->eventDispatcher->dispatch(new TestBatchStarting($info));
3435

36+
$provider = $this->options->provider;
37+
if (\is_string($provider)) {
38+
$info->testDefinition->reflection instanceof \ReflectionMethod or throw new \LogicException(
39+
'DataProvider provider must be a callable or method name string.',
40+
);
41+
42+
/** @var \ReflectionClass $class */
43+
$class = $info->testDefinition->reflection->getDeclaringClass();
44+
$class->hasMethod($provider) or throw new \LogicException(
45+
"DataProvider method '$provider' does not exist.",
46+
);
47+
$provider = [$class->getName(), $provider];
48+
}
49+
3550
// Fetch data sets from the provider
36-
$dataSets = ($this->options->provider)();
51+
$dataSets = $provider();
3752

3853
// Run the test for each data set
3954
$results = [];

tests/Testo/Unit/PathTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public static function providePathsForMatch(): \Generator
461461
yield 'character class does not match' => ['test/file4.txt', 'test/file[123].txt', false];
462462
yield 'no match different extension' => ['test/file.php', 'test/*.txt', false];
463463
yield 'no match different path' => ['other/file.txt', 'test/*.txt', false];
464-
yield 'wildcard double asterisk simulation' => ['test/deep/nested/file.txt', 'test/*/nested/*.txt', false];
464+
yield 'wildcard double asterisk simulation' => ['test/deep/nested/file.txt', 'test/*/nested/*.txt', true];
465465
yield 'pattern with no wildcards no match' => ['test/file.txt', 'test/other.txt', false];
466466
}
467467

0 commit comments

Comments
 (0)