AdminPermissionControllerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\AdminPermission;
  4. use Tests\AdminTestCase;
  5. use Illuminate\Foundation\Testing\WithFaker;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Tests\Traits\RequestActions;
  8. class AdminPermissionControllerTest extends AdminTestCase
  9. {
  10. use RefreshDatabase;
  11. use WithFaker;
  12. use RequestActions;
  13. protected $resourceName = 'admin-permissions';
  14. protected function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->login();
  18. }
  19. public function testStoreValidation()
  20. {
  21. // name slug required
  22. // http_method array
  23. // http_path valid
  24. $res = $this->storeResource([
  25. 'name' => '',
  26. 'slug' => '',
  27. 'http_method' => 'not array',
  28. 'http_path' => "ERR:err/err",
  29. ]);
  30. $res->assertStatus(422)
  31. ->assertJsonValidationErrors(['name', 'slug', 'http_method', 'http_path']);
  32. factory(AdminPermission::class)->create(['slug' => 'slug']);
  33. factory(AdminPermission::class)->create(['name' => 'name']);
  34. // name slug unique
  35. // http_method.* in
  36. $res = $this->storeResource([
  37. 'name' => 'name',
  38. 'slug' => 'slug',
  39. 'http_method' => ['GET', 'TEST'],
  40. ]);
  41. $res->assertStatus(422)
  42. ->assertJsonValidationErrors(['name', 'slug', 'http_method.1']);
  43. }
  44. public function testStore()
  45. {
  46. /** @var AdminPermission $model */
  47. $model = factory(AdminPermission::class)->make();
  48. $this->assertStore($model);
  49. // http_method 和 http_path 为空
  50. $model = factory(AdminPermission::class)->make([
  51. 'http_method' => null,
  52. 'http_path' => null,
  53. ]);
  54. $this->assertStore($model);
  55. }
  56. protected function assertStore(AdminPermission $model)
  57. {
  58. $inputs = $model->toArray();
  59. $inputs['http_path'] = implode("\n", $inputs['http_path']);
  60. $res = $this->storeResource($inputs);
  61. $res->assertStatus(201);
  62. $this->assertDatabaseHas('admin_permissions', array_merge($model->getAttributes()));
  63. }
  64. public function testIndex()
  65. {
  66. factory(AdminPermission::class, 20)->create();
  67. $res = $this->getResources();
  68. $res->assertStatus(200)
  69. ->assertJsonFragment(['total' => 20])
  70. ->assertJsonFragment(['last_page' => 2]);
  71. // 筛选
  72. $id = factory(AdminPermission::class)->create([
  73. 'http_path' => 'path/to/query',
  74. 'slug' => 'slug query',
  75. 'name' => 'name query',
  76. ])->id;
  77. $res = $this->getResources([
  78. 'id' => $id,
  79. ]);
  80. $res->assertStatus(200)
  81. ->assertJsonCount(1, 'data')
  82. ->assertJsonFragment(['id' => $id]);
  83. $res = $this->getResources([
  84. 'id' => $id,
  85. 'http_path' => 'to',
  86. 'slug' => 'slug',
  87. 'name' => 'name',
  88. ]);
  89. $res->assertStatus(200)
  90. ->assertJsonCount(1, 'data')
  91. ->assertJsonFragment(['id' => $id]);
  92. // 测试不分页 和 只包含特定字段
  93. $res = $this->getResources([
  94. 'all' => 1,
  95. 'only' => ['id', 'name'],
  96. ]);
  97. $res->assertJsonCount(21);
  98. }
  99. public function testEdit()
  100. {
  101. $res = $this->editResource(99999);
  102. $res->assertStatus(404);
  103. $id = factory(AdminPermission::class)->create()->id;
  104. $res = $this->editResource($id);
  105. $res->assertStatus(200)
  106. ->assertJsonFragment(['id' => $id]);
  107. }
  108. public function testUpdate()
  109. {
  110. // id = 1
  111. $id1 = factory(AdminPermission::class)->create()->id;
  112. // id = 2
  113. $id2 = factory(AdminPermission::class)->create([
  114. 'name' => 'name',
  115. 'slug' => 'slug',
  116. ])->id;
  117. $res = $this->updateResource($id1, [
  118. 'name' => 'name',
  119. 'slug' => 'slug',
  120. ]);
  121. $res->assertStatus(422)
  122. ->assertJsonValidationErrors(['name', 'slug']);
  123. $inputs = [
  124. 'slug' => 'new slug',
  125. 'http_path' => null,
  126. 'http_method' => null,
  127. ];
  128. $res = $this->updateResource($id2, $inputs);
  129. $res->assertStatus(201);
  130. $this->assertDatabaseHas('admin_permissions', $inputs + ['id' => $id2, 'name' => 'name']);
  131. }
  132. public function testDestroy()
  133. {
  134. $id = factory(AdminPermission::class)->create()->id;
  135. $res = $this->destroyResource($id);
  136. $res->assertStatus(204);
  137. $this->assertDatabaseMissing('admin_permissions', ['id' => $id]);
  138. }
  139. }