ModelTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * 基本测试 ddl 等
  4. * User: Tioncico
  5. * Date: 2019/10/22 0022
  6. * Time: 15:08
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\Mysqli\QueryBuilder;
  10. use EasySwoole\ORM\Db\Config;
  11. use EasySwoole\ORM\Db\Connection;
  12. use EasySwoole\ORM\DbManager;
  13. use EasySwoole\ORM\Utility\Schema\Table;
  14. use PHPUnit\Framework\TestCase;
  15. use EasySwoole\ORM\Tests\models\TestUserModel;
  16. class ModelTest extends TestCase
  17. {
  18. /**
  19. * @var $connection Connection
  20. */
  21. protected $connection;
  22. protected $tableName = 'test_user_model';
  23. protected function setUp(): void
  24. {
  25. parent::setUp(); // TODO: Change the autogenerated stub
  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. $this->createTestTable();
  32. }
  33. function createTestTable()
  34. {
  35. $query = new QueryBuilder();
  36. $tableDDL = new Table($this->tableName);
  37. $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  38. $tableDDL->colVarChar('name', 255);
  39. $tableDDL->colTinyInt('age', 1);
  40. $tableDDL->colDateTime('addTime');
  41. $tableDDL->colTinyInt('state', 1);
  42. $tableDDL->setIfNotExists();
  43. $sql = $tableDDL->__createDDL();
  44. $query->raw($sql);
  45. $data = $this->connection->defer()->query($query);
  46. $this->assertTrue($data->getResult());
  47. }
  48. function testGetSchemaInfo()
  49. {
  50. $testUserModel = new TestUserModel();
  51. $schemaInfo = $testUserModel->schemaInfo();
  52. $this->assertTrue($schemaInfo instanceof Table);
  53. }
  54. function testAdd()
  55. {
  56. $testUserModel = new TestUserModel();
  57. $testUserModel->state = 1;
  58. $testUserModel->name = '仙士可';
  59. $testUserModel->age = 100;
  60. $testUserModel->addTime = date('Y-m-d H:i:s');
  61. $data = $testUserModel->save();
  62. $this->assertIsInt($data);
  63. }
  64. /**
  65. * @depends testAdd
  66. * testUpdate
  67. * @author Tioncico
  68. * Time: 15:41
  69. */
  70. function testUpdate()
  71. {
  72. $testUserModel = new TestUserModel();
  73. /**
  74. * @var $user TestUserModel
  75. */
  76. $user = $testUserModel->get();
  77. $user->name = '仙士可2号';
  78. $result = $user->update();
  79. $this->assertTrue($result);
  80. $user = $testUserModel->get(['id' => $user->id]);
  81. $this->assertEquals('仙士可2号', $user->name);
  82. $user = $testUserModel->where(function (QueryBuilder $builder)use ($user){
  83. $builder->where('id', $user->id);
  84. })->get();
  85. $this->assertEquals('仙士可2号', $user->name);
  86. }
  87. /**
  88. * @depends testAdd
  89. * testUpdateWithLimit
  90. * @author XueSi
  91. * Time: 18:04
  92. */
  93. function testUpdateWithLimit()
  94. {
  95. $testUserModel = new TestUserModel();
  96. // 无条件进行 limit 更新
  97. /**
  98. * @var $user TestUserModel
  99. */
  100. $user = $testUserModel->get();
  101. $user->name = 'XueSi-1号';
  102. $result = $user->limit(1)->update([], null, true);
  103. $this->assertEquals("UPDATE `test_user_model` SET `name` = 'XueSi-1号' LIMIT 1", $user->lastQuery()->getLastQuery());
  104. $this->assertTrue($result);
  105. // 无条件进行 limit 更新
  106. /**
  107. * @var $user TestUserModel
  108. */
  109. $user = $testUserModel->get();
  110. $user->name = 'XueSi-2号';
  111. $result = $user->limit(2, 3)->update([], null, true);
  112. $this->assertEquals("UPDATE `test_user_model` SET `name` = 'XueSi-2号' LIMIT 2", $user->lastQuery()->getLastQuery());
  113. $this->assertTrue($result);
  114. // 无条件进行 limit 更新
  115. $result = $testUserModel->limit(3)->update(['name' => 'XueSi-1号-1'], null, true);
  116. $this->assertEquals("UPDATE `test_user_model` SET `name` = 'XueSi-1号-1' LIMIT 3", $testUserModel->lastQuery()->getLastQuery());
  117. $this->assertTrue($result);
  118. // 无条件进行 limit 更新
  119. $testUserModel = new TestUserModel();
  120. $result = $testUserModel->limit(4, 5)->update(['name' => 'XueSi-2号-2'], null, true);
  121. $this->assertEquals("UPDATE `test_user_model` SET `name` = 'XueSi-2号-2' LIMIT 4", $testUserModel->lastQuery()->getLastQuery());
  122. $this->assertTrue($result);
  123. // 根据条件进行 limit 更新
  124. $testUserModel = new TestUserModel();
  125. $result = $testUserModel->limit(1)->update(['name' => 'XueSi-3号'], ['id' => 1]);
  126. $this->assertEquals("UPDATE `test_user_model` SET `name` = 'XueSi-3号' WHERE `id` = 1 LIMIT 1", $testUserModel->lastQuery()->getLastQuery());
  127. $this->assertTrue($result);
  128. }
  129. function testGetAll()
  130. {
  131. $testUserModel = new TestUserModel();
  132. $data = $testUserModel->all();
  133. $this->assertIsArray($data);
  134. }
  135. function testDelete()
  136. {
  137. $testUserModel = new TestUserModel();
  138. /**
  139. * @var $user TestUserModel
  140. */
  141. $user = $testUserModel->get();
  142. $result = $user->destroy();
  143. $this->assertEquals(1, $result);
  144. $user = $user->get(['id' => $user->id]);
  145. $this->assertNull($user);
  146. }
  147. }