ProductControllerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Product;
  4. use App\Models\Role;
  5. use App\Models\User;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Tests\TestCase;
  8. class ProductControllerTest extends TestCase
  9. {
  10. use RefreshDatabase;
  11. protected bool $seed = true;
  12. protected User $adminUser;
  13. protected User $managerUser;
  14. protected function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  18. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  19. }
  20. private function validProductData(): array
  21. {
  22. return [
  23. 'article' => 'TEST-001',
  24. 'name_tz' => 'Тестовая горка',
  25. 'type_tz' => 'Горка',
  26. 'nomenclature_number' => '123456',
  27. 'sizes' => '1000x2000x1500',
  28. 'manufacturer' => 'ООО Тест',
  29. 'manufacturer_name' => 'ООО Тест Производитель',
  30. 'unit' => 'шт',
  31. 'type' => 'standard',
  32. 'product_price' => 100000,
  33. 'installation_price' => 20000,
  34. 'total_price' => 120000,
  35. 'note' => 'тестовое примечание',
  36. 'year' => date('Y'),
  37. ];
  38. }
  39. // --- Guest redirects (3) ---
  40. public function test_guest_cannot_access_catalog_index(): void
  41. {
  42. $response = $this->get(route('catalog.index'));
  43. $response->assertRedirect(route('login'));
  44. }
  45. public function test_guest_cannot_access_catalog_create(): void
  46. {
  47. $response = $this->get(route('catalog.create'));
  48. $response->assertRedirect(route('login'));
  49. }
  50. public function test_guest_cannot_store_product(): void
  51. {
  52. $response = $this->post(route('catalog.store'), $this->validProductData());
  53. $response->assertRedirect(route('login'));
  54. }
  55. // --- Authorization (2) ---
  56. public function test_manager_cannot_create_product(): void
  57. {
  58. $response = $this->actingAs($this->managerUser)
  59. ->post(route('catalog.store'), $this->validProductData());
  60. $response->assertForbidden();
  61. }
  62. public function test_manager_cannot_delete_product(): void
  63. {
  64. $product = Product::factory()->create();
  65. $response = $this->actingAs($this->managerUser)
  66. ->delete(route('catalog.delete', $product));
  67. $response->assertForbidden();
  68. }
  69. // --- Index (2) ---
  70. public function test_admin_can_access_catalog_index(): void
  71. {
  72. $response = $this->actingAs($this->adminUser)
  73. ->get(route('catalog.index'));
  74. $response->assertOk();
  75. $response->assertViewIs('catalog.index');
  76. }
  77. public function test_manager_can_access_catalog_index(): void
  78. {
  79. $response = $this->actingAs($this->managerUser)
  80. ->get(route('catalog.index'));
  81. $response->assertOk();
  82. $response->assertViewIs('catalog.index');
  83. }
  84. // --- Create form (1) ---
  85. public function test_admin_can_access_catalog_create(): void
  86. {
  87. $response = $this->actingAs($this->adminUser)
  88. ->get(route('catalog.create'));
  89. $response->assertOk();
  90. $response->assertViewIs('catalog.edit');
  91. }
  92. // --- Show (2) ---
  93. public function test_admin_can_view_product(): void
  94. {
  95. $product = Product::factory()->create();
  96. $response = $this->actingAs($this->adminUser)
  97. ->get(route('catalog.show', $product));
  98. $response->assertOk();
  99. $response->assertViewIs('catalog.edit');
  100. }
  101. public function test_manager_can_view_product(): void
  102. {
  103. $product = Product::factory()->create();
  104. $response = $this->actingAs($this->managerUser)
  105. ->get(route('catalog.show', $product));
  106. $response->assertOk();
  107. $response->assertViewIs('catalog.edit');
  108. }
  109. // --- Store (3) ---
  110. public function test_admin_can_create_product(): void
  111. {
  112. $data = $this->validProductData();
  113. $response = $this->actingAs($this->adminUser)
  114. ->post(route('catalog.store'), $data);
  115. $response->assertRedirect();
  116. $this->assertDatabaseHas('products', ['article' => $data['article']]);
  117. }
  118. public function test_store_product_requires_article(): void
  119. {
  120. $data = $this->validProductData();
  121. unset($data['article']);
  122. $response = $this->actingAs($this->adminUser)
  123. ->post(route('catalog.store'), $data);
  124. $response->assertSessionHasErrors('article');
  125. }
  126. public function test_store_product_requires_name_tz(): void
  127. {
  128. $data = $this->validProductData();
  129. unset($data['name_tz']);
  130. $response = $this->actingAs($this->adminUser)
  131. ->post(route('catalog.store'), $data);
  132. $response->assertSessionHasErrors('name_tz');
  133. }
  134. // --- Update (2) ---
  135. public function test_admin_can_update_product(): void
  136. {
  137. $product = Product::factory()->create();
  138. $data = $this->validProductData();
  139. $data['article'] = 'UPDATED-001';
  140. $data['name_tz'] = 'Обновлённая горка';
  141. $response = $this->actingAs($this->adminUser)
  142. ->post(route('catalog.update', $product), $data);
  143. $response->assertRedirect();
  144. $this->assertDatabaseHas('products', [
  145. 'id' => $product->id,
  146. 'article' => 'UPDATED-001',
  147. 'name_tz' => 'Обновлённая горка',
  148. ]);
  149. }
  150. public function test_manager_cannot_update_product(): void
  151. {
  152. $product = Product::factory()->create();
  153. $response = $this->actingAs($this->managerUser)
  154. ->post(route('catalog.update', $product), $this->validProductData());
  155. $response->assertForbidden();
  156. }
  157. // --- Delete (1) ---
  158. public function test_admin_can_delete_product(): void
  159. {
  160. $product = Product::factory()->create();
  161. $response = $this->actingAs($this->adminUser)
  162. ->delete(route('catalog.delete', $product));
  163. $response->assertRedirect();
  164. $this->assertSoftDeleted('products', ['id' => $product->id]);
  165. }
  166. }