Skip to content

Commit

Permalink
Merge bd94bed into bc4c6fd
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Mar 30, 2020
2 parents bc4c6fd + bd94bed commit c2547a1
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/AsyncTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Amp\PHPUnit;

use Amp\Loop;
use Amp\Promise;
use Amp\Success;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
use function Amp\call;
Expand Down Expand Up @@ -55,7 +57,21 @@ final public function runAsyncTest(...$args)
$start = \microtime(true);

Loop::run(function () use (&$returnValue, &$exception, &$invoked, $args) {
$promise = call([$this, $this->realTestName], ...$args);
yield $this->setUpAsync();
$promise = call(function () use ($args): \Generator {
$e = null;

try {
yield call([$this, $this->realTestName], ...$args);
} catch (\Throwable $e) {
}

yield $this->tearDownAsync();

if (null !== $e) {
throw $e;
}
});
$promise->onResolve(function ($error, $value) use (&$invoked, &$exception, &$returnValue) {
$invoked = true;
$exception = $error;
Expand Down Expand Up @@ -146,4 +162,14 @@ final protected function createCallback(int $invocationCount, callable $returnCa

return $mock;
}

protected function setUpAsync(): Promise
{
return new Success();
}

protected function tearDownAsync(): Promise
{
return new Success();
}
}
44 changes: 44 additions & 0 deletions test/AsyncTestCaseWithSetUpAndTearDownTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Amp\PHPUnit\Test;

use Amp\Deferred;
use Amp\Delayed;
use Amp\Loop;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Promise;
use Amp\Success;
use PHPUnit\Framework\AssertionFailedError;
use function Amp\call;

class AsyncTestCaseWithSetUpAndTearDownTest extends AsyncTestCase
{
protected $setupCalled = false;
protected $tearDownCalled = false;

protected function setUpAsync(): Promise
{
$this->setupCalled = true;

return new Success();
}

protected function tearDownAsync(): Promise
{
$this->tearDownCalled = true;

return new Success();
}

public function testThatSetupAndTearDownIsInvoked(): Promise
{
$this->assertTrue($this->setupCalled);

$promise = new Delayed(100);
$promise->onResolve(function () {
$this->assertTrue($this->tearDownCalled);
});

return new Success();
}
}

0 comments on commit c2547a1

Please sign in to comment.