Skip to content

Commit a0aa35d

Browse files
committed
TestCaseRunner WIP
1 parent 355525f commit a0aa35d

File tree

8 files changed

+160
-0
lines changed

8 files changed

+160
-0
lines changed

demo/TestFour.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
class TestFour extends Tester\TestCase
7+
{
8+
public function testMe()
9+
{
10+
Tester\Assert::true(true);
11+
echo __FUNCTION__ . ',';
12+
}
13+
14+
15+
public function testMe2()
16+
{
17+
echo __FUNCTION__ . ',';
18+
}
19+
}

demo/TestOne.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
class TestOne extends Tester\TestCase
7+
{
8+
public function testMe()
9+
{
10+
Tester\Assert::true(true);
11+
echo __FUNCTION__ . ',';
12+
}
13+
}

demo/TestThree.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
class TestThree extends Tester\TestCase
7+
{
8+
public function testMe()
9+
{
10+
Tester\Assert::true(true);
11+
echo __FUNCTION__ . ',';
12+
}
13+
}

demo/TestTwo.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
class TestTwo extends Tester\TestCase
7+
{
8+
public function testMe()
9+
{
10+
Tester\Assert::true(true);
11+
echo __FUNCTION__ . ',';
12+
}
13+
}

demo/runner.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* @testCase
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
require __DIR__ . '/../src/bootstrap.php';
10+
11+
12+
(new Tester\TestCaseRunner)
13+
->findTests(__DIR__ . '/Test*.php')
14+
->run();

src/Framework/TestCaseRunner.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Tester.
5+
* Copyright (c) 2009 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Tester;
11+
12+
13+
/**
14+
* Runner of TestCase.
15+
*/
16+
class TestCaseRunner
17+
{
18+
private const LIST_METHODS = 'nette-tester-list-methods';
19+
20+
/** @var array */
21+
private $classes = [];
22+
23+
24+
public function findTests(string $fileMask): self
25+
{
26+
$files = [];
27+
foreach (glob($fileMask) as $file) {
28+
require_once $file;
29+
$files[realpath($file)] = true;
30+
}
31+
foreach (get_declared_classes() as $class) {
32+
$rc = new \ReflectionClass($class);
33+
if ($rc->isSubclassOf(TestCase::class) && isset($files[$rc->getFileName()])) {
34+
$this->classes[] = $class;
35+
}
36+
}
37+
return $this;
38+
}
39+
40+
41+
public function run(): void
42+
{
43+
if ($this->runFromCli()) {
44+
return;
45+
}
46+
47+
foreach ($this->classes as $class) {
48+
$test = $this->createInstance($class);
49+
$test->run();
50+
}
51+
}
52+
53+
54+
private function runFromCli(): bool
55+
{
56+
$args = preg_filter('#--method=([\w:-]+)$#Ai', '$1', $_SERVER['argv'] ?? []);
57+
$arg = reset($args);
58+
if (!$arg) {
59+
return false;
60+
61+
} elseif ($arg === self::LIST_METHODS) {
62+
Environment::$checkAssertions = false;
63+
$methods = [];
64+
foreach ($this->classes as $class) {
65+
foreach ($class::findMethods() as $method) {
66+
$methods[] = $class . '::' . $method;
67+
}
68+
}
69+
header('Content-Type: text/plain');
70+
echo '[' . implode(',', $methods) . ']';
71+
72+
} else {
73+
[$class, $method] = explode('::', $arg);
74+
$test = $this->createInstance($class);
75+
$test->runTest($method);
76+
}
77+
return true;
78+
}
79+
80+
81+
protected function createInstance(string $class): TestCase
82+
{
83+
// TOO: can be altered via setFactory(callable)
84+
return new $class;
85+
}
86+
}

src/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require __DIR__ . '/Framework/Dumper.php';
1515
require __DIR__ . '/Framework/FileMock.php';
1616
require __DIR__ . '/Framework/TestCase.php';
17+
require __DIR__ . '/Framework/TestCaseRunner.php';
1718
require __DIR__ . '/Framework/DomQuery.php';
1819
require __DIR__ . '/Framework/FileMutator.php';
1920
require __DIR__ . '/CodeCoverage/Collector.php';

src/tester.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
require __DIR__ . '/Framework/Dumper.php';
2727
require __DIR__ . '/Framework/DataProvider.php';
2828
require __DIR__ . '/Framework/TestCase.php';
29+
require __DIR__ . '/Framework/TestCaseRunner.php';
2930
require __DIR__ . '/CodeCoverage/PhpParser.php';
3031
require __DIR__ . '/CodeCoverage/Generators/AbstractGenerator.php';
3132
require __DIR__ . '/CodeCoverage/Generators/HtmlGenerator.php';

0 commit comments

Comments
 (0)