/** * @param string $id * @param array $data * @param string $className * @param callable $findOneCallable * @param callable $update * * @return \Symfony\Component\HttpFoundation\Response */ public function get($id, array $data, $className, callable $findOneCallable, callable $update) { try { DataObject::assertPatch($data, $this->serializer, $className, $this->errorBag); $model = $findOneCallable(); if (empty($model)) { $mapping = $this->serializer->getTransformer()->getMappingByClassName($className); return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)])); } $values = DataObject::getAttributes($data, $this->serializer); $update($model, $data, $values, $this->errorBag); $response = $this->resourceUpdated($this->serializer->serialize($model)); } catch (Exception $e) { $response = $this->getErrorResponse($e); } return $response; }
public function testAssertPatchWillReturnErrors() { $errorBag = new ErrorBag(); $data = ['type' => 'post0', 'attributes' => ['content' => 'Ever heard of Lorem Ipsum?', 'author' => 1]]; $hasErrors = false; try { DataObject::assertPatch($data, $this->serializer, Post::class, $errorBag); } catch (DataException $e) { $hasErrors = true; } $this->assertTrue($hasErrors); }