class DataProvider
Same name in this branch
- 11.1.x vendor/phpunit/phpunit/src/Framework/Attributes/DataProvider.php \PHPUnit\Framework\Attributes\DataProvider
- 11.1.x vendor/phpunit/phpunit/src/Metadata/DataProvider.php \PHPUnit\Metadata\DataProvider
@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
@internal This class is not covered by the backward compatibility promise for PHPUnit
Hierarchy
- class \PHPUnit\Metadata\Api\DataProvider
Expanded class hierarchy of DataProvider
1 file declares its use of DataProvider
- TestBuilder.php in vendor/
phpunit/ phpunit/ src/ Framework/ TestBuilder.php
1 string reference to 'DataProvider'
- AnnotationParser::forMethod in vendor/
phpunit/ phpunit/ src/ Metadata/ Parser/ AnnotationParser.php - @psalm-param class-string $className @psalm-param non-empty-string $methodName
File
-
vendor/
phpunit/ phpunit/ src/ Metadata/ Api/ DataProvider.php, line 51
Namespace
PHPUnit\Metadata\ApiView source
final class DataProvider {
/**
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
*
* @throws InvalidDataProviderException
*/
public function providedData(string $className, string $methodName) : ?array {
$dataProvider = MetadataRegistry::parser()->forMethod($className, $methodName)
->isDataProvider();
$testWith = MetadataRegistry::parser()->forMethod($className, $methodName)
->isTestWith();
if ($dataProvider->isEmpty() && $testWith->isEmpty()) {
return $this->dataProvidedByTestWithAnnotation($className, $methodName);
}
if ($dataProvider->isNotEmpty()) {
$data = $this->dataProvidedByMethods($className, $methodName, $dataProvider);
}
else {
$data = $this->dataProvidedByMetadata($testWith);
}
if ($data === []) {
throw new InvalidDataProviderException('Empty data set provided by data provider');
}
foreach ($data as $key => $value) {
if (!is_array($value)) {
throw new InvalidDataProviderException(sprintf('Data set %s is invalid', is_int($key) ? '#' . $key : '"' . $key . '"'));
}
}
return $data;
}
/**
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
*
* @throws InvalidDataProviderException
*/
private function dataProvidedByMethods(string $className, string $methodName, MetadataCollection $dataProvider) : array {
$testMethod = new Event\Code\ClassMethod($className, $methodName);
$methodsCalled = [];
$result = [];
foreach ($dataProvider as $_dataProvider) {
assert($_dataProvider instanceof DataProviderMetadata);
$dataProviderMethod = new Event\Code\ClassMethod($_dataProvider->className(), $_dataProvider->methodName());
Event\Facade::emitter()->dataProviderMethodCalled($testMethod, $dataProviderMethod);
$methodsCalled[] = $dataProviderMethod;
try {
$class = new ReflectionClass($_dataProvider->className());
$method = $class->getMethod($_dataProvider->methodName());
$object = null;
if (!$method->isPublic()) {
Event\Facade::emitter()->testTriggeredPhpunitDeprecation($this->valueObjectForTestMethodWithoutTestData($className, $methodName), sprintf('Data Provider method %s::%s() is not public', $_dataProvider->className(), $_dataProvider->methodName()));
}
if (!$method->isStatic()) {
Event\Facade::emitter()->testTriggeredPhpunitDeprecation($this->valueObjectForTestMethodWithoutTestData($className, $methodName), sprintf('Data Provider method %s::%s() is not static', $_dataProvider->className(), $_dataProvider->methodName()));
$object = $class->newInstanceWithoutConstructor();
}
if ($method->getNumberOfParameters() === 0) {
$data = $method->invoke($object);
}
else {
Event\Facade::emitter()->testTriggeredPhpunitDeprecation($this->valueObjectForTestMethodWithoutTestData($className, $methodName), sprintf('Data Provider method %s::%s() expects an argument', $_dataProvider->className(), $_dataProvider->methodName()));
$data = $method->invoke($object, $_dataProvider->methodName());
}
} catch (Throwable $e) {
Event\Facade::emitter()->dataProviderMethodFinished($testMethod, ...$methodsCalled);
throw new InvalidDataProviderException($e->getMessage(), $e->getCode(), $e);
}
foreach ($data as $key => $value) {
if (is_int($key)) {
$result[] = $value;
}
elseif (is_string($key)) {
if (array_key_exists($key, $result)) {
Event\Facade::emitter()->dataProviderMethodFinished($testMethod, ...$methodsCalled);
throw new InvalidDataProviderException(sprintf('The key "%s" has already been defined by a previous data provider', $key));
}
$result[$key] = $value;
}
else {
throw new InvalidDataProviderException(sprintf('The key must be an integer or a string, %s given', get_debug_type($key)));
}
}
}
Event\Facade::emitter()->dataProviderMethodFinished($testMethod, ...$methodsCalled);
return $result;
}
private function dataProvidedByMetadata(MetadataCollection $testWith) : array {
$result = [];
foreach ($testWith as $_testWith) {
assert($_testWith instanceof TestWith);
$result[] = $_testWith->data();
}
return $result;
}
/**
* @psalm-param class-string $className
*
* @throws InvalidDataProviderException
*/
private function dataProvidedByTestWithAnnotation(string $className, string $methodName) : ?array {
$docComment = (new ReflectionMethod($className, $methodName))->getDocComment();
if ($docComment === false) {
return null;
}
$docComment = str_replace("\r\n", "\n", $docComment);
$docComment = preg_replace('/\\n\\s*\\*\\s?/', "\n", $docComment);
$docComment = substr($docComment, 0, -1);
$docComment = rtrim($docComment, "\n");
if (!preg_match('/@testWith\\s+/', $docComment, $matches, PREG_OFFSET_CAPTURE)) {
return null;
}
$offset = strlen($matches[0][0]) + (int) $matches[0][1];
$annotationContent = substr($docComment, $offset);
$data = [];
foreach (explode("\n", $annotationContent) as $candidateRow) {
$candidateRow = trim($candidateRow);
if ($candidateRow === '' || $candidateRow[0] !== '[') {
break;
}
$dataSet = json_decode($candidateRow, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidDataProviderException('The data set for the @testWith annotation cannot be parsed: ' . json_last_error_msg());
}
$data[] = $dataSet;
}
if (!$data) {
throw new InvalidDataProviderException('The data set for the @testWith annotation cannot be parsed.');
}
return $data;
}
/**
* @psalm-param class-string $className
* @psalm-param non-empty-string $methodName
*
* @throws MoreThanOneDataSetFromDataProviderException
*/
private function valueObjectForTestMethodWithoutTestData(string $className, string $methodName) : TestMethod {
$location = Reflection::sourceLocationFor($className, $methodName);
return new TestMethod($className, $methodName, $location['file'], $location['line'], Event\Code\TestDoxBuilder::fromClassNameAndMethodName($className, $methodName), MetadataRegistry::parser()->forClassAndMethod($className, $methodName), TestDataCollection::fromArray([]));
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
DataProvider::dataProvidedByMetadata | private | function | |
DataProvider::dataProvidedByMethods | private | function | @psalm-param class-string $className @psalm-param non-empty-string $methodName |
DataProvider::dataProvidedByTestWithAnnotation | private | function | @psalm-param class-string $className |
DataProvider::providedData | public | function | @psalm-param class-string $className @psalm-param non-empty-string $methodName |
DataProvider::valueObjectForTestMethodWithoutTestData | private | function | @psalm-param class-string $className @psalm-param non-empty-string $methodName |