Add transaction/retry support for yii\db\Command

This commit is contained in:
Sergey Makinen
2017-12-26 13:10:36 +03:00
parent 7e90962029
commit cc2e28bd0e
2 changed files with 137 additions and 7 deletions

View File

@ -10,6 +10,7 @@ namespace yiiunit\framework\db;
use yii\caching\FileCache;
use yii\db\Connection;
use yii\db\DataReader;
use yii\db\Exception;
use yii\db\Expression;
use yii\db\Schema;
@ -1129,4 +1130,50 @@ SQL;
$db->createCommand()->dropTable($tableName)->execute();
$this->assertNull($db->getSchema()->getTableSchema($tableName));
}
/**
* @group iss
*/
public function testTransaction()
{
$connection = $this->getConnection(false);
$this->assertNull($connection->transaction);
$command = $connection->createCommand("INSERT INTO {{profile}}([[description]]) VALUES('command transaction')");
$this->invokeMethod($command, 'requireTransaction');
$command->execute();
$this->assertNull($connection->transaction);
$this->assertEquals(1, $connection->createCommand("SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'command transaction'")->queryScalar());
}
/**
* @group iss
*/
public function testRetryHandler()
{
$connection = $this->getConnection(false);
$this->assertNull($connection->transaction);
$connection->createCommand("INSERT INTO {{profile}}([[description]]) VALUES('command retry')")->execute();
$this->assertNull($connection->transaction);
$this->assertEquals(1, $connection->createCommand("SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'command retry'")->queryScalar());
$attempts = null;
$hitHandler = false;
$hitCatch = false;
$command = $connection->createCommand("INSERT INTO {{profile}}([[id]], [[description]]) VALUES(1, 'command retry')");
$this->invokeMethod($command, 'setRetryHandler', [function ($exception, $attempt) use (&$attempts, &$hitHandler) {
$attempts = $attempt;
$hitHandler = true;
return $attempt <= 2;
}]);
try {
$command->execute();
} catch (Exception $e) {
$hitCatch = true;
$this->assertInstanceOf('yii\db\IntegrityException', $e);
}
$this->assertNull($connection->transaction);
$this->assertSame(3, $attempts);
$this->assertTrue($hitHandler);
$this->assertTrue($hitCatch);
}
}