Skip to content

Commit

Permalink
TestCaseRunner WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 7, 2019
1 parent ab54955 commit f8468b5
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 0 deletions.
19 changes: 19 additions & 0 deletions demo/TestFour.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);


class TestFour extends Tester\TestCase
{
public function testMe()
{
Tester\Assert::true(true);
echo __FUNCTION__ . ',';
}


public function testMe2()
{
echo __FUNCTION__ . ',';
}
}
13 changes: 13 additions & 0 deletions demo/TestOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);


class TestOne extends Tester\TestCase
{
public function testMe()
{
Tester\Assert::true(true);
echo __FUNCTION__ . ',';
}
}
13 changes: 13 additions & 0 deletions demo/TestThree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);


class TestThree extends Tester\TestCase
{
public function testMe()
{
Tester\Assert::true(true);
echo __FUNCTION__ . ',';
}
}
13 changes: 13 additions & 0 deletions demo/TestTwo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);


class TestTwo extends Tester\TestCase
{
public function testMe()
{
Tester\Assert::true(true);
echo __FUNCTION__ . ',';
}
}
17 changes: 17 additions & 0 deletions demo/runner.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @testCase
*/

declare(strict_types=1);

namespace Tester;


require __DIR__ . '/../src/bootstrap.php';


(new TestCaseRunner)
->findTests(__DIR__ . '/Test*.php')
->run();
83 changes: 83 additions & 0 deletions src/Framework/TestCaseRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* This file is part of the Nette Tester.
* Copyright (c) 2009 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Tester;


/**
* Runner of TestCase.
*/
class TestCaseRunner
{
private const LIST_METHODS = 'nette-tester-list-methods';

/** @var array */
private $classes = [];


public function findTests(string $fileMask): self
{
foreach (glob($fileMask) as $file) {
require_once $file; // TODO: use autoloading
$this->classes[] = basename($file, '.php');
}
return $this;
}


public function run(): void
{
if (!$this->runFromCli()) {
$this->runTests();
}
}


private function runTests(): void
{
foreach ($this->classes as $class) {
$test = $this->createInstance($class);
$test->run();
}
}


private function runFromCli(): bool
{
$args = preg_filter('#--method=([\w:-]+)$#Ai', '$1', $_SERVER['argv'] ?? []);
$arg = reset($args);
if (!$arg) {
return false;

} elseif ($arg === self::LIST_METHODS) {
Environment::$checkAssertions = false;
$methods = [];
foreach ($this->classes as $class) {
foreach ($class::findMethods() as $method) {
$methods[] = $class . '::' . $method;
}
}
header('Content-Type: text/plain');
echo '[' . implode(',', $methods) . ']';

} else {
[$class, $method] = explode('::', $arg);
$test = $this->createInstance($class);
$test->runTest($method);
}
return true;
}


protected function createInstance(string $class): TestCase
{
// TOO: can be altered via setFactory(callable)
return new $class;
}
}
1 change: 1 addition & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require __DIR__ . '/Framework/Dumper.php';
require __DIR__ . '/Framework/FileMock.php';
require __DIR__ . '/Framework/TestCase.php';
require __DIR__ . '/Framework/TestCaseRunner.php';
require __DIR__ . '/Framework/DomQuery.php';
require __DIR__ . '/Framework/FileMutator.php';
require __DIR__ . '/CodeCoverage/Collector.php';
Expand Down

0 comments on commit f8468b5

Please sign in to comment.