Commit 938d7c4a authored by Karamel's avatar Karamel
Browse files

Fix importing EOS fiscal tickets. Add them in tests.

parent ca5441bc
......@@ -274,8 +274,19 @@ $app->POST('/api/fiscal/import', function($request, $response, $args) {
$tkts = [];
foreach ($data as $tkt) {
$tkt['date'] = DateUtils::readDate($tkt['date']);
unset($tkt['id']); // We don't care about id as FiscalTicketAPI checks with snapshots.
// And it messes things up with Doctrine.
if (!empty($tkt['id'])) {
// We don't care about id as FiscalTicketAPI checks with snapshots.
// And it messes things up with Doctrine
if ($tkt['id']['number'] === 0 && $tkt['number'] === 0) {
// Unless it's EOS which has to be messed up
if ($ptApp->getDao()->read(FiscalTicket::class, $tkt['id']) == null) {
// only when it already exists.
unset($tkt['id']);
}
} else {
unset($tkt['id']);
}
}
$tkts[] = FiscalTicket::fromStruct($tkt, $ptApp->getDao());
}
$apiResult = APICaller::run($ptApp, 'fiscal', 'batchImport', [$tkts]);
......
......@@ -33,6 +33,17 @@ function getBaseFiscalTicket($type, $sequence, $number, $date, $content, $signat
'signature' => $signature];
}
function createEos($lastTicket) {
$eos = new FiscalTicket();
$eos->setType($lastTicket->getType());
$eos->setSequence($lastTicket->getSequence());
$eos->setNumber(0);
$eos->setDate($lastTicket->getDate());
$eos->setContent('EOS');
$eos->sign($lastTicket);
return $eos;
}
function assertFiscalTicketModelEqModel($m1, $m2, $ut) {
$ut->assertEquals($m1->getType(), $m2->getType());
$ut->assertEquals($m1->getSequence(), $m2->getSequence());
......
......@@ -77,7 +77,8 @@ class HttpFiscalTest extends TestCase
$tkt2->setDate(new \DateTime('2018-01-01 15:02'));
$tkt2->setContent('Mewtwo');
$tkt2->sign($tkt);
$tkts = [$tkt->toStruct(), $tkt2->toStruct()];
$eos = createEos($tkt2);
$tkts = [$tkt->toStruct(), $tkt2->toStruct(), $eos->toStruct()];
// Import
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/fiscal/import'));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
......@@ -87,15 +88,18 @@ class HttpFiscalTest extends TestCase
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$result = json_decode($resp, true);
$this->assertEquals(2, count($result['successes']));
$this->assertEquals(3, count($result['successes']));
$this->assertEquals(0, count($result['failures']));
assertFiscalTicketModelEqStruct($tkt, $result['successes'][0], $this);
assertFiscalTicketModelEqStruct($tkt2, $result['successes'][1], $this);
assertFiscalTicketModelEqStruct($eos, $result['successes'][2], $this);
$snap1 = readFiscalTicketSnapshot(FiscalTicket::TYPE_TICKET, '1', 1, $this->dao);
$snap2 = readFiscalTicketSnapshot(FiscalTicket::TYPE_TICKET, '1', 2, $this->dao);
$snapEos = readFiscalTicketSnapshot(FiscalTicket::TYPE_TICKET, '1', 0, $this->dao);
assertFiscalTicketModelEqStruct($snap1, $tkts[0], $this);
assertFiscalTicketModelEqStruct($snap2, $tkts[1], $this);
$this->assertEquals(2, $this->dao->count(FiscalTicket::class));
assertFiscalTicketModelEqStruct($snapEos, $tkts[2], $this);
$this->assertEquals(3, $this->dao->count(FiscalTicket::class));
}
/** @depends testImportNew
......@@ -109,7 +113,8 @@ class HttpFiscalTest extends TestCase
$tkt->setDate(new \DateTime('2018-01-01 12:00'));
$tkt->setContent('Nippon Ichi!');
$tkt->sign(null);
$tkts = [$tkt->toStruct()];
$eos = createEos($tkt);
$tkts = [$tkt->toStruct(), $eos->toStruct()];
// Import
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/fiscal/import'));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
......@@ -119,6 +124,7 @@ class HttpFiscalTest extends TestCase
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$result = json_decode($resp, true);
$this->assertEquals(2, count($result['successes']), 'Initial import failed.');
// Create a new ticket
$tkt2 = new FiscalTicket();
$tkt2->setType(FiscalTicket::TYPE_TICKET);
......@@ -127,7 +133,9 @@ class HttpFiscalTest extends TestCase
$tkt2->setDate(new \DateTime('2018-01-01 15:02'));
$tkt2->setContent('Mewtwo');
$tkt2->sign($tkt);
$tkts[] = $tkt2->toStruct();
$eos->setDate(new \DateTime('2018-01-01 15:03'));
$eos->sign($tkt2);
$tkts = [$tkt->toStruct(), $tkt2->toStruct(), $eos->toStruct()];
// Import the whole
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/fiscal/import'));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
......@@ -137,15 +145,18 @@ class HttpFiscalTest extends TestCase
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$result = json_decode($resp, true);
$this->assertEquals(2, count($result['successes']));
$this->assertEquals(3, count($result['successes']));
$this->assertEquals(0, count($result['failures']));
assertFiscalTicketModelEqStruct($tkt, $result['successes'][0], $this);
assertFiscalTicketModelEqStruct($tkt2, $result['successes'][1], $this);
assertFiscalTicketModelEqStruct($eos, $result['successes'][2], $this);
$snap1 = readFiscalTicketSnapshot(FiscalTicket::TYPE_TICKET, '1', 1, $this->dao);
$snap2 = readFiscalTicketSnapshot(FiscalTicket::TYPE_TICKET, '1', 2, $this->dao);
$snapEos = readFiscalTicketSnapshot(FiscalTicket::TYPE_TICKET, '1', 0, $this->dao);
assertFiscalTicketModelEqStruct($snap1, $tkts[0], $this);
assertFiscalTicketModelEqStruct($snap2, $tkts[1], $this);
$this->assertEquals(2, $this->dao->count(FiscalTicket::class));
assertFiscalTicketModelEqStruct($snapEos, $tkts[2], $this);
$this->assertEquals(3, $this->dao->count(FiscalTicket::class));
}
/** @depends testImportNew */
......@@ -158,7 +169,8 @@ class HttpFiscalTest extends TestCase
$tkt->setDate(new \DateTime('2018-01-01 12:00'));
$tkt->setContent('Nippon Ichi!');
$tkt->sign(null);
$tkts = [$tkt->toStruct()];
$eos = createEos($tkt);
$tkts = [$tkt->toStruct(), $eos->toStruct()];
// Import
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/fiscal/import'));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
......@@ -168,6 +180,7 @@ class HttpFiscalTest extends TestCase
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$result = json_decode($resp, true);
$this->assertEquals(2, count($result['successes']), 'Initial import failed.');
// Update that ticket and reimport it
$tkts[0]['content'] = 'Metamorphosis';
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/fiscal/import'));
......@@ -178,7 +191,7 @@ class HttpFiscalTest extends TestCase
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$result = json_decode($resp, true);
$this->assertEquals(0, count($result['successes']));
$this->assertEquals(1, count($result['successes']));
$this->assertEquals(1, count($result['failures']));
$fail = $result['failures'][0]['ticket'];
$this->assertEquals($tkts[0]['type'], $fail['type']);
......@@ -188,7 +201,7 @@ class HttpFiscalTest extends TestCase
$this->assertEquals($tkts[0]['signature'], $fail['signature']);
$this->assertEquals('Trying to override an existing fiscal ticket.',
$result['failures'][0]['reason']);
$this->assertEquals(1, $this->dao->count(FiscalTicket::class));
$this->assertEquals(2, $this->dao->count(FiscalTicket::class));
$snap = readFiscalTicketSnapshot(FiscalTicket::TYPE_TICKET, '1', 1, $this->dao);
$this->assertEquals($tkt->getContent(), $snap->getContent());
}
......
Supports Markdown
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