Skip to content

Commit dde9d4d

Browse files
author
José Fernando Cordova
committed
updating test
1 parent 9e0edf2 commit dde9d4d

File tree

13 files changed

+74
-230
lines changed

13 files changed

+74
-230
lines changed

README.md

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* API Laravel Boilerplate 5.5
77
* Apache 2
88
* MySQL
9+
* Xdebug
910
* Docker
1011

1112
## Docker Environments
@@ -24,7 +25,7 @@ Clone this respository and run the following commands:
2425
```bash
2526
cd docker-laravel-api-dev/
2627
docker-compose -f docker-compose.yml up --build -d
27-
# wait for it to build and follow the docker instructions!...
28+
# wait for it to build!...
2829
```
2930
### PWD
3031
With Play with Docker and following the docker instructions, it is easy to deploy and test this environment!
@@ -33,36 +34,22 @@ With Play with Docker and following the docker instructions, it is easy to deplo
3334

3435
## Docker Instructions
3536

36-
### Execute Laravel Pre-requisites
3737
In the root directory:
3838
```bash
3939
# container lists
4040
docker ps
41-
# next, execute an interactive bash shell on the php container.
42-
docker container exec -t -i [dockerlaravelapidev_php_1 or container Id] bash
41+
# make sure that the docker dockerlaravelapidev_php_1 or php container is (healthy),
42+
normally the process begins in starting mode (2 or 3 minutes)
4343
```
44-
#### Run the following commands:
4544

46-
##### Compose and Swarm Mode
47-
```bash
48-
composer install && cp .env.example .env && php artisan key:generate && php artisan migrate
49-
chmod 755 -R storage
50-
# forward to the port 80, go to localhost and enjoy!...
51-
```
52-
##### Play With Docker (PWD)
53-
```bash
54-
composer install && php artisan migrate
55-
# forward to the port 80, go to localhost and enjoy!...
56-
```
45+
### Environments
46+
47+
* docker-compose-dev.yml: generate automatically severals folders and require-dev dependencies on your local. (Including Xdebug).
48+
49+
* docker-compose-pwd.yml: you can use it in play with docker and test its process.
50+
51+
* docker-compose-prod.yml: if you are going to use this yaml, make sure to generate the migrations before, however you can modify the entrypoint.
5752

58-
### How to fix Error: laravel.log could not be opened?
59-
In the root directory or inside the container php:
60-
<pre><code>chmod -R 775 storage </code></pre>
61-
* 7 - Owner can write
62-
* 7 - Group can write
63-
* 5 - Others cannot write!
64-
Reference:
65-
https://stackoverflow.com/questions/23411520/how-to-fix-error-laravel-log-could-not-be-opened
6653

6754
### API Boilerplate Reference
6855
https://github.com/francescomalatesta/laravel-api-boilerplate-jwt/blob/master/readme.md

app/Api/V1/Controllers/ForgotPasswordController.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,31 @@
44

55
use App\User;
66
use App\Http\Controllers\Controller;
7-
use Illuminate\Http\JsonResponse;
87
use Illuminate\Support\Facades\Password;
98
use App\Api\V1\Requests\ForgotPasswordRequest;
109
use Symfony\Component\HttpKernel\Exception\HttpException;
1110
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12-
use App\Helpers\ApiResponse;
1311

1412
class ForgotPasswordController extends Controller
1513
{
16-
/**
17-
* @param ForgotPasswordRequest $request
18-
* @return \Illuminate\Http\JsonResponse
19-
*/
20-
public function sendResetEmail(ForgotPasswordRequest $request): JsonResponse
14+
public function sendResetEmail(ForgotPasswordRequest $request)
2115
{
2216
$user = User::where('email', '=', $request->get('email'))->first();
23-
if (!$user) {
17+
18+
if(!$user) {
2419
throw new NotFoundHttpException();
2520
}
2621

2722
$broker = $this->getPasswordBroker();
2823
$sendingResponse = $broker->sendResetLink($request->only('email'));
2924

30-
if ($sendingResponse !== Password::RESET_LINK_SENT) {
25+
if($sendingResponse !== Password::RESET_LINK_SENT) {
3126
throw new HttpException(500);
3227
}
3328

34-
ApiResponse::response(200, 'Ok');
29+
return response()->json([
30+
'status' => 'ok'
31+
], 200);
3532
}
3633

3734
/**
@@ -43,4 +40,4 @@ private function getPasswordBroker()
4340
{
4441
return Password::broker();
4542
}
46-
}
43+
}

app/Api/V1/Controllers/LoginController.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace App\Api\V1\Controllers;
44

5-
use App\Helpers\ApiResponse;
6-
use Illuminate\Http\JsonResponse;
75
use Symfony\Component\HttpKernel\Exception\HttpException;
86
use Tymon\JWTAuth\JWTAuth;
97
use App\Http\Controllers\Controller;
@@ -21,7 +19,7 @@ class LoginController extends Controller
2119
* @param JWTAuth $JWTAuth
2220
* @return \Illuminate\Http\JsonResponse
2321
*/
24-
public function login(LoginRequest $request, JWTAuth $JWTAuth): JsonResponse
22+
public function login(LoginRequest $request, JWTAuth $JWTAuth)
2523
{
2624
$credentials = $request->only(['email', 'password']);
2725

@@ -36,9 +34,11 @@ public function login(LoginRequest $request, JWTAuth $JWTAuth): JsonResponse
3634
throw new HttpException(500);
3735
}
3836

39-
return ApiResponse::response(200, 'Ok', [
40-
'token' => $token,
41-
'expires_in' => Auth::guard()->factory()->getTTL() * 60
42-
]);
37+
return response()
38+
->json([
39+
'status' => 'ok',
40+
'token' => $token,
41+
'expires_in' => Auth::guard()->factory()->getTTL() * 60
42+
]);
4343
}
44-
}
44+
}

app/Api/V1/Controllers/LogoutController.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace App\Api\V1\Controllers;
44

5-
use App\Helpers\ApiResponse;
65
use App\Http\Controllers\Controller;
76
use Auth;
8-
use Illuminate\Http\JsonResponse;
97

108
class LogoutController extends Controller
119
{
@@ -16,17 +14,19 @@ class LogoutController extends Controller
1614
*/
1715
public function __construct()
1816
{
19-
$this->middleware('auth:api', []);
17+
$this->middleware('jwt.auth', []);
2018
}
2119

2220
/**
2321
* Log the user out (Invalidate the token)
2422
*
2523
* @return \Illuminate\Http\JsonResponse
2624
*/
27-
public function logout(): JsonResponse
25+
public function logout()
2826
{
2927
Auth::guard()->logout();
30-
ApiResponse::response(200, 'Ok', ['message' => 'Successfully logged out']);
28+
29+
return response()
30+
->json(['message' => 'Successfully logged out']);
3131
}
32-
}
32+
}

app/Api/V1/Controllers/RefreshController.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<?php
22

33
namespace App\Api\V1\Controllers;
4-
use App\Helpers\ApiResponse;
4+
5+
use Symfony\Component\HttpKernel\Exception\HttpException;
6+
use Tymon\JWTAuth\JWTAuth;
57
use App\Http\Controllers\Controller;
8+
use App\Api\V1\Requests\LoginRequest;
9+
use Tymon\JWTAuth\Exceptions\JWTException;
10+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
611
use Auth;
712

813
class RefreshController extends Controller
@@ -16,8 +21,10 @@ public function refresh()
1621
{
1722
$token = Auth::guard()->refresh();
1823

19-
return ApiResponse::response(200, 'Ok',
20-
['token' => $token, 'expires_in' => Auth::guard()->factory()->getTTL() * 60]);
21-
24+
return response()->json([
25+
'status' => 'ok',
26+
'token' => $token,
27+
'expires_in' => Auth::guard()->factory()->getTTL() * 60
28+
]);
2229
}
23-
}
30+
}

app/Api/V1/Controllers/ResetPasswordController.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace App\Api\V1\Controllers;
44

5-
use App\Helpers\ApiResponse;
65
use Config;
76
use App\User;
8-
use Illuminate\Http\JsonResponse;
97
use Tymon\JWTAuth\JWTAuth;
108
use App\Http\Controllers\Controller;
119
use Illuminate\Support\Facades\Password;
@@ -14,12 +12,7 @@
1412

1513
class ResetPasswordController extends Controller
1614
{
17-
/**
18-
* @param ResetPasswordRequest $request
19-
* @param JWTAuth $JWTAuth
20-
* @return \Illuminate\Http\JsonResponse
21-
*/
22-
public function resetPassword(ResetPasswordRequest $request, JWTAuth $JWTAuth): JsonResponse
15+
public function resetPassword(ResetPasswordRequest $request, JWTAuth $JWTAuth)
2316
{
2417
$response = $this->broker()->reset(
2518
$this->credentials($request), function ($user, $password) {
@@ -39,7 +32,10 @@ public function resetPassword(ResetPasswordRequest $request, JWTAuth $JWTAuth):
3932

4033
$user = User::where('email', '=', $request->get('email'))->first();
4134

42-
return ApiResponse::response(200, 'Ok', ['token' => $JWTAuth->fromUser($user)]);
35+
return response()->json([
36+
'status' => 'ok',
37+
'token' => $JWTAuth->fromUser($user)
38+
]);
4339
}
4440

4541
/**
@@ -77,4 +73,4 @@ protected function reset($user, $password)
7773
$user->password = $password;
7874
$user->save();
7975
}
80-
}
76+
}

app/Api/V1/Controllers/SignUpController.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,32 @@
22

33
namespace App\Api\V1\Controllers;
44

5-
use App\Helpers\ApiResponse;
65
use Config;
76
use App\User;
8-
use Illuminate\Http\JsonResponse;
97
use Tymon\JWTAuth\JWTAuth;
108
use App\Http\Controllers\Controller;
119
use App\Api\V1\Requests\SignUpRequest;
1210
use Symfony\Component\HttpKernel\Exception\HttpException;
1311

1412
class SignUpController extends Controller
1513
{
16-
/**
17-
* @param SignUpRequest $request
18-
* @param JWTAuth $JWTAuth
19-
* @return \Illuminate\Http\JsonResponse
20-
*/
21-
public function signUp(SignUpRequest $request, JWTAuth $JWTAuth): JsonResponse
14+
public function signUp(SignUpRequest $request, JWTAuth $JWTAuth)
2215
{
2316
$user = new User($request->all());
2417
if(!$user->save()) {
2518
throw new HttpException(500);
2619
}
2720

2821
if(!Config::get('boilerplate.sign_up.release_token')) {
29-
return ApiResponse::response(201, 'Ok');
22+
return response()->json([
23+
'status' => 'ok'
24+
], 201);
3025
}
3126

3227
$token = $JWTAuth->fromUser($user);
33-
return ApiResponse::response(201, 'Ok', ['token' => $token]);
28+
return response()->json([
29+
'status' => 'ok',
30+
'token' => $token
31+
], 201);
3432
}
35-
}
33+
}

app/Api/V1/Controllers/UserController.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace App\Api\V1\Controllers;
44

5-
use App\Helpers\ApiResponse;
5+
use Symfony\Component\HttpKernel\Exception\HttpException;
6+
use Tymon\JWTAuth\JWTAuth;
67
use App\Http\Controllers\Controller;
8+
use App\Api\V1\Requests\LoginRequest;
9+
use Tymon\JWTAuth\Exceptions\JWTException;
10+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
711
use Auth;
812

913
class UserController extends Controller
@@ -13,17 +17,18 @@ class UserController extends Controller
1317
*
1418
* @return void
1519
*/
16-
public function __construct(){
17-
$this->middleware('auth:api', []);
20+
public function __construct()
21+
{
22+
$this->middleware('jwt.auth', []);
1823
}
1924

2025
/**
2126
* Get the authenticated User
2227
*
2328
* @return \Illuminate\Http\JsonResponse
2429
*/
25-
public function profile(){
26-
return ApiResponse::response(200, 'Ok', Auth::guard()->user());
30+
public function me()
31+
{
32+
return response()->json(Auth::guard()->user());
2733
}
28-
29-
}
34+
}

routes/api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
$api->post('logout', 'App\\Api\\V1\\Controllers\\LogoutController@logout');
2020
$api->post('refresh', 'App\\Api\\V1\\Controllers\\RefreshController@refresh');
21-
$api->get('profile', 'App\\Api\\V1\\Controllers\\UserController@profile');
21+
$api->get('me', 'App\\Api\\V1\\Controllers\\UserController@me');
2222
});
2323

2424
$api->group(['middleware' => 'jwt.auth'], function(Router $api) {

scripts/runtests.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/bin/bash
22
set -ev
33
# Make sure starter fixtures can load successfully and all tests pass.
4-
docker exec dockerlaravelapidev_php_1 composer install && cp .env.example .env && php artisan key:generate
5-
docker exec dockerlaravelapidev_php_1 php artisan migrate
4+
docker exec dockerlaravelapidev_php_1 vendor/bin/phpunit
65

76

87

0 commit comments

Comments
 (0)