Commit 1a9aef25 authored by Karamel's avatar Karamel
Browse files

Fixes to some product routes, add some tests.

parent 96cd8a41
......@@ -121,7 +121,7 @@ $app->PUT('/api/product/{reference}', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
if (!empty($tab['id'])) {
return $response->withResponse(400, 'New record cannot have an Id');
return $response->withStatus(400, 'New record cannot have an Id');
}
$tab['reference'] = $args['reference'];
$product = \Pasteque\Server\Model\Product::fromStruct($tab,$ptApp->getDao());
......@@ -129,10 +129,10 @@ $app->PUT('/api/product/{reference}', function($request, $response, $args) {
$existingPrdReq = APICaller::run($ptApp, 'product', 'getByReference',
$product->getReference());
if ($existingPrdReq->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withResponse($existingPrdReq);
return $response->withAPIResult($existingPrdReq);
}
if ($existingPrdReq->getContent() != null) {
return $response->withResponse(400, 'Reference is already taken');
return $response->withStatus(400, 'Reference is already taken');
}
return $response->withApiResult(APICaller::run($ptApp, 'product', 'write',
$product));
......@@ -144,7 +144,7 @@ $app->PATCH('/api/product/{reference}', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
if (!empty($tab['id'])) {
return $response->withResponse(400, 'Do not send Id, use reference instead.');
return $response->withStatus(400, 'Do not send Id, use reference instead.');
}
$tab['reference'] = $args['reference'];
$product = \Pasteque\Server\Model\Product::fromStruct($tab,$ptApp->getDao());
......@@ -152,13 +152,13 @@ $app->PATCH('/api/product/{reference}', function($request, $response, $args) {
$existingPrdReq = APICaller::run($ptApp, 'product', 'getByReference',
$product->getReference());
if ($existingPrdReq->getStatus() != APIResult::STATUS_CALL_OK) {
return $response->withResponse($existingPrdReq);
return $response->withAPIResult($existingPrdReq);
}
if ($existingPrdReq->getContent() == null) {
return $response->withResponse(404, 'No product found.');
return $response->withStatus(404, 'No product found.');
}
if ($existingPrdReq->getContent()->getId() != $product->getId()) {
return $response->withResponse(500, 'Id mismatch.');
return $response->withStatus(500, 'Id mismatch.');
}
return $response->withApiResult(APICaller::run($ptApp, 'product', 'write', $product));
});
<?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\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 HttpProductTest extends TestCase
{
private $curl;
private static $token;
private $dao;
private $cat;
private $tax;
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->dao->commit();
}
protected function tearDown() {
curl_close($this->curl);
foreach ([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() {
$prd = new Product();
$prd->setCategory($this->cat);
$prd->setTax($this->tax);
$prd->setReference('ref');
$prd->setLabel('label');
$prd->setPriceSell(10);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/product/%s', urlencode($prd->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($prd->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));
$dbPrd = $this->dao->search(Product::class,
new DAOCondition('reference', '=', $prd->getReference()));
$this->assertEquals(1, count($dbPrd));
}
public function testPutId() {
$prd = new Product();
$prd->setCategory($this->cat);
$prd->setTax($this->tax);
$prd->setReference('ref');
$prd->setLabel('label');
$prd->setPriceSell(10);
$json = $prd->toStruct();
$json['id'] = 1;
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/product/%s', urlencode($prd->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() {
$prd = new Product();
$prd->setCategory($this->cat);
$prd->setTax($this->tax);
$prd->setReference('ref');
$prd->setLabel('label');
$prd->setPriceSell(10);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/product/notRef'));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($prd->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));
$dbPrd = $this->dao->search(Product::class,
new DAOCondition('reference', '=', 'notRef'));
$this->assertEquals(1, count($dbPrd));
}
public function testPutRefExisting() {
$prd = new Product();
$prd->setCategory($this->cat);
$prd->setTax($this->tax);
$prd->setReference('ref');
$prd->setLabel('label');
$prd->setPriceSell(10);
$this->dao->write($prd);
$this->dao->commit();
$prd2 = new Product();
$prd2->setCategory($this->cat);
$prd2->setTax($this->tax);
$prd2->setReference('ref');
$prd2->setLabel('other label');
$prd2->setPriceSell(10);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/product/%s', urlencode($prd2->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($prd2->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');
}
}
......@@ -37,6 +37,7 @@
<file>http/HttpCustomerTest.php</file>
<file>http/HttpImageTest.php</file>
<file>http/HttpCategoryTest.php</file>
<file>http/HttpProductTest.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