feat(commercial) : validation back de la relation + suivi de revue MR (ERP-119)
- validation serveur « relation choisie => FK obligatoire » : champ transitoire relationType (non persiste) + Assert\Callback portant la 422 sur distributor / broker, que le back ne pouvait pas deriver des seules FK nullable - mutualisation des payloads d'ecriture clients : new.vue consomme buildMainPayload / buildAddressPayload / buildRibPayload (fin de la duplication create/edit) - COMMENT ON TABLE client_address : ajout des types Courtier / Distributeur (catalogue + migration Version20260609120000) - tests : violationsByPath remonte dans AbstractCommercialApiTestCase (fin des copies inline) + couverture de la nouvelle RG relation
This commit is contained in:
@@ -137,6 +137,27 @@ abstract class AbstractCommercialApiTestCase extends AbstractApiTestCase
|
||||
return $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indexe les violations d'un corps de reponse 422 par propertyPath. Permet
|
||||
* d'asserter qu'un 422 porte bien sur le champ attendu (et n'est pas un 422
|
||||
* orthogonal) : un test qui se contente du code 422 passerait meme si la RG
|
||||
* visee etait cassee pour une autre raison. Mutualise ici (et non dans la
|
||||
* sous-classe Supplier) pour etre accessible a tous les tests Commercial.
|
||||
*
|
||||
* @param array<string, mixed> $body corps decode de la reponse (toArray(false))
|
||||
*
|
||||
* @return array<string, string> propertyPath => message
|
||||
*/
|
||||
protected function violationsByPath(array $body): array
|
||||
{
|
||||
$byPath = [];
|
||||
foreach ($body['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
|
||||
return $byPath;
|
||||
}
|
||||
|
||||
private function cleanupCommercialTestData(): void
|
||||
{
|
||||
$em = $this->getEm();
|
||||
|
||||
@@ -298,26 +298,6 @@ abstract class AbstractSupplierApiTestCase extends AbstractCommercialApiTestCase
|
||||
return $this->referential(Bank::class, $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indexe les violations d'un corps de reponse 422 par propertyPath. Permet
|
||||
* d'asserter qu'un 422 porte bien sur le champ attendu (et n'est pas un 422
|
||||
* orthogonal) : un test qui se contente du code 422 passerait meme si la RG
|
||||
* visee etait cassee pour une autre raison.
|
||||
*
|
||||
* @param array<string, mixed> $body corps decode de la reponse (toArray(false))
|
||||
*
|
||||
* @return array<string, string> propertyPath => message
|
||||
*/
|
||||
protected function violationsByPath(array $body): array
|
||||
{
|
||||
$byPath = [];
|
||||
foreach ($body['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
|
||||
return $byPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recupere un referentiel comptable seede (CommercialReferentialFixtures) par
|
||||
* code. Echoue explicitement si absent (fixtures non chargees).
|
||||
|
||||
@@ -241,10 +241,7 @@ final class ClientAddressTest extends AbstractCommercialApiTestCase
|
||||
])->toArray(false);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = [];
|
||||
foreach ($body['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
$byPath = $this->violationsByPath($body);
|
||||
self::assertArrayHasKey('billingEmailSecondary', $byPath);
|
||||
}
|
||||
|
||||
@@ -402,10 +399,7 @@ final class ClientAddressTest extends AbstractCommercialApiTestCase
|
||||
])->toArray(false);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = [];
|
||||
foreach ($body['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
$byPath = $this->violationsByPath($body);
|
||||
self::assertArrayHasKey('isProspect', $byPath);
|
||||
self::assertSame('Le type d\'adresse est obligatoire.', $byPath['isProspect']);
|
||||
}
|
||||
@@ -484,10 +478,7 @@ final class ClientAddressTest extends AbstractCommercialApiTestCase
|
||||
])->toArray(false);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = [];
|
||||
foreach ($body['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
$byPath = $this->violationsByPath($body);
|
||||
self::assertArrayHasKey('isProspect', $byPath);
|
||||
self::assertSame('Une adresse Courtier ne peut pas avoir d\'autre type.', $byPath['isProspect']);
|
||||
}
|
||||
|
||||
@@ -85,4 +85,77 @@ final class ClientFormulaireMainTest extends AbstractCommercialApiTestCase
|
||||
self::assertNotNull($persisted);
|
||||
self::assertSame('LEGACY FIELDS SARL', $persisted->getCompanyName());
|
||||
}
|
||||
|
||||
/**
|
||||
* RG-1.03 bis : declarer une relation « depend d'un distributeur »
|
||||
* (relationType, champ transitoire) sans renseigner la FK distributor doit
|
||||
* produire une 422 portee sur `distributor`. Le back ne peut pas deviner
|
||||
* l'intention depuis la seule FK nullable (distributor=null = client
|
||||
* independant), d'ou relationType qui la transporte.
|
||||
*/
|
||||
public function testRelationDistributeurSansDistributeurEst422(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$cat = $this->createCategory('SECTEUR');
|
||||
|
||||
$body = $client->request('POST', '/api/clients', [
|
||||
'headers' => ['Content-Type' => self::LD],
|
||||
'json' => [
|
||||
'companyName' => 'Relation Sans Distrib SARL',
|
||||
'categories' => ['/api/categories/'.$cat->getId()],
|
||||
'relationType' => 'distributeur',
|
||||
],
|
||||
])->toArray(false);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = $this->violationsByPath($body);
|
||||
self::assertArrayHasKey('distributor', $byPath);
|
||||
self::assertSame('Le nom du distributeur est obligatoire.', $byPath['distributor']);
|
||||
}
|
||||
|
||||
/** Idem courtier : relationType=courtier sans broker -> 422 portee sur `broker`. */
|
||||
public function testRelationCourtierSansCourtierEst422(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$cat = $this->createCategory('SECTEUR');
|
||||
|
||||
$body = $client->request('POST', '/api/clients', [
|
||||
'headers' => ['Content-Type' => self::LD],
|
||||
'json' => [
|
||||
'companyName' => 'Relation Sans Courtier SARL',
|
||||
'categories' => ['/api/categories/'.$cat->getId()],
|
||||
'relationType' => 'courtier',
|
||||
],
|
||||
])->toArray(false);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = $this->violationsByPath($body);
|
||||
self::assertArrayHasKey('broker', $byPath);
|
||||
self::assertSame('Le nom du courtier est obligatoire.', $byPath['broker']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Le champ transitoire relationType ne casse pas la creation nominale : avec
|
||||
* la FK correspondante renseignee, le client se cree (201) et relationType
|
||||
* n'est jamais serialise en sortie (write-only, aucun groupe de lecture).
|
||||
*/
|
||||
public function testRelationDistributeurAvecDistributeurEst201(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$cat = $this->createCategory('SECTEUR');
|
||||
$distributor = $this->seedClient('Distrib Cible', false, 'DISTRIBUTEUR');
|
||||
|
||||
$data = $client->request('POST', '/api/clients', [
|
||||
'headers' => ['Content-Type' => self::LD],
|
||||
'json' => [
|
||||
'companyName' => 'Relation Ok SARL',
|
||||
'categories' => ['/api/categories/'.$cat->getId()],
|
||||
'relationType' => 'distributeur',
|
||||
'distributor' => '/api/clients/'.$distributor->getId(),
|
||||
],
|
||||
])->toArray();
|
||||
|
||||
self::assertResponseStatusCodeSame(201);
|
||||
self::assertArrayNotHasKey('relationType', $data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,10 +89,7 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = [];
|
||||
foreach ($response->toArray(false)['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
$byPath = $this->violationsByPath($response->toArray(false));
|
||||
|
||||
self::assertArrayHasKey('email', $byPath, 'La violation email doit porter propertyPath=email (mapping front).');
|
||||
self::assertSame('L\'adresse email n\'est pas valide.', $byPath['email']);
|
||||
@@ -135,10 +132,7 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = [];
|
||||
foreach ($response->toArray(false)['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
$byPath = $this->violationsByPath($response->toArray(false));
|
||||
self::assertArrayHasKey('email', $byPath);
|
||||
self::assertSame('L\'adresse email n\'est pas valide.', $byPath['email']);
|
||||
}
|
||||
@@ -386,10 +380,7 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = [];
|
||||
foreach ($response->toArray(false)['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
$byPath = $this->violationsByPath($response->toArray(false));
|
||||
|
||||
self::assertArrayHasKey('bic', $byPath, 'Le mismatch pays BIC/IBAN doit porter propertyPath=bic (mapping front).');
|
||||
self::assertSame('Le BIC ne correspond pas au pays de l\'IBAN.', $byPath['bic']);
|
||||
|
||||
Reference in New Issue
Block a user