Skip to content

Commit 5b901e6

Browse files
committed
Deprecate TestCase
Added some more docs and tests.
1 parent d4209dc commit 5b901e6

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

src/AsyncTestCase.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,31 @@ abstract class AsyncTestCase extends PHPUnitTestCase
1515
{
1616
const RUNTIME_PRECISION = 2;
1717

18+
/** @var string|null Timeout watcher ID. */
1819
private $timeoutId;
1920

21+
/** @var string Temporary storage for actual test name. */
2022
private $realTestName;
2123

22-
private $minimumRuntime;
24+
/** @var int Minimum runtime in milliseconds. */
25+
private $minimumRuntime = 0;
2326

24-
public function setName($name)
27+
/**
28+
* @codeCoverageIgnore Invoked before code coverage data is being collected.
29+
*/
30+
final public function setName($name)
2531
{
2632
parent::setName($name);
2733
$this->realTestName = $name;
2834
}
2935

30-
protected function runTest()
36+
final protected function runTest()
3137
{
3238
parent::setName('runAsyncTest');
3339
return parent::runTest();
3440
}
3541

42+
/** @internal */
3643
final public function runAsyncTest(...$args)
3744
{
3845
parent::setName($this->realTestName);
@@ -65,11 +72,21 @@ final public function runAsyncTest(...$args)
6572
return $returnValue;
6673
}
6774

75+
/**
76+
* Fails the test if the loop does not run for at least the given amount of time.
77+
*
78+
* @param int $runtime Required run time in milliseconds.
79+
*/
6880
final protected function setMinimumRuntime(int $runtime)
6981
{
7082
$this->minimumRuntime = $runtime;
7183
}
7284

85+
/**
86+
* Fails the test (and stops the loop) after the given timeout.
87+
*
88+
* @param int $timeout Timeout in milliseconds.
89+
*/
7390
final protected function setTimeout(int $timeout)
7491
{
7592
$this->timeoutId = Loop::delay($timeout, function () use ($timeout) {

src/LoopReset.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use PHPUnit\Framework\BaseTestListener;
77
use PHPUnit\Framework\Test;
88

9+
/**
10+
* @deprecated Use AsyncTestCase. TestListeners are now deprecated in PHPUnit.
11+
*/
912
class LoopReset extends BaseTestListener
1013
{
1114
public function endTest(Test $test, $time)

src/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
/**
66
* Abstract test class with methods for creating callbacks and asserting runtimes.
7+
*
8+
* @deprecated Use AsyncTestCase instead.
79
*/
810
abstract class TestCase extends \PHPUnit\Framework\TestCase
911
{

test/AsyncTestCaseTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
use Amp\Delayed;
77
use Amp\Loop;
88
use Amp\PHPUnit\AsyncTestCase;
9+
use Amp\Promise;
910
use PHPUnit\Framework\AssertionFailedError;
1011
use function Amp\call;
1112

1213
class AsyncTestCaseTest extends AsyncTestCase
1314
{
14-
public function testThatMethodRunsInLoopContext()
15+
public function testThatMethodRunsInLoopContext(): Promise
1516
{
1617
$returnDeferred = new Deferred(); // make sure our test runs to completion
1718
$testDeferred = new Deferred(); // used by our defer callback to ensure we're running on the Loop
@@ -26,7 +27,7 @@ public function testThatMethodRunsInLoopContext()
2627
return $returnDeferred->promise();
2728
}
2829

29-
public function testThatWeHandleNotPromiseReturned()
30+
public function testThatWeHandleNotPromiseReturned(): \Generator
3031
{
3132
$testDeferred = new Deferred();
3233
$testData = new \stdClass();
@@ -41,7 +42,7 @@ public function testThatWeHandleNotPromiseReturned()
4142
$this->assertTrue($testData->val, 'Expected our test to run on loop to completion');
4243
}
4344

44-
public function testExpectingAnExceptionThrown()
45+
public function testExpectingAnExceptionThrown(): \Generator
4546
{
4647
$throwException = function () {
4748
return call(function () {
@@ -56,7 +57,7 @@ public function testExpectingAnExceptionThrown()
5657
yield $throwException();
5758
}
5859

59-
public function argumentSupportProvider()
60+
public function argumentSupportProvider(): array
6061
{
6162
return [
6263
['foo', 42, true],
@@ -77,7 +78,17 @@ public function testArgumentSupport(string $foo, int $bar, bool $baz)
7778
$this->assertTrue($baz);
7879
}
7980

80-
public function testSetMinimumRunTime()
81+
public function testSetTimeout(): \Generator
82+
{
83+
$this->setTimeout(100);
84+
85+
$this->expectException(AssertionFailedError::class);
86+
$this->expectExceptionMessage('Expected test to complete before 100ms time limit');
87+
88+
yield new Delayed(200);
89+
}
90+
91+
public function testSetMinimumRunTime(): \Generator
8192
{
8293
$this->setMinimumRuntime(100);
8394
$func = function () {

0 commit comments

Comments
 (0)