StoreReclamationRequestTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Tests\Unit\Requests;
  3. use App\Http\Requests\StoreReclamationRequest;
  4. use Illuminate\Support\Facades\Validator;
  5. use Tests\TestCase;
  6. class StoreReclamationRequestTest extends TestCase
  7. {
  8. /**
  9. * Return rules with exists constraints replaced so we can test
  10. * required/type/format rules without a database connection.
  11. */
  12. private function rulesWithoutExists(): array
  13. {
  14. $rules = (new StoreReclamationRequest())->rules();
  15. $rules['user_id'] = 'required';
  16. $rules['status_id'] = 'required';
  17. $rules['brigadier_id'] = 'nullable';
  18. return $rules;
  19. }
  20. public function test_empty_data_fails(): void
  21. {
  22. $validator = Validator::make([], $this->rulesWithoutExists());
  23. $this->assertTrue($validator->fails());
  24. }
  25. public function test_missing_user_id_fails(): void
  26. {
  27. $data = [
  28. 'status_id' => 1,
  29. 'create_date' => '2026-01-01',
  30. 'finish_date' => '2026-02-01',
  31. ];
  32. $validator = Validator::make($data, $this->rulesWithoutExists());
  33. $this->assertTrue($validator->fails());
  34. $this->assertArrayHasKey('user_id', $validator->errors()->toArray());
  35. }
  36. public function test_missing_status_id_fails(): void
  37. {
  38. $data = [
  39. 'user_id' => 1,
  40. 'create_date' => '2026-01-01',
  41. 'finish_date' => '2026-02-01',
  42. ];
  43. $validator = Validator::make($data, $this->rulesWithoutExists());
  44. $this->assertTrue($validator->fails());
  45. $this->assertArrayHasKey('status_id', $validator->errors()->toArray());
  46. }
  47. public function test_missing_create_date_fails(): void
  48. {
  49. $data = [
  50. 'user_id' => 1,
  51. 'status_id' => 1,
  52. 'finish_date' => '2026-02-01',
  53. ];
  54. $validator = Validator::make($data, $this->rulesWithoutExists());
  55. $this->assertTrue($validator->fails());
  56. $this->assertArrayHasKey('create_date', $validator->errors()->toArray());
  57. }
  58. public function test_missing_finish_date_fails(): void
  59. {
  60. $data = [
  61. 'user_id' => 1,
  62. 'status_id' => 1,
  63. 'create_date' => '2026-01-01',
  64. ];
  65. $validator = Validator::make($data, $this->rulesWithoutExists());
  66. $this->assertTrue($validator->fails());
  67. $this->assertArrayHasKey('finish_date', $validator->errors()->toArray());
  68. }
  69. public function test_invalid_create_date_format_fails(): void
  70. {
  71. $data = [
  72. 'user_id' => 1,
  73. 'status_id' => 1,
  74. 'create_date' => 'not-a-date',
  75. 'finish_date' => '2026-02-01',
  76. ];
  77. $validator = Validator::make($data, $this->rulesWithoutExists());
  78. $this->assertTrue($validator->fails());
  79. $this->assertArrayHasKey('create_date', $validator->errors()->toArray());
  80. }
  81. public function test_invalid_finish_date_format_fails(): void
  82. {
  83. $data = [
  84. 'user_id' => 1,
  85. 'status_id' => 1,
  86. 'create_date' => '2026-01-01',
  87. 'finish_date' => 'bad-date',
  88. ];
  89. $validator = Validator::make($data, $this->rulesWithoutExists());
  90. $this->assertTrue($validator->fails());
  91. $this->assertArrayHasKey('finish_date', $validator->errors()->toArray());
  92. }
  93. public function test_valid_required_fields_pass_non_exists_rules(): void
  94. {
  95. $data = [
  96. 'user_id' => 1,
  97. 'status_id' => 1,
  98. 'create_date' => '2026-01-01',
  99. 'finish_date' => '2026-06-30',
  100. ];
  101. $validator = Validator::make($data, $this->rulesWithoutExists());
  102. $this->assertFalse($validator->fails());
  103. }
  104. public function test_nullable_fields_accept_null_values(): void
  105. {
  106. $data = [
  107. 'user_id' => 1,
  108. 'status_id' => 1,
  109. 'create_date' => '2026-01-01',
  110. 'finish_date' => '2026-06-30',
  111. 'reason' => null,
  112. 'guarantee' => null,
  113. 'whats_done' => null,
  114. 'comment' => null,
  115. 'work_days' => null,
  116. ];
  117. $validator = Validator::make($data, $this->rulesWithoutExists());
  118. $this->assertFalse($validator->fails());
  119. }
  120. public function test_factory_reclamation_number_max_length_fails(): void
  121. {
  122. $data = [
  123. 'user_id' => 1,
  124. 'status_id' => 1,
  125. 'create_date' => '2026-01-01',
  126. 'finish_date' => '2026-06-30',
  127. 'factory_reclamation_number' => str_repeat('a', 256),
  128. ];
  129. $validator = Validator::make($data, $this->rulesWithoutExists());
  130. $this->assertTrue($validator->fails());
  131. $this->assertArrayHasKey('factory_reclamation_number', $validator->errors()->toArray());
  132. }
  133. public function test_invalid_start_work_date_fails(): void
  134. {
  135. $data = [
  136. 'user_id' => 1,
  137. 'status_id' => 1,
  138. 'create_date' => '2026-01-01',
  139. 'finish_date' => '2026-06-30',
  140. 'start_work_date' => 'invalid',
  141. ];
  142. $validator = Validator::make($data, $this->rulesWithoutExists());
  143. $this->assertTrue($validator->fails());
  144. $this->assertArrayHasKey('start_work_date', $validator->errors()->toArray());
  145. }
  146. }