StoreSparePartOrderRequestTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Tests\Unit\Requests;
  3. use App\Http\Requests\StoreSparePartOrderRequest;
  4. use Illuminate\Support\Facades\Validator;
  5. use Tests\TestCase;
  6. class StoreSparePartOrderRequestTest extends TestCase
  7. {
  8. /**
  9. * Return rules with exists constraint on spare_part_id removed
  10. * so required/type/format rules can be tested without a database.
  11. */
  12. private function rulesWithoutExists(): array
  13. {
  14. $rules = (new StoreSparePartOrderRequest())->rules();
  15. $rules['spare_part_id'] = 'required';
  16. return $rules;
  17. }
  18. public function test_empty_data_fails(): void
  19. {
  20. $validator = Validator::make([], $this->rulesWithoutExists());
  21. $this->assertTrue($validator->fails());
  22. }
  23. public function test_missing_spare_part_id_fails(): void
  24. {
  25. $data = [
  26. 'status' => 'ordered',
  27. 'ordered_quantity' => 1,
  28. ];
  29. $validator = Validator::make($data, $this->rulesWithoutExists());
  30. $this->assertTrue($validator->fails());
  31. $this->assertArrayHasKey('spare_part_id', $validator->errors()->toArray());
  32. }
  33. public function test_missing_status_fails(): void
  34. {
  35. $data = [
  36. 'spare_part_id' => 1,
  37. 'ordered_quantity' => 1,
  38. ];
  39. $validator = Validator::make($data, $this->rulesWithoutExists());
  40. $this->assertTrue($validator->fails());
  41. $this->assertArrayHasKey('status', $validator->errors()->toArray());
  42. }
  43. public function test_invalid_status_value_fails(): void
  44. {
  45. $data = [
  46. 'spare_part_id' => 1,
  47. 'status' => 'invalid_status',
  48. 'ordered_quantity' => 1,
  49. ];
  50. $validator = Validator::make($data, $this->rulesWithoutExists());
  51. $this->assertTrue($validator->fails());
  52. $this->assertArrayHasKey('status', $validator->errors()->toArray());
  53. }
  54. public function test_status_ordered_passes(): void
  55. {
  56. $data = [
  57. 'spare_part_id' => 1,
  58. 'status' => 'ordered',
  59. 'ordered_quantity' => 1,
  60. ];
  61. $validator = Validator::make($data, $this->rulesWithoutExists());
  62. $errors = $validator->errors()->toArray();
  63. $this->assertArrayNotHasKey('status', $errors);
  64. }
  65. public function test_status_in_stock_passes(): void
  66. {
  67. $data = [
  68. 'spare_part_id' => 1,
  69. 'status' => 'in_stock',
  70. 'ordered_quantity' => 1,
  71. ];
  72. $validator = Validator::make($data, $this->rulesWithoutExists());
  73. $errors = $validator->errors()->toArray();
  74. $this->assertArrayNotHasKey('status', $errors);
  75. }
  76. public function test_status_shipped_passes(): void
  77. {
  78. $data = [
  79. 'spare_part_id' => 1,
  80. 'status' => 'shipped',
  81. 'ordered_quantity' => 1,
  82. ];
  83. $validator = Validator::make($data, $this->rulesWithoutExists());
  84. $errors = $validator->errors()->toArray();
  85. $this->assertArrayNotHasKey('status', $errors);
  86. }
  87. public function test_missing_ordered_quantity_fails(): void
  88. {
  89. $data = [
  90. 'spare_part_id' => 1,
  91. 'status' => 'ordered',
  92. ];
  93. $validator = Validator::make($data, $this->rulesWithoutExists());
  94. $this->assertTrue($validator->fails());
  95. $this->assertArrayHasKey('ordered_quantity', $validator->errors()->toArray());
  96. }
  97. public function test_ordered_quantity_zero_fails_min_rule(): void
  98. {
  99. $data = [
  100. 'spare_part_id' => 1,
  101. 'status' => 'ordered',
  102. 'ordered_quantity' => 0,
  103. ];
  104. $validator = Validator::make($data, $this->rulesWithoutExists());
  105. $this->assertTrue($validator->fails());
  106. $this->assertArrayHasKey('ordered_quantity', $validator->errors()->toArray());
  107. }
  108. public function test_ordered_quantity_must_be_integer(): void
  109. {
  110. $data = [
  111. 'spare_part_id' => 1,
  112. 'status' => 'ordered',
  113. 'ordered_quantity' => 'ten',
  114. ];
  115. $validator = Validator::make($data, $this->rulesWithoutExists());
  116. $this->assertTrue($validator->fails());
  117. $this->assertArrayHasKey('ordered_quantity', $validator->errors()->toArray());
  118. }
  119. public function test_valid_minimal_data_passes(): void
  120. {
  121. $data = [
  122. 'spare_part_id' => 1,
  123. 'status' => 'ordered',
  124. 'ordered_quantity' => 5,
  125. ];
  126. $validator = Validator::make($data, $this->rulesWithoutExists());
  127. $this->assertFalse($validator->fails());
  128. }
  129. public function test_valid_full_data_passes(): void
  130. {
  131. $data = [
  132. 'spare_part_id' => 1,
  133. 'order_number' => 'PO-2026-001',
  134. 'source_text' => 'Рекламация',
  135. 'sourceable_id' => 42,
  136. 'sourceable_type' => 'App\\Models\\Reclamation',
  137. 'status' => 'in_stock',
  138. 'ordered_quantity' => 10,
  139. 'with_documents' => true,
  140. 'note' => 'Срочно',
  141. ];
  142. $validator = Validator::make($data, $this->rulesWithoutExists());
  143. $this->assertFalse($validator->fails());
  144. }
  145. }