Skip to content

Commit 04a1956

Browse files
committed
Merge branch 'release/1.1.0'
2 parents 42156a3 + 1608ed4 commit 04a1956

File tree

9 files changed

+193
-12
lines changed

9 files changed

+193
-12
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor
2+
coverage
3+
composer.lock
4+
.phpunit.result.cache

README.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Laravel API Response
2-
> A fluent helper class to ensure idempotent API responses in Laravel and Lumen
2+
> A fluent helper to provide a consistent shaped API responses in Laravel
33
44
[![Latest Stable Version](https://poser.pugx.org/myerscode/laravel-api-response/v/stable)](https://packagist.org/packages/myerscode/laravel-api-response)
55
[![Total Downloads](https://poser.pugx.org/myerscode/laravel-api-response/downloads)](https://packagist.org/packages/myerscode/laravel-api-response)
66
[![License](https://poser.pugx.org/myerscode/laravel-api-response/license)](https://packagist.org/packages/myerscode/laravel-api-response)
77

8+
## Why is this package helpful?
9+
810
This package ensures your API will always return the same envelope shape, so consuming apps always know what to expect!
911

1012
## Install
@@ -17,18 +19,31 @@ composer require myerscode/laravel-api-response
1719

1820
## Usage
1921

20-
When using a Laravel controller you can simply return a chained `Builder` from the `api()` helper,
21-
as it implements the [Responsable](https://laravel.com/api/master/Illuminate/Contracts/Support/Responsable.html) trait.
22+
In a Laravel controller you just to build up your response and return it!
2223

23-
```php
24+
The `api()` helper return a Response `Builder` and as it implements the [Responsable](https://laravel.com/api/master/Illuminate/Contracts/Support/Responsable.html)
25+
trait you dont need to do anything more than return the builder
2426

25-
function handler()
27+
### Using the api() helper function
28+
```php
29+
function resource()
2630
{
2731
return api()->status(201)->data(['name' => 'Foo Bar'])->message('Record Created!');
2832
}
2933
```
3034

31-
Would return the following `JSON`
35+
### Using a Builder class
36+
```php
37+
38+
function resource() {
39+
$buillder = new Builder();
40+
$builder->status(201)->data(['name' => 'Foo Bar'])->message('Record Created!');
41+
return $builder;
42+
}
43+
```
44+
45+
46+
Would return the following `JSON` response.
3247

3348
```json
3449
{
@@ -43,9 +58,6 @@ Would return the following `JSON`
4358
}
4459
```
4560

46-
You can alternatively call `api()->respond()` to get a `JsonResponse`
47-
48-
4961
## License
5062

51-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
63+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
}
2626
],
2727
"require": {
28-
"php": ">=7.0"
28+
"php": "^7.1"
2929
},
3030
"require-dev": {
31-
"phpunit/phpunit": "^6.0"
31+
"orchestra/testbench": "~3.6.0",
32+
"phpunit/phpunit": "^7.3"
3233
},
3334
"autoload": {
3435
"psr-4": {

phpunit.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
cacheResult="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Unit">
14+
<directory suffix="Test.php">./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<logging>
18+
<log type="coverage-html" target="./coverage/coverage"/>
19+
<log type="testdox-html" target="./coverage/tests.html"/>
20+
<log type="coverage-clover" target="./coverage/coverage.xml"/>
21+
</logging>
22+
<filter>
23+
<whitelist processUncoveredFilesFromWhitelist="true">
24+
<directory suffix=".php">./src/</directory>
25+
<exclude>
26+
</exclude>
27+
</whitelist>
28+
</filter>
29+
<php>
30+
</php>
31+
</phpunit>

src/Builder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public function fresh(): Builder
3434
return $this;
3535
}
3636

37+
/**
38+
* Return the set response body
39+
*
40+
* @return Body
41+
*/
42+
public function body()
43+
{
44+
return $this->body;
45+
}
46+
3747
/**
3848
* Set the api data response
3949
*

tests/BodyTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Illuminate\Database\Eloquent\JsonEncodingException;
6+
use Myerscode\Laravel\ApiResponse\Body;
7+
8+
class BodyTest extends TestCase
9+
{
10+
11+
public function provider()
12+
{
13+
return [
14+
[200, [], [], []],
15+
];
16+
}
17+
18+
19+
/**
20+
* @dataProvider provider
21+
*/
22+
public function testResponsePropertiesAreSet($status, $data, $messages, $meta)
23+
{
24+
$body = new Body();
25+
$body->setStatus(($status))->setData($data)->setMessages($messages)->setMeta($meta);
26+
27+
$this->assertEquals($status, $body->getStatus());
28+
$this->assertEquals($data, $body->getData());
29+
$this->assertEquals($messages, $body->getMessages());
30+
$this->assertEquals($meta, $body->getMeta());
31+
}
32+
33+
public function testAddMessageToBody()
34+
{
35+
$body = new Body();
36+
$body->addMessage(('Hello World'));
37+
38+
$this->assertEquals(['Hello World'], $body->getMessages());
39+
}
40+
41+
42+
/**
43+
* @dataProvider provider
44+
*/
45+
public function testBodyConvertedToJson($status, $data, $messages, $meta)
46+
{
47+
$body = new Body();
48+
$body->setStatus(($status))->setData($data)->setMessages($messages)->setMeta($meta);
49+
50+
$this->assertJson($body->toJson());
51+
}
52+
}

tests/BuilderTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
class BuilderTest extends TestCase
6+
{
7+
8+
public function provider()
9+
{
10+
return [
11+
[200, [], [], []],
12+
];
13+
}
14+
15+
16+
/**
17+
* @dataProvider provider
18+
*/
19+
public function testResponsePropertiesAreSet($status, $data, $messages, $meta)
20+
{
21+
$builder = api()->status($status)->data($data)->messages($messages)->meta($meta);
22+
23+
$this->assertEquals($status, $builder->body()->getStatus());
24+
$this->assertEquals($data, $builder->body()->getData());
25+
$this->assertEquals($messages, $builder->body()->getMessages());
26+
$this->assertEquals($meta, $builder->body()->getMeta());
27+
}
28+
29+
public function testAddMessageToResponse()
30+
{
31+
$builder = api()->message('Hello World');
32+
33+
$this->assertEquals(['Hello World'], $builder->body()->getMessages());
34+
}
35+
36+
}

tests/RespondTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Illuminate\Http\JsonResponse;
6+
use Illuminate\Http\Request;
7+
use Myerscode\Laravel\ApiResponse\Builder;
8+
9+
class RespondTest extends TestCase
10+
{
11+
12+
public function testHelperMakesBuilder()
13+
{
14+
$this->assertInstanceOf(Builder::class, api());
15+
}
16+
17+
public function testRespondCreatesJsonResponse()
18+
{
19+
$this->assertInstanceOf(JsonResponse::class, api()->respond());
20+
$this->assertInstanceOf(JsonResponse::class, api()->toResponse( new Request()));
21+
}
22+
23+
24+
25+
}

tests/TestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Orchestra\Testbench\TestCase as Orchestra;
6+
7+
class TestCase extends Orchestra
8+
{
9+
10+
}

0 commit comments

Comments
 (0)