BelongsToManyTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Siam
  5. * Date: 2020/2/27
  6. * Time: 9:55
  7. */
  8. namespace EasySwoole\ORM\Tests;
  9. use EasySwoole\Mysqli\QueryBuilder;
  10. use EasySwoole\ORM\AbstractModel;
  11. use EasySwoole\ORM\DbManager;
  12. use EasySwoole\ORM\Db\Config;
  13. use EasySwoole\ORM\Db\Connection;
  14. use EasySwoole\ORM\Tests\models\Roles;
  15. use EasySwoole\ORM\Tests\models\UserInfo;
  16. use EasySwoole\ORM\Tests\models\UserRole;
  17. use EasySwoole\ORM\Tests\models\UserRoleDifferentField;
  18. use EasySwoole\ORM\Tests\models\Users;
  19. use EasySwoole\ORM\Utility\Schema\Table;
  20. use PHPUnit\Framework\TestCase;
  21. class BelongsToManyTest extends TestCase
  22. {
  23. /**
  24. * @var $connection Connection
  25. */
  26. protected $connection;
  27. protected function setUp(): void
  28. {
  29. parent::setUp(); // TODO: Change the autogenerated stub
  30. $config = new Config(MYSQL_CONFIG);
  31. $this->connection = new Connection($config);
  32. DbManager::getInstance()->addConnection($this->connection);
  33. $connection = DbManager::getInstance()->getConnection();
  34. $this->assertTrue($connection === $this->connection);
  35. $this->createUsersTable();
  36. $this->createMiddleTable();
  37. $this->createRoleTable();
  38. $this->createUserInfoTable();
  39. }
  40. // 创建三个关联表
  41. function createUsersTable()
  42. {
  43. $sql = "SHOW TABLES LIKE 'users';";
  44. $query = new QueryBuilder();
  45. $query->raw($sql);
  46. $data = $this->connection->defer()->query($query);
  47. if (empty($data->getResult())) {
  48. $tableDDL = new Table('users');
  49. $tableDDL->colInt('user_id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  50. $tableDDL->colVarChar('name', 255);
  51. $tableDDL->setIfNotExists();
  52. $sql = $tableDDL->__createDDL();
  53. $query->raw($sql);
  54. $data = $this->connection->defer()->query($query);
  55. $this->assertTrue($data->getResult());
  56. }
  57. }
  58. function createMiddleTable()
  59. {
  60. $sql = "SHOW TABLES LIKE 'user_role';";
  61. $query = new QueryBuilder();
  62. $query->raw($sql);
  63. $data = $this->connection->defer()->query($query);
  64. if (empty($data->getResult())) {
  65. $tableDDL = new Table('user_role');
  66. $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  67. $tableDDL->colInt('user_id', 11);
  68. $tableDDL->colInt('role_id', 11);
  69. $tableDDL->setIfNotExists();
  70. $sql = $tableDDL->__createDDL();
  71. $query->raw($sql);
  72. $data = $this->connection->defer()->query($query);
  73. $this->assertTrue($data->getResult());
  74. }
  75. // 创建一个中间表,主键名不同的
  76. $sql = "SHOW TABLES LIKE 'user_role_different_field';";
  77. $query = new QueryBuilder();
  78. $query->raw($sql);
  79. $data = $this->connection->defer()->query($query);
  80. if (empty($data->getResult())) {
  81. $tableDDL = new Table('user_role_different_field');
  82. $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  83. $tableDDL->colInt('u_id', 11);
  84. $tableDDL->colInt('r_id', 11);
  85. $tableDDL->setIfNotExists();
  86. $sql = $tableDDL->__createDDL();
  87. $query->raw($sql);
  88. $data = $this->connection->defer()->query($query);
  89. $this->assertTrue($data->getResult());
  90. }
  91. }
  92. function createRoleTable()
  93. {
  94. $sql = "SHOW TABLES LIKE 'roles';";
  95. $query = new QueryBuilder();
  96. $query->raw($sql);
  97. $data = $this->connection->defer()->query($query);
  98. if (empty($data->getResult())) {
  99. $tableDDL = new Table('roles');
  100. $tableDDL->colInt('role_id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  101. $tableDDL->colVarChar('role_name', 255);
  102. $tableDDL->setIfNotExists();
  103. $sql = $tableDDL->__createDDL();
  104. $query->raw($sql);
  105. $data = $this->connection->defer()->query($query);
  106. $this->assertTrue($data->getResult());
  107. // 默认角色列表插入
  108. Roles::create([
  109. 'role_name' => '管理员'
  110. ])->save();
  111. Roles::create([
  112. 'role_name' => 'VIP用户'
  113. ])->save();
  114. Roles::create([
  115. 'role_name' => '普通用户'
  116. ])->save();
  117. }
  118. }
  119. public function createUserInfoTable()
  120. {
  121. $sql = "SHOW TABLES LIKE 'user_info';";
  122. $query = new QueryBuilder();
  123. $query->raw($sql);
  124. $data = $this->connection->defer()->query($query);
  125. if (empty($data->getResult())) {
  126. $tableDDL = new Table('user_info');
  127. $tableDDL->colInt('id', 11)->setIsPrimaryKey()->setIsAutoIncrement();
  128. $tableDDL->colInt('user_id', 11);
  129. $tableDDL->colVarChar('user_email', 255);
  130. $tableDDL->setIfNotExists();
  131. $sql = $tableDDL->__createDDL();
  132. $query->raw($sql);
  133. $data = $this->connection->defer()->query($query);
  134. $this->assertTrue($data->getResult());
  135. }
  136. }
  137. function testInsert()
  138. {
  139. ### 插入第1个用户
  140. $user = Users::create([
  141. 'name' => 'SiamBelongsToManySimpleRole'
  142. ])->save();
  143. $this->assertIsInt($user);
  144. $userRole = UserRole::create([
  145. 'user_id' => $user,
  146. 'role_id' => 1
  147. ])->save();
  148. $this->assertIsInt($userRole);
  149. UserRoleDifferentField::create([
  150. 'u_id' => $user,
  151. 'r_id' => 1
  152. ])->save();
  153. // 添加user_info信息
  154. UserInfo::create([
  155. 'user_id' => $user,
  156. 'user_email' => '1@qq.com'
  157. ])->save();
  158. ### 插入第2个用户
  159. $userManyRole = Users::create([
  160. 'name' => 'SiamBelongsToManyManyRole'
  161. ])->save();
  162. $this->assertIsInt($userManyRole);
  163. UserRole::create([
  164. 'user_id' => $userManyRole,
  165. 'role_id' => 1
  166. ])->save();
  167. UserRoleDifferentField::create([
  168. 'u_id' => $userManyRole,
  169. 'r_id' => 1
  170. ])->save();
  171. UserRole::create([
  172. 'user_id' => $userManyRole,
  173. 'role_id' => 3
  174. ])->save();
  175. UserRoleDifferentField::create([
  176. 'u_id' => $userManyRole,
  177. 'r_id' => 3
  178. ])->save();
  179. // 添加user_info信息
  180. UserInfo::create([
  181. 'user_id' => $userManyRole,
  182. 'user_email' => '3@qq.com'
  183. ])->save();
  184. }
  185. // 关联查询
  186. function testGet()
  187. {
  188. $user = Users::create()->where('name', 'SiamBelongsToManySimpleRole')->get();
  189. // var_dump($user);
  190. $this->assertInstanceOf(Users::class, $user);
  191. $this->assertEquals(count($user->roles()), 1);
  192. $this->assertInstanceOf(Roles::class, $user->roles()[0]);
  193. $this->assertEquals('管理员', $user->roles()[0]->role_name);
  194. $userMany = Users::create()->where('name', 'SiamBelongsToManyManyRole')->get();
  195. $this->assertEquals(count($userMany->roles()), 2);
  196. $this->assertInstanceOf(Roles::class, $userMany->roles()[0]);
  197. $this->assertInstanceOf(Roles::class, $userMany->roles()[1]);
  198. $this->assertEquals('管理员', $userMany->roles()[0]->role_name);
  199. $this->assertEquals('普通用户', $userMany->roles()[1]->role_name);
  200. // 自定义键名
  201. $user = Users::create()->where('name', 'SiamBelongsToManySimpleRole')->get();
  202. $this->assertInstanceOf(Users::class, $user);
  203. $this->assertEquals(count($user->roles_different_field()), 1);
  204. $this->assertInstanceOf(Roles::class, $user->roles_different_field()[0]);
  205. $this->assertEquals('管理员', $user->roles_different_field()[0]->role_name);
  206. $userMany = Users::create()->where('name', 'SiamBelongsToManyManyRole')->get();
  207. $this->assertEquals(count($userMany->roles_different_field()), 2);
  208. $this->assertInstanceOf(Roles::class, $userMany->roles_different_field()[0]);
  209. $this->assertInstanceOf(Roles::class, $userMany->roles_different_field()[1]);
  210. $this->assertEquals('管理员', $userMany->roles_different_field()[0]->role_name);
  211. $this->assertEquals('普通用户', $userMany->roles_different_field()[1]->role_name);
  212. }
  213. function testToArray()
  214. {
  215. $user = Users::create()->where('name', 'SiamBelongsToManySimpleRole')->get();
  216. $user->roles();
  217. $this->assertInstanceOf(Users::class, $user);
  218. $this->assertEquals($user->toArray(false, false)['roles'][0]['role_name'], '管理员');
  219. $user = Users::create()->where('name', 'SiamBelongsToManyManyRole')->get();
  220. $user->roles();
  221. $this->assertInstanceOf(Users::class, $user);
  222. $this->assertEquals($user->toArray(false, false)['roles'][0]['role_name'], '管理员');
  223. $this->assertEquals($user->toArray(false, false)['roles'][1]['role_name'], '普通用户');
  224. // 自定义键名
  225. $user = Users::create()->where('name', 'SiamBelongsToManySimpleRole')->get();
  226. $user->roles_different_field();
  227. $this->assertInstanceOf(Users::class, $user);
  228. $this->assertEquals($user->toArray(false, false)['roles_different_field'][0]['role_name'], '管理员');
  229. $user = Users::create()->where('name', 'SiamBelongsToManyManyRole')->get();
  230. $user->roles_different_field();
  231. $this->assertInstanceOf(Users::class, $user);
  232. $this->assertEquals($user->toArray(false, false)['roles_different_field'][0]['role_name'], '管理员');
  233. $this->assertEquals($user->toArray(false, false)['roles_different_field'][1]['role_name'], '普通用户');
  234. }
  235. function testWithGet()
  236. {
  237. $user = Users::create()->where('name', 'SiamBelongsToManySimpleRole')->with(['roles'])->get();
  238. $this->assertInstanceOf(Users::class, $user);
  239. $this->assertEquals($user->toArray(false, false)['roles'][0]['role_name'], '管理员');
  240. $user = Users::create()->where('name', 'SiamBelongsToManyManyRole')->with(['roles'])->get();
  241. $this->assertInstanceOf(Users::class, $user);
  242. $this->assertEquals($user->toArray(false, false)['roles'][0]['role_name'], '管理员');
  243. $this->assertEquals($user->toArray(false, false)['roles'][1]['role_name'], '普通用户');
  244. // 自定义键名
  245. $user = Users::create()->where('name', 'SiamBelongsToManySimpleRole')->with(['roles_different_field'])->get();
  246. $this->assertInstanceOf(Users::class, $user);
  247. $this->assertEquals($user->toArray(false, false)['roles_different_field'][0]['role_name'], '管理员');
  248. $user = Users::create()->where('name', 'SiamBelongsToManyManyRole')->with(['roles_different_field'])->get();
  249. $this->assertInstanceOf(Users::class, $user);
  250. $this->assertEquals($user->toArray(false, false)['roles_different_field'][0]['role_name'], '管理员');
  251. $this->assertEquals($user->toArray(false, false)['roles_different_field'][1]['role_name'], '普通用户');
  252. }
  253. function testWithAll()
  254. {
  255. $user = Users::create()->with(['roles'])->all();
  256. $this->assertInstanceOf(Users::class, $user[0]);
  257. $this->assertInstanceOf(Users::class, $user[1]);
  258. $this->assertEquals($user[0]->toArray(false, false)['roles'][0]['role_name'], '管理员');
  259. $this->assertEquals($user[1]->toArray(false, false)['roles'][0]['role_name'], '管理员');
  260. $this->assertEquals($user[1]->toArray(false, false)['roles'][1]['role_name'], '普通用户');
  261. $user = Users::create()->with(['roles_different_field'])->all();
  262. $this->assertInstanceOf(Users::class, $user[0]);
  263. $this->assertInstanceOf(Users::class, $user[1]);
  264. $this->assertEquals($user[0]->toArray(false, false)['roles_different_field'][0]['role_name'], '管理员');
  265. $this->assertEquals($user[1]->toArray(false, false)['roles_different_field'][0]['role_name'], '管理员');
  266. $this->assertEquals($user[1]->toArray(false, false)['roles_different_field'][1]['role_name'], '普通用户');
  267. }
  268. function testCallCondition()
  269. {
  270. $user = Users::create()->with(['roles_different_field_call'])->all();
  271. $this->assertInstanceOf(Users::class, $user[0]);
  272. $this->assertInstanceOf(Users::class, $user[1]);
  273. // 在模型中设置了field 没有role_name
  274. $this->assertTrue(!isset($user[0]->toArray(false, false)['roles_different_field_call'][0]['role_name']));
  275. $this->assertTrue(!isset($user[1]->toArray(false, false)['roles_different_field_call'][0]['role_name']));
  276. $this->assertTrue(!isset($user[1]->toArray(false, false)['roles_different_field_call'][1]['role_name']));
  277. }
  278. // 关联查询同时使用 join
  279. public function testWithJoinAll()
  280. {
  281. $user = Users::create()
  282. ->alias('users')
  283. ->where('users.name', 'SiamBelongsToManySimpleRole')
  284. ->with(['roles'])
  285. ->join('user_info user_info', 'users.user_id = user_info.user_id', 'INNER')
  286. ->get();
  287. $this->assertInstanceOf(Users::class, $user);
  288. $this->assertEquals($user->toArray(false, false)['roles'][0]['role_name'], '管理员');
  289. $this->assertEquals($user->toArray(false, false)['user_email'], '1@qq.com');
  290. // 自定义键名
  291. $user = Users::create()
  292. ->alias('users')
  293. ->where('users.name', 'SiamBelongsToManySimpleRole')
  294. ->with(['roles_different_field'])
  295. ->join('user_info user_info', 'users.user_id = user_info.user_id', 'INNER')
  296. ->get();
  297. $this->assertInstanceOf(Users::class, $user);
  298. $this->assertEquals($user->toArray(false, false)['roles_different_field'][0]['role_name'], '管理员');
  299. $this->assertEquals($user->toArray(false, false)['user_email'], '1@qq.com');
  300. /** @var AbstractModel[] $user */
  301. $user = Users::create()
  302. ->alias('users')
  303. ->field('user_info.user_email')
  304. ->with(['roles_join'])
  305. ->join('user_info as user_info', 'users.user_id = user_info.user_id', 'INNER')
  306. ->all();
  307. $this->assertIsArray($user);
  308. $this->assertTrue(count($user) > 0);
  309. foreach ($user as $k => $v) {
  310. $this->assertStringContainsString('@qq.com', $v->toArray(false, false)['user_email']);
  311. }
  312. }
  313. function testDelete()
  314. {
  315. Users::create()->destroy(null, true);
  316. UserRole::create()->destroy(null, true);
  317. UserRoleDifferentField::create()->destroy(null, true);
  318. $this->assertIsInt(1);
  319. }
  320. }