-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #515 from bravo-kernel/jsonapi-201
Return 201 for successfully created JSON API records
- Loading branch information
Showing
6 changed files
with
140 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace Crud\Test\App\Controller; | ||
|
||
use Cake\Controller\Controller; | ||
use Crud\Controller\ControllerTrait; | ||
|
||
/** | ||
* Test controller for JsonApiListener integration tests. | ||
*/ | ||
class CountriesController extends Controller | ||
{ | ||
|
||
use ControllerTrait; | ||
|
||
public $paginate = [ | ||
'limit' => 3 | ||
]; | ||
|
||
public $components = [ | ||
'RequestHandler', | ||
'Crud.Crud' => [ | ||
'actions' => [ | ||
'Crud.Index', | ||
'Crud.View', | ||
'Crud.Add', | ||
'Crud.Edit', | ||
'Crud.Delete', | ||
], | ||
'listeners' => [ | ||
'Crud.JsonApi' | ||
] | ||
] | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
namespace Crud\TestCase\Controller; | ||
|
||
use Cake\Routing\Router; | ||
use Crud\TestSuite\IntegrationTestCase; | ||
|
||
class JsonApiIntegrationTest extends IntegrationTestCase | ||
{ | ||
|
||
public $fixtures = [ | ||
'plugin.crud.countries', | ||
'plugin.crud.currencies' | ||
]; | ||
|
||
/** | ||
* Set up required RESTful resource routes. | ||
*/ | ||
public function setUp() | ||
{ | ||
Router::scope('/', function ($routes) { | ||
$routes->resources('Countries', [ | ||
'inflect' => 'dasherize' | ||
]); | ||
}); | ||
} | ||
|
||
/** | ||
* Test most basic `index` action | ||
* | ||
* @return void | ||
*/ | ||
public function testGet() | ||
{ | ||
$this->configRequest([ | ||
'headers' => ['Accept' => 'application/vnd.api+json'] | ||
]); | ||
|
||
$this->get('/countries'); | ||
|
||
$this->assertResponseOk(); | ||
$this->_assertJsonApiResponse(); | ||
$this->assertResponseCode(200); | ||
$this->assertResponseNotEmpty(); | ||
} | ||
|
||
/** | ||
* Make sure HTTP Status Code 201 is returned after successfully | ||
* creating a new record using POST method. | ||
* | ||
* @link http://jsonapi.org/format/#crud-creating-responses-201 | ||
* @link https://github.com/FriendsOfCake/crud/issues/496 | ||
* @return void | ||
*/ | ||
public function testSuccessfulPostReturnsStatusCode201() | ||
{ | ||
$postData = [ | ||
'data' => [ | ||
'type' => 'countries', | ||
'attributes' => [ | ||
'code' => 'NZ', | ||
'name' => 'New Zealand', | ||
'currency_id' => 1 | ||
] | ||
] | ||
]; | ||
|
||
$this->configRequest([ | ||
'headers' => [ | ||
'Accept' => 'application/vnd.api+json', | ||
'Content-Type' => 'application/vnd.api+json' | ||
], | ||
'input' => json_encode($postData) | ||
]); | ||
|
||
$this->post('/countries'); | ||
|
||
$this->assertResponseOk(); | ||
$this->_assertJsonApiResponse(); | ||
$this->assertResponseCode(201); | ||
$this->assertResponseNotEmpty(); | ||
} | ||
|
||
/** | ||
* Helper method to prevent repetition since all JSON API responses | ||
* must pass this test. | ||
*/ | ||
protected function _assertJsonApiResponse() | ||
{ | ||
$this->assertHeader('Content-Type', 'application/vnd.api+json'); | ||
$this->assertContentType('application/vnd.api+json'); | ||
} | ||
} |