Return promise from StatementPool::prepare(); update tests

This commit is contained in:
Aaron Piotrowski 2018-09-26 11:41:35 -05:00
parent 04fc107cc0
commit 480e90cba8
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
2 changed files with 17 additions and 5 deletions

View File

@ -27,14 +27,14 @@ abstract class StatementPool implements Statement
private $prepare; private $prepare;
/** /**
* Performs any necessary actions to the statement to prepare it for execution, returning the same or a new * Performs any necessary actions to the statement to prepare it for execution, returning a promise for the same or
* Statement object if necessary. * a new Statement object if necessary.
* *
* @param Statement $statement * @param Statement $statement
* *
* @return Statement * @return Promise<Statement>
*/ */
abstract protected function prepare(Statement $statement): Statement; abstract protected function prepare(Statement $statement): Promise;
/** /**
* @param ResultSet $resultSet * @param ResultSet $resultSet
@ -97,7 +97,8 @@ abstract class StatementPool implements Statement
\assert($statement instanceof Statement); \assert($statement instanceof Statement);
try { try {
$statement = $this->prepare($statement); $statement = yield $this->prepare($statement);
\assert($statement instanceof Statement);
$result = yield $statement->execute($params); $result = yield $statement->execute($params);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$this->push($statement); $this->push($statement);

View File

@ -8,6 +8,7 @@ use Amp\PHPUnit\TestCase;
use Amp\Sql\Pool; use Amp\Sql\Pool;
use Amp\Sql\Statement; use Amp\Sql\Statement;
use Amp\Sql\StatementPool; use Amp\Sql\StatementPool;
use Amp\Success;
class StatementPoolTest extends TestCase class StatementPoolTest extends TestCase
{ {
@ -35,6 +36,11 @@ class StatementPoolTest extends TestCase
->setConstructorArgs([$pool, $statement, $this->createCallback(0)]) ->setConstructorArgs([$pool, $statement, $this->createCallback(0)])
->getMockForAbstractClass(); ->getMockForAbstractClass();
$statementPool->method('prepare')
->willReturnCallback(function (Statement $statement) {
return new Success($statement);
});
$this->assertTrue($statementPool->isAlive()); $this->assertTrue($statementPool->isAlive());
$this->assertSame(\time(), $statementPool->getLastUsedAt()); $this->assertSame(\time(), $statementPool->getLastUsedAt());
@ -71,6 +77,11 @@ class StatementPoolTest extends TestCase
->setConstructorArgs([$pool, $statement, $this->createCallback(1)]) ->setConstructorArgs([$pool, $statement, $this->createCallback(1)])
->getMockForAbstractClass(); ->getMockForAbstractClass();
$statementPool->method('prepare')
->willReturnCallback(function (Statement $statement) {
return new Success($statement);
});
$this->assertTrue($statementPool->isAlive()); $this->assertTrue($statementPool->isAlive());
$this->assertSame(\time(), $statementPool->getLastUsedAt()); $this->assertSame(\time(), $statementPool->getLastUsedAt());