WithTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace EasySwoole\ORM\Tests;
  3. use EasySwoole\Mysqli\QueryBuilder;
  4. use EasySwoole\ORM\Db\Config;
  5. use EasySwoole\ORM\Db\Connection;
  6. use EasySwoole\ORM\DbManager;
  7. use EasySwoole\ORM\Tests\models\TestA;
  8. use EasySwoole\ORM\Tests\models\TestB;
  9. use EasySwoole\ORM\Tests\models\TestC;
  10. use EasySwoole\ORM\Tests\models\TestRelationModel;
  11. use EasySwoole\ORM\Tests\models\TestUserListModel;
  12. use EasySwoole\ORM\Utility\Schema\Table;
  13. use PHPUnit\Framework\TestCase;
  14. class WithTest extends TestCase
  15. {
  16. /**
  17. * @var $connection Connection
  18. */
  19. protected $connection;
  20. protected function setUp(): void
  21. {
  22. parent::setUp(); // TODO: Change the autogenerated stub
  23. $config = new Config(MYSQL_CONFIG);
  24. $config->setReturnCollection(true);
  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. public function testAdd()
  31. {
  32. $test_user_model = TestRelationModel::create();
  33. $test_user_model->name = 'gaobinzhan';
  34. $test_user_model->age = 20;
  35. $test_user_model->addTime = ($time = date('Y-m-d H:i:s'));
  36. $test_user_model->state = 2;
  37. $test_user_model->save();
  38. $user_list = TestUserListModel::create();
  39. $user_list->name = 'gaobinzhan';
  40. $user_list->age = 20;
  41. $user_list->addTime = $time;
  42. $user_list->state = 1;
  43. $user_list->save();
  44. $test_user_model = TestRelationModel::create();
  45. $test_user_model->name = 'gaobinzhan1';
  46. $test_user_model->age = 20;
  47. $test_user_model->addTime = ($time = date('Y-m-d H:i:s'));
  48. $test_user_model->state = 2;
  49. $test_user_model->save();
  50. $user_list = TestUserListModel::create();
  51. $user_list->name = 'gaobinzhan1';
  52. $user_list->age = 20;
  53. $user_list->addTime = $time;
  54. $user_list->state = 1;
  55. $user_list->save();
  56. }
  57. public function testHasOne()
  58. {
  59. $model = TestRelationModel::create();
  60. $result = $model->with(['hasOneEqName' => 'gaobinzhan1'])->where(['name' => 'gaobinzhan'])->get()->toArray(null, false);
  61. $this->assertEmpty($result['hasOneEqName']);
  62. $result = $model->with(['hasOneEqName' => 'gaobinzhan'])->where(['name' => 'gaobinzhan'])->get()->toArray(null, false);
  63. $this->assertEquals($result['hasOneEqName']['name'], 'gaobinzhan');
  64. $result = $model->where(['name' => 'gaobinzhan'])->get()->toArray(null, false);
  65. $this->assertFalse(isset($result['hasOneEqName']));
  66. }
  67. public function testHasMany()
  68. {
  69. $user_list = TestUserListModel::create();
  70. $user_list->name = 'gaobinzhan';
  71. $user_list->age = 20;
  72. $user_list->addTime = date('Y-m-d H:i:s');
  73. $user_list->state = 1;
  74. $user_list->save();
  75. $result = TestRelationModel::create()->with(['hasManyEqName' => ['gaobinzhan1']])->where(['name' => 'gaobinzhan'])->all()->toArray(null, false);
  76. $this->assertFalse(isset($result[0]['hasManyEqName']));
  77. $result = TestRelationModel::create()->with(['hasManyEqName' => ['gaobinzhan']])->where(['name' => 'gaobinzhan'])->all()->toArray(null, false);
  78. $this->assertEquals(count($result[0]['hasManyEqName']), 2);
  79. $this->assertEquals($result[0]['hasManyEqName'][0]['name'], 'gaobinzhan');
  80. }
  81. public function testCreateTable()
  82. {
  83. // 主表a b副表 c为b副表
  84. $sql = "SHOW TABLES LIKE 'test_a';";
  85. $query = new QueryBuilder();
  86. $query->raw($sql);
  87. $data = $this->connection->defer()->query($query);
  88. if (empty($data->getResult())) {
  89. $tableDDL = new Table('test_a');
  90. $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  91. $tableDDL->colVarChar('a_name', 255);
  92. $tableDDL->setIfNotExists();
  93. $sql = $tableDDL->__createDDL();
  94. $query->raw($sql);
  95. $data = $this->connection->defer()->query($query);
  96. $this->assertTrue($data->getResult());
  97. }
  98. $sql = "SHOW TABLES LIKE 'test_b';";
  99. $query = new QueryBuilder();
  100. $query->raw($sql);
  101. $data = $this->connection->defer()->query($query);
  102. if (empty($data->getResult())) {
  103. $tableDDL = new Table('test_b');
  104. $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  105. $tableDDL->colInt('a_id', 11);
  106. $tableDDL->colVarChar('b_name', 255);
  107. $tableDDL->setIfNotExists();
  108. $sql = $tableDDL->__createDDL();
  109. $query->raw($sql);
  110. $data = $this->connection->defer()->query($query);
  111. $this->assertTrue($data->getResult());
  112. }
  113. $sql = "SHOW TABLES LIKE 'test_c';";
  114. $query = new QueryBuilder();
  115. $query->raw($sql);
  116. $data = $this->connection->defer()->query($query);
  117. if (empty($data->getResult())) {
  118. $tableDDL = new Table('test_c');
  119. $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  120. $tableDDL->colInt('b_id', 11);
  121. $tableDDL->colVarChar('c_name', 255);
  122. $tableDDL->setIfNotExists();
  123. $sql = $tableDDL->__createDDL();
  124. $query->raw($sql);
  125. $data = $this->connection->defer()->query($query);
  126. $this->assertTrue($data->getResult());
  127. }
  128. }
  129. public function testWithToArray()
  130. {
  131. $aId = TestA::create()->data(['a_name' => 'testA'])->save();
  132. $bId1 = TestB::create()->data(['a_id' => $aId, 'b_name' => 'testB1'])->save();
  133. $bId2 = TestB::create()->data(['a_id' => $aId, 'b_name' => 'testB2'])->save();
  134. $cId1 = TestC::create()->data(['b_id' => $bId1, 'c_name' => 'testC1'])->save();
  135. $cId2 = TestC::create()->data(['b_id' => $bId1, 'c_name' => 'testC2'])->save();
  136. $cId3 = TestC::create()->data(['b_id' => $bId2, 'c_name' => 'testC1'])->save();
  137. $cId4 = TestC::create()->data(['b_id' => $bId2, 'c_name' => 'testC2'])->save();
  138. $testAM = TestA::create();
  139. // hasOne
  140. // get
  141. $result = $testAM->with('hasOneList')->get($aId)->toArray(null, false);
  142. $this->assertEquals('testB1-bar-b', $result['hasOneList']['b_name']);
  143. $this->assertEquals('testC1', $result['hasOneList']['c_name']);
  144. $result = $testAM->with('hasOneList')->get($aId)->toArray(null, null);
  145. $this->assertTrue(!isset($result['hasOneList']));
  146. $result = $testAM->with(['hasOneJoinList'], false)->field('*,test_a.id as aid,test_b.id as bid')->join('test_b', 'test_b.a_id = test_a.id', 'left')->get()->toArray(false, false);
  147. $this->assertEquals($aId, $result['aid']);
  148. $this->assertEquals($bId1, $result['bid']);
  149. $this->assertEquals($cId1, $result['hasOneJoinList']['cid']);
  150. // all
  151. $result = $testAM->with('hasOneList')->all()->toArray(null, false);
  152. $this->assertEquals('testB2-bar-b', $result[0]['hasOneList']['b_name']);
  153. $this->assertEquals('testC2', $result[0]['hasOneList']['c_name']);
  154. $result = $testAM->with('hasOneList')->all()->toArray(null, null);
  155. $this->assertTrue(!isset($result[0]['hasOneList']));
  156. $result = $testAM->with(['hasOneJoinList'], false)->field('*,test_a.id as aid,test_b.id as bid')->join('test_b', 'test_b.a_id = test_a.id', 'left')->all()->toArray(false, false);
  157. $this->assertEquals($aId, $result[0]['aid']);
  158. $this->assertEquals($bId1, $result[0]['bid']);
  159. $this->assertEquals($cId2, $result[0]['hasOneJoinList']['cid']);
  160. // hasMany
  161. // get
  162. $result = $testAM->with('hasManyList')->get($aId)->toArray(null, false);
  163. $this->assertEquals('testB1-bar-b', $result['hasManyList'][0]['b_name']);
  164. $this->assertEquals('testC1', $result['hasManyList'][0]['c_name']);
  165. $result = $testAM->with('hasManyList')->get($aId)->toArray(null, null);
  166. $this->assertTrue(!isset($result['hasManyList']));
  167. $result = $testAM->with(['hasManyJoinList'], false)->field('*,test_a.id as aid,test_b.id as bid')->join('test_b', 'test_b.a_id = test_a.id', 'left')->get()->toArray(false, false);
  168. $this->assertEquals($aId, $result['aid']);
  169. $this->assertEquals($bId1, $result['bid']);
  170. $this->assertEquals($cId1, $result['hasManyJoinList'][0]['cid']);
  171. // all
  172. $result = $testAM->with('hasManyList')->all()->toArray(null, false);
  173. $this->assertEquals('testB1-bar-b', $result[0]['hasManyList'][0]['b_name']);
  174. $this->assertEquals('testC1', $result[0]['hasManyList'][0]['c_name']);
  175. $result = $testAM->with('hasManyList')->all()->toArray(null, null);
  176. $this->assertTrue(!isset($result[0]['hasManyList']));
  177. $result = $testAM->with(['hasManyJoinList'], false)->field('*,test_a.id as aid,test_b.id as bid')->join('test_b', 'test_b.a_id = test_a.id', 'left')->all()->toArray(false, false);
  178. $this->assertEquals($aId, $result[0]['aid']);
  179. $this->assertEquals($bId1, $result[0]['bid']);
  180. $this->assertEquals($cId1, $result[0]['hasManyJoinList'][0]['cid']);
  181. }
  182. public function testDeleteAll()
  183. {
  184. $res = TestRelationModel::create()->destroy(null, true);
  185. $this->assertIsInt($res);
  186. $res = TestUserListModel::create()->destroy(null, true);
  187. $this->assertIsInt($res);
  188. $res = TestA::create()->destroy(null, true);
  189. $this->assertIsInt($res);
  190. $res = TestB::create()->destroy(null, true);
  191. $this->assertIsInt($res);
  192. $res = TestC::create()->destroy(null, true);
  193. $this->assertIsInt($res);
  194. }
  195. }