SaveAllTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * 批量插入、更新
  4. * User: Siam
  5. * Date: 2019/12/5
  6. * Time: 17:22
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\Mysqli\Exception\Exception;
  10. use EasySwoole\ORM\Db\Config;
  11. use EasySwoole\ORM\Db\Connection;
  12. use EasySwoole\ORM\DbManager;
  13. use PHPUnit\Framework\TestCase;
  14. use EasySwoole\ORM\Tests\models\TestUserListModel;
  15. class SaveAllTest extends TestCase
  16. {
  17. /**
  18. * @var $connection Connection
  19. */
  20. protected $connection;
  21. protected $tableName = 'user_test_list';
  22. protected function setUp(): void
  23. {
  24. parent::setUp(); // TODO: Change the autogenerated stub
  25. $config = new Config(MYSQL_CONFIG);
  26. $this->connection = new Connection($config);
  27. // DbManager::getInstance()->addConnection($this->connection);
  28. DbManager::getInstance()->addConnection($this->connection, 'siam');
  29. $connection = DbManager::getInstance()->getConnection('siam');
  30. $this->assertTrue($connection === $this->connection);
  31. }
  32. public function testSaveAllUseConnectName()
  33. {
  34. $data = [
  35. [
  36. 'name' => 'siam,你好',
  37. 'age' => 21,
  38. 'addTime' => "2019-11-22 20:19:16",
  39. 'state' => 1
  40. ],
  41. [
  42. 'name' => 'siam,你好',
  43. 'age' => 21,
  44. 'addTime' => "2019-11-22 20:19:16",
  45. 'state' => 2
  46. ]
  47. ];
  48. $res = TestUserListModel::create()->connection('siam')->saveAll($data);
  49. $this->assertEquals(count($res), 2);
  50. $this->assertIsInt($res[1]['id']);
  51. }
  52. public function testSaveAll()
  53. {
  54. $data = [
  55. [
  56. 'name' => 'siam,你好',
  57. 'age' => 21,
  58. 'addTime' => "2019-11-22 20:19:16",
  59. 'state' => 1
  60. ],
  61. [
  62. 'name' => 'siam,你好',
  63. 'age' => 21,
  64. 'addTime' => "2019-11-22 20:19:16",
  65. 'state' => 2
  66. ]
  67. ];
  68. $res = TestUserListModel::create()->connection('siam')->saveAll($data);
  69. $this->assertEquals(count($res), 2);
  70. $this->assertIsInt($res[1]['id']);
  71. return [
  72. $res[0]['id'],
  73. $res[1]['id'],
  74. ];
  75. }
  76. /**
  77. * @depends testSaveAll
  78. */
  79. public function testUpdateAll($ids)
  80. {
  81. $data = [
  82. [
  83. 'id' => $ids[0],
  84. 'name' => 'siam,你好',
  85. 'age' => 21,
  86. 'addTime' => "error",
  87. 'state' => 127,
  88. ],
  89. [
  90. 'id' => $ids[1],
  91. 'name' => 'siam,你好',
  92. 'age' => 21,
  93. 'addTime' => "2019-11-22 20:19:16",
  94. 'state' => 127
  95. ]
  96. ];
  97. try {
  98. $res = TestUserListModel::create()->connection('siam')->saveAll($data);
  99. $this->assertEquals(count($res), 2);
  100. $this->assertEquals($res[0]['id'], $ids[0]);
  101. $this->assertEquals($res[0]['state'], 127);
  102. $this->assertIsInt($res[1]['id']);
  103. } catch (Exception $e) {
  104. } catch (\EasySwoole\ORM\Exception\Exception $e) {
  105. $res = strpos($e->getMessage(), "SQLSTATE[22007] [1292] Incorrect datetime value: 'error' for column 'addTime' at row 1") !== false;
  106. $this->assertTrue($res);
  107. } catch (\Throwable $e) {
  108. }
  109. }
  110. /**
  111. * @depends testSaveAll
  112. */
  113. public function testSaveAllNotReplace($ids)
  114. {
  115. $data = [
  116. [
  117. 'id' => $ids[0],
  118. 'name' => 'siam,你好',
  119. 'age' => 21,
  120. 'addTime' => "2019-11-22 20:19:16",
  121. 'state' => 127,
  122. ],
  123. [
  124. 'id' => $ids[1],
  125. 'name' => 'siam,你好',
  126. 'age' => 21,
  127. 'addTime' => "2019-11-22 20:19:16",
  128. 'state' => 127
  129. ]
  130. ];
  131. try {
  132. $res = TestUserListModel::create()->connection('siam')->saveAll($data, FALSE);
  133. } catch (Exception $e) {
  134. } catch (\EasySwoole\ORM\Exception\Exception $e) {
  135. $this->assertNotFalse(strpos($e->getMessage(), "SQLSTATE[23000] [1062] Duplicate entry '{$ids[0]}'"));
  136. } catch (\Throwable $e) {
  137. }
  138. }
  139. public function testDeleteAll()
  140. {
  141. $res = TestUserListModel::create()->connection('siam')->destroy(null, true);
  142. $this->assertIsInt($res);
  143. }
  144. }