ldc-mesbr-app/tests/Feature/Api/V0/ContactTest.php

134 lines
3.9 KiB
PHP

<?php
namespace Tests\Feature\Api\V0;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\Contact;
use App\Models\Site;
class ContactTest extends TestCase
{
use RefreshDatabase;
public function test_update_contact_success_existing_contact()
{
$endpoint = '/updatecontact';
// Prepare a Site and existing Contact
$site = Site::factory()->create(['site_id' => 1]);
$contact = Contact::factory()->create([
'contact_siteId' => $site->site_id,
'contact_ref' => 'ref123',
]);
// API payload updates the existing contact info
$payload = [
'refadherent' => $contact->contact_ref,
'nom' => 'NewName',
'prenom' => 'NewPrenom',
'email' => 'test@example.com',
'refsite' => $site->site_id,
'code_postal' => '12345',
'commune' => 'NewCommune',
'pays' => '',
'hashcode' => 'customhash123',
];
$response = $this->postJson($endpoint, $payload);
$response->assertStatus(200)
->assertJson([
'Statut' => 'OK',
'contact' => [
'contact_prenom' => 'NewPrenom',
'contact_nom' => 'NewName',
'contact_email' => 'test@example.com',
'contact_cp' => '12345',
'contact_ville' => 'NewCommune',
'contact_country' => 250,
'contact_hash' => 'customhash123',
]
]);
$this->assertDatabaseHas('contact', [
'contact_siteId' => $site->site_id,
'contact_ref' => 'ref123',
'contact_nom' => 'NewName',
]);
}
public function test_update_contact_success_new_contact()
{
$endpoint = '/updatecontact';
$site = Site::factory()->create(['site_id' => 2]);
$payload = [
'refadherent' => 'newref',
'nom' => 'John',
'prenom' => 'Doe',
'email' => 'john@example.com',
'refsite' => $site->site_id,
'code_postal' => '54321',
'commune' => 'CommuneX',
'pays' => 250,
];
$response = $this->postJson($endpoint, $payload);
$response->assertStatus(200)
->assertJson(['Statut' => 'OK'])
->assertJsonStructure(['Statut', 'contact']);
$this->assertDatabaseHas('contact', [
'contact_siteId' => $site->site_id,
'contact_ref' => 'newref',
'contact_nom' => 'John',
]);
}
public function test_update_contact_error_missing_parameters()
{
$endpoint = '/updatecontact';
$payload = [
'nom' => 'MissingFieldsTest',
];
$response = $this->postJson($endpoint, $payload);
$response->assertStatus(200)
->assertJson([
'Statut' => 'KO',
'Code' => 101,
'Message' => 'Des paramètres de la requête sont mal renseignés ou non présents',
]);
}
public function test_update_contact_error_site_not_exists()
{
$endpoint = '/updatecontact';
$payload = [
'refadherent' => 'ref123',
'nom' => 'John',
'prenom' => 'Doe',
'email' => 'john@example.com',
'refsite' => 9999, // site ID that does not exist
'code_postal' => '54321',
'commune' => 'CommuneX',
'pays' => 250,
];
$response = $this->postJson($endpoint, $payload);
$response->assertStatus(200)
->assertJson([
'Statut' => 'KO',
'Code' => 102,
'Message' => "Le site spécifié n'existe pas",
]);
}
}