90 lines
2.8 KiB
PHP
90 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Auth;
|
|
|
|
use App\Domains\Auth\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class RegisterControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_it_registers_a_user(): void
|
|
{
|
|
$response = $this->postJson('/api/register', [
|
|
'nombre_apellido' => 'Ada Lovelace',
|
|
'email' => 'ada@example.com',
|
|
'password' => 'secret123',
|
|
'password_confirmation' => 'secret123',
|
|
'dni' => '12345678A',
|
|
'telefono' => '+541122334455',
|
|
]);
|
|
|
|
$response
|
|
->assertCreated()
|
|
->assertJsonPath('message', 'Usuario registrado correctamente.')
|
|
->assertJsonPath('data.nombre_apellido', 'Ada Lovelace')
|
|
->assertJsonPath('data.email', 'ada@example.com')
|
|
->assertJsonPath('data.dni', '12345678A')
|
|
->assertJsonPath('data.telefono', '+541122334455');
|
|
|
|
$this->assertDatabaseHas('users', [
|
|
'nombre_apellido' => 'Ada Lovelace',
|
|
'email' => 'ada@example.com',
|
|
'dni' => '12345678A',
|
|
'telefono' => '+541122334455',
|
|
]);
|
|
|
|
$user = User::query()->where('email', 'ada@example.com')->firstOrFail();
|
|
|
|
$this->assertNotSame('secret123', $user->password);
|
|
}
|
|
|
|
public function test_it_registers_a_user_without_optional_fields(): void
|
|
{
|
|
$response = $this->postJson('/api/register', [
|
|
'nombre_apellido' => 'Alan Turing',
|
|
'email' => 'alan@example.com',
|
|
'password' => 'secret123',
|
|
'password_confirmation' => 'secret123',
|
|
]);
|
|
|
|
$response
|
|
->assertCreated()
|
|
->assertJsonPath('message', 'Usuario registrado correctamente.')
|
|
->assertJsonPath('data.nombre_apellido', 'Alan Turing')
|
|
->assertJsonPath('data.email', 'alan@example.com')
|
|
->assertJsonPath('data.dni', null)
|
|
->assertJsonPath('data.telefono', null);
|
|
|
|
$this->assertDatabaseHas('users', [
|
|
'nombre_apellido' => 'Alan Turing',
|
|
'email' => 'alan@example.com',
|
|
'dni' => null,
|
|
'telefono' => null,
|
|
]);
|
|
}
|
|
|
|
public function test_it_validates_required_fields_and_unique_email(): void
|
|
{
|
|
User::factory()->create([
|
|
'nombre_apellido' => 'Existing User',
|
|
'email' => 'existing@example.com',
|
|
]);
|
|
|
|
$response = $this->postJson('/api/register', [
|
|
'nombre_apellido' => '',
|
|
'email' => 'existing@example.com',
|
|
'password' => 'secret123',
|
|
'password_confirmation' => 'different-secret',
|
|
]);
|
|
|
|
$response->assertUnprocessable()->assertJsonValidationErrors([
|
|
'nombre_apellido',
|
|
'email',
|
|
'password',
|
|
]);
|
|
}
|
|
}
|