Commit 03451efa authored by Karamel's avatar Karamel
Browse files

Add response 404 for get cash session, add tests.

parent 611169e0
<?php
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Model\CashRegister;
use \Pasteque\Server\Model\CashSession;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\API\APICaller;
......@@ -19,9 +21,20 @@ use \Pasteque\Server\System\DAO\DAOCondition;
$app->GET('/api/cashsession/{cashregisterid}/{sequence}',
function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
return $response->withApiResult(APICaller::run($ptApp, 'cashSession', 'get',
$apiResp = APICaller::run($ptApp, 'cashSession', 'get',
[['cashRegister' => $args['cashregisterid'],
'sequence' => $args['sequence']]]));
'sequence' => $args['sequence']]]);
if ($apiResp->getStatus() == APIResult::STATUS_CALL_OK
&& $apiResp->getContent() == null) {
$cashRegister = CashRegister::loadFromId($args['cashregisterid'],
$ptApp->getDao());
if ($cashRegister === null) {
$e = new RecordNotFoundException(CashRegister::class,
['id' => $args['cashregisterid']]);
return $response->notFound($e, 'Cash register not found');
}
}
return $response->withApiResult($apiResp);
});
......
......@@ -140,6 +140,45 @@ class HttpCashSessionTest extends TestCase
$this->dao->close();
}
public function testGet() {
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/cashsession/%d/%d',
$this->cash->getId(), 1)));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'GET');
$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($this->cash->getId(), $jsResp['cashRegister']);
$this->assertEquals(1, $jsResp['sequence']);
}
public function testGetNone() {
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/cashsession/%d/%d',
$this->cash->getId(), 2)));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'GET');
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertEquals(null, $jsResp);
}
public function testGetNotFound() {
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/cashsession/%d/%d',
$this->cash->getId() + 1, 1)));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'GET');
$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($this->cash->getId() + 1, $jsResp['key']['id']);
}
public function testOpen() {
$structSess = $this->session->toStruct();
$structSess['openDate'] = DateUtils::toTimestamp(new \DateTime('2018-01-01 10:00'));
......
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