Skip to content

Commit

Permalink
Fix DB connection tests (#15503)
Browse files Browse the repository at this point in the history
testTransactionsDecrementedOnTransactionException() didn't actually test
that the transaction level was decremented because the exception caused
the test to end early.

testCantSwapPDOWithOpenTransaction() changed to use @ExpectedException
annotation, as is the convention, instead of
$this->setExpectedException(). Also, use $connection->setPdo(null)
instead of $connection->disconnect() which aligns better with the intent
of the test.
  • Loading branch information
nhowell authored and taylorotwell committed Sep 19, 2016
1 parent 6524fea commit eff1791
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions tests/Database/DatabaseConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,23 @@ public function testTransactionsDecrementedOnTransactionException()
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
$pdo->expects($this->once())->method('beginTransaction')->will($this->throwException(new ErrorException('MySQL server has gone away')));
$connection = $this->getMockConnection([], $pdo);
$this->setExpectedException('ErrorException', 'MySQL server has gone away');
$connection->beginTransaction();
$connection->disconnect();
$this->assertNull($connection->getPdo());
try {
$connection->beginTransaction();
} catch (ErrorException $e) {
$this->assertEquals(0, $connection->transactionLevel());
}
}

/**
* @expectedException RuntimeException
*/
public function testCantSwapPDOWithOpenTransaction()
{
$pdo = $this->getMock('DatabaseConnectionTestMockPDO');
$pdo->expects($this->once())->method('beginTransaction')->will($this->returnValue(true));
$connection = $this->getMockConnection([], $pdo);
$connection->beginTransaction();
$this->setExpectedException('RuntimeException', "Can't swap PDO instance while within transaction.");
$connection->disconnect();
$connection->setPdo(null);
}

public function testBeganTransactionFiresEventsIfSet()
Expand Down Expand Up @@ -203,7 +206,7 @@ public function testTransactionMethodDisallowPDOChanging()
$mock = $this->getMockConnection([], $pdo);

$mock->setReconnector(function ($connection) {
$connection->setPDO(null);
$connection->setPdo(null);
});

$mock->transaction(function ($connection) {
Expand Down

0 comments on commit eff1791

Please sign in to comment.