Commit 5aadabdb authored by Karamel's avatar Karamel
Browse files

Add response 400 for TariffArea, PATCH route and some tests.

parent 345441fb
<?php
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Model\TariffArea;
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
......@@ -35,14 +37,29 @@ $app->POST('/api/tariffarea',
function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
$ta = null;
if (!empty($tab['id'])) {
$ta = TariffArea::loadFromId($tab['id'], $ptApp->getDao());
}
if ($ta === null) {
$ta = TariffArea::loadFromId($tab['id'], $ptApp->getDao());
if ($ta == null) {
$ta = new TariffArea();
}
$ta->merge($tab, $ptApp->getDao());
$otherTa = TariffArea::load($tab['reference'], $ptApp->getDao());
if ($otherTa !== null) {
if ($ta->getId() != $otherTa->getId()) {
$loadKey = null;
if (!empty($tab['id'])) {
$loadKey = ['id' => $tab['id']];
} else {
$loadKey = TariffArea::getLoadKey($tab);
}
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
Currency::class, 'reference', $loadKey, $tab['reference']);
return $response->reject($e, 'Reference is already taken');
}
}
try {
$ta->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
return $response->withApiResult(APICaller::run($ptApp, 'tariffArea',
'write', $ta));
});
......@@ -61,10 +78,48 @@ $app->PUT('/api/tariffarea/{reference}',
$tab['reference'] = $args['reference'];
$ta = TariffArea::load($tab['reference'], $ptApp->getDao());
if ($ta !== null) {
return $response->withStatus(400, 'Reference is already taken');
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
TariffArea::class, 'reference',
['reference' => $tab['reference']], $tab['reference']);
return $response->reject($e, 'Reference is already taken');
}
$ta = new TariffArea();
$ta->merge($tab, $ptApp->getDao());
try {
$ta->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
return $response->withApiResult(APICaller::run($ptApp, 'tariffArea',
'write', $ta));
});
$app->PATCH('/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');
}
$ta = TariffArea::load($args['reference'], $ptApp->getDao());
if ($ta === null) {
$e = new RecordNotFoundException(TariffArea::class,
['reference' => $args['reference']]);
return $response->notFound($e, 'Tariff area not found');
}
if ($tab['reference'] != $args['reference']) {
$otherTa = TariffArea::load($tab['reference']);
if ($otherTa !== null) {
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
TariffArea::class, 'reference',
['reference' => $args['reference']], $tab['reference']);
return $response->reject($e);
}
}
try {
$ta->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
return $response->withApiResult(APICaller::run($ptApp, 'tariffArea',
'write', $ta));
});
......@@ -76,14 +131,11 @@ $app->PUT('/api/tariffarea/{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 = TariffArea::load($tab['reference'], $ptApp->getDao());
$ta = TariffArea::load($args['reference'], $ptApp->getDao());
if ($ta === null) {
return $response->withStatus(404, 'No tariff area found.');
$e = new RecordNotFoundException(TariffArea::class,
['reference' => $args['reference']]);
return $response->notFound($e, 'No tariff area found.');
}
return $response->withApiResult(APICaller::run($ptApp, 'tariffArea',
'delete', $ta->getId()));
......
......@@ -21,6 +21,8 @@
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server;
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Model\Category;
use \Pasteque\Server\Model\Product;
use \Pasteque\Server\Model\TariffArea;
......@@ -193,7 +195,33 @@ class HttpTariffAreaTest extends TestCase
$this->markTestIncomplete('Test response message, not available easily with curl');
}
public function testUpdatePrices() {
public function testPostNew() {
$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, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/tariffarea'));
curl_setopt($this->curl, CURLOPT_POST, true);
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', '=', $struct['reference']));
$this->assertEquals(1, count($dbTa));
}
public function testPostUpdate() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Tariff area');
......@@ -208,7 +236,9 @@ class HttpTariffAreaTest extends TestCase
$this->dao->write($ta);
$this->dao->commit();
$struct = $ta->toStruct();
$struct['label'] = 'Edited';
array_splice($struct['prices'], 0, 1);
$struct['prices'][0]['price'] = 0.1;
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/tariffarea'));
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($struct));
......@@ -219,10 +249,106 @@ class HttpTariffAreaTest extends TestCase
$this->assertEquals(200, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$read = $this->dao->readSnapshot(TariffArea::class, $ta->getId());
$this->assertEquals(1, count($read->getPrices()));
$this->assertEquals(0.2, $read->getPrices()->get(0)->getPrice());
$this->assertEquals('Edited', $read->getLabel());
$this->assertEquals(0.1, $read->getPrices()->get(0)->getPrice());
}
public function testPatchOk() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Tariff area');
$taPrice = new TariffAreaPrice();
$taPrice->setPrice(0.5);
$taPrice->setProduct($this->prd);
$ta->addPrice($taPrice);
$taPrice2 = new TariffAreaPrice();
$taPrice2->setPrice(0.2);
$taPrice2->setProduct($this->prd2);
$ta->addPrice($taPrice2);
$this->dao->write($ta);
$this->dao->commit();
$struct = $ta->toStruct();
$struct['label'] = 'Edited';
array_splice($struct['prices'], 0, 1);
$struct['prices'][0]['price'] = 0.1;
unset($struct['id']);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/tariffarea/%s',
urlencode($struct['reference']))));
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));
$read = $this->dao->readSnapshot(TariffArea::class, $ta->getId());
$this->assertEquals(1, count($read->getPrices()));
$this->assertEquals('Edited', $read->getLabel());
$this->assertEquals(0.1, $read->getPrices()->get(0)->getPrice());
}
public function testPatchId() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Tariff area');
$taPrice = new TariffAreaPrice();
$taPrice->setPrice(0.5);
$taPrice->setProduct($this->prd);
$ta->addPrice($taPrice);
$taPrice2 = new TariffAreaPrice();
$taPrice2->setPrice(0.2);
$taPrice2->setProduct($this->prd2);
$ta->addPrice($taPrice2);
$this->dao->write($ta);
$this->dao->commit();
$struct = $ta->toStruct();
$struct['label'] = 'Edited';
array_splice($struct['prices'], 0, 1);
$struct['prices'][0]['price'] = 0.1;
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/tariffarea/%s',
urlencode($struct['reference']))));
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 testPatchNotFound() {
$ta = new TariffArea();
$ta->setReference('ta');
$ta->setLabel('Tariff area');
$taPrice = new TariffAreaPrice();
$taPrice->setPrice(0.5);
$taPrice->setProduct($this->prd);
$ta->addPrice($taPrice);
$taPrice2 = new TariffAreaPrice();
$taPrice2->setPrice(0.2);
$taPrice2->setProduct($this->prd2);
$ta->addPrice($taPrice2);
$struct = $ta->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/tariffarea/nope'));
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(404, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('RecordNotFound', $jsResp['error']);
$this->assertEquals(TariffArea::class, $jsResp['class']);
$this->assertEquals('nope', $jsResp['key']['reference']);
}
public function testDelete() {
$ta = new TariffArea();
$ta->setReference('ta');
......@@ -244,4 +370,19 @@ class HttpTariffAreaTest extends TestCase
$dbTA = $this->dao->search(TariffArea::class);
$this->assertEquals(0, count($dbTA));
}
public function testDeleteNotFound() {
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/tariffarea/nothing'));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
[Login::TOKEN_HEADER . ': ' . static::$token,
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(404, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('RecordNotFound', $jsResp['error']);
$this->assertEquals(TariffArea::class, $jsResp['class']);
$this->assertEquals('nothing', $jsResp['key']['reference']);
}
}
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