Skip to content

Commit c2ec3be

Browse files
Merge pull request #69 from MarcinOrlowski/dev
Release 4.0.2
2 parents dd5fc74 + e038e34 commit c2ec3be

File tree

6 files changed

+75
-36
lines changed

6 files changed

+75
-36
lines changed

CHANGES.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ See [compatibility docs](docs/compatibility.md) for details about backward compa
44

55
## CHANGE LOG ##
66

7+
* v4.0.2 (2017-04-13)
8+
* Enforced HTTP code for error messages fits 400-499 range
9+
* `validateResponseStructure()` deprecated in favor of `assertValidResponse()`
10+
* Moved Orchestra's `getPackageProviders()` out of `TestingHelpers` trait
11+
712
* v4.0.1 (2017-04-10)
813
* TestingHelpers trait's `validateResponseStructure()` method is now public
914
* [RB-64] Fixed Exception Handler generated HTTP code being out of allowed range in some cases

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "marcin-orlowski/laravel-api-response-builder",
33
"description": "Helps building nice, normalized and easy to consume REST API responses.",
44
"homepage": "https://github.com/MarcinOrlowski/laravel-api-response-builder",
5-
"version": "4.0.1",
5+
"version": "4.0.2",
66
"keywords": [
77
"laravel",
88
"json",

docs/testing.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
`ResponseBuilder` is [Laravel](https://laravel.com/)'s helper designed to simplify building
44
nice, normalized and easy to consume REST API responses.
55

6-
76
## Unit testing your ApiCodes ##
87

98
`ResponseBuilder` ships with traits that you can use to ensure your ApiCodes class and its values
@@ -51,3 +50,33 @@ and its namespace is `App`.
5150

5251

5352
And that's it. From now on, you have your `ApiCodes` covered with tests too.
53+
54+
55+
## Testing other code using ResponseBuilder ##
56+
57+
If you want to test other code that uses response builder, then there provided traits can also be
58+
helpful. Let's say your Laravel API exposes `/v1/session/foo` which is expected to return some
59+
data. Let's test the response structure and data:
60+
61+
<?php
62+
63+
use MarcinOrlowski\ResponseBuilder\Tests\Traits\TestingHelpers;
64+
class LoginTest extends \Illuminate\Foundation\Testing\TestCase
65+
{
66+
use TestingHelpers;
67+
68+
public function testLogin()
69+
{
70+
// call your method
71+
$response = $this->call('POST', '/v1/session/foo');
72+
73+
// get the JSON object
74+
$j = json_decode($response->getContent());
75+
76+
// validate JSON structure matches what ResponseBuilder produced
77+
$this->assertValidResponse($j);
78+
79+
// some other tests of your choice
80+
$this->assertTrue($j->success);
81+
}
82+
}

src/ResponseBuilder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ protected static function buildErrorResponse($data, $api_code, $http_code, $lang
428428
if (!is_int($http_code)) {
429429
throw new \InvalidArgumentException(sprintf('http_code must be integer (%s given)', gettype($http_code)));
430430
}
431-
if ($http_code < 400) {
432-
throw new \InvalidArgumentException('http_code cannot be lower than 400');
431+
if (($http_code < 400) || ($http_code > 499)) {
432+
throw new \InvalidArgumentException('http_code must be in range from 400 to 499 inclusive');
433433
}
434434

435435
if ($message === null) {

tests/TestCase.php

+17
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,21 @@ public function getApiCodesClassName()
2424
return \MarcinOrlowski\ResponseBuilder\BaseApiCodes::class;
2525
}
2626

27+
28+
// -----------------------------------------------------------
29+
30+
/**
31+
* [Orchestra] Load service providers we need during the tests
32+
*
33+
* @param \Illuminate\Foundation\Application $app
34+
*
35+
* @return array
36+
*/
37+
protected function getPackageProviders($app)
38+
{
39+
return [
40+
\MarcinOrlowski\ResponseBuilder\Tests\Providers\ResponseBuilderServiceProvider::class,
41+
];
42+
}
43+
2744
}

tests/Traits/TestingHelpers.php

+20-32
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
/**
2020
* Unit test helper trait
2121
*/
22-
trait TestingHelpers {
22+
trait TestingHelpers
23+
{
2324

2425
/**
2526
* @return string
@@ -62,8 +63,6 @@ public function setUp()
6263
{
6364
parent::setUp();
6465

65-
$this->clearCache();
66-
6766
// Obtain configuration params
6867
$class_name = $this->getApiCodesClassName();
6968
$obj = new $class_name();
@@ -82,7 +81,7 @@ public function setUp()
8281
$map = $this->getProtectedMember(\MarcinOrlowski\ResponseBuilder\BaseApiCodes::class, 'base_map');
8382
$idx = mt_rand(1, count($map));
8483

85-
$this->random_api_code_message_key = $map[array_keys($map)[$idx-1]];
84+
$this->random_api_code_message_key = $map[ array_keys($map)[ $idx - 1 ] ];
8685
$this->random_api_code_message = \Lang::get($this->random_api_code_message_key, [
8786
'api_code' => $this->random_api_code,
8887
]);
@@ -93,31 +92,6 @@ public function setUp()
9392
}
9493

9594

96-
/**
97-
* Clears Laravel config/route etc caches...
98-
*/
99-
protected function clearCache()
100-
{
101-
// $commands = ['clear-compiled', 'cache:clear', 'view:clear', 'config:clear', 'route:clear'];
102-
// foreach ($commands as $command) {
103-
// \Illuminate\Support\Facades\Artisan::call($command);
104-
// }
105-
}
106-
107-
/**
108-
* Load service providers we need during the tests
109-
*
110-
* @param \Illuminate\Foundation\Application $app
111-
*
112-
* @return array
113-
*/
114-
protected function getPackageProviders($app)
115-
{
116-
return [
117-
\MarcinOrlowski\ResponseBuilder\Tests\Providers\ResponseBuilderServiceProvider::class,
118-
];
119-
}
120-
12195
// -----------------------------------------------------------
12296

12397

@@ -230,6 +204,21 @@ private function getResponseObjectRaw($expected_api_code, $expected_http_code, $
230204
}
231205

232206

207+
/**
208+
* Use assertValidResponse() instead
209+
*
210+
* @param StdClass $json_object
211+
* @param array $extra_keys
212+
*
213+
* @return void
214+
*
215+
* @deprecated Use assertValidResponse() instead
216+
*/
217+
public function validateResponseStructure($json_object, array $extra_keys = [])
218+
{
219+
$this->assertValidResponse($json_object, $extra_keys);
220+
}
221+
233222
/**
234223
* Validates if given $json_object contains all expected elements
235224
*
@@ -238,7 +227,7 @@ private function getResponseObjectRaw($expected_api_code, $expected_http_code, $
238227
*
239228
* @return void
240229
*/
241-
public function validateResponseStructure($json_object, array $extra_keys = [])
230+
public function assertValidResponse($json_object, array $extra_keys = [])
242231
{
243232
$this->assertTrue(is_object($json_object));
244233

@@ -260,7 +249,7 @@ public function validateResponseStructure($json_object, array $extra_keys = [])
260249
$this->assertObjectHasAttribute($item, $json_object, "No '{$item}' element in response structure found");
261250
}
262251

263-
$this->assertTrue(is_bool($json_object->{$items[ResponseBuilder::KEY_SUCCESS]}));
252+
$this->assertTrue(is_bool($json_object->{$items[ ResponseBuilder::KEY_SUCCESS ]}));
264253
$this->assertTrue(is_int($json_object->code));
265254
$this->assertTrue(is_string($json_object->locale));
266255
/** @noinspection UnNecessaryDoubleQuotesInspection */
@@ -484,5 +473,4 @@ protected function escape8($string)
484473
return $escaped;
485474
}
486475

487-
488476
}

0 commit comments

Comments
 (0)