InvokeOrmTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * invoke模式
  4. * User: Siam
  5. * Date: 2019/12/12
  6. * Time: 17:57
  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 PHPUnit\Framework\TestCase;
  13. use EasySwoole\ORM\Tests\models\TestUserListGetterModel;
  14. class InvokeOrmTest extends TestCase
  15. {
  16. /**
  17. * @var $connection Connection
  18. */
  19. protected $connection;
  20. protected $tableName = 'user_test_list';
  21. protected function setUp(): void
  22. {
  23. parent::setUp(); // TODO: Change the autogenerated stub
  24. $config = new Config(MYSQL_CONFIG);
  25. $this->connection = new Connection($config);
  26. DbManager::getInstance()->addConnection($this->connection);
  27. $connection = DbManager::getInstance()->getConnection();
  28. $this->assertTrue($connection === $this->connection);
  29. }
  30. /**
  31. * @throws \Throwable
  32. */
  33. public function testAdd()
  34. {
  35. DbManager::getInstance()->invoke(function ($client) {
  36. $testUserModel = TestUserListGetterModel::invoke($client);
  37. $testUserModel->state = 1;
  38. $testUserModel->name = 'Siam';
  39. $testUserModel->age = 18;
  40. $testUserModel->addTime = date('Y-m-d H:i:s');
  41. $data = $testUserModel->save();
  42. $this->assertIsInt($data);
  43. });
  44. }
  45. /**
  46. * @throws \Throwable
  47. */
  48. public function testGet()
  49. {
  50. DbManager::getInstance()->invoke(function ($client) {
  51. $testUserModel = TestUserListGetterModel::invoke($client)->get([
  52. 'state' => 1,
  53. 'name' => 'Siam',
  54. 'age' => 18
  55. ]);
  56. $this->assertInstanceOf(TestUserListGetterModel::class, $testUserModel);
  57. });
  58. }
  59. /**
  60. * @throws \Throwable
  61. */
  62. public function testGetVal()
  63. {
  64. DbManager::getInstance()->invoke(function ($client) {
  65. $res = TestUserListGetterModel::invoke($client)->where([
  66. 'state' => 1,
  67. 'name' => 'Siam',
  68. 'age' => 18
  69. ])->val('name');
  70. $this->assertEquals($res, "Siam");
  71. });
  72. DbManager::getInstance()->invoke(function ($client) {
  73. $res = TestUserListGetterModel::invoke($client)->where([
  74. 'state' => 1,
  75. 'name' => 'undefined',
  76. 'age' => 18
  77. ])->val('name');
  78. $this->assertNull($res);
  79. });
  80. }
  81. /**
  82. * @throws \Throwable
  83. */
  84. public function testUpdate()
  85. {
  86. DbManager::getInstance()->invoke(function ($client) {
  87. $testUserModel = TestUserListGetterModel::invoke($client)->get([
  88. 'state' => 1,
  89. 'name' => 'Siam',
  90. 'age' => 18
  91. ]);
  92. $this->assertInstanceOf(TestUserListGetterModel::class, $testUserModel);
  93. $testUserModel->age = 28;
  94. $res = $testUserModel->update();
  95. $this->assertTrue($res);
  96. });
  97. }
  98. /**
  99. * @throws \Throwable
  100. */
  101. public function testAffairAdd()
  102. {
  103. DbManager::getInstance()->invoke(function ($client) {
  104. $res = DbManager::getInstance()->startTransaction($client);
  105. $this->assertTrue($res);
  106. $testUserModel = TestUserListGetterModel::invoke($client);
  107. $testUserModel->state = 1;
  108. $testUserModel->name = 'Siam';
  109. $testUserModel->age = 22;
  110. $testUserModel->addTime = date('Y-m-d H:i:s');
  111. $data = $testUserModel->save();
  112. $this->assertIsInt($data);
  113. $res = DbManager::getInstance()->rollback($client);
  114. $this->assertTrue($res);
  115. $testUserModelGet = TestUserListGetterModel::invoke($client)->get([
  116. 'state' => 1,
  117. 'name' => 'Siam',
  118. 'age' => 22
  119. ]);
  120. $this->assertNull($testUserModelGet);
  121. });
  122. }
  123. /**
  124. * @throws \Throwable
  125. */
  126. public function testAll()
  127. {
  128. DbManager::getInstance()->invoke(function ($client) {
  129. $test = TestUserListGetterModel::invoke($client)->all();
  130. $this->assertEquals(1, count($test));
  131. });
  132. }
  133. /**
  134. * @throws \Throwable
  135. */
  136. public function testDelete()
  137. {
  138. DbManager::getInstance()->invoke(function ($client) {
  139. $testUserModel = TestUserListGetterModel::invoke($client);
  140. $res = $testUserModel->destroy(null, true);
  141. $this->assertIsInt($res);
  142. });
  143. }
  144. }