| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace Tests\Unit\Requests;
- use App\Http\Requests\StoreSparePartOrderRequest;
- use Illuminate\Support\Facades\Validator;
- use Tests\TestCase;
- class StoreSparePartOrderRequestTest extends TestCase
- {
- /**
- * Return rules with exists constraint on spare_part_id removed
- * so required/type/format rules can be tested without a database.
- */
- private function rulesWithoutExists(): array
- {
- $rules = (new StoreSparePartOrderRequest())->rules();
- $rules['spare_part_id'] = 'required';
- return $rules;
- }
- public function test_empty_data_fails(): void
- {
- $validator = Validator::make([], $this->rulesWithoutExists());
- $this->assertTrue($validator->fails());
- }
- public function test_missing_spare_part_id_fails(): void
- {
- $data = [
- 'status' => 'ordered',
- 'ordered_quantity' => 1,
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $this->assertTrue($validator->fails());
- $this->assertArrayHasKey('spare_part_id', $validator->errors()->toArray());
- }
- public function test_missing_status_fails(): void
- {
- $data = [
- 'spare_part_id' => 1,
- 'ordered_quantity' => 1,
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $this->assertTrue($validator->fails());
- $this->assertArrayHasKey('status', $validator->errors()->toArray());
- }
- public function test_invalid_status_value_fails(): void
- {
- $data = [
- 'spare_part_id' => 1,
- 'status' => 'invalid_status',
- 'ordered_quantity' => 1,
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $this->assertTrue($validator->fails());
- $this->assertArrayHasKey('status', $validator->errors()->toArray());
- }
- public function test_status_ordered_passes(): void
- {
- $data = [
- 'spare_part_id' => 1,
- 'status' => 'ordered',
- 'ordered_quantity' => 1,
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $errors = $validator->errors()->toArray();
- $this->assertArrayNotHasKey('status', $errors);
- }
- public function test_status_in_stock_passes(): void
- {
- $data = [
- 'spare_part_id' => 1,
- 'status' => 'in_stock',
- 'ordered_quantity' => 1,
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $errors = $validator->errors()->toArray();
- $this->assertArrayNotHasKey('status', $errors);
- }
- public function test_status_shipped_passes(): void
- {
- $data = [
- 'spare_part_id' => 1,
- 'status' => 'shipped',
- 'ordered_quantity' => 1,
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $errors = $validator->errors()->toArray();
- $this->assertArrayNotHasKey('status', $errors);
- }
- public function test_missing_ordered_quantity_fails(): void
- {
- $data = [
- 'spare_part_id' => 1,
- 'status' => 'ordered',
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $this->assertTrue($validator->fails());
- $this->assertArrayHasKey('ordered_quantity', $validator->errors()->toArray());
- }
- public function test_ordered_quantity_zero_fails_min_rule(): void
- {
- $data = [
- 'spare_part_id' => 1,
- 'status' => 'ordered',
- 'ordered_quantity' => 0,
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $this->assertTrue($validator->fails());
- $this->assertArrayHasKey('ordered_quantity', $validator->errors()->toArray());
- }
- public function test_ordered_quantity_must_be_integer(): void
- {
- $data = [
- 'spare_part_id' => 1,
- 'status' => 'ordered',
- 'ordered_quantity' => 'ten',
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $this->assertTrue($validator->fails());
- $this->assertArrayHasKey('ordered_quantity', $validator->errors()->toArray());
- }
- public function test_valid_minimal_data_passes(): void
- {
- $data = [
- 'spare_part_id' => 1,
- 'status' => 'ordered',
- 'ordered_quantity' => 5,
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $this->assertFalse($validator->fails());
- }
- public function test_valid_full_data_passes(): void
- {
- $data = [
- 'spare_part_id' => 1,
- 'order_number' => 'PO-2026-001',
- 'source_text' => 'Рекламация',
- 'sourceable_id' => 42,
- 'sourceable_type' => 'App\\Models\\Reclamation',
- 'status' => 'in_stock',
- 'ordered_quantity' => 10,
- 'with_documents' => true,
- 'note' => 'Срочно',
- ];
- $validator = Validator::make($data, $this->rulesWithoutExists());
- $this->assertFalse($validator->fails());
- }
- }
|