Commit f8a46351 authored by Karamel's avatar Karamel
Browse files

Add GET /api/category/getChildren/{ref}, add errors and tests for category routes.

parent 8fda1524
<?php
use Pasteque\Server\Exception\InvalidFieldException;
use Pasteque\Server\Exception\RecordNotFoundException;
use Pasteque\Server\Model\Category;
use Pasteque\Server\System\API\APICaller;
use Pasteque\Server\System\API\APIResult;
......@@ -31,10 +34,26 @@ $app->GET('/api/category/getChildrens',
$queryParams = $request->getQueryParams();
$parentId = $queryParams['parentId'];
$ptApp = $this->get('settings')['ptApp'];
$cat = Category::loadFromId($parentId);
if ($cat === null) {
$e = new RecordNotFoundException(Category::class, ['id' => $parentId]);
return $response->notFound($e);
}
return $response->withApiResult(APICaller::run($ptApp, 'category',
'getChildren',[$parentId]));
});
$app->GET('/api/category/getChildren/{reference}',
function($request, $response, $args) {
$cat = Category::load($args['reference']);
if ($cat === null) {
$e = new RecordNotFoundException(Category::class, ['id' => $parentId]);
return $response->notFound($e);
}
return $response->withAPIResult(APICaller::run($ptApp, 'category',
'getChildren', [$cat->getId()]));
});
/**
* GET categoryId
* Summary:
......@@ -97,7 +116,11 @@ $app->PUT('/api/category',
$response->withStatus(400, 'Reference is already taken');
}
$cat = new Category();
$cat->merge($tab);
try {
$cat->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
return $response->withApiResult(APICaller::run($ptApp, 'category', 'write',
$category));
});
......@@ -107,11 +130,29 @@ $app->POST('/api/category',
function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
$cat = Category::load($tab['reference'], $ptApp->getDao());
$cat = Category::loadFromId($tab['id'], $ptApp->getDao());
if ($cat == null) {
$cat = new Category();
}
$cat->merge($tab);
$otherCat = Category::load($tab['reference'], $ptApp->getDao());
if ($otherCat !== null) {
if ($cat->getId() != $otherCat->getId()) {
$loadKey = null;
if (!empty($tab['id'])) {
$loadKey = ['id' => $tab['id']];
} else {
$loadKey = Category::getLoadKey($tab);
}
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
Category::class, 'reference', $loadKey, $tab['reference']);
return $response->reject($e, 'Reference is already taken');
}
}
try {
$cat->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
return $response->withApiResult(APICaller::run($ptApp, 'category', 'write',
$cat));
});
......@@ -132,10 +173,17 @@ $app->PUT('/api/category/{reference}',
$tab['reference'] = $args['reference'];
$cat = Category::load($tab['reference'], $ptApp->getDao());
if ($cat != null) {
return $response->withStatus(400, 'Reference is already taken');
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
Category::class, 'reference',
['reference' => $args['reference']], $args['reference']);
return $response->reject($e, 'Reference is already taken');
}
$cat = new Category();
$cat->merge($tab, $ptApp->getDao());
try {
$cat->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
return $response->withApiResult(APICaller::run($ptApp, 'category', 'write',
$cat));
});
......@@ -144,7 +192,7 @@ $app->PUT('/api/category/{reference}',
* Update an existing category from it's reference. Returns an error if an id
* is given or if there aren't any category with this reference.
*/
$app->PATCH('api/category/{reference}',
$app->PATCH('/api/category/{reference}',
function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
......@@ -153,9 +201,15 @@ $app->PATCH('api/category/{reference}',
}
$cat = Category::load($args['reference'], $ptApp->getDao());
if ($cat == null) {
return $response->withStatus(404, 'No category found.');
$e = new RecordNotFoundException(Category::class,
['reference' => $args['reference']]);
return $response->notFound($e, 'No category found.');
}
try {
$cat->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
$cat->merge($tab);
return $response->withApiResult(APICaller::run($ptApp, 'category', 'write',
$category));
$cat));
});
......@@ -69,13 +69,101 @@ class HttpCategoryTest extends TestCase
$this->dao->close();
}
public function testPostNew() {
$cat = new Category();
$cat->setReference('ref');
$cat->setLabel('category');
$struct = $cat->toStruct();
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/category'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
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));
$dbCat = $this->dao->search(Category::class,
new DAOCondition('reference', '=', $cat->getReference()));
$this->assertEquals(1, count($dbCat));
}
public function testPostUpdate() {
$cat = new Category();
$cat->setReference('ref');
$cat->setLabel('category');
$struct = $cat->toStruct();
$struct['id'] = $this->cat->getId();
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/category'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
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));
$dbCat = $this->dao->readSnapshot(Category::class, $this->cat->getId());
$this->assertNotNull($dbCat);
$this->assertEquals('ref', $dbCat->getReference());
$this->assertEquals('category', $dbCat->getLabel());
}
public function testPostRefExisting() {
$cat = new Category();
$cat->setReference($this->cat->getReference());
$cat->setLabel('category');
$struct = $cat->toStruct();
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/category'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
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));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('UniqueValue', $jsResp['constraint']);
$this->assertEquals(Category::class, $jsResp['class']);
$this->assertEquals('reference', $jsResp['field']);
$this->assertEquals($cat->getReference(), $jsResp['value']);
$this->assertEquals($cat->getReference(), $jsResp['key']['reference']);
}
public function testPostInvalidParent() {
$cat = new Category();
$cat->setReference($this->cat->getReference());
$cat->setLabel('category');
$struct = $cat->toStruct();
$struct['id'] = $this->cat->getId();
$struct['parent'] = $this->cat->getId() + 1;
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/category'));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
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));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('AssociationNotFound', $jsResp['constraint']);
$this->assertEquals(Category::class, $jsResp['class']);
$this->assertEquals('parent', $jsResp['field']);
$this->assertEquals($this->cat->getId() + 1, $jsResp['value']);
$this->assertEquals($cat->getReference(), $jsResp['key']['reference']);
}
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()))));
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()));
......@@ -133,7 +221,8 @@ class HttpCategoryTest extends TestCase
$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()))));
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()));
......@@ -142,7 +231,149 @@ class HttpCategoryTest extends TestCase
'Content-Type: application/json']);
$resp = curl_exec($this->curl);
$this->assertEquals(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('UniqueValue', $jsResp['constraint']);
$this->assertEquals(Category::class, $jsResp['class']);
$this->assertEquals('reference', $jsResp['field']);
$this->assertEquals($cat->getReference(), $jsResp['value']);
$this->assertEquals($cat->getReference(), $jsResp['key']['reference']);
}
public function testPutInvalidParent() {
$cat = new Category();
$cat->setReference('New ref');
$cat->setLabel('category');
$struct = $cat->toStruct();
$struct['parent'] = $this->cat->getId() + 1;
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/category/%s',
urlencode($struct['reference']))));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
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));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('AssociationNotFound', $jsResp['constraint']);
$this->assertEquals(Category::class, $jsResp['class']);
$this->assertEquals('parent', $jsResp['field']);
$this->assertEquals($this->cat->getId() + 1, $jsResp['value']);
$this->assertEquals($cat->getReference(), $jsResp['key']['reference']);
}
public function testPatchOk() {
$cat = new Category();
$cat->setReference($this->cat->getReference());
$cat->setLabel('New label');
$struct = $cat->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/category/%s',
urlencode($struct['reference']))));
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));
$dbCat = $this->dao->readSnapshot(Category::class, $this->cat->getId());
$this->assertNotNull($dbCat);
$this->assertEquals('New label', $dbCat->getLabel());
}
public function testPatchId() {
$cat = new Category();
$cat->setReference($this->cat->getReference());
$cat->setLabel('New label');
$struct = $cat->toStruct();
$struct['id'] = 1;
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/category/%s',
urlencode($struct['reference']))));
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(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$this->markTestIncomplete('Test response message, not available easily with curl');
}
public function testPatchRefMismatch() {
$cat = new Category();
$cat->setReference('New ref');
$cat->setLabel('New label');
$struct = $cat->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
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($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));
$dbCat = $this->dao->readSnapshot(Category::class, $this->cat->getId());
$this->assertEquals('New ref', $dbCat->getReference());
}
public function testPatchNotFound() {
$cat = new Category();
$cat->setReference('Reference');
$cat->setLabel('New label');
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/category/notfound'));
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(404, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('RecordNotFound', $jsResp['error']);
$this->assertEquals(Category::class, $jsResp['class']);
$this->assertEquals('notfound', $jsResp['key']['reference']);
}
public function testPatchInvalidParent() {
$cat = new Category();
$cat->setReference($this->cat->getReference());
$cat->setLabel('category');
$struct = $cat->toStruct();
$struct['parent'] = $this->cat->getId() + 1;
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/category/%s',
urlencode($struct['reference']))));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
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));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('AssociationNotFound', $jsResp['constraint']);
$this->assertEquals(Category::class, $jsResp['class']);
$this->assertEquals('parent', $jsResp['field']);
$this->assertEquals($this->cat->getId() + 1, $jsResp['value']);
$this->assertEquals($cat->getReference(), $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