Commit 611169e0 authored by Karamel's avatar Karamel
Browse files

Add dateStart and dateStop to api/fiscal/export. Add tests.

parent b76e00c6
......@@ -2,6 +2,7 @@
use Dflydev\FigCookies\FigResponseCookies;
use Dflydev\FigCookies\SetCookie;
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Model\FiscalTicket;
use \Pasteque\Server\System\DateUtils;
use \Pasteque\Server\System\Login;
......@@ -303,8 +304,36 @@ $app->GET('/fiscal/export',
$app->GET('/api/fiscal/export',
function ($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$apiResult = APICaller::run($ptApp, 'fiscal', 'getAll', [['type', 'sequence', 'number']]);
return $response->withAPIResult($apiResult);
$queryParams = $request->getQueryParams();
$dateStart = (empty($queryParams['dateStart'])) ? null
: DateUtils::readDate($queryParams['dateStart']);
$dateStop = (empty($queryParams['dateStop'])) ? null
: DateUtils::readDate($queryParams['dateStop']);
if ($dateStart === false) {
$e = new InvalidFieldException(InvalidFieldException::CSTR_INVALID_DATE,
null, 'dateStart', null, $queryParams['dateStart']);
return $response->reject($e, 'Invalid dateStart');
}
if ($dateStop === false) {
$e = new InvalidFieldException(InvalidFieldException::CSTR_INVALID_DATE,
null, 'dateStop', null, $queryParams['dateStop']);
return $response->reject($e, 'Invalid dateStop');
}
if ($dateStart === null && $dateStop === null) {
return $response->withAPIResult(APICaller::run($ptApp, 'fiscal',
'getAll', [['type', 'sequence', 'number']]));
} else {
$conditions = [];
if ($dateStart != null) {
$conditions[] = new DAOCondition('date', '>=', $dateStart);
}
if ($dateStop != null) {
$conditions[] = new DAOCondition('date', '<=', $dateStop);
}
return $response->withAPIResult(APICaller::run($ptApp, 'fiscal',
'search', [$conditions, null, null,
['type', 'sequence', 'number']]));
}
});
function fiscal_importTickets($ptApp, $data) {
......
......@@ -99,6 +99,74 @@ class HttpFiscalTest extends TestCase
$this->assertEquals('2', $export[1]['number'], 'Invalid exported data.');
}
public function testExportDate() {
// Write a few tickets
$tkt = new FiscalTicket();
$tkt->setType(FiscalTicket::TYPE_TICKET);
$tkt->setSequence('1');
$tkt->setNumber('1');
$tkt->setDate(new \DateTime('2018-01-01 12:00'));
$tkt->setContent('Nippon Ichi!');
$tkt->sign(null);
$tkt2 = new FiscalTicket();
$tkt2->setType(FiscalTicket::TYPE_TICKET);
$tkt2->setSequence('1');
$tkt2->setNumber('2');
$tkt2->setDate(new \DateTime('2018-01-01 15:02'));
$tkt2->setContent('Mewtwo');
$tkt2->sign($tkt);
$tkt3 = new FiscalTicket();
$tkt3->setType(FiscalTicket::TYPE_TICKET);
$tkt3->setSequence('1');
$tkt3->setNumber('3');
$tkt3->setDate(new \DateTime('2018-01-01 18:05'));
$tkt3->setContent('trois');
$tkt3->sign($tkt2);
$this->dao->write($tkt);
$this->dao->write($tkt2);
$this->dao->write($tkt3);
$this->dao->commit();
// Export
$queryParams = http_build_query([
'dateStart' => '2018-01-01 15:00',
'dateStop' => '2018-01-01 16:00',
]);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/fiscal/export?%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']);
}
public function testExportDateInvalid() {
curl_setopt($this->curl, CURLOPT_URL,
apiUrl('api/fiscal/export?dateStart=notadate'));
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('notadate', $jsResp['value']);
curl_setopt($this->curl, CURLOPT_URL,
apiUrl('api/fiscal/export?dateStop=notadate'));
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('notadate', $jsResp['value']);
}
/** Test that import is not available on a regular server. */
public function testImport() {
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/fiscal/import'));
......
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