MafOrderControllerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\MafOrder;
  4. use App\Models\Product;
  5. use App\Models\Role;
  6. use App\Models\User;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Tests\TestCase;
  9. class MafOrderControllerTest extends TestCase
  10. {
  11. use RefreshDatabase;
  12. protected $seed = true;
  13. private User $adminUser;
  14. private User $managerUser;
  15. protected function setUp(): void
  16. {
  17. parent::setUp();
  18. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  19. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  20. }
  21. // ==================== Authentication ====================
  22. public function test_guest_cannot_access_maf_orders_index(): void
  23. {
  24. $response = $this->get(route('maf_order.index'));
  25. $response->assertRedirect(route('login'));
  26. }
  27. public function test_authenticated_admin_can_access_maf_orders_index(): void
  28. {
  29. $response = $this->actingAs($this->adminUser)
  30. ->get(route('maf_order.index'));
  31. $response->assertStatus(200);
  32. $response->assertViewIs('maf_orders.index');
  33. }
  34. public function test_manager_cannot_access_maf_orders_index(): void
  35. {
  36. $response = $this->actingAs($this->managerUser)
  37. ->get(route('maf_order.index'));
  38. $response->assertStatus(403);
  39. }
  40. // ==================== Index ====================
  41. public function test_maf_orders_index_displays_orders(): void
  42. {
  43. MafOrder::factory()->create();
  44. $response = $this->actingAs($this->adminUser)
  45. ->get(route('maf_order.index'));
  46. $response->assertStatus(200);
  47. }
  48. // ==================== Store (create) ====================
  49. public function test_can_create_maf_order(): void
  50. {
  51. $product = Product::factory()->create();
  52. $response = $this->actingAs($this->adminUser)
  53. ->post(route('maf_order.store'), [
  54. 'product_id' => $product->id,
  55. 'quantity' => 5,
  56. 'order_number' => 'MO-TEST-001',
  57. ]);
  58. $response->assertRedirect();
  59. $this->assertDatabaseHas('maf_orders', [
  60. 'product_id' => $product->id,
  61. 'quantity' => 5,
  62. 'order_number' => 'MO-TEST-001',
  63. 'in_stock' => 0,
  64. ]);
  65. }
  66. public function test_store_maf_order_requires_product_id(): void
  67. {
  68. $response = $this->actingAs($this->adminUser)
  69. ->post(route('maf_order.store'), [
  70. 'quantity' => 5,
  71. ]);
  72. $response->assertSessionHasErrors('product_id');
  73. }
  74. public function test_store_maf_order_requires_quantity(): void
  75. {
  76. $product = Product::factory()->create();
  77. $response = $this->actingAs($this->adminUser)
  78. ->post(route('maf_order.store'), [
  79. 'product_id' => $product->id,
  80. ]);
  81. $response->assertSessionHasErrors('quantity');
  82. }
  83. public function test_guest_cannot_create_maf_order(): void
  84. {
  85. $product = Product::factory()->create();
  86. $response = $this->post(route('maf_order.store'), [
  87. 'product_id' => $product->id,
  88. 'quantity' => 3,
  89. ]);
  90. $response->assertRedirect(route('login'));
  91. }
  92. // ==================== Show ====================
  93. public function test_can_view_maf_order_details(): void
  94. {
  95. $mafOrder = MafOrder::factory()->create();
  96. $response = $this->actingAs($this->adminUser)
  97. ->get(route('maf_order.show', $mafOrder));
  98. $response->assertStatus(200);
  99. $response->assertViewIs('maf_orders.edit');
  100. }
  101. public function test_guest_cannot_view_maf_order_details(): void
  102. {
  103. $mafOrder = MafOrder::factory()->create();
  104. $response = $this->get(route('maf_order.show', $mafOrder));
  105. $response->assertRedirect(route('login'));
  106. }
  107. // ==================== Update ====================
  108. public function test_can_update_maf_order(): void
  109. {
  110. $product = Product::factory()->create();
  111. $mafOrder = MafOrder::factory()->create([
  112. 'product_id' => $product->id,
  113. 'quantity' => 3,
  114. ]);
  115. $response = $this->actingAs($this->adminUser)
  116. ->post(route('maf_order.update', $mafOrder), [
  117. 'product_id' => $product->id,
  118. 'quantity' => 10,
  119. 'order_number' => 'MO-UPDATED',
  120. ]);
  121. $response->assertRedirect();
  122. $this->assertDatabaseHas('maf_orders', [
  123. 'id' => $mafOrder->id,
  124. 'quantity' => 10,
  125. ]);
  126. }
  127. public function test_guest_cannot_update_maf_order(): void
  128. {
  129. $product = Product::factory()->create();
  130. $mafOrder = MafOrder::factory()->create(['product_id' => $product->id]);
  131. $response = $this->post(route('maf_order.update', $mafOrder), [
  132. 'product_id' => $product->id,
  133. 'quantity' => 10,
  134. ]);
  135. $response->assertRedirect(route('login'));
  136. }
  137. // ==================== Destroy ====================
  138. public function test_can_delete_maf_order(): void
  139. {
  140. $mafOrder = MafOrder::factory()->create();
  141. $mafOrderId = $mafOrder->id;
  142. $response = $this->actingAs($this->adminUser)
  143. ->delete(route('maf_order.delete', $mafOrder));
  144. $response->assertRedirect();
  145. $this->assertSoftDeleted('maf_orders', ['id' => $mafOrderId]);
  146. }
  147. public function test_guest_cannot_delete_maf_order(): void
  148. {
  149. $mafOrder = MafOrder::factory()->create();
  150. $response = $this->delete(route('maf_order.delete', $mafOrder));
  151. $response->assertRedirect(route('login'));
  152. }
  153. // ==================== SetInStock ====================
  154. public function test_set_in_stock_updates_stock_and_status(): void
  155. {
  156. $mafOrder = MafOrder::factory()->create([
  157. 'quantity' => 8,
  158. 'in_stock' => 0,
  159. 'status' => 'active',
  160. ]);
  161. $response = $this->actingAs($this->adminUser)
  162. ->post(route('maf_order.set_in_stock', $mafOrder));
  163. $response->assertRedirect(route('maf_order.show', $mafOrder));
  164. $this->assertDatabaseHas('maf_orders', [
  165. 'id' => $mafOrder->id,
  166. 'in_stock' => 8,
  167. 'status' => 'на складе',
  168. ]);
  169. }
  170. public function test_guest_cannot_set_in_stock(): void
  171. {
  172. $mafOrder = MafOrder::factory()->create(['quantity' => 5]);
  173. $response = $this->post(route('maf_order.set_in_stock', $mafOrder));
  174. $response->assertRedirect(route('login'));
  175. }
  176. }