|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Inventario; |
| 4 | + |
| 5 | +use Tests\TestCase; |
| 6 | +use Illuminate\Support\Facades\Http; |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | + |
| 9 | +class ProduccionControllerTest extends TestCase |
| 10 | +{ |
| 11 | + // =========================================== |
| 12 | + // GET /inventario/produccion -> index |
| 13 | + // =========================================== |
| 14 | + |
| 15 | + #[Test] |
| 16 | + public function index_retorna_200_con_historial_y_recetas(): void |
| 17 | + { |
| 18 | + Http::fake([ |
| 19 | + '*/inventario/produccion' => Http::response([ |
| 20 | + [ |
| 21 | + 'idProduccion' => 1, |
| 22 | + 'idProducto' => 1, |
| 23 | + 'cantidadProducida' => 10, |
| 24 | + 'fechaProduccion' => '2023-10-01' |
| 25 | + ] |
| 26 | + ], 200), |
| 27 | + '*/inventario/recetas/optimizadas' => Http::response([ |
| 28 | + [ |
| 29 | + 'idProducto' => 1, |
| 30 | + 'nombreProducto' => 'Pan Frances', |
| 31 | + 'idIngrediente' => 2, |
| 32 | + 'cantidadRequerida' => 500, |
| 33 | + 'idUnidad' => 1 |
| 34 | + ] |
| 35 | + ], 200) |
| 36 | + ]); |
| 37 | + |
| 38 | + $response = $this->get(route('produccion.index')); |
| 39 | + |
| 40 | + $response->assertStatus(200); |
| 41 | + $response->assertViewIs('inventarioviews.produccion.index'); |
| 42 | + $response->assertViewHas('historial'); |
| 43 | + $response->assertViewHas('productosConReceta'); |
| 44 | + $response->assertViewHas('recetas'); |
| 45 | + } |
| 46 | + |
| 47 | + #[Test] |
| 48 | + public function index_maneja_error_y_devuelve_vista_con_array_vacio(): void |
| 49 | + { |
| 50 | + Http::fake([ |
| 51 | + '*/inventario/produccion' => Http::response('Server Error', 500), |
| 52 | + '*/inventario/recetas/optimizadas' => Http::response('Server Error', 500) |
| 53 | + ]); |
| 54 | + |
| 55 | + $response = $this->get(route('produccion.index')); |
| 56 | + |
| 57 | + $response->assertStatus(200); |
| 58 | + $response->assertViewIs('inventarioviews.produccion.index'); |
| 59 | + $response->assertViewHas('historial', []); |
| 60 | + $response->assertViewHas('recetas', []); |
| 61 | + $response->assertViewHas('error'); |
| 62 | + } |
| 63 | + |
| 64 | + // =========================================== |
| 65 | + // POST /inventario/produccion/store -> store |
| 66 | + // =========================================== |
| 67 | + |
| 68 | + #[Test] |
| 69 | + public function store_falla_validacion_si_faltan_campos_requeridos(): void |
| 70 | + { |
| 71 | + $response = $this->post(route('produccion.store'), []); |
| 72 | + |
| 73 | + $response->assertSessionHasErrors([ |
| 74 | + 'idProducto', |
| 75 | + 'cantidadProducida' |
| 76 | + ]); |
| 77 | + } |
| 78 | + |
| 79 | + #[Test] |
| 80 | + public function store_registra_produccion_exitosamente(): void |
| 81 | + { |
| 82 | + Http::fake([ |
| 83 | + '*/inventario/produccion' => Http::response([ |
| 84 | + 'mensaje' => 'Producción registrada', |
| 85 | + 'idProduccion' => 100 |
| 86 | + ], 200) |
| 87 | + ]); |
| 88 | + |
| 89 | + $response = $this->post(route('produccion.store'), [ |
| 90 | + 'idProducto' => 1, |
| 91 | + 'cantidadProducida' => 20.5 |
| 92 | + ]); |
| 93 | + |
| 94 | + $response->assertRedirect(route('produccion.index')); |
| 95 | + $response->assertSessionHas('success'); |
| 96 | + } |
| 97 | + |
| 98 | + #[Test] |
| 99 | + public function store_falla_cuando_api_rechaza(): void |
| 100 | + { |
| 101 | + Http::fake([ |
| 102 | + '*/inventario/produccion' => Http::response(['error' => 'Sin ingredientes'], 400) |
| 103 | + ]); |
| 104 | + |
| 105 | + $response = $this->post(route('produccion.store'), [ |
| 106 | + 'idProducto' => 1, |
| 107 | + 'cantidadProducida' => 20.5 |
| 108 | + ]); |
| 109 | + |
| 110 | + $response->assertRedirect(); |
| 111 | + $response->assertSessionHas('error'); |
| 112 | + } |
| 113 | + |
| 114 | + // =========================================== |
| 115 | + // DELETE /inventario/produccion/delete/{id} -> destroy |
| 116 | + // =========================================== |
| 117 | + |
| 118 | + #[Test] |
| 119 | + public function destroy_elimina_produccion_exitosamente(): void |
| 120 | + { |
| 121 | + Http::fake([ |
| 122 | + '*/inventario/produccion/1' => Http::response('', 204) |
| 123 | + ]); |
| 124 | + |
| 125 | + $response = $this->delete(route('produccion.destroy', ['id' => 1])); |
| 126 | + |
| 127 | + $response->assertRedirect(route('produccion.index')); |
| 128 | + $response->assertSessionHas('success'); |
| 129 | + } |
| 130 | + |
| 131 | + #[Test] |
| 132 | + public function destroy_falla_cuando_api_retorna_error(): void |
| 133 | + { |
| 134 | + Http::fake([ |
| 135 | + '*/inventario/produccion/99' => Http::response(['error' => 'No encontrado'], 404) |
| 136 | + ]); |
| 137 | + |
| 138 | + $response = $this->delete(route('produccion.destroy', ['id' => 99])); |
| 139 | + |
| 140 | + $response->assertRedirect(); |
| 141 | + $response->assertSessionHas('error'); |
| 142 | + } |
| 143 | +} |
0 commit comments