Skip to content

Commit

Permalink
Merge branch 'master' into createTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed Dec 16, 2023
2 parents b1429e9 + 21b7e2c commit f9db19c
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions tests/AutoReview/ProjectCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,53 +452,34 @@ public function testExpectedInputOrder(string $testClassName): void
}

/**
* @dataProvider provideTestClassCases
* @dataProvider provideDataProviderMethodCases
*/
public function testDataProvidersDeclaredReturnType(string $testClassName): void
public function testDataProvidersDeclaredReturnType(string $testClassName, \ReflectionMethod $method): void
{
$reflectionClass = new \ReflectionClass($testClassName);
$methodId = $testClassName.'::'.$method->getName();

$publicMethods = array_filter(
$reflectionClass->getMethods(),
static fn (\ReflectionMethod $reflectionMethod): bool => $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName()
);
self::assertSame('iterable', $method->hasReturnType() ? $method->getReturnType()->__toString() : null, sprintf('DataProvider `%s` must provide `iterable` as return in method prototype.', $methodId));

$dataProviderMethods = array_filter(
$publicMethods,
static fn (\ReflectionMethod $reflectionMethod): bool => str_starts_with($reflectionMethod->getName(), 'provide')
);
$doc = new DocBlock(false !== $method->getDocComment() ? $method->getDocComment() : '/** */');

if ([] === $dataProviderMethods) {
$this->expectNotToPerformAssertions(); // no methods to test, all good!
$returnDocs = $doc->getAnnotationsOfType('return');

return;
if (\count($returnDocs) > 1) {
throw new \UnexpectedValueException(sprintf('Multiple `%s@return` annotations.', $methodId));
}

/** @var \ReflectionMethod $method */
foreach ($dataProviderMethods as $method) {
$methodId = $method->getDeclaringClass()->getName().'::'.$method->getName();
if (1 !== \count($returnDocs)) {
$this->addToAssertionCount(1); // no @return annotation, all good!

self::assertSame('iterable', $method->hasReturnType() ? $method->getReturnType()->__toString() : null, sprintf('DataProvider `%s` must provide `iterable` as return in method prototype.', $methodId));

$doc = new DocBlock(false !== $method->getDocComment() ? $method->getDocComment() : '/** */');

$returnDocs = $doc->getAnnotationsOfType('return');
if (\count($returnDocs) > 1) {
throw new \UnexpectedValueException(sprintf('Multiple `%s@return` annotations.', $methodId));
}
if (1 !== \count($returnDocs)) {
$this->addToAssertionCount(1); // no @return annotation, all good!

continue;
}
return;
}

$returnDoc = $returnDocs[0];
$types = $returnDoc->getTypes();
$returnDoc = $returnDocs[0];
$types = $returnDoc->getTypes();

self::assertCount(1, $types, sprintf('DataProvider `%s@return` must provide single type.', $methodId));
self::assertMatchesRegularExpression('/^iterable\</', $types[0], sprintf('DataProvider `%s@return` must return iterable.', $methodId));
self::assertMatchesRegularExpression('/^iterable\\<(?:(?:int\\|)?string, )?array\\{/', $types[0], sprintf('DataProvider `%s@return` must return iterable of tuples (eg `iterable<string, array{string, string}>`).', $methodId));
}
self::assertCount(1, $types, sprintf('DataProvider `%s@return` must provide single type.', $methodId));
self::assertMatchesRegularExpression('/^iterable\</', $types[0], sprintf('DataProvider `%s@return` must return iterable.', $methodId));
self::assertMatchesRegularExpression('/^iterable\\<(?:(?:int\\|)?string, )?array\\{/', $types[0], sprintf('DataProvider `%s@return` must return iterable of tuples (eg `iterable<string, array{string, string}>`).', $methodId));
}

/**
Expand Down

0 comments on commit f9db19c

Please sign in to comment.