Skip to content

Commit cfedd76

Browse files
committed
TestCase: refactoring
1 parent 05ebbce commit cfedd76

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

src/Framework/TestCase.php

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function runTest(string $method, array $args = null): void
8282
throw new TestCaseException("Method {$method->getName()} is not public. Make it public or rename it.");
8383
}
8484

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

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

95-
$data = [];
96-
if ($args === null) {
97-
$defaultParams = [];
98-
foreach ($method->getParameters() as $param) {
99-
$defaultParams[$param->getName()] = $param->isDefaultValueAvailable()
100-
? $param->getDefaultValue()
101-
: null;
102-
}
103-
104-
foreach ((array) $info['dataprovider'] as $i => $provider) {
105-
$res = $this->getData($provider);
106-
if (!is_array($res) && !$res instanceof \Traversable) {
107-
throw new TestCaseException("Data provider $provider() doesn't return array or Traversable.");
108-
}
109-
110-
foreach ($res as $k => $set) {
111-
$data["$i-$k"] = is_string(key($set))
112-
? array_merge($defaultParams, $set)
113-
: $set;
114-
}
115-
}
116-
117-
if (!$info['dataprovider']) {
118-
if ($method->getNumberOfRequiredParameters()) {
119-
throw new TestCaseException("Method {$method->getName()}() has arguments, but @dataProvider is missing.");
120-
}
121-
$data[] = [];
122-
}
123-
} else {
124-
$data[] = $args;
125-
}
126-
95+
$data = $args === null
96+
? $this->prepareTestData($method, (array) ($info['dataprovider'] ?? []))
97+
: [$args];
12798

12899
if ($this->prevErrorHandler === false) {
129100
$this->prevErrorHandler = set_error_handler(function (int $severity): ?bool {
@@ -255,6 +226,39 @@ private function sendMethodList(array $methods): void
255226
}
256227
echo 'Dependency:' . implode("\nDependency:", array_keys($dependentFiles)) . "\n";
257228
}
229+
230+
231+
private function prepareTestData(\ReflectionMethod $method, array $dataprovider): array
232+
{
233+
$data = $defaultParams = [];
234+
235+
foreach ($method->getParameters() as $param) {
236+
$defaultParams[$param->getName()] = $param->isDefaultValueAvailable()
237+
? $param->getDefaultValue()
238+
: null;
239+
}
240+
241+
foreach ($dataprovider as $i => $provider) {
242+
$res = $this->getData($provider);
243+
if (!is_array($res) && !$res instanceof \Traversable) {
244+
throw new TestCaseException("Data provider $provider() doesn't return array or Traversable.");
245+
}
246+
247+
foreach ($res as $k => $set) {
248+
$data["$i-$k"] = is_string(key($set))
249+
? array_merge($defaultParams, $set)
250+
: $set;
251+
}
252+
}
253+
254+
if (!$dataprovider) {
255+
if ($method->getNumberOfRequiredParameters()) {
256+
throw new TestCaseException("Method {$method->getName()}() has arguments, but @dataProvider is missing.");
257+
}
258+
$data[] = [];
259+
}
260+
return $data;
261+
}
258262
}
259263

260264

0 commit comments

Comments
 (0)