123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace EasySwoole\ORM\Tests;
- use EasySwoole\ORM\Db\Config;
- use EasySwoole\ORM\Db\Connection;
- use EasySwoole\ORM\DbManager;
- use EasySwoole\ORM\Tests\models\TestCastsModel;
- use EasySwoole\ORM\Tests\service\TestTransationService;
- use PHPUnit\Framework\TestCase;
- class TransactionWithCountTest extends TestCase
- {
-
- protected $connection;
- protected $tableName = 'user_test_list';
- private $lastId;
- protected function setUp(): void
- {
- parent::setUp();
- $config = new Config(MYSQL_CONFIG);
- $this->connection = new Connection($config);
- DbManager::getInstance()->addConnection($this->connection);
- $connection = DbManager::getInstance()->getConnection();
- $this->assertTrue($connection === $this->connection);
- }
- public function testClear()
- {
- $res = TestCastsModel::create()->destroy(null, true);
- }
-
- public function testNormalCommit()
- {
- DbManager::getInstance()->startTransactionWithCount();
- $id = TestCastsModel::create([
- "name" => "siam",
- "age" => 21,
- "addTime" => "2020-6-8 10:11:05",
- "state" => 0,
- ])->save();
- $this->assertIsInt($id);
- DbManager::getInstance()->commitWithCount();
- $confirm = TestCastsModel::create()->get($id);
- $this->assertInstanceOf(TestCastsModel::class, $confirm);
- }
- public function testNormalRollback()
- {
- DbManager::getInstance()->startTransactionWithCount();
- $id = TestCastsModel::create([
- "name" => "siam",
- "age" => 21,
- "addTime" => "2020-6-8 10:11:05",
- "state" => 0,
- ])->save();
- $this->assertIsInt($id);
- DbManager::getInstance()->rollbackWithCount();
- $confirm = TestCastsModel::create()->get($id);
- $this->assertEquals(null, $confirm);
- }
-
- public function testNestingCommit()
- {
- DbManager::getInstance()->startTransactionWithCount();
- $id = TestCastsModel::create([
- "name" => "siam",
- "age" => 21,
- "addTime" => "2020-6-8 10:11:05",
- "state" => 0,
- ])->save();
- $this->assertIsInt($id);
- $nestingRes = TestTransationService::getUser();
- if (!$nestingRes) {
- DbManager::getInstance()->rollbackWithCount();
-
- $confirm = TestCastsModel::create()->get($id);
- $this->assertEquals(null, $confirm);
- return false;
- }
- DbManager::getInstance()->commitWithCount();
- $confirm = TestCastsModel::create()->get($id);
- $this->assertInstanceOf(TestCastsModel::class, $confirm);
- }
- public function testNestingRollback()
- {
- TestTransationService::$res = false;
- DbManager::getInstance()->startTransactionWithCount();
- $id = TestCastsModel::create([
- "name" => "siam",
- "age" => 21,
- "addTime" => "2020-6-8 10:11:05",
- "state" => 0,
- ])->save();
- $this->assertIsInt($id);
- $nestingRes = TestTransationService::getUser();
- if (!$nestingRes) {
- DbManager::getInstance()->rollbackWithCount();
-
- $confirm = TestCastsModel::create()->get($id);
- $this->assertEquals(null, $confirm);
- return false;
- }
- DbManager::getInstance()->commitWithCount();
- $confirm = TestCastsModel::create()->get($id);
- $this->assertInstanceOf(TestCastsModel::class, $confirm);
- }
-
- public function testErrorCommit()
- {
- }
- public function testErrorRollback()
- {
- }
-
- public function testForgetDone()
- {
- go(function () {
- DbManager::getInstance()->startTransactionWithCount();
- $id = TestCastsModel::create([
- "name" => "siam",
- "age" => 21,
- "addTime" => "2020-6-8 10:11:05",
- "state" => 0,
- ])->save();
- $this->assertIsInt($id);
- $this->lastId = $id;
- });
- \co::sleep(0.1);
- $confirm = TestCastsModel::create()->get($this->lastId);
- $this->assertEquals(null, $confirm);
- }
- }
|