Skip to content

Commit

Permalink
Deprecate TestCase
Browse files Browse the repository at this point in the history
Added some more docs and tests.
  • Loading branch information
trowski committed Feb 12, 2019
1 parent d4209dc commit 5b901e6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
23 changes: 20 additions & 3 deletions src/AsyncTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,31 @@ abstract class AsyncTestCase extends PHPUnitTestCase
{
const RUNTIME_PRECISION = 2;

/** @var string|null Timeout watcher ID. */
private $timeoutId;

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

private $minimumRuntime;
/** @var int Minimum runtime in milliseconds. */
private $minimumRuntime = 0;

public function setName($name)
/**
* @codeCoverageIgnore Invoked before code coverage data is being collected.
*/
final public function setName($name)
{
parent::setName($name);
$this->realTestName = $name;
}

protected function runTest()
final protected function runTest()
{
parent::setName('runAsyncTest');
return parent::runTest();
}

/** @internal */
final public function runAsyncTest(...$args)
{
parent::setName($this->realTestName);
Expand Down Expand Up @@ -65,11 +72,21 @@ final public function runAsyncTest(...$args)
return $returnValue;
}

/**
* Fails the test if the loop does not run for at least the given amount of time.
*
* @param int $runtime Required run time in milliseconds.
*/
final protected function setMinimumRuntime(int $runtime)
{
$this->minimumRuntime = $runtime;
}

/**
* Fails the test (and stops the loop) after the given timeout.
*
* @param int $timeout Timeout in milliseconds.
*/
final protected function setTimeout(int $timeout)
{
$this->timeoutId = Loop::delay($timeout, function () use ($timeout) {
Expand Down
3 changes: 3 additions & 0 deletions src/LoopReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use PHPUnit\Framework\BaseTestListener;
use PHPUnit\Framework\Test;

/**
* @deprecated Use AsyncTestCase. TestListeners are now deprecated in PHPUnit.
*/
class LoopReset extends BaseTestListener
{
public function endTest(Test $test, $time)
Expand Down
2 changes: 2 additions & 0 deletions src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

/**
* Abstract test class with methods for creating callbacks and asserting runtimes.
*
* @deprecated Use AsyncTestCase instead.
*/
abstract class TestCase extends \PHPUnit\Framework\TestCase
{
Expand Down
21 changes: 16 additions & 5 deletions test/AsyncTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use Amp\Delayed;
use Amp\Loop;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Promise;
use PHPUnit\Framework\AssertionFailedError;
use function Amp\call;

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

public function testThatWeHandleNotPromiseReturned()
public function testThatWeHandleNotPromiseReturned(): \Generator
{
$testDeferred = new Deferred();
$testData = new \stdClass();
Expand All @@ -41,7 +42,7 @@ public function testThatWeHandleNotPromiseReturned()
$this->assertTrue($testData->val, 'Expected our test to run on loop to completion');
}

public function testExpectingAnExceptionThrown()
public function testExpectingAnExceptionThrown(): \Generator
{
$throwException = function () {
return call(function () {
Expand All @@ -56,7 +57,7 @@ public function testExpectingAnExceptionThrown()
yield $throwException();
}

public function argumentSupportProvider()
public function argumentSupportProvider(): array
{
return [
['foo', 42, true],
Expand All @@ -77,7 +78,17 @@ public function testArgumentSupport(string $foo, int $bar, bool $baz)
$this->assertTrue($baz);
}

public function testSetMinimumRunTime()
public function testSetTimeout(): \Generator
{
$this->setTimeout(100);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Expected test to complete before 100ms time limit');

yield new Delayed(200);
}

public function testSetMinimumRunTime(): \Generator
{
$this->setMinimumRuntime(100);
$func = function () {
Expand Down

0 comments on commit 5b901e6

Please sign in to comment.