Commit 350fe5b7 authored by Karamel's avatar Karamel
Browse files

Add UnicityException in APIRefHelper.

parent a0f0f227
......@@ -22,6 +22,7 @@
namespace Pasteque\Server\API;
use \Pasteque\Server\Exception\UnicityException;
use \Pasteque\Server\System\DAO\DAOCondition;
/** CRUD API for models with reference field is used as user-friendly id. */
......@@ -54,6 +55,47 @@ abstract class APIRefHelper extends APIHelper
return parent::write(($arrayArgs) ? $data : $data[0]);
}
public function write($data) {
try {
return parent::write($data);
} catch (\Exception $e) {
// Look for duplicated references
if (!is_array($data)) {
$ref = $data->getReference();
$refData = $this->search(new DAOCondition('reference', '=',
$ref), 1, 0) ;
if (count($refData) == 1) {
throw new UnicityException(static::MODEL_NAME,
'reference', $ref);
}
} else {
$refs = [];
foreach ($data as $d) {
$ref = $d->getReference();
if (array_search($ref, $refs) !== false) {
throw new UnicityException(static::MODEL_NAME,
'reference', $ref);
} else {
$refs[] = $ref;
}
}
$refData = $this->search(new DAOCondition('reference',
'in', $refs));
$foundRefs = [];
foreach ($refData as $rd) {
$ref = $rd->getReference();
if (array_key_exists($ref, $foundRefs)) {
throw new UnicityException(static::MODEL_NAME,
'reference', $ref);
} else {
$foundRefs[$ref] = true;
}
}
} // Otherwise not a duplicated reference error
throw $e;
}
}
/** Get a single entry from it's reference. */
public function getByReference($reference) {
$data = $this->dao->search(static::MODEL_NAME, new DAOCondition('reference', '=', $reference));
......
......@@ -22,6 +22,7 @@
namespace Pasteque\Server;
use \Pasteque\Server\API\APIRefHelper;
use \Pasteque\Server\Exception\UnicityException;
use \Pasteque\Server\Model\Category;
use \Pasteque\Server\System\DAO\DAOFactory;
use \PHPUnit\Framework\TestCase;
......@@ -71,6 +72,43 @@ class APIRefHelperTest extends TestCase
$this->assertEquals($cat2->getLabel(), $read->getLabel());
}
public function testUnicityErrorInData() {
$cat = new Category();
$cat->setReference('A');
$cat->setLabel('u');
$cat2 = new Category();
$cat2->setReference('A');
$cat2->setLabel('w');
$exceptionThrown = false;
try {
$this->api->write([$cat, $cat2]);
} catch (UnicityException $e) {
$exceptionThrown = true;
$this->assertEquals('reference', $e->getField());
$this->assertEquals('A', $e->getValue());
}
$this->assertTrue($exceptionThrown, 'Expecting UnicityException.');
}
public function testUnicityErrorPreviousData() {
$cat = new Category();
$cat->setReference('A');
$cat->setLabel('u');
$this->api->write($cat);
$cat2 = new Category();
$cat2->setReference('A');
$cat2->setLabel('w');
$exceptionThrown = false;
try {
$this->api->write($cat2);
} catch (UnicityException $e) {
$exceptionThrown = true;
$this->assertEquals('reference', $e->getField());
$this->assertEquals('A', $e->getValue());
}
$this->assertTrue($exceptionThrown, 'Expecting UnicityException.');
}
public function testGetByReference() {
$cat = new Category();
$cat->setReference('A');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment