Skip to content

Commit

Permalink
UPDATE:
Browse files Browse the repository at this point in the history
- add the possibility to test any protected or private method in a class
- add the possibility to test any protected or private static method in a class
- write new tests for the new features
  • Loading branch information
rawsrc committed Nov 11, 2021
1 parent 858c65f commit c8d2137
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
6 changes: 5 additions & 1 deletion Pilot.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ public function run(int|string|null $id, Closure $test, string $description = ''
* @param string $description
* @param string|null $method
* @param array $params
* @param bool $is_static
* @return int|string
* @throws Exception("Runner's id: {$id} is already defined and locked")
* @throws ReflectionException
*/
public function runClassMethod(
Expand Down Expand Up @@ -282,6 +282,10 @@ public function runClassMethod(
$reflection_method = new ReflectionMethod($class, $method);
$reflection_method->setAccessible(true);

if ($reflection_method->isStatic()) {
$class = null;
}

$runner = new Runner(fn() => $reflection_method->invoke($class, ...$params), $description);
$runner->setId($id);
$this->current_runner = $runner;
Expand Down
21 changes: 17 additions & 4 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ protected function qrs(int $p, int $q): int
{
return 2*$p+4*$q;
}

private static function tuv(): string
{
return 'tuv';
}
}

$foo = new Foo();
Expand Down Expand Up @@ -224,6 +229,14 @@ class: 'Foo',
);
$pilot->assertIsInt();
$pilot->assertEqual(250);

$pilot->runClassMethod(
id: '018',
description: 'private static method unit test',
class: 'Foo::tuv',
);
$pilot->assertIsString();
$pilot->assertEqual('tuv');
//endregion

// manual test
Expand All @@ -236,13 +249,13 @@ class: 'Foo',
);
$pilot->assertIsArray();
$pilot->assertEqual([
'nb_runs' => 17,
'passed_runs' => 17,
'nb_runs' => 18,
'passed_runs' => 18,
'failed_runs' => 0,
'passed_runs_percent' => 100.0,
'failed_runs_percent' => 0.0,
'nb_assertions' => 42,
'passed_assertions' => 42,
'nb_assertions' => 44,
'passed_assertions' => 44,
'failed_assertions' => 0,
'passed_assertions_percent' => 100.0,
'failed_assertions_percent' => 0.0
Expand Down

0 comments on commit c8d2137

Please sign in to comment.