Skip to content

Commit

Permalink
TestCase::run() does not accept arguments (BC break) [Closes nette#244]
Browse files Browse the repository at this point in the history
  • Loading branch information
milo committed May 16, 2016
1 parent 8dffd30 commit f3b8179
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 51 deletions.
29 changes: 15 additions & 14 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,30 @@ class TestCase
* Runs the test case.
* @return void
*/
public function run($method = NULL)
public function run()
{
$r = new \ReflectionObject($this);
if (func_num_args()) {
throw new \LogicException('Calling TestCase::run($method) is deprecated. Use TestCase::runTest($method) instead.');
}

$methods = array_values(preg_grep(self::METHOD_PATTERN, array_map(function (\ReflectionMethod $rm) {
return $rm->getName();
}, $r->getMethods())));

if (substr($method, 0, 2) === '--') { // back compatibility
$method = NULL;
}
}, (new \ReflectionObject($this))->getMethods())));

if ($method === NULL && isset($_SERVER['argv']) && ($tmp = preg_filter('#(--method=)?([\w-]+)$#Ai', '$2', $_SERVER['argv']))) {
if (isset($_SERVER['argv']) && ($tmp = preg_filter('#(--method=)?([\w-]+)$#Ai', '$2', $_SERVER['argv']))) {
$method = reset($tmp);
if ($method === self::LIST_METHODS) {
Environment::$checkAssertions = FALSE;
header('Content-Type: text/plain');
echo '[' . implode(',', $methods) . ']';
return;
}
}
$this->runTest($method);

if ($method === NULL) {
} else {
foreach ($methods as $method) {
$this->runTest($method);
}
} elseif (in_array($method, $methods, TRUE)) {
$this->runTest($method);
} else {
throw new TestCaseException("Method '$method' does not exist or it is not a testing method.");
}
}

Expand All @@ -70,6 +65,12 @@ public function run($method = NULL)
*/
public function runTest($method, array $args = NULL)
{
if (!method_exists($this, $method)) {
throw new TestCaseException("Method '$method' does not exist.");
} elseif (!preg_match(self::METHOD_PATTERN, $method)) {
throw new TestCaseException("Method '$method' is not a testing method.");
}

$method = new \ReflectionMethod($this, $method);
if (!$method->isPublic()) {
throw new TestCaseException("Method {$method->getName()} is not public. Make it public or rename it.");
Expand Down
28 changes: 14 additions & 14 deletions tests/Framework/TestCase.annotationThrows.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -93,44 +93,44 @@ class MyTest extends Tester\TestCase


$test = new MyTest;
$test->run('testThrows');
$test->run('testThrowsMessage');
$test->runTest('testThrows');
$test->runTest('testThrowsMessage');

Assert::exception(function () use ($test) {
$test->run('testThrowsButDont');
$test->runTest('testThrowsButDont');
}, 'Tester\AssertException', 'Exception was expected, but none was thrown in testThrowsButDont()');

Assert::exception(function () use ($test) {
$test->run('testFailAssertPass');
$test->runTest('testFailAssertPass');
}, 'Tester\AssertException', 'failed in testFailAssertPass()');

Assert::exception(function () use ($test) {
$test->run('testThrowsBadClass');
$test->runTest('testThrowsBadClass');
}, 'Tester\AssertException', 'MyException was expected but got Exception in testThrowsBadClass()');

Assert::exception(function () use ($test) {
$test->run('testThrowsBadMessage');
$test->runTest('testThrowsBadMessage');
}, 'Tester\AssertException', "Exception with a message matching 'With message' was expected but got 'Bad message' in testThrowsBadMessage()");

Assert::exception(function () use ($test) {
$test->run('testWithoutThrows');
$test->runTest('testWithoutThrows');
}, 'Exception');

Assert::exception(function () use ($test) {
$test->run('testThrowsWithDataprovider');
$test->runTest('testThrowsWithDataprovider');
}, 'Exception', 'Exception was expected, but none was thrown in testThrowsWithDataprovider(1)');

Assert::exception(function () use ($test) {
$test->run('testUndefinedMethod');
}, 'Tester\TestCaseException', "Method 'testUndefinedMethod' does not exist or it is not a testing method.");
$test->runTest('testUndefinedMethod');
}, 'Tester\TestCaseException', "Method 'testUndefinedMethod' does not exist.");

$test->run('testNotice');
$test->run('testNoticeMessage');
$test->runTest('testNotice');
$test->runTest('testNoticeMessage');

Assert::exception(function () use ($test) {
$test->run('testBadError');
$test->runTest('testBadError');
}, 'Tester\AssertException', 'E_WARNING was expected, but E_NOTICE (Undefined variable: a) was generated in %a%testBadError()');

Assert::exception(function () use ($test) {
$test->run('testNoticeBadMessage');
$test->runTest('testNoticeBadMessage');
}, 'Tester\AssertException', "E_NOTICE with a message matching 'With message' was expected but got 'Undefined variable: a' in testNoticeBadMessage()");
4 changes: 2 additions & 2 deletions tests/Framework/TestCase.annotationThrows.syntax.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class MyTest extends Tester\TestCase
$test = new MyTest;

Assert::exception(function () use ($test) {
$test->run('testThrowsNoClass');
$test->runTest('testThrowsNoClass');
}, 'Tester\TestCaseException', 'Missing class name in @throws annotation for testThrowsNoClass().');

Assert::exception(function () use ($test) {
$test->run('testThrowsMultiple');
$test->runTest('testThrowsMultiple');
}, 'Tester\TestCaseException', 'Annotation @throws for testThrowsMultiple() can be specified only once.');
4 changes: 2 additions & 2 deletions tests/Framework/TestCase.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TestCaseTearDownException extends TestCaseTest

Assert::exception(function () {
$test = new TestCaseTest;
$test->run('testAssertion');
$test->runTest('testAssertion');
}, 'Tester\AssertException', 'FALSE should be TRUE in testAssertion()');


Expand All @@ -34,5 +34,5 @@ Assert::exception(function () use ($test) {
}, 'RuntimeException');

Assert::exception(function () use ($test) {
$test->run('testAssertion');
$test->runTest('testAssertion');
}, 'Tester\AssertException', 'FALSE should be TRUE in testAssertion()');
3 changes: 1 addition & 2 deletions tests/Framework/TestCase.dataProvider.generator.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

use Tester\Assert;
use Tester\Environment;

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

Expand All @@ -30,7 +29,7 @@ class MyTest extends Tester\TestCase


$test = new MyTest;
$test->run('testDataProviderGenerator');
$test->runTest('testDataProviderGenerator');
Assert::same([
['MyTest::testDataProviderGenerator', [0]],
['MyTest::testDataProviderGenerator', [1]],
Expand Down
10 changes: 5 additions & 5 deletions tests/Framework/TestCase.dataProvider.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MyTest extends Tester\TestCase


$test = new MyTest;
$test->run('testSingleDataProvider');
$test->runTest('testSingleDataProvider');
Assert::same([
'MyTest::dataProvider',
['MyTest::testSingleDataProvider', [1, 2]],
Expand All @@ -72,7 +72,7 @@ Assert::same([


$test = new MyTest;
$test->run('testMultipleDataProvider');
$test->runTest('testMultipleDataProvider');
Assert::same([
'MyTest::dataProvider',
'MyTest::dataProvider',
Expand All @@ -84,7 +84,7 @@ Assert::same([


$test = new MyTest;
$test->run('testIteratorDataProvider');
$test->runTest('testIteratorDataProvider');
Assert::same([
'MyTest::dataProviderIterator',
['MyTest::testIteratorDataProvider', [1, 2]],
Expand All @@ -93,7 +93,7 @@ Assert::same([


$test = new MyTest;
$test->run('testFileDataProvider');
$test->runTest('testFileDataProvider');
Assert::same([
['MyTest::testFileDataProvider', ['1', 'b']],
['MyTest::testFileDataProvider', ['a', '2']],
Expand All @@ -102,5 +102,5 @@ Assert::same([

Assert::exception(function () {
$test = new MyTest;
$test->run('testAssertion');
$test->runTest('testAssertion');
}, 'Tester\AssertException', 'FALSE should be TRUE in testAssertion(1, 2)');
16 changes: 8 additions & 8 deletions tests/Framework/TestCase.invalidMethods.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ class MyTest extends Tester\TestCase
$test = new MyTest;

Assert::exception(function () use ($test) {
$test->run('testProtected');
$test->runTest('testProtected');
}, 'Tester\TestCaseException', 'Method testProtected is not public. Make it public or rename it.');

Assert::exception(function () use ($test) {
$test->run('testPrivate');
$test->runTest('testPrivate');
}, 'Tester\TestCaseException', 'Method testPrivate is not public. Make it public or rename it.');

Assert::exception(function () use ($test) {
$test->run('testUndefined');
}, 'Tester\TestCaseException', "Method 'testUndefined' does not exist or it is not a testing method.");
$test->runTest('testUndefined');
}, 'Tester\TestCaseException', "Method 'testUndefined' does not exist.");

Assert::exception(function () use ($test) {
$test->run('notTesting');
}, 'Tester\TestCaseException', "Method 'notTesting' does not exist or it is not a testing method.");
$test->runTest('notTesting');
}, 'Tester\TestCaseException', "Method 'notTesting' is not a testing method.");

Assert::exception(function () use ($test) {
$test->run('notTestingUndefined');
}, 'Tester\TestCaseException', "Method 'notTestingUndefined' does not exist or it is not a testing method.");
$test->runTest('notTestingUndefined');
}, 'Tester\TestCaseException', "Method 'notTestingUndefined' does not exist.");
4 changes: 2 additions & 2 deletions tests/Framework/TestCase.invalidProvider.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class InvalidProviderTest extends Tester\TestCase

Assert::exception(function () {
$test = new InvalidProviderTest;
$test->run('testEmptyProvider');
$test->runTest('testEmptyProvider');
}, 'Tester\TestCaseException', "Data provider invalidDataProvider() doesn't return array or Traversable.");

Assert::exception(function () {
$test = new InvalidProviderTest;
$test->run('testMissingDataProvider');
$test->runTest('testMissingDataProvider');
}, 'Tester\TestCaseException', 'Method testMissingDataProvider() has arguments, but @dataProvider is missing.');
4 changes: 2 additions & 2 deletions tests/Framework/TestCase.order.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ class FailingTest extends Tester\TestCase
$test = new FailingTest;

Assert::exception(function () use ($test) {
$test->run('testPublic');
$test->runTest('testPublic');
}, 'Tester\AssertException');

Assert::exception(function () use ($test) {
$test->run('testPublicStatic');
$test->runTest('testPublicStatic');
}, 'Tester\AssertException');


Expand Down

0 comments on commit f3b8179

Please sign in to comment.