VueRouterControllerTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\AdminPermission;
  4. use App\Models\AdminRole;
  5. use App\Models\VueRouter;
  6. use Illuminate\Foundation\Testing\WithFaker;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Tests\AdminTestCase;
  9. use Tests\Traits\RequestActions;
  10. class VueRouterControllerTest extends AdminTestCase
  11. {
  12. use RefreshDatabase;
  13. use WithFaker;
  14. use RequestActions;
  15. protected $resourceName = 'vue-routers';
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->login();
  20. }
  21. public function testStoreValidation()
  22. {
  23. // title required
  24. // order integer
  25. // cache menu boolean
  26. // permission exists
  27. // roles.* exists
  28. $res = $this->storeResource([
  29. 'title' => '',
  30. 'order' => 15.1,
  31. 'cache' => 'not bool',
  32. 'menu' => 'not bool',
  33. 'permission' => 'not exists',
  34. 'roles' => [999],
  35. ]);
  36. $res->assertJsonValidationErrors(['title', 'order', 'cache', 'menu', 'permission', 'roles.0']);
  37. // permission nullable
  38. $res = $this->storeResource([
  39. 'permission' => '',
  40. ]);
  41. $res->assertJsonMissingValidationErrors(['permission']);
  42. // max
  43. $res = $this->storeResource([
  44. 'title' => str_repeat('a', 51),
  45. 'icon' => str_repeat('a', 51),
  46. 'path' => str_repeat('a', 51),
  47. 'order' => 10000,
  48. ]);
  49. $res->assertJsonValidationErrors(['icon', 'path', 'title', 'order']);
  50. // order min
  51. $res = $this->storeResource([
  52. 'order' => -10000,
  53. ]);
  54. $res->assertJsonValidationErrors(['order']);
  55. factory(VueRouter::class)->create();
  56. // parent_id exists
  57. $res = $this->storeResource([
  58. 'parent_id' => 999,
  59. ]);
  60. $res->assertJsonValidationErrors('parent_id');
  61. }
  62. public function testStore()
  63. {
  64. $routerId1 = factory(VueRouter::class)->create()->id;
  65. $permissionId = factory(AdminPermission::class)->create(['slug' => 'slug'])->id;
  66. $roleId = factory(AdminRole::class)->create()->id;
  67. $inputs = factory(VueRouter::class)->make([
  68. 'parent_id' => $routerId1,
  69. 'path' => 'no/start/slash',
  70. 'permission' => 'slug',
  71. ])->toArray();
  72. $res = $this->storeResource($inputs + ['roles' => [$roleId]]);
  73. $res->assertStatus(201);
  74. $routerId2 = $this->getLastInsertId('vue_routers');
  75. $this->assertDatabaseHas(
  76. 'vue_routers',
  77. array_merge($inputs, [
  78. 'id' => $routerId2,
  79. 'parent_id' => $routerId1,
  80. 'permission' => 'slug',
  81. ])
  82. );
  83. $this->assertDatabaseHas('vue_router_role', [
  84. 'vue_router_id' => $routerId2,
  85. 'role_id' => $roleId,
  86. ]);
  87. // 不传 parent_id 默认为 0
  88. $inputs['parent_id'] = null;
  89. $res = $this->storeResource($inputs);
  90. $res->assertStatus(201);
  91. $this->assertDatabaseHas('vue_routers', [
  92. 'id' => $this->getLastInsertId('vue_routers'),
  93. 'parent_id' => 0,
  94. ]);
  95. }
  96. public function testUpdate()
  97. {
  98. $routerIds = factory(VueRouter::class, 2)->create()->pluck('id');
  99. $roleIds = factory(AdminRole::class, 2)->create()->pluck('id');
  100. VueRouter::find($routerIds[1])->roles()->attach($roleIds[0]);
  101. $inputs = [
  102. 'parent_id' => $routerIds[1],
  103. 'title' => 'new title',
  104. 'icon' => 'new icon',
  105. 'path' => 'new/path',
  106. 'order' => 99,
  107. ];
  108. $res = $this->updateResource($routerIds[0], $inputs + ['roles' => [$roleIds[1]]]);
  109. $res->assertStatus(201);
  110. $this->assertDatabaseHas('vue_routers', ['id' => $routerIds[0]] + $inputs);
  111. $this->assertDatabaseHas('vue_router_role', [
  112. 'vue_router_id' => $routerIds[0],
  113. 'role_id' => $roleIds[1],
  114. ]);
  115. $this->assertDatabaseMissing('vue_router_role', [
  116. 'vue_router_id' => $routerIds[0],
  117. 'role_id' => $roleIds[0],
  118. ]);
  119. $res = $this->updateResource($routerIds[0], ['roles' => []]);
  120. $res->assertStatus(201);
  121. $this->assertDatabaseMissing('vue_router_role', [
  122. 'vue_router_id' => $routerIds[0],
  123. 'role_id' => $roleIds[1],
  124. ]);
  125. }
  126. public function testEdit()
  127. {
  128. $router = factory(VueRouter::class)->create()->toArray();
  129. $res = $this->editResource($router['id']);
  130. $res->assertStatus(200)
  131. ->assertJsonFragment($router);
  132. }
  133. public function testIndex()
  134. {
  135. app(\VueRoutersTableSeeder::class)->run();
  136. // 手动查出 3 级嵌套菜单
  137. $vueRouter = VueRouter::with([
  138. 'children',
  139. 'children.children',
  140. 'children.children.children',
  141. ])->find(2);
  142. $vueRouter = $vueRouter->toArray();
  143. $res = $this->getResources();
  144. $res->assertStatus(200)
  145. ->assertJsonFragment($vueRouter);
  146. // 第二级路由 id
  147. $except = $vueRouter['children'][0]['id'];
  148. $fragment = $vueRouter;
  149. // 排除了第二级的 id 后,该路由和所有子路由都不会返回了
  150. unset($fragment['children']);
  151. $res = $this->getResources(['except' => $except]);
  152. $res->assertStatus(200)
  153. ->assertJsonFragment($fragment);
  154. }
  155. public function testDestroy()
  156. {
  157. $this->destroyResource(999)->assertStatus(404);
  158. app(\VueRoutersTableSeeder::class)->run();
  159. $vueRouter = VueRouter::with(['children', 'children.children'])->find(2);
  160. $this->destroyResource($vueRouter->id)->assertStatus(204);
  161. // 子菜单全部删除
  162. $this->assertDatabaseMissing('vue_routers', ['id' => $vueRouter->id]);
  163. // hack 一下,无妨,,,
  164. $this->assertDatabaseMissing('vue_routers', ['id' => $vueRouter['children'][0]['id']]);
  165. $this->assertDatabaseMissing('vue_routers', ['id' => $vueRouter['children'][0]['children'][0]['id']]);
  166. }
  167. public function testBatchUpdateOrder()
  168. {
  169. $ids = factory(VueRouter::class, 3)->create()->pluck('id');
  170. $res = $this->put($this->route('vue-routers.batch.update'), [
  171. '_order' => [
  172. [
  173. 'id' => $ids[2],
  174. 'children' => [['id' => $ids[0]]],
  175. ],
  176. [
  177. 'id' => $ids[1],
  178. ],
  179. ],
  180. ]);
  181. $res->assertStatus(201);
  182. $this->assertDatabaseHas('vue_routers', [
  183. 'id' => $ids[0],
  184. 'parent_id' => $ids[2],
  185. 'order' => 2,
  186. ]);
  187. }
  188. }