Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge 3.2.x into 4.0.x #4759

Merged
merged 27 commits into from
Aug 27, 2021
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
80af3bc
Refactor DataAccessTest
morozov Jul 24, 2021
fbd0a89
Fix setup in TableGeneratorTest
morozov Jul 25, 2021
234815b
Merge pull request #4731 from morozov/randomized-test-suite
morozov Jul 25, 2021
916c90f
Deprecate reference from foreign key to its referencing table
morozov Aug 3, 2021
634797b
Merge pull request #4743 from morozov/issues/4506-deprecation
morozov Aug 3, 2021
0a54d82
Improve ConnectionTest by triggering real exceptions
grongor Jan 7, 2019
5a360f8
Merge pull request #3425 from grongor/add-functional-tests-for-transa…
morozov Aug 17, 2021
d971d9f
Remove unused static Exception methods
morozov Jul 31, 2021
89d4331
Remove trigger-related methods
morozov Jul 31, 2021
55894c0
Remove unnecessary escaping in regular expressions
morozov Jul 31, 2021
f5fc751
Merge pull request #4752 from morozov/code-cleanup
morozov Aug 24, 2021
ae4bbe6
Deprecate AbstractPlatform::getNowExpression()
morozov Jul 31, 2021
8143950
Merge pull request #4753 from morozov/deprecate-now-expression
morozov Aug 24, 2021
6907620
Remove unused local variables
morozov Jul 31, 2021
57ad4ac
Remove unnecessary property default values
morozov Jul 31, 2021
f3dadc9
Merge pull request #4756 from morozov/code-cleanup
morozov Aug 25, 2021
7b2a322
Deprecate VersionAwarePlatformDriver and ServerInfoAwareConnection
morozov Aug 23, 2021
b46a318
Merge pull request #4751 from morozov/deprecate-server-info-aware-con…
morozov Aug 25, 2021
a380840
Improve isolation in integration tests
morozov Aug 15, 2021
ad5b860
Parametrize AbstractSchemaManager with AbstractPlatform
morozov Aug 22, 2021
1809a91
Platform-aware schema comparison
morozov Aug 14, 2021
0453eef
Merge pull request #4746 from morozov/platform-schema-comparison
morozov Aug 26, 2021
666c065
Reinstate PostgreSQLPlatform and PostgreSQLKeywords
morozov Aug 26, 2021
c8cad0e
Reinstate SQLServerPlatform and SQLServerKeywords
morozov Aug 26, 2021
4b174ad
Deprecate AbstractPlatform::getName()
morozov Aug 24, 2021
d04d0d6
Merge pull request #4755 from morozov/deprecate-platform-get-name
morozov Aug 26, 2021
cc378c3
Merge branch '3.2.x' into 4.0.x
morozov Aug 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve ConnectionTest by triggering real exceptions
Co-authored-by: Simon Podlipsky <simon@podlipsky.net>
  • Loading branch information
grongor and simPod committed Aug 17, 2021
commit 0a54d8289c0dddfcae7bffee405e7b68c38df52e
119 changes: 81 additions & 38 deletions tests/Functional/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ConnectionException;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\IBMDB2;
use Doctrine\DBAL\Driver\PDO\Connection as PDOConnection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use Doctrine\DBAL\Types\Types;
use Error;
use Exception;
use PDO;
use RuntimeException;
use Throwable;

use function file_exists;
Expand All @@ -26,6 +28,8 @@

class ConnectionTest extends FunctionalTestCase
{
private const TABLE = 'connection_test';

protected function tearDown(): void
{
if (file_exists('/tmp/test_nesting.sqlite')) {
Expand Down Expand Up @@ -53,29 +57,32 @@ public function testCommitWithRollbackOnlyThrowsException(): void

public function testTransactionNestingBehavior(): void
{
$this->createTestTable();

try {
$this->connection->beginTransaction();
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
self::assertSame(1, $this->connection->getTransactionNestingLevel());

try {
$this->connection->beginTransaction();
self::assertEquals(2, $this->connection->getTransactionNestingLevel());
self::assertSame(2, $this->connection->getTransactionNestingLevel());

throw new Exception();
$this->connection->insert(self::TABLE, ['id' => 1]);
self::fail('Expected exception to be thrown because of the unique constraint.');
} catch (Throwable $e) {
$this->assertIsUniqueConstraintException($e);
$this->connection->rollBack();
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
//no rethrow
self::assertSame(1, $this->connection->getTransactionNestingLevel());
}

self::assertTrue($this->connection->isRollbackOnly());

$this->connection->commit(); // should throw exception
self::fail('Transaction commit after failed nested transaction should fail.');
} catch (ConnectionException $e) {
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
self::assertSame(1, $this->connection->getTransactionNestingLevel());
$this->connection->rollBack();
self::assertEquals(0, $this->connection->getTransactionNestingLevel());
self::assertSame(0, $this->connection->getTransactionNestingLevel());
}

$this->connection->beginTransaction();
Expand Down Expand Up @@ -119,24 +126,27 @@ public function testTransactionNestingBehaviorWithSavepoints(): void
self::markTestSkipped('This test requires the platform to support savepoints.');
}

$this->createTestTable();

$this->connection->setNestTransactionsWithSavepoints(true);
try {
$this->connection->beginTransaction();
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
self::assertSame(1, $this->connection->getTransactionNestingLevel());

try {
$this->connection->beginTransaction();
self::assertEquals(2, $this->connection->getTransactionNestingLevel());
self::assertSame(2, $this->connection->getTransactionNestingLevel());
$this->connection->beginTransaction();
self::assertEquals(3, $this->connection->getTransactionNestingLevel());
self::assertSame(3, $this->connection->getTransactionNestingLevel());
self::assertTrue($this->connection->commit());
self::assertEquals(2, $this->connection->getTransactionNestingLevel());
self::assertSame(2, $this->connection->getTransactionNestingLevel());

throw new Exception();
$this->connection->insert(self::TABLE, ['id' => 1]);
self::fail('Expected exception to be thrown because of the unique constraint.');
} catch (Throwable $e) {
$this->assertIsUniqueConstraintException($e);
$this->connection->rollBack();
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
//no rethrow
self::assertSame(1, $this->connection->getTransactionNestingLevel());
}

self::assertFalse($this->connection->isRollbackOnly());
Expand Down Expand Up @@ -222,43 +232,45 @@ public function testRollbackSavepointsNotSupportedThrowsException(): void

public function testTransactionBehaviorWithRollback(): void
{
$this->createTestTable();

try {
$this->connection->beginTransaction();
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
self::assertSame(1, $this->connection->getTransactionNestingLevel());

throw new Exception();
$this->connection->insert(self::TABLE, ['id' => 1]);
self::fail('Expected exception to be thrown because of the unique constraint.');
} catch (Throwable $e) {
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
$this->assertIsUniqueConstraintException($e);
self::assertSame(1, $this->connection->getTransactionNestingLevel());
$this->connection->rollBack();
self::assertEquals(0, $this->connection->getTransactionNestingLevel());
self::assertSame(0, $this->connection->getTransactionNestingLevel());
}
}

public function testTransactionBehaviour(): void
{
try {
$this->connection->beginTransaction();
self::assertEquals(1, $this->connection->getTransactionNestingLevel());
$this->connection->commit();
} catch (Throwable $e) {
$this->connection->rollBack();
self::assertEquals(0, $this->connection->getTransactionNestingLevel());
}
$this->createTestTable();

self::assertEquals(0, $this->connection->getTransactionNestingLevel());
$this->connection->beginTransaction();
self::assertSame(1, $this->connection->getTransactionNestingLevel());
$this->connection->insert(self::TABLE, ['id' => 2]);
$this->connection->commit();
self::assertSame(0, $this->connection->getTransactionNestingLevel());
}

public function testTransactionalWithException(): void
{
try {
$this->connection->transactional(static function (Connection $conn): void {
$conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
$this->createTestTable();

throw new RuntimeException('Ooops!');
try {
$this->connection->transactional(static function (Connection $connection): void {
$connection->insert(self::TABLE, ['id' => 1]);
});
self::fail('Expected exception');
} catch (RuntimeException $expected) {
self::assertEquals(0, $this->connection->getTransactionNestingLevel());
self::fail('Expected exception to be thrown because of the unique constraint.');
} catch (Throwable $e) {
$this->assertIsUniqueConstraintException($e);
self::assertSame(0, $this->connection->getTransactionNestingLevel());
}
}

Expand All @@ -278,11 +290,14 @@ public function testTransactionalWithThrowable(): void

public function testTransactional(): void
{
$res = $this->connection->transactional(static function (Connection $conn): void {
$conn->executeQuery($conn->getDatabasePlatform()->getDummySelectSQL());
$this->createTestTable();

$res = $this->connection->transactional(static function (Connection $connection): void {
$connection->insert(self::TABLE, ['id' => 2]);
});

self::assertNull($res);
self::assertSame(0, $this->connection->getTransactionNestingLevel());
}

public function testTransactionalReturnValue(): void
Expand Down Expand Up @@ -373,4 +388,32 @@ public function testPersistentConnection(): void

self::assertTrue($pdo->getAttribute(PDO::ATTR_PERSISTENT));
}

private function createTestTable(): void
{
$table = new Table(self::TABLE);
$table->addColumn('id', 'integer');
$table->setPrimaryKey(['id']);

$this->connection->createSchemaManager()->dropAndCreateTable($table);

$this->connection->insert(self::TABLE, ['id' => 1]);
}

private function assertIsUniqueConstraintException(Throwable $exception): void
{
if ($this->connection->getDriver() instanceof IBMDB2\Driver) {
// The IBM DB2 driver currently doesn't instantiate specialized exceptions

return;
}

if ($this->connection->getDriver() instanceof AbstractSQLServerDriver) {
// The SQL Server drivers currently don't instantiate specialized exceptions

return;
}

self::assertInstanceOf(UniqueConstraintViolationException::class, $exception);
}
}