Commit 869e0a90 authored by Karamel's avatar Karamel
Browse files

Add errors responses for cash register routes, add tests.

parent 5aadabdb
<?php
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Model\CashRegister;
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
......@@ -30,7 +32,9 @@ $app->GET('/api/cashregister/getByReference/{reference}',
$args['reference']);
if ($apiRes->getStatus() == APIResult::STATUS_CALL_OK
&& $apiRes->getContent() === null) {
return $response->withStatus(404, 'Cash register not found');
$e = new RecordNotFoundException(CashRegister::class,
['reference' => $args['reference']]);
return $response->notFound($e, 'Cash register not found');
}
return $response->withApiResult($apiRes);
});
......@@ -42,7 +46,9 @@ $app->GET('/api/cashregister/getByName/{name}',
$args['name']);
if ($apiRes->getStatus() == APIResult::STATUS_CALL_OK
&& $apiRes->getContent() === null) {
return $response->withStatus(404, 'Cash register not found');
$e = new RecordNotFoundException(CashRegister::class,
['name' => $args['name']]);
return $response->notFound($e, 'Cash register not found');
}
return $response->withApiResult($apiRes);
});
......@@ -56,8 +62,14 @@ $app->GET('/api/cashregister/getByName/{name}',
$app->GET('/api/cashregister/{id}',
function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
return $response->withApiResult(APICaller::run($ptApp, 'cashRegister',
'get', $args['id']));
$apiRes = APICaller::run($ptApp, 'cashRegister', 'get', $args['id']);
if ($apiRes->getStatus() == APIResult::STATUS_CALL_OK
&& $apiRes->getContent() === null) {
$e = new RecordNotFoundException(CashRegister::class,
['id' => $args['id']]);
return $response->notFound($e, 'Cash register not found');
}
return $response->withApiResult($apiRes);
});
/** Low level call. If an id is set, it's an update. If not, it's a create. */
......@@ -69,6 +81,21 @@ $app->POST('/api/cashregister',
if ($cr == null) {
$cr = new CashRegister();
}
$otherCr = CashRegister::load($tab['reference'], $ptApp->getDao());
if ($otherCr !== null) {
if ($cr->getId() != $otherCr->getId()) {
$loadKey = null;
if (!empty($tab['id'])) {
$loadKey = ['id' => $tab['id']];
} else {
$loadKey = CashRegister::getLoadKey($tab);
}
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
CashRegister::class, 'reference',
$loadKey, $tab['reference']);
return $response->reject($e, 'Reference is already taken');
}
}
$cr->merge($tab, $ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'cashregister',
'write', $cr));
......@@ -90,9 +117,40 @@ $app->PUT('/api/cashregister/{reference}',
$tab['reference'] = $args['reference'];
$cr = CashRegister::load($tab['reference'], $ptApp->getDao());
if ($cr != null) {
return $response->withStatus(400, 'Reference is already taken');
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
CashRegister::class, 'reference',
['reference' => $tab['reference']], $tab['reference']);
return $response->reject($e, 'Reference is already taken');
}
$cr = new CashRegister();
$cr->merge($tab);
return $response->withApiResult(APICaller::run($ptApp, 'cashregister', 'write', $cr));
$cr->merge($tab, $ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'cashregister',
'write', $cr));
});
$app->PATCH('/api/cashregister/{reference}',
function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
if (!empty($tab['id'])) {
return $response->withStatus(400, 'New record cannot have an Id');
}
$cr = CashRegister::load($args['reference'], $ptApp->getDao());
if ($cr == null) {
$e = new RecordNotFoundException(CashRegister::class,
['reference' => $args['reference']]);
return $response->notFound($e, 'Cash register not found');
}
if ($tab['reference'] != $args['reference']) {
$otherCr = CashRegister::load($tab['reference'], $ptApp->getDao());
if ($otherCr != null) {
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
CashRegister::class, 'reference',
['reference' => $args['reference']], $tab['reference']);
return $response->reject($e, 'Reference is already taken');
}
}
$cr->merge($tab, $ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'cashregister',
'write', $cr));
});
......@@ -32,7 +32,7 @@ use \Pasteque\Server\System\DAO\DoctrineMainModel;
class CashRegister extends DoctrineMainModel
{
protected static function getDirectFieldNames() {
return ['id', 'reference', 'label', 'nextTicketId', 'nextSessionId'];
return ['reference', 'label', 'nextTicketId', 'nextSessionId'];
}
protected static function getAssociationFields() {
return [
......
......@@ -112,4 +112,214 @@ class HttpCashRegisterTest extends TestCase
$dbCR = $this->dao->readSnapshot(CashRegister::class, $this->cr->getId());
$this->assertEquals('Edited CR', $dbCR->getLabel());
}
public function testPostRefExisting() {
$cr = new CashRegister();
$cr->setLabel('new');
$cr->setReference($this->cr->getReference());
$struct = $cr->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/cashregister'));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('UniqueValue', $jsResp['constraint']);
$this->assertEquals(CashRegister::class, $jsResp['class']);
$this->assertEquals('reference', $jsResp['field']);
$this->assertEquals($this->cr->getReference(), $jsResp['value']);
$this->assertEquals($this->cr->getReference(),
$jsResp['key']['reference']);
}
public function testPutOk() {
$cr = new CashRegister();
$cr->setLabel('New');
$cr->setReference('New ref');
$struct = $cr->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/cashregister/%s',
urlencode($struct['reference']))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$dbCurr = $this->dao->search(CashRegister::class,
new DAOCondition('reference', '=', $struct['reference']));
$this->assertEquals(1, count($dbCurr));
}
public function testPutId() {
$cr = new CashRegister();
$cr->setLabel('New');
$cr->setReference('New ref');
$struct = $cr->toStruct();
$struct['id'] = 1;
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/cashregister/%s',
urlencode($struct['reference']))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$this->markTestIncomplete('Test response message, not available easily with curl');
}
public function testPutRefMismatch() {
$cr = new CashRegister();
$cr->setLabel('New');
$cr->setReference('New ref');
$struct = $cr->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl('api/cashregister/notNewRef'));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$dbCurr = $this->dao->search(CashRegister::class,
new DAOCondition('reference', '=', 'notNewRef'));
$this->assertEquals(1, count($dbCurr));
}
public function testPutRefExisting() {
$cr = new CashRegister();
$cr->setLabel('New');
$cr->setReference($this->cr->getReference());
$struct = $cr->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/cashregister/cr',
urlencode($this->cr->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('UniqueValue', $jsResp['constraint']);
$this->assertEquals(CashRegister::class, $jsResp['class']);
$this->assertEquals('reference', $jsResp['field']);
$this->assertEquals($this->cr->getReference(), $jsResp['value']);
$this->assertEquals($this->cr->getReference(),
$jsResp['key']['reference']);
}
public function testPatchOk() {
$struct = $this->cr->toStruct();
unset($struct['id']);
$struct['label'] = 'edited';
$struct['reference'] = 'newref';
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/cashregister/%s',
urlencode($this->cr->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('edited', $jsResp['label']);
$dbCr = $this->dao->search(CashRegister::class);
$this->assertEquals(1, count($dbCr));
$dbCr = $this->dao->readSnapshot(CashRegister::class,
$this->cr->getId());
$this->assertEquals('edited', $dbCr->getLabel());
$this->assertEquals('newref', $dbCr->getReference());
}
public function testPatchId() {
$struct = $this->cr->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/cashregister/%s',
$this->cr->getReference())));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$this->markTestIncomplete('Test response message, not available easily with curl');
}
public function testPatchRefExisting() {
$cr = new CashRegister();
$cr->setReference('ref2');
$cr->setLabel('label2');
$this->dao->write($cr);
$this->dao->commit();
$struct = $this->cr->toStruct();
unset($struct['id']);
$struct['reference'] = 'ref2';
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/cashregister/%s',
$this->cr->getReference())));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('UniqueValue', $jsResp['constraint']);
$this->assertEquals(CashRegister::class, $jsResp['class']);
$this->assertEquals('reference', $jsResp['field']);
$this->assertEquals('ref2', $jsResp['value']);
$this->assertEquals($this->cr->getReference(),
$jsResp['key']['reference']);
}
public function testPatchNotFound() {
$struct = $this->cr->toStruct();
unset($struct['id']);
$struct['reference'] = 'notFound';
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl('api/cashregister/notFound'));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(404, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('RecordNotFound', $jsResp['error']);
$this->assertEquals(CashRegister::class, $jsResp['class']);
$this->assertEquals('notFound', $jsResp['key']['reference']);
}
}
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