TransactionWithCountTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Siam
  5. * Date: 2020/6/8
  6. * Time: 10:07
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\ORM\Db\Config;
  10. use EasySwoole\ORM\Db\Connection;
  11. use EasySwoole\ORM\DbManager;
  12. use EasySwoole\ORM\Tests\models\TestCastsModel;
  13. use EasySwoole\ORM\Tests\service\TestTransationService;
  14. use PHPUnit\Framework\TestCase;
  15. class TransactionWithCountTest extends TestCase
  16. {
  17. /**
  18. * @var $connection Connection
  19. */
  20. protected $connection;
  21. protected $tableName = 'user_test_list';
  22. private $lastId;
  23. protected function setUp(): void
  24. {
  25. parent::setUp();
  26. $config = new Config(MYSQL_CONFIG);
  27. $this->connection = new Connection($config);
  28. DbManager::getInstance()->addConnection($this->connection);
  29. $connection = DbManager::getInstance()->getConnection();
  30. $this->assertTrue($connection === $this->connection);
  31. }
  32. public function testClear()
  33. {
  34. $res = TestCastsModel::create()->destroy(null, true);
  35. }
  36. /** 正常情况下 一次的开启事务 */
  37. public function testNormalCommit()
  38. {
  39. DbManager::getInstance()->startTransactionWithCount();
  40. $id = TestCastsModel::create([
  41. "name" => "siam",
  42. "age" => 21,
  43. "addTime" => "2020-6-8 10:11:05",
  44. "state" => 0,
  45. ])->save();
  46. $this->assertIsInt($id);
  47. DbManager::getInstance()->commitWithCount();
  48. $confirm = TestCastsModel::create()->get($id);
  49. $this->assertInstanceOf(TestCastsModel::class, $confirm);
  50. }
  51. public function testNormalRollback()
  52. {
  53. DbManager::getInstance()->startTransactionWithCount();
  54. $id = TestCastsModel::create([
  55. "name" => "siam",
  56. "age" => 21,
  57. "addTime" => "2020-6-8 10:11:05",
  58. "state" => 0,
  59. ])->save();
  60. $this->assertIsInt($id);
  61. DbManager::getInstance()->rollbackWithCount();
  62. $confirm = TestCastsModel::create()->get($id);
  63. $this->assertEquals(null, $confirm);
  64. }
  65. /** 嵌套情况 */
  66. public function testNestingCommit()
  67. {
  68. DbManager::getInstance()->startTransactionWithCount();
  69. $id = TestCastsModel::create([
  70. "name" => "siam",
  71. "age" => 21,
  72. "addTime" => "2020-6-8 10:11:05",
  73. "state" => 0,
  74. ])->save();
  75. $this->assertIsInt($id);
  76. $nestingRes = TestTransationService::getUser();
  77. if (!$nestingRes) {
  78. DbManager::getInstance()->rollbackWithCount();
  79. // 如果回滚了 测一下是否正常回滚
  80. $confirm = TestCastsModel::create()->get($id);
  81. $this->assertEquals(null, $confirm);
  82. return false;
  83. }
  84. DbManager::getInstance()->commitWithCount();
  85. $confirm = TestCastsModel::create()->get($id);
  86. $this->assertInstanceOf(TestCastsModel::class, $confirm);
  87. }
  88. public function testNestingRollback()
  89. {
  90. TestTransationService::$res = false;
  91. DbManager::getInstance()->startTransactionWithCount();
  92. $id = TestCastsModel::create([
  93. "name" => "siam",
  94. "age" => 21,
  95. "addTime" => "2020-6-8 10:11:05",
  96. "state" => 0,
  97. ])->save();
  98. $this->assertIsInt($id);
  99. $nestingRes = TestTransationService::getUser();
  100. if (!$nestingRes) {
  101. DbManager::getInstance()->rollbackWithCount();
  102. // 如果回滚了 测一下是否正常回滚
  103. $confirm = TestCastsModel::create()->get($id);
  104. $this->assertEquals(null, $confirm);
  105. return false;
  106. }
  107. DbManager::getInstance()->commitWithCount();
  108. $confirm = TestCastsModel::create()->get($id);
  109. $this->assertInstanceOf(TestCastsModel::class, $confirm);
  110. }
  111. /** 处理异常情况 */
  112. public function testErrorCommit()
  113. {
  114. }
  115. public function testErrorRollback()
  116. {
  117. }
  118. /** 测试处理不完整,自动回滚 */
  119. public function testForgetDone()
  120. {
  121. go(function () {
  122. DbManager::getInstance()->startTransactionWithCount();
  123. $id = TestCastsModel::create([
  124. "name" => "siam",
  125. "age" => 21,
  126. "addTime" => "2020-6-8 10:11:05",
  127. "state" => 0,
  128. ])->save();
  129. $this->assertIsInt($id);
  130. $this->lastId = $id;
  131. });
  132. \co::sleep(0.1);
  133. $confirm = TestCastsModel::create()->get($this->lastId);
  134. $this->assertEquals(null, $confirm);
  135. }
  136. }