Commit c8fa4bd9 authored by Karamel's avatar Karamel
Browse files

Add http tests for PUT api/category/{reference}.

parent a9e03de8
......@@ -106,8 +106,10 @@ $app->POST('/api/category', function($request, $response, $args) {
$category = \Pasteque\Server\Model\Category::fromStruct($tab,$ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'category', 'write', $category));
});
/** Create a new category from it's reference. Returns an error if an id is given
* or if a category already exists with the given reference. */
/** Create a new category 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 category already exists
* with the given reference. */
$app->PUT('/api/category/{reference}', function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
......
<?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\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 HttpCategoryTest extends TestCase
{
private $curl;
private static $token;
private $dao;
private $cat;
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('Reference');
$this->cat->setLabel('Label');
$this->dao->write($this->cat);
$this->dao->commit();
}
protected function tearDown() {
curl_close($this->curl);
foreach ([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() {
$cat = new Category();
$cat->setReference('New ref');
$cat->setLabel('New label');
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/category/%s', urlencode($cat->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($cat->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));
$dbCat = $this->dao->search(Category::class,
new DAOCondition('reference', '=', $cat->getReference()));
$this->assertEquals(1, count($dbCat));
}
public function testPutId() {
$cat = new Category();
$cat->setReference('New ref');
$cat->setLabel('New label');
$json = $cat->toStruct();
$json['id'] = 1;
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/category/%s', urlencode($cat->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() {
$cat = new Category();
$cat->setReference('New ref');
$cat->setLabel('New label');
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/category/notNewRef'));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($cat->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));
$dbCat = $this->dao->search(Category::class,
new DAOCondition('reference', '=', 'notNewRef'));
$this->assertEquals(1, count($dbCat));
}
public function testPutRefExisting() {
$cat = new Category();
$cat->setReference('Reference');
$cat->setLabel('New label');
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/category/%s', urlencode($this->cat->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($cat->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');
}
}
......@@ -36,6 +36,7 @@
<file>http/HttpTicketTest.php</file>
<file>http/HttpCustomerTest.php</file>
<file>http/HttpImageTest.php</file>
<file>http/HttpCategoryTest.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