Skip to content

Commit

Permalink
Merge pull request #79 from alexjoffroy/master
Browse files Browse the repository at this point in the history
Access data in updateResourceCallable (fix #77)
  • Loading branch information
nilportugues committed Jul 11, 2016
2 parents e06ad02 + 70df305 commit 54267b1
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
Expand Up @@ -171,7 +171,7 @@ protected function putAction(Request $request, $id)
*/
protected function updateResourceCallable()
{
return function (Model $model, array $values, ErrorBag $errorBag) {
return function (Model $model, array $data, array $values, ErrorBag $errorBag) {
foreach ($values as $attribute => $value) {
$model->$attribute = $value;
}
Expand Down
41 changes: 39 additions & 2 deletions tests/NilPortugues/App/Controller/EmployeesController.php
Expand Up @@ -37,9 +37,9 @@ public function getDataModel()
/**
* @return callable
*/
protected function createResourceCallable()
private function createOrderResourceCallable()
{
$createOrderResource = function (Model $model, array $data) {
return function (Model $model, array $data) {
if (!empty($data['relationships']['order']['data'])) {
$orderData = $data['relationships']['order']['data'];

Expand All @@ -53,6 +53,14 @@ protected function createResourceCallable()
}
}
};
}

/**
* @return callable
*/
protected function createResourceCallable()
{
$createOrderResource = $this->createOrderResourceCallable();

return function (array $data, array $values, ErrorBag $errorBag) use ($createOrderResource) {

Expand Down Expand Up @@ -81,6 +89,35 @@ protected function createResourceCallable()
};
}

/**
* @return callable
*/
protected function updateResourceCallable()
{
$createOrderResource = $this->createOrderResourceCallable();

return function (Model $model, array $data, array $values, ErrorBag $errorBag) use ($createOrderResource) {

foreach ($values as $attribute => $value) {
$model->$attribute = $value;
}

DB::beginTransaction();
try {
$model->update();
$createOrderResource($model, $data);
DB::commit();

return $model;
} catch (\Exception $e) {
DB::rollback();
$errorBag[] = new Error('update_error', 'Resource could not be updated');
throw new \Exception();
}

};
}

/**
* @param $id
*
Expand Down
56 changes: 56 additions & 0 deletions tests/NilPortugues/Laravel5/JsonApi/JsonApiControllerTest.php
Expand Up @@ -58,6 +58,62 @@ public function testGetAction()
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
}

public function testPatchAction()
{
$this->createNewEmployee();

$content = <<<JSON
{
"data": {
"type": "employee",
"attributes": {
"job_title": "Senior Web Developer"
}
}
}
JSON;
$response = $this->call('PATCH', 'http://localhost/employees/1', json_decode($content, true), [], [], [], '');

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
}

public function testPutAction()
{
$this->createNewEmployee();

$content = <<<JSON
{
"data": {
"type": "employee",
"attributes": {
"company": "NilPortugues.com",
"surname": "Portugués",
"first_name": "Nil",
"email_address": "nilportugues@localhost",
"job_title": "Senior Web Developer",
"business_phone": "(123)555-0100",
"home_phone": "(123)555-0102",
"mobile_phone": null,
"fax_number": "(123)555-0103",
"address": "Plaça Catalunya 1",
"city": "Barcelona",
"state_province": "Barcelona",
"zip_postal_code": "08028",
"country_region": "Spain",
"web_page": "http://nilportugues.com",
"notes": null,
"attachments": null
}
}
}
JSON;
$response = $this->call('PUT', 'http://localhost/employees/1', json_decode($content, true), [], [], [], '');

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
}

public function testDeleteAction()
{
$this->createNewEmployee();
Expand Down

0 comments on commit 54267b1

Please sign in to comment.