When writing request validators in Laravel, it is useful to know which incoming data will lead to a validation error, and which data will pass. A convenient way to test this, is to write unit tests that make use of https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html?highlight=data%20provider#data-providers. Data providers can provide the arguments for your tests, in this case the request data.
In this test we will add two providers, one for the data that should fail validation, and one for the data that should pass: class StoreOrderRequestValidationTest extends TestCase { /** * @dataProvider dataThatShouldFail */ public function testValidationWillFail(array $requestData): void { $response = $this->json('POST', 'api/order', $requestData); $response->assertStatus(422); } /** * @dataProvider dataThatShouldPass */ public function testValidationWillPass(array $requestData): void { $response = $this->json('POST', 'api/order', $requestData); $response->assertOk(); } public function dataThatShouldFail(): array { $productName = 'pizza'; $amount = 2; $deliveryDate = CarbonImmutable::tomorrow()->toDateString(); return [ 'no_product_name' => [[ 'amount' => $amount, 'delivery_date' => $deliveryDate, ], ], 'no_amount' => [[ 'product_name' => $productName, 'delivery_date' => $deliveryDate, ], ], 'no_delivery_date' => [[ 'product_name' => $productName, 'amount' => $amount, ], ], 'invalid_product_name' => [[ 'product_name' => 'hamburger', 'amount' => $amount, 'delivery_date' => $deliveryDate, ], ], 'invalid_amount' => [[ 'product_name' => $productName, 'amount' => 26, 'delivery_date' => $deliveryDate, ], ], 'invalid_delivery_date' => [[ 'product_name' => $productName, 'amount' => $amount, 'delivery_date' => CarbonImmutable::today(), ], ], ]; } public function dataThatShouldPass(): array { return [ 'pizza' => [[ 'product_name' => 'pizza', 'amount' => 3, 'delivery_date' => CarbonImmutable::tomorrow(), ], ], 'beer' => [[ 'product_name' => 'beer', 'amount' => 10, 'delivery_date' => CarbonImmutable::now()->addDays(10), ], ], ]; }}
It contains request data for creating an order that should not result in a validation error.