Commit b8366278 authored by Karamel's avatar Karamel
Browse files

Add routes for TariffAreas and tests.

parent 0a964f05
<?php
use \Pasteque\Server\Model\TariffArea;
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
/**
* GET tariffareaGetAllGet
......@@ -24,3 +26,57 @@ $app->GET('/api/tariffarea/{id}', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
return $response->withApiResult(APICaller::run($ptApp, 'tariffArea', 'get', $args['id']));
});
/** Low level call. If an id is set, it's an update. If not, it's a create. */
$app->POST('/api/tariffarea', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
$ta = \Pasteque\Server\Model\TariffArea::fromStruct($tab, $ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'tariffArea', 'write', $ta));
});
/** Create a new tariff area from it's reference. Returns an error if an id is given
* or if an area already exists with the given reference. */
$app->PUT('/api/tariffarea/{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'];
$ta = \Pasteque\Server\Model\TariffArea::fromStruct($tab, $ptApp->getDao());
// Check for an existing reference
$existingTAReq = APICaller::run($ptApp, 'tariffArea', 'getByReference',
$ta->getReference());
if ($existingTAReq->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withAPIResult($existingTAReq);
}
if ($existingTAReq->getContent() != null) {
return $response->withStatus(400, 'Reference is already taken');
}
return $response->withApiResult(APICaller::run($ptApp, 'tariffArea', 'write',
$ta));
});
/** Delete an existing tariff area from it's reference. Returns an error if an id
* is given or if there aren't any area with this reference. */
$app->DELETE('/api/tariffarea/{reference}', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
if (!empty($tab['id'])) {
return $response->withStatus(400, 'Do not send Id, use reference instead.');
}
$tab['reference'] = $args['reference'];
$ta = \Pasteque\Server\Model\TariffArea::fromStruct($tab, $ptApp->getDao());
// Check for an existing reference
$existingTAReq = APICaller::run($ptApp, 'tariffArea', 'getByReference',
$ta->getReference());
if ($existingTAReq->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withAPIResult($existingTAReq);
}
if ($existingTAReq->getContent() == null) {
return $response->withStatus(404, 'No tariff area found.');
}
return $response->withApiResult(APICaller::run($ptApp, 'tariffArea', 'delete',
$existingTAReq->getContent()));
});
......@@ -27,7 +27,7 @@ use \Pasteque\Server\Model\TariffAreaPrice;
use \Pasteque\Server\System\DAO\DAOCondition;
/** CRUD API for TariffArea (attention: lowercase a in name). */
class TariffareaAPI extends APIHelper
class TariffareaAPI extends APIRefHelper
{
const MODEL_NAME = 'Pasteque\Server\Model\TariffArea';
const DEFAULT_ORDER = 'dispOrder';
......
<?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\Category;
use \Pasteque\Server\Model\Product;
use \Pasteque\Server\Model\TariffArea;
use \Pasteque\Server\Model\TariffAreaPrice;
use \Pasteque\Server\Model\Tax;
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 HttpTariffAreaTest extends TestCase
{
private $curl;
private static $token;
private $dao;
private $cat;
private $tax;
private $prd;
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->cat = new Category();
$this->cat->setReference('category');
$this->cat->setLabel('Category');
$this->dao->write($this->cat);
$this->tax= new Tax();
$this->tax->setLabel('VAT');
$this->tax->setRate(0.1);
$this->dao->write($this->tax);
$this->prd = new Product();
$this->prd->setReference('product');
$this->prd->setLabel('Product');
$this->prd->setCategory($this->cat);
$this->prd->setTax($this->tax);
$this->prd->setPriceSell(1.0);
$this->dao->write($this->prd);
$this->dao->commit();
}
protected function tearDown() {
curl_close($this->curl);
foreach ([TariffArea::class, Product::class, Tax::class,
Category::class] as $class) {
$all = $this->dao->search($class);
foreach($all as $record) {
$this->dao->delete($record);
}
}
$this->dao->commit();
$this->dao->close();
}
public function testPutOk() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Tariff area');
$taPrice = new TariffAreaPrice();
$taPrice->setPrice(0.5);
$taPrice->setProduct($this->prd);
$ta->addPrice($taPrice);
$struct = $ta->toStruct();
unset($struct['id']);
unset($struct['prices'][0]['id']);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/tariffarea/%s', urlencode($ta->getReference()))));
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
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));
$dbTA = $this->dao->search(TariffArea::class,
new DAOCondition('reference', '=', $ta->getReference()));
$this->assertEquals(1, count($dbTA));
}
public function testPutId() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Tariff area');
$taPrice = new TariffAreaPrice();
$taPrice->setPrice(0.5);
$taPrice->setProduct($this->prd);
$ta->addPrice($taPrice);
$struct = $ta->toStruct();
$struct['id'] = 1;
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/tariffarea/%s', urlencode($ta->getReference()))));
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$this->markTestIncomplete('Test response message, not available easily with curl');
}
public function testPutRefMismatch() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Tariff area');
$taPrice = new TariffAreaPrice();
$taPrice->setPrice(0.5);
$taPrice->setProduct($this->prd);
$ta->addPrice($taPrice);
$struct = $ta->toStruct();
unset($struct['id']);
unset($struct['prices'][0]['id']);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/tariffarea/notRef'));
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
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));
$dbTA = $this->dao->search(TariffArea::class,
new DAOCondition('reference', '=', 'notRef'));
$this->assertEquals(1, count($dbTA));
}
public function testPutRefExisting() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Tariff area');
$taPrice = new TariffAreaPrice();
$taPrice->setPrice(0.5);
$taPrice->setProduct($this->prd);
$ta->addPrice($taPrice);
$this->dao->write($ta);
$this->dao->commit();
$ta2 = new TariffArea();
$ta2->setReference('ta');
$ta2->setLabel('Other area');
$struct = $ta2->toStruct();
unset($struct['id']);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/tariffarea/%s', urlencode($ta2->getReference()))));
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$this->markTestIncomplete('Test response message, not available easily with curl');
}
public function testDelete() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Tariff area');
$taPrice = new TariffAreaPrice();
$taPrice->setPrice(0.5);
$taPrice->setProduct($this->prd);
$ta->addPrice($taPrice);
$this->dao->write($ta);
$this->dao->commit();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/tariffarea/%s', urlencode($ta->getReference()))));
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));
$dbTA = $this->dao->search(TariffArea::class);
$this->assertEquals(0, count($dbTA));
}
}
......@@ -43,6 +43,7 @@
<file>http/HttpImageTest.php</file>
<file>http/HttpCategoryTest.php</file>
<file>http/HttpProductTest.php</file>
<file>http/HttpTariffAreaTest.php</file>
<file>http/HttpPlaceTest.php</file>
<file>http/HttpPaymentModeTest.php</file>
<file>http/HttpUserTest.php</file>
......
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