Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pulzarraider committed Jun 25, 2019
1 parent 7205a51 commit f78fe8a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 82 deletions.
4 changes: 0 additions & 4 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,6 @@ public function beginTransaction()
/**
* {@inheritDoc}
*
* @return bool
*
* @throws ConnectionException If the commit failed due to no active transaction or
* because the transaction was marked for rollback only.
*/
Expand Down Expand Up @@ -1326,8 +1324,6 @@ private function commitAll()
/**
* Cancels any database changes done during the current transaction.
*
* @return bool
*
* @throws ConnectionException If the rollback operation failed.
*/
public function rollBack()
Expand Down
76 changes: 18 additions & 58 deletions tests/Doctrine/Tests/DBAL/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,95 +299,55 @@ public function testCommitStartsTransactionInNoAutoCommitMode() : void
}

/**
* @group DBAL-81
* @dataProvider resultProvider
*/
public function testCommitReturnTrue() : void
public function testCommitReturn(bool $expectedResult) : void
{
$driverMock = $this->createMock(Driver::class);
$driverMock->expects($this->any())
->method('connect')
->will($this->returnValue(
$this->createMock(DriverConnection::class)
));
$pdo = $this->createMock(PDO::class);
$pdo->expects($this->once())
->method('commit')->willReturn(true);

$conn = new Connection(['pdo' => $pdo], $driverMock);

$conn->connect();
$conn->beginTransaction();

self::assertTrue($conn->commit());
}
->method('commit')->willReturn($expectedResult);

/**
* @group DBAL-81
*/
public function testCommitReturnFalse() : void
{
$driverMock = $this->createMock(Driver::class);
$driverMock->expects($this->any())
->method('connect')
->will($this->returnValue(
$this->createMock(DriverConnection::class)
));
$pdo = $this->createMock(PDO::class);
$pdo->expects($this->once())
->method('commit')->willReturn(false);
->will($this->returnValue($pdo));

$conn = new Connection(['pdo' => $pdo], $driverMock);
$conn = new Connection([], $driverMock);

$conn->connect();
$conn->beginTransaction();

self::assertFalse($conn->commit());
self::assertSame($expectedResult, $conn->commit());
}

/**
* @group DBAL-81
* @dataProvider resultProvider
*/
public function testRollackReturnTrue() : void
public function testRollackReturn(bool $expectedResult) : void
{
$pdo = $this->createMock(PDO::class);
$pdo->expects($this->once())
->method('rollback')->willReturn($expectedResult);

$driverMock = $this->createMock(Driver::class);
$driverMock->expects($this->any())
->method('connect')
->will($this->returnValue(
$this->createMock(DriverConnection::class)
));
$pdo = $this->createMock(PDO::class);
$pdo->expects($this->once())
->method('rollback')->willReturn(true);
->will($this->returnValue($pdo));

$conn = new Connection(['pdo' => $pdo], $driverMock);
$conn = new Connection([], $driverMock);

$conn->connect();
$conn->beginTransaction();

self::assertTrue($conn->rollback());
self::assertSame($expectedResult, $conn->rollback());
}

/**
* @group DBAL-81
* @return array
*/
public function testRollackReturnFalse() : void
public function resultProvider() : array
{
$driverMock = $this->createMock(Driver::class);
$driverMock->expects($this->any())
->method('connect')
->will($this->returnValue(
$this->createMock(DriverConnection::class)
));
$pdo = $this->createMock(PDO::class);
$pdo->expects($this->once())
->method('rollback')->willReturn(false);

$conn = new Connection(['pdo' => $pdo], $driverMock);

$conn->connect();
$conn->beginTransaction();

self::assertFalse($conn->rollback());
return [[true], [false]];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/DBAL/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testTransactionNestingBehavior() : void
}
self::assertTrue($this->connection->isRollbackOnly());

self::assertTrue($this->connection->commit()); // should throw exception
$this->connection->commit(); // should throw exception
$this->fail('Transaction commit after failed nested transaction should fail.');
} catch (ConnectionException $e) {
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
Expand Down
28 changes: 9 additions & 19 deletions tests/Doctrine/Tests/DBAL/Functional/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\Tests\DbalFunctionalTestCase;
use Throwable;
use function in_array;
use PHPUnit\Framework\Error\Warning;
use function sleep;

class TransactionTest extends DbalFunctionalTestCase
Expand All @@ -14,7 +13,9 @@ protected function setUp() : void
{
parent::setUp();

if (in_array($this->connection->getDatabasePlatform()->getName(), ['mysql'])) {
Warning::$enabled = false; // we will ignore `MySQL server has gone away` warnings

if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
return;
}

Expand All @@ -23,6 +24,7 @@ protected function setUp() : void

protected function tearDown() : void
{
Warning::$enabled = true;
$this->resetSharedConn();

parent::tearDown();
Expand All @@ -32,22 +34,10 @@ public function testCommitFalse() : void
{
$this->connection->query('SET SESSION wait_timeout=1');

$table = new Table('foo');
$table->addColumn('id', 'integer');
$table->setPrimaryKey(['id']);

$this->connection->getSchemaManager()->createTable($table);
$this->assertTrue($this->connection->beginTransaction());

self::assertEquals('foo', $table->getName());
try {
$this->assertTrue($this->connection->beginTransaction());
$this->connection->executeUpdate('INSERT INTO foo (id) VALUES(1)');
sleep(2); // during the sleep mysql will close the connection

sleep(2); // during the sleep mysql will close the connection

$this->assertFalse(@$this->connection->commit()); // we will ignore `MySQL server has gone away` warnings
} catch (Throwable $exception) {
$this->fail('Is the PHP/PDO bug https://bugs.php.net/bug.php?id=66528 fixed? Normally no exception is thrown.');
}
$this->assertFalse($this->connection->commit());
}
}

0 comments on commit f78fe8a

Please sign in to comment.