Skip to content

Commit

Permalink
TestCase: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 5, 2021
1 parent 05ebbce commit cfedd76
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function runTest(string $method, array $args = null): void
throw new TestCaseException("Method {$method->getName()} is not public. Make it public or rename it.");
}

$info = Helpers::parseDocComment((string) $method->getDocComment()) + ['dataprovider' => null, 'throws' => null];
$info = Helpers::parseDocComment((string) $method->getDocComment()) + ['throws' => null];

if ($info['throws'] === '') {
throw new TestCaseException("Missing class name in @throws annotation for {$method->getName()}().");
Expand All @@ -92,38 +92,9 @@ public function runTest(string $method, array $args = null): void
$throws = is_string($info['throws']) ? preg_split('#\s+#', $info['throws'], 2) : [];
}

$data = [];
if ($args === null) {
$defaultParams = [];
foreach ($method->getParameters() as $param) {
$defaultParams[$param->getName()] = $param->isDefaultValueAvailable()
? $param->getDefaultValue()
: null;
}

foreach ((array) $info['dataprovider'] as $i => $provider) {
$res = $this->getData($provider);
if (!is_array($res) && !$res instanceof \Traversable) {
throw new TestCaseException("Data provider $provider() doesn't return array or Traversable.");
}

foreach ($res as $k => $set) {
$data["$i-$k"] = is_string(key($set))
? array_merge($defaultParams, $set)
: $set;
}
}

if (!$info['dataprovider']) {
if ($method->getNumberOfRequiredParameters()) {
throw new TestCaseException("Method {$method->getName()}() has arguments, but @dataProvider is missing.");
}
$data[] = [];
}
} else {
$data[] = $args;
}

$data = $args === null
? $this->prepareTestData($method, (array) ($info['dataprovider'] ?? []))
: [$args];

if ($this->prevErrorHandler === false) {
$this->prevErrorHandler = set_error_handler(function (int $severity): ?bool {
Expand Down Expand Up @@ -255,6 +226,39 @@ private function sendMethodList(array $methods): void
}
echo 'Dependency:' . implode("\nDependency:", array_keys($dependentFiles)) . "\n";
}


private function prepareTestData(\ReflectionMethod $method, array $dataprovider): array
{
$data = $defaultParams = [];

foreach ($method->getParameters() as $param) {
$defaultParams[$param->getName()] = $param->isDefaultValueAvailable()
? $param->getDefaultValue()
: null;
}

foreach ($dataprovider as $i => $provider) {
$res = $this->getData($provider);
if (!is_array($res) && !$res instanceof \Traversable) {
throw new TestCaseException("Data provider $provider() doesn't return array or Traversable.");
}

foreach ($res as $k => $set) {
$data["$i-$k"] = is_string(key($set))
? array_merge($defaultParams, $set)
: $set;
}
}

if (!$dataprovider) {
if ($method->getNumberOfRequiredParameters()) {
throw new TestCaseException("Method {$method->getName()}() has arguments, but @dataProvider is missing.");
}
$data[] = [];
}
return $data;
}
}


Expand Down

0 comments on commit cfedd76

Please sign in to comment.