Commit 76f4e9b8 authored by Karamel's avatar Karamel
Browse files

Use RecordNotFound in Sync and set response to 404. Update test.

parent cf2dd492
<?php
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
......@@ -11,12 +12,19 @@ use \Pasteque\Server\System\API\APIResult;
*/
$app->GET('/api/sync/{cashregister}', function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
return $response->withApiResult(APICaller::run($ptApp,
'sync', 'syncCashRegister', $args['cashregister']));
$result = APICaller::run($ptApp, 'sync', 'syncCashRegister',
$args['cashregister']);
if ($result->getStatus() == APIResult::STATUS_CALL_REJECTED
&& is_a($result->getContent(), RecordNotFoundException::class)) {
return $response->notFound($result->getContent(),
sprintf('No cash register found with name %s',
$args['cashregister']));
}
return $response->withApiResult($result);
});
$app->GET('/api/sync', function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
return $response->withApiResult(APICaller::run($ptApp,
'sync', 'sync'));
});
\ No newline at end of file
});
......@@ -20,6 +20,7 @@
namespace Pasteque\Server\API;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Model\GenericModel;
class SyncAPI implements API {
......@@ -83,14 +84,19 @@ class SyncAPI implements API {
return $result;
}
/** Get all data for a cashregister to operate. This doesn't includes
* invisible/disabled entries. */
/**
* Get all data for a cashregister to operate. This doesn't includes
* invisible/disabled entries.
* @throws RecordNotFoundException When no cash register was found.
*/
public function syncCashRegister($cashRegisterName) {
$result = new GenericModel();
// Look for the cash register, reject if not found.
$cashRegister = $this->cashRegisterAPI->getByName($cashRegisterName);
if ($cashRegister === null) {
throw new \BadMethodCallException(sprintf('No cash register found with name %s', $cashRegisterName));
throw new RecordNotFoundException(
\Pasteque\Server\Model\CashRegister::class,
['label' => $cashRegisterName]);
}
// Send everything
$result->set('cashRegister', $cashRegister);
......
......@@ -77,7 +77,13 @@ class HttpSyncTest extends TestCase
public function testSyncWrongCashRegister() {
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/sync/NotTheCash'));
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$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(\Pasteque\Server\Model\CashRegister::class,
$jsResp['class']);
$this->assertEquals('NotTheCash', $jsResp['key']['label']);
}
public function testSyncCashRegister() {
......
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