Commit 8bb51023 authored by Karamel's avatar Karamel
Browse files

Add error responses, parameters and tests for ticket searches.

parent 81372b25
<?php
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Exception\PastequeException;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Model\CashRegister;
use \Pasteque\Server\Model\CashSession;
use \Pasteque\Server\Model\Customer;
use \Pasteque\Server\Model\Ticket;
use \Pasteque\Server\Model\User;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
......@@ -13,7 +19,9 @@ $app->GET('/api/ticket/{cashregister}/{number}',
$ptApp = $this->get('settings')['ptApp'];
$cashRegister = CashRegister::loadFromId($args['cashregister']);
if ($cashRegister === null) {
return $response->withStatus(404, 'Cash register not found');
$e = new RecordNotFoundException(CashRegister::class,
['id' => $args['cashregister']]);
return $response->notFound($e, 'Cash register not found');
}
$apiRes = APICaller::run($ptApp, 'ticket', 'search',
[new DAOCondition('cashRegister', '=', $cashRegister),
......@@ -23,7 +31,10 @@ $app->GET('/api/ticket/{cashregister}/{number}',
if (count($ticket) > 0) {
return $response->withAPIResult(APIResult::success($ticket[0]));
} else {
$response->withStatus(404, 'Ticket not found');
$e = new RecordNotFoundException(Ticket::class,
['cashRegister' => $args['cashRegister'],
'number' => $args['number']]);
$response->notFound($e, 'Ticket not found');
}
}
return $response->withApiResult($apiRes);
......@@ -37,9 +48,9 @@ $app->GET('/api/ticket/search',
$cashRegisterId = (empty($queryParams['cashRegister'])) ? null
: $queryParams['cashRegister'];
$dateStart = (empty($queryParams['dateStart'])) ? null
: DateUtils::readDate(intval($queryParams['dateStart']));
: DateUtils::readDate($queryParams['dateStart']);
$dateStop = (empty($queryParams['dateStop'])) ? null
: DateUtils::readDate(intval($queryParams['dateStop']));
: DateUtils::readDate($queryParams['dateStop']);
$userId = (empty($queryParams['user'])) ? null
: $queryParams['user'];
$customerId = (empty($queryParams['customer'])) ? null
......@@ -56,31 +67,40 @@ $app->GET('/api/ticket/search',
}
// Search criterias
if ($dateStart === false) {
return $response->withApiResult(APIResult::reject('Invalid dateStart'));
$e = new InvalidFieldException(InvalidFieldException::CSTR_INVALID_DATE,
null, 'dateStart', null, $queryParams['dateStart']);
return $response->reject($e, 'Invalid dateStart');
}
if ($dateStop === false) {
return $response->withApiResult(APIResult::reject('Invalid dateStop'));
$e = new InvalidFieldException(InvalidFieldException::CSTR_INVALID_DATE,
null, 'dateStop', null, $queryParams['dateStop']);
return $response->reject($e, 'Invalid dateStop');
}
$cashRegister = null;
if ($cashRegisterId !== null) {
$cashRegister = CashRegister::loadFromId($cashRegisterId,
$ptApp->getDao());
if ($cashRegister === null) {
return $response->withStatus(404, 'Cash register not found');
$e = new RecordNotFoundException(CashRegister::class,
['id' => $cashRegisterId]);
return $response->notFound($e, 'Cash register not found');
}
}
$customer = null;
if ($customerId !== null) {
$customer = Customer::loadFromId($customerId, $ptApp->getDao());
if ($customer === null) {
return $response->withStatus(404, 'Customer not found');
$e = new RecordNotFoundException(Customer::class,
['id' => $customerId]);
return $response->notFound($e, 'Customer not found');
}
}
$user = null;
if ($userId !== null) {
$user = User::loadFromId($userId, $ptApp->getDao());
if ($user === null) {
return $response->withStatus(404, 'User not found');
$e = new RecordNotFoundException(User::class, ['id' => $userId]);
return $response->notFound($e, 'User not found');
}
}
$conditions = [];
......@@ -88,10 +108,10 @@ $app->GET('/api/ticket/search',
$conditions[] = new DAOCondition('cashRegister', '=', $cashRegister);
}
if ($dateStart !== null) {
$conditions[] = new DAOCondition('date', '>', $dateStart);
$conditions[] = new DAOCondition('date', '>=', $dateStart);
}
if ($dateStop !== null) {
$conditions[] = new DAOCondition('date', '<', $dateStop);
$conditions[] = new DAOCondition('date', '<=', $dateStop);
}
if ($user !== null) {
$conditions[] = new DAOCondition('user', '=', $user);
......@@ -112,6 +132,17 @@ $app->GET('/api/ticket/search',
$app->GET('/api/ticket/session/{cashregister}/{sequence}',
function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$queryParams = $request->getQueryParams();
$offset = (empty($queryParams['offset'])) ? null
: intval($queryParams['offset']);
if ($offset === 0) {
$offset = null;
}
$limit = (empty($queryParams['limit'])) ? null
: intval($queryParams['limit']);
if ($limit === 0) {
$limit = null;
}
$cashRes = APICaller::run($ptApp, 'cashSession', 'get',
[['cashRegister' => $args['cashregister'],
'sequence' => $args['sequence']]]);
......@@ -120,12 +151,29 @@ $app->GET('/api/ticket/session/{cashregister}/{sequence}',
}
$session = $cashRes->getContent();
if ($session === null) {
return $response->withStatus(404, 'Cash session not found');
$cashRegister = CashRegister::loadFromId($args['cashregister'],
$ptApp->getDao());
if ($cashRegister == null) {
$e = new RecordNotFoundException(CashRegister::class,
['id' => $args['cashregister']]);
return $response->notFound($e, 'Cash register not found');
}
$e = new RecordNotFoundException(CashSession::class,
['cashRegister' => $args['cashregister'],
'sequence' => $args['sequence']]);
return $response->notFound($e, 'Cash session not found');
}
$conditions = [
new DAOCondition('cashRegister', '=', $session->getCashRegister()),
new DAOCondition('sequence', '=', $session->getSequence())
];
if (!empty($queryParams['count'])) {
return $response->withAPIResult(APICaller::run($ptApp, 'ticket',
'count', [$conditions]));
} else {
return $response->withAPIResult(APICaller::run($ptApp, 'ticket',
'search', [$conditions], $limit, $offset));
}
$apiRes = APICaller::run($ptApp, 'ticket', 'search',
[[new DAOCondition('cashRegister', '=', $session->getCashRegister()),
new DAOCondition('sequence', '=', $session->getSequence())]]);
return $response->withApiResult($apiRes);
});
/**
......
......@@ -163,6 +163,231 @@ class HttpTicketTest extends TestCase
assertTicketModelEqStruct($model, $struct, $this);
}
public function testSearch() {
$tktStruct = $this->getBaseTicket(1);
$tktStruct['date'] = DateUtils::readDate('2018-01-02 10:00');
$tkt = new Ticket();
$tkt->merge($tktStruct, $this->dao);
$this->dao->write($tkt);
$tkt2Struct = $this->getBaseTicket(2);
$tkt2Struct['date'] = DateUtils::readDate('2018-01-03 11:00');
$tkt2 = new Ticket();
$tkt2->merge($tkt2Struct, $this->dao);
$this->dao->write($tkt2);
$this->dao->commit();
$queryParams = http_build_query([
'cashRegister' => $this->cash->getId(),
'dateStart' => '2018-01-03',
'dateStop' => '2018-01-04',
]);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/search?%s', $queryParams)));
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(1, count($jsResp));
$this->assertEquals(2, $jsResp[0]['number']);
$queryParams = http_build_query([
'cashRegister' => $this->cash->getId(),
'offset' => 1
]);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/search?%s', $queryParams)));
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(1, count($jsResp));
$this->assertEquals(1, $jsResp[0]['number']);
}
public function testSearchCount() {
$tktStruct = $this->getBaseTicket(1);
$tktStruct['date'] = DateUtils::readDate('2018-01-02 10:00');
$tkt = new Ticket();
$tkt->merge($tktStruct, $this->dao);
$this->dao->write($tkt);
$tkt2Struct = $this->getBaseTicket(2);
$tkt2Struct['date'] = DateUtils::readDate('2018-01-03 11:00');
$tkt2 = new Ticket();
$tkt2->merge($tkt2Struct, $this->dao);
$this->dao->write($tkt2, $this->dao);
$this->dao->write($tkt2);
$this->dao->commit();
$queryParams = http_build_query([
'cashRegister' => $this->cash->getId(),
'dateStart' => '2018-01-03',
'dateStop' => '2018-01-04',
'count' => true
]);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/search?%s', $queryParams)));
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(1, $jsResp);
}
public function testSearchInvalid() {
$queryParams = http_build_query(['dateStart' => 'notadate']);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/search?%s', $queryParams)));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'GET');
$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('InvalidDate', $jsResp['constraint']);
$this->assertEquals('dateStart', $jsResp['field']);
$this->assertEquals(null, $jsResp['key']);
$this->assertEquals('notadate', $jsResp['value']);
$queryParams = http_build_query(['dateStop' => 'notadate']);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/search?%s', $queryParams)));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'GET');
$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('InvalidDate', $jsResp['constraint']);
$this->assertEquals('dateStop', $jsResp['field']);
$this->assertEquals(null, $jsResp['key']);
$this->assertEquals('notadate', $jsResp['value']);
}
public function testSearchNotFound() {
$queryParams = http_build_query(
['cashRegister' => $this->cash->getId() + 1]);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/search?%s', $queryParams)));
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']);
$queryParams = http_build_query(
['customer' => 1]);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/search?%s', $queryParams)));
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(Customer::class, $jsResp['class']);
$this->assertEquals(1, $jsResp['key']['id']);
$queryParams = http_build_query(
['user' => $this->user->getId() + 1]);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/search?%s', $queryParams)));
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(User::class, $jsResp['class']);
$this->assertEquals($this->user->getId() + 1, $jsResp['key']['id']);
}
public function testSearchSession() {
$tktStruct = $this->getBaseTicket(1);
$tktStruct['date'] = DateUtils::readDate('2018-01-02 8:00');
$tkt = new Ticket();
$tkt->merge($tktStruct, $this->dao);
$this->dao->write($tkt);
$session = new CashSession();
$session->setCashRegister($this->cash);
$session->setSequence(2);
$session->setOpenDate(new \DateTime('2018-01-03 8:00'));
$this->dao->write($session);
$tkt2Struct = $this->getBaseTicket(2);
$tkt2Struct['date'] = DateUtils::readDate($tkt2Struct['date']);
$tkt2Struct['sequence'] = 2;
$tkt2 = new Ticket();
$tkt2->merge($tkt2Struct, $this->dao);
$this->dao->write($tkt2);
$this->dao->commit();
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/session/%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->assertNotEquals(false, $jsResp);
$this->assertEquals(1, count($jsResp));
$this->assertEquals(2, $jsResp[0]['number']);
}
public function testSearchSessionCount() {
$tktStruct = $this->getBaseTicket(1);
$tktStruct['date'] = DateUtils::readDate('2018-01-02 8:00');
$tkt = new Ticket();
$tkt->merge($tktStruct, $this->dao);
$this->dao->write($tkt);
$session = new CashSession();
$session->setCashRegister($this->cash);
$session->setSequence(2);
$session->setOpenDate(new \DateTime('2018-01-03 8:00'));
$this->dao->write($session);
$tkt2Struct = $this->getBaseTicket(2);
$tkt2Struct['date'] = DateUtils::readDate($tkt2Struct['date']);
$tkt2Struct['sequence'] = 2;
$tkt2 = new Ticket();
$tkt2->merge($tkt2Struct, $this->dao);
$this->dao->write($tkt2);
$this->dao->commit();
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/session/%d/%d?count=1',
$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->assertNotEquals(false, $jsResp);
$this->assertEquals(1, $jsResp);
}
public function testSearchSessionNotFound() {
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/session/%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']);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/ticket/session/%d/%d',
$this->cash->getId(), 3)));
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(CashSession::class, $jsResp['class']);
$this->assertEquals($this->cash->getId(),
$jsResp['key']['cashRegister']);
$this->assertEquals(3, $jsResp['key']['sequence']);
}
public function testEmpty() {
$input = [];
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
......
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