ConfigCategoryControllerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Config;
  4. use App\Models\ConfigCategory;
  5. use Tests\AdminTestCase;
  6. use Illuminate\Foundation\Testing\WithFaker;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Tests\Traits\RequestActions;
  9. class ConfigCategoryControllerTest extends AdminTestCase
  10. {
  11. use RefreshDatabase;
  12. use RequestActions;
  13. protected $resourceName = 'config-categories';
  14. protected function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->login();
  18. }
  19. public function testStoreValidation()
  20. {
  21. // slug 和 name 验证规则一样,验证一个即可
  22. // name required
  23. $res = $this->storeResource([
  24. 'name' => '',
  25. ]);
  26. $res->assertStatus(422)
  27. ->assertJsonValidationErrors(['name']);
  28. // name string
  29. $res = $this->storeResource([
  30. 'name' => [],
  31. ]);
  32. $res->assertStatus(422)
  33. ->assertJsonValidationErrors(['name']);
  34. // name max:50
  35. $res = $this->storeResource([
  36. 'name' => str_repeat('a', 51),
  37. ]);
  38. $res->assertStatus(422)
  39. ->assertJsonValidationErrors(['name']);
  40. factory(ConfigCategory::class)->create(['name' => 'name']);
  41. // name unique
  42. $res = $this->storeResource([
  43. 'name' => 'name',
  44. ]);
  45. $res->assertStatus(422)
  46. ->assertJsonValidationErrors(['name']);
  47. }
  48. public function testStore()
  49. {
  50. $res = $this->storeResource([
  51. 'name' => 'name',
  52. 'slug' => 'slug',
  53. ]);
  54. $res->assertStatus(201);
  55. $this->assertDatabaseHas('config_categories', [
  56. 'id' => $this->getLastInsertId('config_categories'),
  57. 'name' => 'name',
  58. 'slug' => 'slug',
  59. ]);
  60. }
  61. public function testUpdate()
  62. {
  63. $id = factory(ConfigCategory::class)->create(['name' => 'name'])->id;
  64. $res = $this->updateResource($id, [
  65. 'name' => 'name',
  66. ]);
  67. $res->assertStatus(201);
  68. $res = $this->updateResource($id, [
  69. 'name' => 'new',
  70. 'slug' => 'new',
  71. ]);
  72. $res->assertStatus(201);
  73. $this->assertDatabaseHas('config_categories', [
  74. 'id' => $id,
  75. 'name' => 'new',
  76. 'slug' => 'new',
  77. ]);
  78. }
  79. public function testDestroy()
  80. {
  81. $categoryId1 = factory(ConfigCategory::class)->create()->id;
  82. $categoryId2 = factory(ConfigCategory::class)->create()->id;
  83. $configId = factory(Config::class)->make(['category_id' => $categoryId2])->id;
  84. // 关联删除
  85. $res = $this->destroyResource($categoryId2);
  86. $res->assertStatus(204);
  87. $this->assertDatabaseMissing('config_categories', [
  88. 'id' => $categoryId2,
  89. ]);
  90. $this->assertDatabaseMissing('configs', [
  91. 'id' => $configId,
  92. ]);
  93. $res = $this->destroyResource($categoryId1);
  94. $res->assertStatus(204);
  95. $this->assertDatabaseMissing('config_categories', [
  96. 'id' => $categoryId1,
  97. ]);
  98. }
  99. public function testIndex()
  100. {
  101. ConfigCategory::insert(factory(ConfigCategory::class, 20)->make()->toArray());
  102. ConfigCategory::first()->update(['name' => 'test query name']);
  103. ConfigCategory::offset(1)->first()->update(['name' => 'test query name 2']);
  104. // 查出所有
  105. $res = $this->getResources(['all' => 1]);
  106. $res->assertStatus(200)
  107. ->assertJsonCount(20);
  108. // name like %?%
  109. $res = $this->getResources([
  110. 'name' => 'query',
  111. ]);
  112. $res->assertStatus(200)
  113. ->assertJsonCount(2, 'data');
  114. }
  115. }