<?php

namespace Tests\Unit;

use App\Actions\Fortify\CreateNewUser;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Validation\ValidationException;
use Mtc\MercuryDataModels\User;
use Tests\TestCase;

class CreateNewUserTest extends TestCase
{
    use RefreshDatabase;

    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function testCreateUser()
    {
        $input = [
            'name' => 'John Doe',
            'email' => 'test@example.com',
            'password' => 'SomethingSuperStrong2#',
            'password_confirmation' => 'SomethingSuperStrong2#',
        ];

        $user = (new CreateNewUser())->create($input);

        $this->assertInstanceOf(User::class, $user);
        $this->assertEquals('John Doe', $user->name);
        $this->assertEquals('test@example.com', $user->email);
        $this->assertTrue(password_verify('SomethingSuperStrong2#', $user->password));
    }

    public function testFailsWithoutRequiredFields()
    {
        $input = [
            'email' => 'test@example.com',
            'password' => 'SomethingSuperStrong2#',
            'password_confirmation' => 'SomethingSuperStrong2#',
        ];

        $this->expectException(ValidationException::class);
        $user = (new CreateNewUser())->create($input);

        $input = [
            'name' => 'John Doe',
            'password' => 'SomethingSuperStrong2#',
            'password_confirmation' => 'SomethingSuperStrong2#',
        ];

        $this->expectException(ValidationException::class);
        $user = (new CreateNewUser())->create($input);

        $input = [
            'name' => 'John Doe',
            'email' => 'test@example.com',
            'password' => 'SomethingSuperStrong2#',
        ];

        $this->expectException(ValidationException::class);
        $user = (new CreateNewUser())->create($input);
    }
}
