Skip to content

Commit 54267b1

Browse files
authored
Merge pull request #79 from alexjoffroy/master
Access data in updateResourceCallable (fix #77)
2 parents e06ad02 + 70df305 commit 54267b1

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

src/NilPortugues/Laravel5/JsonApi/Controller/JsonApiTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ protected function putAction(Request $request, $id)
171171
*/
172172
protected function updateResourceCallable()
173173
{
174-
return function (Model $model, array $values, ErrorBag $errorBag) {
174+
return function (Model $model, array $data, array $values, ErrorBag $errorBag) {
175175
foreach ($values as $attribute => $value) {
176176
$model->$attribute = $value;
177177
}

tests/NilPortugues/App/Controller/EmployeesController.php

+39-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public function getDataModel()
3737
/**
3838
* @return callable
3939
*/
40-
protected function createResourceCallable()
40+
private function createOrderResourceCallable()
4141
{
42-
$createOrderResource = function (Model $model, array $data) {
42+
return function (Model $model, array $data) {
4343
if (!empty($data['relationships']['order']['data'])) {
4444
$orderData = $data['relationships']['order']['data'];
4545

@@ -53,6 +53,14 @@ protected function createResourceCallable()
5353
}
5454
}
5555
};
56+
}
57+
58+
/**
59+
* @return callable
60+
*/
61+
protected function createResourceCallable()
62+
{
63+
$createOrderResource = $this->createOrderResourceCallable();
5664

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

@@ -81,6 +89,35 @@ protected function createResourceCallable()
8189
};
8290
}
8391

92+
/**
93+
* @return callable
94+
*/
95+
protected function updateResourceCallable()
96+
{
97+
$createOrderResource = $this->createOrderResourceCallable();
98+
99+
return function (Model $model, array $data, array $values, ErrorBag $errorBag) use ($createOrderResource) {
100+
101+
foreach ($values as $attribute => $value) {
102+
$model->$attribute = $value;
103+
}
104+
105+
DB::beginTransaction();
106+
try {
107+
$model->update();
108+
$createOrderResource($model, $data);
109+
DB::commit();
110+
111+
return $model;
112+
} catch (\Exception $e) {
113+
DB::rollback();
114+
$errorBag[] = new Error('update_error', 'Resource could not be updated');
115+
throw new \Exception();
116+
}
117+
118+
};
119+
}
120+
84121
/**
85122
* @param $id
86123
*

tests/NilPortugues/Laravel5/JsonApi/JsonApiControllerTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,62 @@ public function testGetAction()
5858
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
5959
}
6060

61+
public function testPatchAction()
62+
{
63+
$this->createNewEmployee();
64+
65+
$content = <<<JSON
66+
{
67+
"data": {
68+
"type": "employee",
69+
"attributes": {
70+
"job_title": "Senior Web Developer"
71+
}
72+
}
73+
}
74+
JSON;
75+
$response = $this->call('PATCH', 'http://localhost/employees/1', json_decode($content, true), [], [], [], '');
76+
77+
$this->assertEquals(200, $response->getStatusCode());
78+
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
79+
}
80+
81+
public function testPutAction()
82+
{
83+
$this->createNewEmployee();
84+
85+
$content = <<<JSON
86+
{
87+
"data": {
88+
"type": "employee",
89+
"attributes": {
90+
"company": "NilPortugues.com",
91+
"surname": "Portugués",
92+
"first_name": "Nil",
93+
"email_address": "nilportugues@localhost",
94+
"job_title": "Senior Web Developer",
95+
"business_phone": "(123)555-0100",
96+
"home_phone": "(123)555-0102",
97+
"mobile_phone": null,
98+
"fax_number": "(123)555-0103",
99+
"address": "Plaça Catalunya 1",
100+
"city": "Barcelona",
101+
"state_province": "Barcelona",
102+
"zip_postal_code": "08028",
103+
"country_region": "Spain",
104+
"web_page": "http://nilportugues.com",
105+
"notes": null,
106+
"attachments": null
107+
}
108+
}
109+
}
110+
JSON;
111+
$response = $this->call('PUT', 'http://localhost/employees/1', json_decode($content, true), [], [], [], '');
112+
113+
$this->assertEquals(200, $response->getStatusCode());
114+
$this->assertEquals('application/vnd.api+json', $response->headers->get('Content-type'));
115+
}
116+
61117
public function testDeleteAction()
62118
{
63119
$this->createNewEmployee();

0 commit comments

Comments
 (0)