Skip to content

Commit

Permalink
Corrige le model Person et son test unitaire
Browse files Browse the repository at this point in the history
  • Loading branch information
polosson committed May 5, 2021
1 parent 7329919 commit 0587c8b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
7 changes: 4 additions & 3 deletions server/src/App/Errors/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public function setPDOValidationException(QueryException $e): self
if (isDuplicateException($e)) {
$this->code = ERROR_DUPLICATE;
$offsetKeyName = strripos($details, "for key '") + strlen("for key '");
$offsetEnd = strripos($details, "' (SQL") + strlen("' (SQL");
$keyNameWithoutUnique = substr($details, $offsetKeyName, -$offsetEnd - 1);
$message = "Duplicate entry: index $keyNameWithoutUnique must be unique";
$offsetEnd = strripos($details, "' (SQL:");
$keyName = substr($details, $offsetKeyName, $offsetEnd - $offsetKeyName);
$keyNameWithoutUnique = str_replace('_UNIQUE', '', $keyName);
$message = "Duplicate entry: value for index '$keyNameWithoutUnique' must be unique";
}
$this->setValidationErrors([$message, $details]);

Expand Down
4 changes: 2 additions & 2 deletions server/src/App/Models/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function edit(?int $id = null, array $data = []): BaseModel
->setPDOValidationException($e);
}

if (preg_match('/persons\.reference/', $e->getMessage())) {
if (preg_match('/(persons\.)?reference/', $e->getMessage())) {
$i18n = new I18n(Config::getSettings('defaultLang'));
throw (new ValidationException)
->setValidationErrors([
Expand Down Expand Up @@ -240,7 +240,7 @@ protected function _setOtherTag(Model $person): void
}, $person->tags);

$diff = array_values(array_diff($defaultTags, $existingTags));
if (empty($diff)) {
if (empty($diff) || empty($diff[0])) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public function up()
'after' => 'last_name',
'null' => true,
])
->addIndex(['reference'], ['unique' => true])
->addIndex(['reference'], [
'unique' => true,
'name' => 'reference_UNIQUE',
])
->update();
}

Expand Down
12 changes: 12 additions & 0 deletions server/tests/models/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ public function testCreatePersonDuplicate(): void
$this->assertEquals('Megacity', $resultData['locality']);
}

public function testCreatePersonDuplicateRef(): void
{
$this->expectException(ValidationException::class);
$this->expectExceptionCode(ERROR_VALIDATION);
$this->model->edit(null, [
'first_name' => 'Paul',
'last_name' => 'Newtests',
'email' => 'paul@tests.new',
'reference' => '0001',
]);
}

public function testCreatePerson(): void
{
// - Test d'ajout de personne, avec n° de téléphone et tags
Expand Down

0 comments on commit 0587c8b

Please sign in to comment.