Skip to content

Commit f8468b5

Browse files
committed
TestCaseRunner WIP
1 parent ab54955 commit f8468b5

File tree

7 files changed

+159
-0
lines changed

7 files changed

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

src/Framework/TestCaseRunner.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
foreach (glob($fileMask) as $file) {
27+
require_once $file; // TODO: use autoloading
28+
$this->classes[] = basename($file, '.php');
29+
}
30+
return $this;
31+
}
32+
33+
34+
public function run(): void
35+
{
36+
if (!$this->runFromCli()) {
37+
$this->runTests();
38+
}
39+
}
40+
41+
42+
private function runTests(): void
43+
{
44+
foreach ($this->classes as $class) {
45+
$test = $this->createInstance($class);
46+
$test->run();
47+
}
48+
}
49+
50+
51+
private function runFromCli(): bool
52+
{
53+
$args = preg_filter('#--method=([\w:-]+)$#Ai', '$1', $_SERVER['argv'] ?? []);
54+
$arg = reset($args);
55+
if (!$arg) {
56+
return false;
57+
58+
} elseif ($arg === self::LIST_METHODS) {
59+
Environment::$checkAssertions = false;
60+
$methods = [];
61+
foreach ($this->classes as $class) {
62+
foreach ($class::findMethods() as $method) {
63+
$methods[] = $class . '::' . $method;
64+
}
65+
}
66+
header('Content-Type: text/plain');
67+
echo '[' . implode(',', $methods) . ']';
68+
69+
} else {
70+
[$class, $method] = explode('::', $arg);
71+
$test = $this->createInstance($class);
72+
$test->runTest($method);
73+
}
74+
return true;
75+
}
76+
77+
78+
protected function createInstance(string $class): TestCase
79+
{
80+
// TOO: can be altered via setFactory(callable)
81+
return new $class;
82+
}
83+
}

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';

0 commit comments

Comments
 (0)