Commit c6fbc482 authored by Karamel's avatar Karamel
Browse files

Add PATCH route for resources, add tests.

parent c7979aac
<?php
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Model\Resource;
use \Pasteque\Server\System\API\APICaller;
......@@ -29,6 +31,30 @@ $app->POST('/api/resource',
$res));
});
$app->PATCH('/api/resource/{label}',
function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
$tab = $request->getParsedBody();
$res = Resource::load($args['label'], $ptApp->getDao());
if ($res === null) {
$e = new RecordNotFoundException(Resource::class,
['label' => $args['label']]);
return $response->notFound($e);
}
if ($tab['label'] != $args['label']) {
$otherRes = Resource::load($tab['label'], $ptApp->getDao());
if ($otherRes !== null) {
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
Resource::class, 'label',
['label' => $args['label']], $tab['label']);
return $response->reject($e);
}
}
$res->merge($tab, $ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'resource', 'write',
$res));
});
$app->DELETE('/api/resource/{label}',
function($request, $response, $args) {
$ptApp = $this->get('settings')['ptApp'];
......
......@@ -96,6 +96,87 @@ class HttpResourceTest extends TestCase
$this->assertEquals('Updated content', $dbRes->getContent());
}
public function testPatchOk() {
$res = new Resource();
$res->setLabel('Test resource');
$res->setType(Resource::TYPE_TEXT);
$res->setContent('Text content');
$this->dao->write($res);
$this->dao->commit();
$struct = $res->toStruct();
$struct['content'] = 'Updated content';
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/resource/%s',
urlencode($struct['label']))));
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));
$dbRes = $this->dao->readSnapshot(Resource::class, 'Test resource');
$this->assertNotNull($dbRes);
$this->assertEquals('Updated content', $dbRes->getContent());
}
public function testPatchLabelExisting() {
$res = new Resource();
$res->setLabel('Test resource');
$res->setType(Resource::TYPE_TEXT);
$res->setContent('Text content');
$this->dao->write($res);
$res2 = new Resource();
$res2->setLabel('Res2');
$res2->setType(Resource::TYPE_TEXT);
$res2->setContent('Content');
$this->dao->write($res2);
$this->dao->commit();
$struct = $res2->toStruct();
$struct['label'] = $res->getLabel();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/resource/%s',
urlencode($res2->getLabel()))));
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(Resource::class, $jsResp['class']);
$this->assertEquals('label', $jsResp['field']);
$this->assertEquals($res2->getLabel(), $jsResp['key']['label']);
$this->assertEquals($res->getLabel(), $jsResp['value']);
}
public function testPatchNotFound() {
$res = new Resource();
$res->setLabel('Test resource');
$res->setType(Resource::TYPE_TEXT);
$res->setContent('Text content');
$postData = $res->toStruct();
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/resource/%s',
urlencode($postData['label']))));
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postData);
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(Resource::class, $jsResp['class']);
$this->assertEquals('Test resource', $jsResp['key']['label']);
}
public function testDelete() {
$res = new Resource();
$res->setLabel('Printer.Ticket.Header');
......
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