Commit 846aa286 authored by Karamel's avatar Karamel
Browse files

Cash register route with tests.

parent 4a134629
......@@ -53,3 +53,34 @@ $app->GET('/api/cashregister/{id}', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
return $response->withApiResult(APICaller::run($ptApp, 'cashRegister', 'get', $args['id']));
});
/** Low level call. If an id is set, it's an update. If not, it's a create. */
$app->POST('/api/cashregister', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
$cr = \Pasteque\Server\Model\CashRegister::fromStruct($tab, $ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'cashregister', 'write', $cr));
});
/** Create a new cash register from it's reference. The reference is read from url
* and ignored from data.
* Returns an error if an id is given or if a cash register already exists
* with the given reference. */
$app->PUT('/api/cashregister/{reference}', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
if (!empty($tab['id'])) {
return $response->withStatus(400, 'New record cannot have an Id');
}
$tab['reference'] = $args['reference'];
$cr = \Pasteque\Server\Model\CashRegister::fromStruct($tab, $ptApp->getDao());
// Check for an existing reference
$existingCrReq = APICaller::run($ptApp, 'cashregister', 'getByReference', $cr->getReference());
if ($existingCrReq->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withAPIResult($existingCrReq);
}
if ($existingCrReq->getContent() != null) {
return $response->withStatus(400, 'Reference is already taken');
}
return $response->withApiResult(APICaller::run($ptApp, 'cashregister', 'write', $cr));
});
<?php
// Pasteque server testing
//
// Copyright (C)
// 2012 Scil (http://scil.coop)
// 2017 Karamel, Association Pastèque (karamel@creativekara.fr, https://pasteque.org)
//
// This file is part of Pasteque.
//
// Pasteque is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pasteque is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server;
use \Pasteque\Server\Model\CashRegister;
use \Pasteque\Server\System\Login;
use \Pasteque\Server\System\DAO\DAOCondition;
use \Pasteque\Server\System\DAO\DAOFactory;
use \PHPUnit\Framework\TestCase;
require_once(dirname(dirname(__FILE__)) . "/common_load.php");
class HttpCashRegisterTest extends TestCase
{
private $curl;
private static $token;
private $dao;
private $cr;
public static function setUpBeforeClass() {
static::$token = obtainToken();
}
public static function tearDownAfterClass() {
}
protected function setUp() {
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token]);
global $dbInfo;
$this->dao = DAOFactory::getDAO($dbInfo, ['debug' => true]);
$this->cr = new CashRegister();
$this->cr->setReference('cr');
$this->cr->setLabel('CashRegister');
$this->dao->write($this->cr);
$this->dao->commit();
$this->dao = DAOFactory::getDAO($dbInfo, ['debug' => true]);
}
protected function tearDown() {
curl_close($this->curl);
$all = $this->dao->search(CashRegister::class);
foreach($all as $record) {
$this->dao->delete($record);
}
$this->dao->commit();
$this->dao->close();
}
public function testGetAll() {
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/cashregister/getAll'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'GET');
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$data = json_decode($resp, true);
$this->assertEquals(1, count($data));
$this->assertEquals($this->cr->getReference(), $data[0]['reference']);
$this->assertEquals($this->cr->getLabel(), $data[0]['label']);
}
public function testPostNew() {
$newCr = new CashRegister();
$newCr->setReference('New CR');
$newCr->setLabel('New Cash Register');
$postData = $newCr->toStruct();
unset($postData['id']);
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/cashregister'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$dbUser = $this->dao->search(CashRegister::class,
new DAOCondition('reference', '=', 'New CR'));
$this->assertEquals(1, count($dbUser));
}
public function testPostUpdate() {
$this->cr->setLabel('Edited CR');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/cashregister'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($this->cr->toStruct()));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$this->assertEquals(1, $this->dao->count(CashRegister::class));
$dbCR = $this->dao->readSnapshot(CashRegister::class, $this->cr->getId());
$this->assertEquals('Edited CR', $dbCR->getLabel());
}
}
......@@ -46,6 +46,7 @@
<file>http/HttpPaymentModeTest.php</file>
<file>http/HttpUserTest.php</file>
<file>http/HttpRoleTest.php</file>
<file>http/HttpCashRegisterTest.php</file>
</testsuite>
</testsuites>
</phpunit>
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