QueryBuilderTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @author gaobinzhan <gaobinzhan@gmail.com>
  4. */
  5. namespace EasySwoole\ORM\Tests;
  6. use EasySwoole\Mysqli\QueryBuilder;
  7. use EasySwoole\ORM\Db\Config;
  8. use EasySwoole\ORM\Db\Connection;
  9. use EasySwoole\ORM\DbManager;
  10. use EasySwoole\ORM\Tests\models\TestRelationModel;
  11. use PHPUnit\Framework\TestCase;
  12. class QueryBuilderTest extends TestCase
  13. {
  14. /**
  15. * @var $connection Connection
  16. */
  17. protected $connection;
  18. protected $ids = [];
  19. protected function setUp(): void
  20. {
  21. parent::setUp(); // TODO: Change the autogenerated stub
  22. $config = new Config(MYSQL_CONFIG);
  23. $config->setReturnCollection(true);
  24. $this->connection = new Connection($config);
  25. DbManager::getInstance()->addConnection($this->connection);
  26. $connection = DbManager::getInstance()->getConnection();
  27. $this->assertTrue($connection === $this->connection);
  28. }
  29. public function testAdd()
  30. {
  31. $model = new TestRelationModel();
  32. $time = date('Y-m-d');
  33. $this->ids[] = $model->data([
  34. 'name' => 'gaobinzhan',
  35. 'age' => 20,
  36. 'addTime' => $time,
  37. 'state' => 1
  38. ])->save();
  39. $this->ids[] = TestRelationModel::create()->data([
  40. 'name' => 'gaobinzhan',
  41. 'age' => 20,
  42. 'addTime' => $time,
  43. 'state' => 1
  44. ])->save();
  45. $this->assertEquals("INSERT INTO `test_user_model` (`name`, `age`, `addTime`, `state`) VALUES ('gaobinzhan', 20, '{$time}', 1)", $model->lastQuery()->getLastQuery());
  46. }
  47. public function testSelect()
  48. {
  49. $model = new TestRelationModel();
  50. $this->testAdd();
  51. $model->where('id', $this->ids, 'in')->all()->toArray();
  52. $ids = implode(', ', $this->ids);
  53. $this->assertEquals("SELECT * FROM `test_user_model` WHERE `id` in ( {$ids} ) ", $model->lastQuery()->getLastQuery());
  54. $model->where(function (QueryBuilder $builder) {
  55. $builder->where('id', $this->ids, 'in');
  56. })->all()->toArray();
  57. $ids = implode(', ', $this->ids);
  58. $this->assertEquals("SELECT * FROM `test_user_model` WHERE `id` in ( {$ids} ) ", $model->lastQuery()->getLastQuery());
  59. }
  60. public function testUpdate()
  61. {
  62. $model = new TestRelationModel();
  63. $this->testAdd();
  64. foreach ($this->ids as $id) {
  65. $model->where('id', $id)->update(['name' => 'gaobinzhan1']);
  66. $this->assertEquals("UPDATE `test_user_model` SET `name` = 'gaobinzhan1' WHERE `id` = {$id} ", $model->lastQuery()->getLastQuery());
  67. $model->where(function (QueryBuilder $builder) use ($id) {
  68. $builder->where('id', $id);
  69. })->update(['name' => 'gaobinzhan1']);
  70. $this->assertEquals("UPDATE `test_user_model` SET `name` = 'gaobinzhan1' WHERE `id` = {$id} ", $model->lastQuery()->getLastQuery());
  71. }
  72. $id = current($this->ids);
  73. $model->where('id', $id)->update(['age' => 22]);
  74. $this->assertEquals("UPDATE `test_user_model` SET `age` = 22 WHERE `id` = {$id} ", $model->lastQuery()->getLastQuery());
  75. $model->where(function (QueryBuilder $builder) use ($id) {
  76. $builder->where('id', $id);
  77. })->update(['age' => 22]);
  78. $this->assertEquals("UPDATE `test_user_model` SET `age` = 22 WHERE `id` = {$id} ", $model->lastQuery()->getLastQuery());
  79. }
  80. public function testDelete()
  81. {
  82. $model = new TestRelationModel();
  83. $model->destroy(null, true);
  84. $this->testAdd();
  85. foreach ($this->ids as $id) {
  86. $model->where('id', $id)->destroy();
  87. $this->assertEquals("DELETE FROM `test_user_model` WHERE `id` = {$id} ", $model->lastQuery()->getLastQuery());
  88. }
  89. }
  90. public function testLock()
  91. {
  92. $model = new TestRelationModel();
  93. $model->lockForUpdate()->get(1);
  94. $this->assertEquals('SELECT * FROM `test_user_model` WHERE `id` = 1 LIMIT 1 FOR UPDATE', $model->lastQuery()->getLastQuery());
  95. $model->get(1);
  96. $this->assertEquals('SELECT * FROM `test_user_model` WHERE `id` = 1 LIMIT 1', $model->lastQuery()->getLastQuery());
  97. $model->sharedLock()->get(1);
  98. $this->assertEquals('SELECT * FROM `test_user_model` WHERE `id` = 1 LIMIT 1 LOCK IN SHARE MODE', $model->lastQuery()->getLastQuery());
  99. $model->get(1);
  100. $this->assertEquals('SELECT * FROM `test_user_model` WHERE `id` = 1 LIMIT 1', $model->lastQuery()->getLastQuery());
  101. }
  102. }