Commit 7c30ae4a authored by Karamel's avatar Karamel
Browse files

PaymentMode routes and tests.

parent 2f9576b8
<?php
// Nothing here right now
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
$app->GET('/api/paymentmode/getAll', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
return $response->withApiResult(APICaller::run($ptApp, 'paymentmode','getAll'));
});
$app->GET('/api/paymentmode/{id}', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
return $response->withApiResult(APICaller::run($ptApp, 'paymentmode','get',$args));
});
/** Low level call. If an id is set, it's an update. If not, it's a create. */
$app->POST('/api/paymentmode', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
$pm = \Pasteque\Server\Model\PaymentMode::fromStruct($tab,$ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'paymentmode', 'write', $pm));
});
/** Create a new paymentMode 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 payment mode already exists
* with the given reference. */
$app->PUT('/api/paymentmode/{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'];
$pm = \Pasteque\Server\Model\PaymentMode::fromStruct($tab, $ptApp->getDao());
// Check for an existing reference
$existingPMReq = APICaller::run($ptApp, 'paymentmode', 'getByReference', $pm->getReference());
if ($existingPMReq->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withAPIResult($existingPMReq);
}
if ($existingPMReq->getContent() != null) {
return $response->withStatus(400, 'Reference is already taken');
}
return $response->withApiResult(APICaller::run($ptApp, 'paymentmode', 'write', $pm));
});
/** Update an existing paymentMode from it's reference. Returns an error if an id
* is given or if there aren't any payment mode with this reference. */
$app->PATCH('api/paymentmode/{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'];
$pm = \Pasteque\Server\Model\PaymentMode::fromStruct($tab, $ptApp->getDao());
// Check for an existing reference
$existingPMReq = APICaller::run($ptApp, 'paymentmode', 'getByReference', $pm->getReference());
if ($existingPMReq->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withAPIResult($existingPMReq);
}
if ($existingPMReq->getContent() == null) {
return $response->withStatus(404, 'No payment mode found.');
}
if ($existingPMReq->getContent()->getId() != $pm->getId()) {
return $response->withStatus(500, 'Id mismatch.');
}
return $response->withApiResult(APICaller::run($ptApp, 'paymentmode', 'write', $pm));
});
......@@ -28,7 +28,7 @@ use \Pasteque\Server\Model\PaymentModeValue;
use \Pasteque\Server\System\DAO\DAOCondition;
/** CRUD API for PaymentMode (attention: lowercase m in name). */
class PaymentmodeAPI extends APIHelper
class PaymentmodeAPI extends APIRefHelper
{
const MODEL_NAME = 'Pasteque\Server\Model\PaymentMode';
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\PaymentMode;
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 HttpPaymentModeTest extends TestCase
{
private $curl;
private static $token;
private $dao;
private $pm;
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->pm = new PaymentMode();
$this->pm->setReference('Reference');
$this->pm->setLabel('Label');
$this->dao->write($this->pm);
$this->dao->commit();
}
protected function tearDown() {
curl_close($this->curl);
foreach ([PaymentMode::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() {
$pm = new PaymentMode();
$pm->setReference('New ref');
$pm->setLabel('New label');
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%s', urlencode($pm->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($pm->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));
$dbPM = $this->dao->search(PaymentMode::class,
new DAOCondition('reference', '=', $pm->getReference()));
$this->assertEquals(1, count($dbPM));
}
public function testPutId() {
$pm = new PaymentMode();
$pm->setReference('New ref');
$pm->setLabel('New label');
$json = $pm->toStruct();
$json['id'] = 1;
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%s', urlencode($pm->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($json));
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() {
$pm = new PaymentMode();
$pm->setReference('New ref');
$pm->setLabel('New label');
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/paymentmode/notNewRef'));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($pm->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));
$dbPM = $this->dao->search(PaymentMode::class,
new DAOCondition('reference', '=', 'notNewRef'));
$this->assertEquals(1, count($dbPM));
}
public function testPutRefExisting() {
$pm = new PaymentMode();
$pm->setReference('Reference');
$pm->setLabel('New label');
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%s', urlencode($this->pm->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($pm->toStruct()));
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');
}
}
......@@ -43,6 +43,7 @@
<file>http/HttpCategoryTest.php</file>
<file>http/HttpProductTest.php</file>
<file>http/HttpPlaceTest.php</file>
<file>http/HttpPaymentModeTest.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