0

I have a test to test, if a client can checkin with all required fields:

public function test_client_can_checkin()
    {
        $client = Client::factory()->create();
        $response = $this->post('/', $client->toArray());

        $response->assertStatus(200);
    }

In addition I have some validation rules in place:

return [
            'uniqid' => ['required', 'string', 'max:255'],
            'realm' => ['required', 'string', 'max:255'],
            'fn' => ['required', 'string', 'max:255'],
            'ln' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email'],
            'phone' => ['required', 'string', 'max:255'],
            'street' => ['required', 'string', 'max:255'],
            'zip' => ['required', 'integer', 'digits:5'],
            'city' => ['required', 'string', 'max:255'],
            'dob' => ['required', 'date']
        ];

Is there a short hand way to test against all validation rules, instead of writing a test for each rule?

E.g.

public function test_client_can_not_checkin_with_missing_values()
    {
        $client = Client::factory()->create();
        unset($client->uniqid);
        $this->post('/', $client->toArray())
            ->assertStatus(302);

        $client = Client::factory()->create();
        unset($client->realm);
        $this->post('/', $client->toArray())
            ->assertStatus(302);

        $client = Client::factory()->create();
        unset($client->fn);
        $this->post('/', $client->toArray())
            ->assertStatus(302);

        $client = Client::factory()->create();
        unset($client->ln);
        $this->post('/', $client->toArray())
            ->assertStatus(302);

        $client = Client::factory()->create();
        unset($client->email);
        $this->post('/', $client->toArray())
            ->assertStatus(302);

        $client = Client::factory()->create();
        unset($client->phone);
        $this->post('/', $client->toArray())
            ->assertStatus(302);

        $client = Client::factory()->create();
        unset($client->street);
        $this->post('/', $client->toArray())
            ->assertStatus(302);

        $client = Client::factory()->create();
        unset($client->zip);
        $this->post('/', $client->toArray())
            ->assertStatus(302);

        $client = Client::factory()->create();
        unset($client->city);
        $this->post('/', $client->toArray())
            ->assertStatus(302);

        $client = Client::factory()->create();
        unset($client->dob);
        $this->post('/', $client->toArray())
            ->assertStatus(302);
    }

I would now continue like this with all the restrictions of type (email / string) and length and so on...

JanBoehmer
  • 395
  • 3
  • 14
  • You can make things simpler by using [dataproviders](https://phpunit.readthedocs.io/en/9.5/annotations.html?highlight=dataprovider#dataprovider). Tighten has a [good starter article](https://tighten.com/blog/tidying-up-your-phpunit-tests-with-data-providers/) on how to implement them. – Peppermintology Sep 13 '22 at 12:52

0 Answers0