Skip to content

Commit 31c58fc

Browse files
committed
TestCaseRunner WIP
1 parent 0708838 commit 31c58fc

File tree

8 files changed

+165
-9
lines changed

8 files changed

+165
-9
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/TestCase.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function run(): void
3434
throw new \LogicException('Calling TestCase::run($method) is deprecated. Use TestCase::runTest($method) instead.');
3535
}
3636

37-
if (self::getCliArgument()) {
37+
if (TestCaseRunner::getCliArgument()) {
3838
$this->runFromCli();
3939
return;
4040
}
@@ -54,16 +54,9 @@ public static function findMethods(): array
5454
}
5555

5656

57-
public static function getCliArgument(): ?string
58-
{
59-
$args = preg_filter('#--method=([\w-]+)$#Ai', '$1', $_SERVER['argv'] ?? []);
60-
return reset($args) ?: null;
61-
}
62-
63-
6457
private function runFromCli(): void
6558
{
66-
$arg = self::getCliArgument();
59+
$arg = TestCaseRunner::getCliArgument();
6760
if ($arg === self::LIST_METHODS) {
6861
Environment::$checkAssertions = false;
6962
header('Content-Type: text/plain');

src/Framework/TestCaseRunner.php

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

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)