Skip to content

Commit

Permalink
Fix referencing of ResultSet in Transaction::execute() + more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Dec 11, 2017
1 parent 2d50dc5 commit b2b3914
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 10 deletions.
7 changes: 6 additions & 1 deletion lib/Internal/ResultProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ public function rowFetched($row) {
}
}

public function __debugInfo() {
/**
* @return array
*
* @codeCoverageIgnore
*/
public function __debugInfo(): array {
$tmp = clone $this;
foreach ($tmp->deferreds as &$type) {
foreach ($type as &$entry) {
Expand Down
3 changes: 1 addition & 2 deletions lib/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ public function execute(string $sql, array $params = []): Promise {
throw $exception;
}

if ($result instanceof Internal\ResultProxy) {
$result = new ResultSet($result);
if ($result instanceof ResultSet) {
$result->onDestruct([$this->queue, "unreference"]);
} else {
$this->queue->unreference();
Expand Down
22 changes: 17 additions & 5 deletions test/AbstractPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use Amp\Mysql\Statement;
use Amp\Mysql\Transaction;
use Amp\Promise;
use PHPUnit\Framework\TestCase;
use function Amp\call;
use function Amp\Mysql\connect;

abstract class AbstractPoolTest extends TestCase {
abstract class AbstractPoolTest extends LinkTest {
/**
* @param array $connections
*
Expand All @@ -23,7 +23,19 @@ abstract class AbstractPoolTest extends TestCase {
abstract protected function createPool(array $connections): Pool;

/**
* @return \PHPUnit_Framework_MockObject_MockObject|\Amp\Mysql\Connection
* @param string $connectionString
*
* @return \Amp\Promise<\Amp\Mysql\Connection>
*/
protected function getLink(string $connectionString): Promise {
return call(function () use ($connectionString) {
$connection = yield connect($connectionString);
return $this->createPool([$connection]);
});
}

/**
* @return \PHPUnit\Framework\MockObject\MockObject|\Amp\Mysql\Connection
*/
protected function createConnection(): Connection {
$mock = $this->createMock(Connection::class);
Expand All @@ -34,7 +46,7 @@ protected function createConnection(): Connection {
/**
* @param int $count
*
* @return \Amp\Mysql\Connection[]|\PHPUnit_Framework_MockObject_MockObject[]
* @return \Amp\Mysql\Connection[]|\PHPUnit\Framework\MockObject\MockObject[]
*/
private function makeConnectionSet(int $count) {
$connections = [];
Expand Down Expand Up @@ -138,7 +150,7 @@ public function getConnectionCounts() {
*
* @param int $count
*/
public function testTransaction(int $count) {
public function testMutlipleTransactions(int $count) {
$connections = $this->makeConnectionSet($count);

$connection = $connections[0];
Expand Down
3 changes: 2 additions & 1 deletion test/ConnectionPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Amp\Mysql\ResultSet;
use Amp\Promise;
use Amp\Success;
use function Amp\Mysql\pool;

class ConnectionPoolTest extends AbstractPoolTest {
protected function getLink(string $connectionString): Promise {
Expand Down Expand Up @@ -61,7 +62,7 @@ public function testSmallPool() {
*/
public function testWrongPassword() {
Loop::run(function () {
$db = new ConnectionPool(ConnectionConfig::parseConnectionString("host=".DB_HOST.";user=".DB_USER.";pass=the_wrong_password;db=test"));
$db = pool("host=".DB_HOST.";user=".DB_USER.";pass=the_wrong_password;db=test");

/* Try a query */
yield $db->query("CREATE TABLE tmp SELECT 1 AS a, 2 AS b");
Expand Down
8 changes: 8 additions & 0 deletions test/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ public function testConnect() {
});
$this->assertTrue($complete, "Database commands did not complete.");
}

/**
* @expectedException \Error
* @expectedExceptionMessage Required parameters host, user and pass need to be passed in connection string
*/
public function testInvalidConnectionString() {
$promise = connect("username=".DB_USER);
}
}
55 changes: 54 additions & 1 deletion test/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,54 @@ public function testPrepareWithInvalidQuery() {
});
}

/**
* @expectedException \Error
* @expectedExceptionMessage Parameter id 1 is not defined for this prepared statement
*/
public function testBindWithInvalidParamId() {
Loop::run(function () {
/** @var \Amp\Mysql\Link $db */
$db = yield $this->getLink("host=" . DB_HOST . ";user=" . DB_USER . ";pass=" . DB_PASS . ";db=test");

/** @var \Amp\Mysql\Statement $statement */
$statement = yield $db->prepare("SELECT * FROM main WHERE a = ?");

$statement->bind(1, 1);
});
}

/**
* @expectedException \Error
* @expectedExceptionMessage Parameter :b is not defined for this prepared statement
*/
public function testBindWithInvalidParamName() {
Loop::run(function () {
/** @var \Amp\Mysql\Link $db */
$db = yield $this->getLink("host=" . DB_HOST . ";user=" . DB_USER . ";pass=" . DB_PASS . ";db=test");

/** @var \Amp\Mysql\Statement $statement */
$statement = yield $db->prepare("SELECT * FROM main WHERE a = :a");

$statement->bind("b", 1);
});
}

/**
* @expectedException \Error
* @expectedExceptionMessage Invalid parameter ID type
*/
public function testBindWithInvalidParamType() {
Loop::run(function () {
/** @var \Amp\Mysql\Link $db */
$db = yield $this->getLink("host=" . DB_HOST . ";user=" . DB_USER . ";pass=" . DB_PASS . ";db=test");

/** @var \Amp\Mysql\Statement $statement */
$statement = yield $db->prepare("SELECT * FROM main WHERE a = :a");

$statement->bind(3.14, 1);
});
}

/**
* @expectedException \Error
* @expectedExceptionMessage Parameter 1 for prepared statement missing
Expand Down Expand Up @@ -252,6 +300,7 @@ public function testPreparedWithNegativeValue() {
/** @var \Amp\Mysql\Statement $stmt */
$stmt = yield $db->prepare("CREATE TABLE tmp SELECT ? AS a");
yield $stmt->execute([-1]);
$stmt->close();

/** @var \Amp\Mysql\ResultSet $result */
$stmt = yield $db->prepare("SELECT a FROM tmp");
Expand All @@ -270,8 +319,11 @@ public function testTransaction() {
/** @var \Amp\Mysql\Transaction $transaction */
$transaction = yield $db->transaction();

$result = yield $transaction->execute("INSERT INTO main VALUES (?, ?)", [6, 7]);
/** @var \Amp\Mysql\Statement $statement */
$statement = yield $transaction->prepare("INSERT INTO main VALUES (?, ?)");
$result = yield $statement->execute([6, 7]);
$this->assertInstanceOf(CommandResult::class, $result);
$statement->close();

/** @var \Amp\Mysql\ResultSet $result */
$result = yield $transaction->execute("SELECT * FROM main WHERE a = ?", [6]);
Expand All @@ -281,6 +333,7 @@ public function testTransaction() {
$got[] = $result->getCurrent();
}
$this->assertCount(1, $got);
yield $result->nextResultSet();

yield $transaction->rollback();

Expand Down

0 comments on commit b2b3914

Please sign in to comment.