Commit c7979aac authored by Karamel's avatar Karamel
Browse files

Add errors in payment mode routes, fix embedded association merging.

parent 02cee256
<?php
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Model\PaymentMode;
use \Pasteque\Server\System\API\APICaller;
use \Pasteque\Server\System\API\APIResult;
......@@ -26,12 +28,31 @@ $app->POST('/api/paymentmode',
$tab = $request->getParsedBody();
$pm = null;
if (!empty($tab['id'])) {
$pm = PaymentMode::loadFromId($tab['id']);
$pm = PaymentMode::loadFromId($tab['id'], $ptApp->getDao());
}
if ($pm === null) {
$pm = new PaymentMode;
if ($pm == null) {
$pm = new PaymentMode();
}
$otherPm = PaymentMode::load($tab['reference'], $ptApp->getDao());
if ($otherPm !== null) {
if ($pm->getId() != $otherPm->getId()) {
$loadKey = null;
if (!empty($tab['id'])) {
$loadKey = ['id' => $tab['id']];
} else {
$loadKey = PaymentMode::getLoadKey($tab);
}
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
PaymentMode::class, 'reference',
$loadKey, $tab['reference']);
return $response->reject($e, 'Reference is already taken');
}
}
try {
$pm->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
$pm->merge($tab, $ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'paymentmode',
'write', $pm));
});
......@@ -50,12 +71,20 @@ $app->PUT('/api/paymentmode/{reference}',
return $response->withStatus(400, 'New record cannot have an Id');
}
$tab['reference'] = $args['reference'];
$pm = PaymentMode::load($tab['reference'], $ptApp->getDao());
$pm = PaymentMode::load($args['reference'], $ptApp->getDao());
if ($pm !== null) {
return $response->withStatus(400, 'Reference is already taken');
$loadKey = ['reference' => $args['reference']];
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
PaymentMode::class, 'reference',
$loadKey, $tab['reference']);
return $response->reject($e, 'Reference is already taken');
}
$pm = new PaymentMode();
$pm->merge($tab, $ptApp->getDao());
try {
$pm->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
return $response->withApiResult(APICaller::run($ptApp, 'paymentmode',
'write', $pm));
});
......@@ -64,19 +93,33 @@ $app->PUT('/api/paymentmode/{reference}',
* Update an existing paymentMode from it's reference. Returns an error if an
* id is given or if there aren't any payment mode with this reference.
*/
$app->PATCH('api/paymentmode/{reference}',
$app->PATCH('/api/paymentmode/{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'];
$pm = PaymentMode::load($tab['reference'], $ptApp->getDao());
$loadKey = ['reference' => $args['reference']];
$pm = PaymentMode::load($loadKey, $ptApp->getDao());
if ($pm === null) {
return $response->withStatus(404, 'No payment mode found.');
$e = new RecordNotFoundException(PaymentMode::class, $loadKey);
return $response->notFound($e, 'No payment mode found.');
}
if ($tab['reference'] !== $args['reference']) {
$otherPm = PaymentMode::structLoad($tab, $ptApp->getDao());
if ($otherPm !== null) {
$e = new InvalidFieldException(InvalidFieldException::CSTR_UNIQUE,
PaymentMode::class, 'reference', $loadKey,
$tab['reference']);
return $response->reject($e, 'Reference is already taken');
}
}
try {
$pm->merge($tab, $ptApp->getDao());
} catch (InvalidFieldException $e) {
return $response->reject($e);
}
$pm->merge($tab, $ptApp->getDao());
return $response->withApiResult(APICaller::run($ptApp, 'paymentmode',
'write', $pm));
});
......@@ -129,8 +129,8 @@ class PaymentmodeAPI extends APIRefHelper
$qValImg->setParameter($i + 3, Image::getPMVModelId($keptValues[$i]));
}
} else {
$qVal = $em->createQuery($dql);
$qValImg = $em->createQuery($dql);
$qVal = $em->createQuery($dqlVal);
$qValImg = $em->createQuery($dqlValImg);
}
$qVal->setParameter(1, $d);
$qVal->execute();
......
......@@ -79,6 +79,12 @@ class InvalidFieldException extends \Exception implements PastequeException
return $this->id;
}
public function setId($id) {
if ($this->id == null) {
$this->id = $id;
}
}
public function getValue() {
return $this->value;
}
......@@ -100,10 +106,14 @@ class InvalidFieldException extends \Exception implements PastequeException
}
public function toStruct() {
$class = $this->class;
if ($class[0] == '\\') {
$class = substr($class, 1);
}
return [
'error' => 'InvalidField',
'constraint' => $this->constraint,
'class' => $this->class,
'class' => $class,
'field' => $this->field,
'key' => $this->getJsonableId(),
'value' => $this->getJsonableValue(),
......
......@@ -52,9 +52,13 @@ class RecordNotFoundException extends \Exception implements PastequeException
}
public function toStruct() {
$class = $this->class;
if ($class[0] == '\\') {
$class = substr($class, 1);
}
return [
'error' => 'RecordNotFound',
'class' => $this->class,
'class' => $class,
'key' => $this->getJsonableId(),
];
}
......
......@@ -21,7 +21,7 @@
namespace Pasteque\Server\System\DAO;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Exception\InvalidFieldException;
/**
* Parent class of all main models (non-embedded) to use with Doctrine.
......@@ -134,4 +134,13 @@ abstract class DoctrineMainModel extends DoctrineModel
public static function structLoad($struct, $dao) {
return static::load(static::getLoadKey($struct), $dao);
}
public function merge($struct, $dao) {
try {
parent::merge($struct, $dao);
} catch (InvalidFieldException $e) {
$e->setId(static::getLoadKey($struct));
throw $e;
}
}
}
......@@ -21,7 +21,7 @@
namespace Pasteque\Server\System\DAO;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Exception\InvalidFieldException;
/** Parent class of all models to use with Doctrine. */
abstract class DoctrineModel
......@@ -188,8 +188,9 @@ abstract class DoctrineModel
* @param $struct The associative array of values to merge.
* @param $dao The dao to use to look for associations.
* @throws InvalidFieldException When trying to assign a null value
* to a non-null association field (constraint NOT_NULL).
* @throws RecordNotFoundException When an association value was not found.
* to a non-null association field (constraint NOT_NULL) or when an
* association was not found (constraint ASSOCIATION_NOT_FOUND). The key
* is not set, subclasses should set it.
*/
public function merge($struct, $dao) {
// Merge direct fields.
......@@ -310,7 +311,7 @@ abstract class DoctrineModel
* getAssociationFields).
* @return The associated record or an array of associated records.
* Embedded records are already merged.
* @throws RecordNotFoundException when an associated record cannot be
* @throws InvalidFieldException when an associated record cannot be
* found with the given id (only for non-embedded records).
*/
protected function readAssociationValue($struct, $dao, $field) {
......@@ -325,10 +326,12 @@ abstract class DoctrineModel
} else {
$load = new \ReflectionMethod($field['class'],
'loadFromId');
$subrecord = $load->invoke(null, $content['id'], $dao);
$subrecord = $load->invoke(null, $content, $dao);
if ($submodel === null) {
throw new RecordNotFoundException($field['class'],
$struct[$field['name']]);
throw new InvalidFieldException(
InvalidFieldException::CSTR_ASSOCIATION_NOT_FOUND,
$field['class'], $field['name'],
null, $content);
}
}
$subrecords->add($subrecord);
......@@ -344,8 +347,10 @@ abstract class DoctrineModel
$load = new \ReflectionMethod($field['class'], 'loadFromId');
$subrecord = $load->invoke(null, $struct[$field['name']], $dao);
if ($subrecord === null) {
throw new RecordNotFoundException($field['class'],
$struct[$field['name']]);
throw new InvalidFieldException(
InvalidFieldException::CSTR_ASSOCIATION_NOT_FOUND,
$field['class'], $field['name'],
null, $struct[$field['name']]);
}
}
return $subrecord;
......
......@@ -22,7 +22,7 @@
namespace Pasteque\Server;
use \Pasteque\Server\AppContext;
use \Pasteque\Server\Exception\RecordNotFoundException;
use \Pasteque\Server\Exception\InvalidFieldException;
use \Pasteque\Server\Model\Category;
use \Pasteque\Server\System\DAO\DAOCondition;
use \Pasteque\Server\System\DAO\DBException;
......@@ -489,13 +489,17 @@ class DoctrineDAOTest extends TestCase
try {
$cat = Category::load($a->getReference(), $dao);
$cat->merge($struct, $dao);
} catch (RecordNotFoundException $e) {
} catch (InvalidFieldException $e) {
$exceptionThrown = true;
$this->assertEquals(
InvalidFieldException::CSTR_ASSOCIATION_NOT_FOUND,
$e->getConstraint());
$this->assertEquals(static::MODEL_NAME, $e->getClass());
$this->assertEquals($a->getId() + 2, $e->getId());
$this->assertEquals('parent', $e->getField());
$this->assertEquals($a->getReference(), $e->getId()['reference']);
$this->assertEquals($a->getId() + 2, $e->getValue());
}
$this->assertTrue($exceptionThrown,
'Expecting RecordNotFoundException.');
$this->assertTrue($exceptionThrown);
}
/** Check if the model created is linked to Doctrine
......
......@@ -69,13 +69,103 @@ class HttpPaymentModeTest extends TestCase
$this->dao->close();
}
public function testPostNew() {
$pm = new PaymentMode();
$pm->merge(['reference' => 'New ref', 'label' => 'New label'],
$this->dao);
$struct = $pm->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/paymentmode'));
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));
$dbPm = $this->dao->search(PaymentMode::class,
new DAOCondition('reference', '=', $struct['reference']));
$this->assertEquals(1, count($dbPm));
}
public function testPostEdit() {
$this->pm->merge(['reference' => 'New ref', 'label' => 'New label'],
$this->dao);
$struct = $this->pm->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/paymentmode'));
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));
$dbPm = $this->dao->search(PaymentMode::class);
$this->assertEquals(1, count($dbPm));
$this->assertEquals('New ref', $dbPm[0]->getReference());
$this->assertEquals('New label', $dbPm[0]->getLabel());
}
public function testPostRefExisting() {
$pm = new PaymentMode();
$pm->merge(['reference' => 'Reference', 'label' => 'New label'],
$this->dao);
$struct = $pm->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/paymentmode'));
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));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('UniqueValue', $jsResp['constraint']);
$this->assertEquals(PaymentMode::class, $jsResp['class']);
$this->assertEquals('reference', $jsResp['field']);
$this->assertEquals('Reference', $jsResp['value']);
$this->assertEquals('Reference', $jsResp['key']['reference']);
}
public function testPostWrongReturn() {
$pm = new PaymentMode();
$pm->merge(['reference' => 'New ref', 'label' => 'New label'],
$this->dao);
$struct = $pm->toStruct();
$struct['returns'][] = ['minAmount' => 0,
'returnMode' => $this->pm->getId() + 1];
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($this->curl, CURLOPT_URL, apiUrl('api/paymentmode'));
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));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('AssociationNotFound', $jsResp['constraint']);
$this->assertEquals(PaymentMode::class, $jsResp['class']);
$this->assertEquals('returnMode', $jsResp['field']);
$this->assertEquals($this->pm->getId() + 1, $jsResp['value']);
$this->assertEquals('New ref', $jsResp['key']['reference']);
}
public function testPutOk() {
$pm = new PaymentMode();
$pm->setReference('New ref');
$pm->setLabel('New label');
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%s', urlencode($pm->getReference()))));
apiUrl(sprintf('api/paymentmode/%s',
urlencode($pm->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($pm->toStruct()));
......@@ -97,7 +187,8 @@ class HttpPaymentModeTest extends TestCase
$json['id'] = 1;
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%s', urlencode($pm->getReference()))));
apiUrl(sprintf('api/paymentmode/%s',
urlencode($pm->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, json_encode($json));
curl_setopt($this->curl, CURLOPT_HTTPHEADER,
......@@ -129,11 +220,12 @@ class HttpPaymentModeTest extends TestCase
public function testPutRefExisting() {
$pm = new PaymentMode();
$pm->setReference('Reference');
$pm->setReference($this->pm->getReference());
$pm->setLabel('New label');
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%s', urlencode($this->pm->getReference()))));
apiUrl(sprintf('api/paymentmode/%s',
urlencode($this->pm->getReference()))));
curl_setopt($this->curl, CURLOPT_POST, true);
curl_setopt($this->curl, CURLOPT_POSTFIELDS,
json_encode($pm->toStruct()));
......@@ -142,7 +234,189 @@ class HttpPaymentModeTest 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(PaymentMode::class, $jsResp['class']);
$this->assertEquals('reference', $jsResp['field']);
$this->assertEquals('Reference', $jsResp['value']);
$this->assertEquals('Reference', $jsResp['key']['reference']);
}
public function testPutWrongReturn() {
$pm = new PaymentMode();
$pm->merge(['reference' => 'New ref', 'label' => 'New label'],
$this->dao);
$struct = $pm->toStruct();
$struct['returns'][] = ['minAmount' => 0,
'returnMode' => $this->pm->getId() + 1];
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%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));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);
$this->assertEquals('AssociationNotFound', $jsResp['constraint']);
$this->assertEquals(PaymentMode::class, $jsResp['class']);
$this->assertEquals('returnMode', $jsResp['field']);
$this->assertEquals($this->pm->getId() + 1, $jsResp['value']);
$this->assertEquals('New ref', $jsResp['key']['reference']);
}
public function testPatchOk() {
$pm = new PaymentMode();
$pm->setReference($this->pm->getReference());
$pm->setLabel('New label');
$struct = $pm->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%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));
$dbPm = $this->dao->search(PaymentMode::class);
$this->assertEquals(1, count($dbPm));
$dbPm = $this->dao->readSnapshot(PaymentMode::class,
$this->pm->getId());
$this->assertEquals('New label', $dbPm->getLabel());
}
public function testPatchId() {
$pm = new PaymentMode();
$pm->setReference($this->pm->getReference());
$pm->setLabel('New label');
$struct = $pm->toStruct();
$struct['id'] = 1;
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%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 testPatchRefUpdate() {
$pm = new PaymentMode();
$pm->merge(['reference' => 'New ref', 'label' => 'New label'],
$this->dao);
$struct = $pm->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%s',
$this->pm->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));
$dbPm = $this->dao->search(PaymentMode::class);
$this->assertEquals(1, count($dbPm));
$dbPm = $this->dao->readSnapshot(PaymentMode::class,
$dbPm[0]->getId());
$this->assertEquals('New ref', $dbPm->getReference());
}
public function testPatchRefExisting() {
$pm2 = new PaymentMode();
$pm2->merge(['reference' => 'New ref', 'label' => 'New label'],
$this->dao);
$this->dao->write($pm2);
$this->dao->commit();
$pm = new PaymentMode();
$pm->merge(['reference' => 'New ref', 'label' => 'New label'],
$this->dao);
$struct = $pm->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%s',
$this->pm->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(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(PaymentMode::class, $jsResp['class']);
$this->assertEquals('reference', $jsResp['field']);
$this->assertEquals('New ref', $jsResp['value']);
$this->assertEquals('Reference', $jsResp['key']['reference']);
}
public function testPatchNotFound() {
$pm = new PaymentMode();
$pm->merge(['reference' => 'New ref', 'label' => 'New label'],
$this->dao);
$struct = $pm->toStruct();
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl('api/paymentmode/notFound'));
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(404, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('RecordNotFound', $jsResp['error']);
$this->assertEquals(PaymentMode::class, $jsResp['class']);
$this->assertEquals('notFound', $jsResp['key']['reference']);
}
public function testPatchWrongReturn() {
$pm = new PaymentMode();
$pm->merge(['reference' => $this->pm->getReference(),
'label' => 'New label'], $this->dao);
$struct = $pm->toStruct();
$struct['returns'][] = ['minAmount' => 0,
'returnMode' => $this->pm->getId() + 1];
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->curl, CURLOPT_URL,
apiUrl(sprintf('api/paymentmode/%s',
$this->pm->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(400, curl_getinfo($this->curl, CURLINFO_HTTP_CODE));
$jsResp = json_decode($resp, true);
$this->assertNotEquals(false, $jsResp);
$this->assertEquals('InvalidField', $jsResp['error']);