ProductSKUControllerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Jobs\ExportMafJob;
  4. use App\Models\Order;
  5. use App\Models\Product;
  6. use App\Models\ProductSKU;
  7. use App\Models\Role;
  8. use App\Models\User;
  9. use Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Illuminate\Support\Facades\Bus;
  11. use Tests\TestCase;
  12. class ProductSKUControllerTest extends TestCase
  13. {
  14. use RefreshDatabase;
  15. protected $seed = true;
  16. private User $adminUser;
  17. private User $managerUser;
  18. private User $brigadierUser;
  19. protected function setUp(): void
  20. {
  21. parent::setUp();
  22. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  23. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  24. $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]);
  25. }
  26. // ==================== Guest redirects ====================
  27. public function test_guest_cannot_access_product_sku_index(): void
  28. {
  29. $response = $this->get(route('product_sku.index'));
  30. $response->assertRedirect(route('login'));
  31. }
  32. public function test_guest_cannot_access_product_sku_show(): void
  33. {
  34. $sku = ProductSKU::factory()->create();
  35. $response = $this->get(route('product_sku.show', $sku));
  36. $response->assertRedirect(route('login'));
  37. }
  38. public function test_guest_cannot_update_product_sku(): void
  39. {
  40. $sku = ProductSKU::factory()->create();
  41. $response = $this->post(route('product_sku.update', $sku), []);
  42. $response->assertRedirect(route('login'));
  43. }
  44. // ==================== Authorization ====================
  45. public function test_brigadier_cannot_access_product_sku_index(): void
  46. {
  47. $response = $this->actingAs($this->brigadierUser)
  48. ->get(route('product_sku.index'));
  49. $response->assertStatus(403);
  50. }
  51. public function test_manager_cannot_export_mafs(): void
  52. {
  53. $response = $this->actingAs($this->managerUser)
  54. ->post(route('mafs.export'));
  55. $response->assertStatus(403);
  56. }
  57. // ==================== Index ====================
  58. public function test_admin_can_access_product_sku_index(): void
  59. {
  60. $response = $this->actingAs($this->adminUser)
  61. ->get(route('product_sku.index'));
  62. $response->assertStatus(200);
  63. $response->assertViewIs('products_sku.index');
  64. }
  65. public function test_manager_can_access_product_sku_index(): void
  66. {
  67. $response = $this->actingAs($this->managerUser)
  68. ->get(route('product_sku.index'));
  69. $response->assertStatus(200);
  70. $response->assertViewIs('products_sku.index');
  71. }
  72. // ==================== Show ====================
  73. public function test_admin_can_view_product_sku(): void
  74. {
  75. $sku = ProductSKU::factory()->create();
  76. $response = $this->actingAs($this->adminUser)
  77. ->get(route('product_sku.show', $sku));
  78. $response->assertStatus(200);
  79. $response->assertViewIs('products_sku.edit');
  80. }
  81. public function test_manager_can_view_product_sku(): void
  82. {
  83. $sku = ProductSKU::factory()->create();
  84. $response = $this->actingAs($this->managerUser)
  85. ->get(route('product_sku.show', $sku));
  86. $response->assertStatus(200);
  87. $response->assertViewIs('products_sku.edit');
  88. }
  89. // ==================== Update ====================
  90. public function test_admin_can_update_product_sku(): void
  91. {
  92. $product = Product::factory()->create();
  93. $order = Order::factory()->create();
  94. $sku = ProductSKU::factory()->create([
  95. 'product_id' => $product->id,
  96. 'order_id' => $order->id,
  97. ]);
  98. $response = $this->actingAs($this->adminUser)
  99. ->post(route('product_sku.update', $sku), [
  100. 'product_id' => $product->id,
  101. 'order_id' => $order->id,
  102. 'status' => 'shipped',
  103. 'factory_number' => 'FN-999999',
  104. 'comment' => 'Updated by admin',
  105. ]);
  106. $response->assertRedirect();
  107. $this->assertDatabaseHas('products_sku', [
  108. 'id' => $sku->id,
  109. 'factory_number' => 'FN-999999',
  110. 'comment' => 'Updated by admin',
  111. ]);
  112. }
  113. public function test_manager_can_update_product_sku(): void
  114. {
  115. $product = Product::factory()->create();
  116. $order = Order::factory()->create();
  117. $sku = ProductSKU::factory()->create([
  118. 'product_id' => $product->id,
  119. 'order_id' => $order->id,
  120. ]);
  121. $response = $this->actingAs($this->managerUser)
  122. ->post(route('product_sku.update', $sku), [
  123. 'product_id' => $product->id,
  124. 'order_id' => $order->id,
  125. 'status' => 'needs',
  126. 'factory_number' => 'FN-777777',
  127. ]);
  128. $response->assertRedirect();
  129. }
  130. // ==================== Export ====================
  131. public function test_admin_can_export_mafs(): void
  132. {
  133. Bus::fake();
  134. $response = $this->actingAs($this->adminUser)
  135. ->post(route('mafs.export'));
  136. $response->assertRedirect(route('product_sku.index'));
  137. $response->assertSessionHas('success');
  138. Bus::assertDispatched(ExportMafJob::class);
  139. }
  140. }