papa
300
8
Testing

Test Validation Email Unique fails always

Posted 4 months ago by papa

The Request

public function rules()
    {
        $id = (isset($this->id) ? $this->id : null) . ',id';

        return [
            'firstName' => ['required'],
            'lastName' => ['required'],
            'email' => ['required', 'email', 'unique:User,email,' . $id],
            'password' => ['required', 'min:6', 'confirmed'],
            'password_confirmation' => ['min:6'],
            'address' => ['required'],
            'zipcode' => ['required'],
            'city' => ['required'],
            'phone' => ['required'],
            'language' => ['required']
        ];
    }

The test Function

public function testUser()
{

$users = [
            [
                'firstName' => 'Name',
                'lastName' => 'Surname',
                'password' => 123456,
                'password_confirmation' => 123456,
                'address' => 'My address',
                'zipcode' => '84763',
                'city' => 'New York',
                'email' => '[email protected]',
                'phone' => '017654875674',
                'mobile' => '763636',
                'language' => 1
            ],
        ];


 foreach ($users as $user) {
            $response = $this->post(route('doRegister'), $user);

            $validator = Validator::make($user, $validation->rules());
            $this->assertEquals(false, $validator->fails());

            $this->assertDatabaseHas('User', ['email' => $user['email']]);
            $this->assertDatabaseHas('Member', [
                'firstName' => $user['firstName'],
                'lastName' => $user['lastName'],
                'address' => $user['address'],
                'zipcode' => $user['zipcode'],
                'city' => $user['city'],
                'phone' => $user['phone'],
                'mobile' => $user['mobile'],
                'langId' => 1
            ]);

            $response->assertRedirect(route('index', 200));
        }
}

The $validator gives me always error on Email Unique Rule and the test does not pass. Have someone an idea why that happens?

Please sign in or create an account to participate in this conversation.