TransactionTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * 事务
  4. * User: Administrator
  5. * Date: 2020/2/12 0012
  6. * Time: 22:32
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\Mysqli\QueryBuilder;
  10. use EasySwoole\ORM\Db\ClientInterface;
  11. use PHPUnit\Framework\TestCase;
  12. use EasySwoole\ORM\Db\Config;
  13. use EasySwoole\ORM\Db\Connection;
  14. use EasySwoole\ORM\DbManager;
  15. use EasySwoole\ORM\Tests\models\TestTimeStampModel;
  16. class TransactionTest extends TestCase
  17. {
  18. /**
  19. * @var $connection Connection
  20. */
  21. protected $connection;
  22. protected $tableName = 'user_test_list';
  23. protected function setUp(): void
  24. {
  25. parent::setUp(); // TODO: Change the autogenerated stub
  26. $config = new Config(MYSQL_CONFIG);
  27. $config->setMinObjectNum(1);
  28. $config->setMaxObjectNum(5);
  29. $this->connection = new Connection($config);
  30. DbManager::getInstance()->addConnection($this->connection);
  31. $connection = DbManager::getInstance()->getConnection();
  32. $this->assertTrue($connection === $this->connection);
  33. }
  34. /**
  35. * @throws \EasySwoole\ORM\Exception\Exception
  36. * @throws \EasySwoole\Pool\Exception\PoolEmpty
  37. * @throws \Throwable
  38. */
  39. public function testInvoke()
  40. {
  41. TestTimeStampModel::create()->destroy([], true);
  42. DbManager::getInstance()->invoke(function ($client) {
  43. DbManager::getInstance()->startTransaction($client);
  44. $model = TestTimeStampModel::invoke($client);
  45. $model->name = 'siam';
  46. $model->age = 21;
  47. $model->save();
  48. $this->assertIsInt($model->id);
  49. DbManager::getInstance()->rollback($client);
  50. // 此时查找 为空
  51. $has = TestTimeStampModel::invoke($client)->get([
  52. 'name' => 'siam'
  53. ]);
  54. $this->assertNull($has);
  55. });
  56. TestTimeStampModel::create()->destroy([], true);
  57. }
  58. public function testWhileCommit()
  59. {
  60. TestTimeStampModel::create()->destroy([], true);
  61. $i = $count = 10;
  62. $ids = [];
  63. while ($i--) {
  64. DbManager::getInstance()->startTransaction();
  65. $model = TestTimeStampModel::create();
  66. $model->name = 'siam';
  67. $model->age = 21;
  68. $model->save();
  69. $ids[] = $model->id;
  70. DbManager::getInstance()->commit();
  71. }
  72. $ret = TestTimeStampModel::create()->where('id', $ids, 'IN')->all();
  73. $this->assertEquals($count, count($ret));
  74. TestTimeStampModel::create()->destroy([], true);
  75. }
  76. public function testWhileCommitInvoke()
  77. {
  78. TestTimeStampModel::create()->destroy([], true);
  79. $i = $count = 10;
  80. while ($i--) {
  81. DbManager::getInstance()->invoke(function (ClientInterface $client) use (&$ids) {
  82. DbManager::getInstance()->startTransaction($client);
  83. $model = TestTimeStampModel::invoke($client);
  84. $model->name = 'siam';
  85. $model->age = 21;
  86. $model->save();
  87. $ids[] = $model->id;
  88. DbManager::getInstance()->commit($client);
  89. });
  90. }
  91. $ret = TestTimeStampModel::create()->where('id', $ids, 'IN')->all();
  92. $this->assertEquals($count, count($ret));
  93. TestTimeStampModel::create()->destroy([], true);
  94. }
  95. public function testWhileRollback()
  96. {
  97. TestTimeStampModel::create()->destroy([], true);
  98. $i = $count = 10;
  99. $ids = [];
  100. while ($i--) {
  101. try {
  102. DbManager::getInstance()->startTransaction();
  103. $model = TestTimeStampModel::create();
  104. $model->name = 'siam';
  105. $model->age = 21;
  106. $model->save();
  107. $ids[] = $model->id;
  108. $builder = new QueryBuilder();
  109. $builder->insert('tiamstamp_test', ['a' => 1]);
  110. DbManager::getInstance()->query($builder, true);
  111. DbManager::getInstance()->commit();
  112. } catch (\Throwable $throwable) {
  113. try {
  114. DbManager::getInstance()->rollback();
  115. } catch (\Throwable $throwable) {
  116. }
  117. }
  118. }
  119. $ret = TestTimeStampModel::create()->where('id', $ids, 'IN')->all();
  120. $this->assertEquals(0, count($ret));
  121. TestTimeStampModel::create()->destroy([], true);
  122. }
  123. public function testWhileRollbackInvoke()
  124. {
  125. $i = $count = 10;
  126. while ($i--) {
  127. DbManager::getInstance()->invoke(function ($client) use (&$ids) {
  128. try {
  129. DbManager::getInstance()->startTransaction($client);
  130. $model = TestTimeStampModel::invoke($client);
  131. $model->name = 'siam';
  132. $model->age = 21;
  133. $model->save();
  134. $ids[] = $model->id;
  135. $builder = new QueryBuilder();
  136. $builder->insert('tiamstamp_test', ['a' => 1]);
  137. DbManager::getInstance()->query($builder, true, $client);
  138. DbManager::getInstance()->commit($client);
  139. } catch (\Throwable $throwable) {
  140. try {
  141. DbManager::getInstance()->rollback($client);
  142. } catch (\Throwable $throwable) {
  143. }
  144. }
  145. });
  146. }
  147. $ret = TestTimeStampModel::create()->where('id', $ids, 'IN')->all();
  148. $this->assertEquals(0, count($ret));
  149. TestTimeStampModel::create()->destroy([], true);
  150. }
  151. }