Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit a4c2cc1

Browse files
author
Jose Fonseca
committed
Improve test coverage part 1
1 parent 6ba28ff commit a4c2cc1

File tree

9 files changed

+103
-106
lines changed

9 files changed

+103
-106
lines changed

app/Exceptions/Handler.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function report(Throwable $exception)
4141
/**
4242
* Render an exception into an HTTP response.
4343
*
44+
* @codeCoverageIgnore
4445
* @param \Illuminate\Http\Request $request
4546
* @param \Exception $exception
4647
* @return \Illuminate\Http\Response
@@ -49,20 +50,4 @@ public function render($request, Throwable $exception)
4950
{
5051
return parent::render($request, $exception);
5152
}
52-
53-
/**
54-
* Convert an authentication exception into an unauthenticated response.
55-
*
56-
* @param \Illuminate\Http\Request $request
57-
* @param \Illuminate\Auth\AuthenticationException $exception
58-
* @return \Illuminate\Http\Response
59-
*/
60-
protected function unauthenticated($request, AuthenticationException $exception)
61-
{
62-
if ($request->expectsJson()) {
63-
return response()->json(['message' => 'Unauthenticated.'], 401);
64-
}
65-
66-
return redirect()->guest('login');
67-
}
6853
}

app/Http/Kernel.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ class Kernel extends HttpKernel
1414
* @var array
1515
*/
1616
protected $middleware = [
17-
\App\Http\Middleware\CheckForMaintenanceMode::class,
17+
// \App\Http\Middleware\TrustHosts::class,
18+
\App\Http\Middleware\TrustProxies::class,
19+
\Fruitcake\Cors\HandleCors::class,
20+
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
1821
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
1922
\App\Http\Middleware\TrimStrings::class,
2023
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
21-
\App\Http\Middleware\TrustProxies::class,
2224
];
2325

2426
/**
@@ -35,8 +37,7 @@ class Kernel extends HttpKernel
3537
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
3638
\App\Http\Middleware\VerifyCsrfToken::class,
3739
\Illuminate\Routing\Middleware\SubstituteBindings::class,
38-
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
39-
],
40+
]
4041
];
4142

4243
/**
@@ -47,33 +48,16 @@ class Kernel extends HttpKernel
4748
* @var array
4849
*/
4950
protected $routeMiddleware = [
50-
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
51+
'auth' => \App\Http\Middleware\Authenticate::class,
5152
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
52-
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
53+
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
5354
'can' => \Illuminate\Auth\Middleware\Authorize::class,
5455
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
5556
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
56-
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
5757
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
5858
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
5959
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
60+
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
6061
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
6162
];
62-
63-
/**
64-
* The priority-sorted list of middleware.
65-
*
66-
* This forces non-global middleware to always be in the given order.
67-
*
68-
* @var array
69-
*/
70-
protected $middlewarePriority = [
71-
\Illuminate\Session\Middleware\StartSession::class,
72-
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
73-
\App\Http\Middleware\Authenticate::class,
74-
\Illuminate\Routing\Middleware\ThrottleRequests::class,
75-
\Illuminate\Session\Middleware\AuthenticateSession::class,
76-
\Illuminate\Routing\Middleware\SubstituteBindings::class,
77-
\Illuminate\Auth\Middleware\Authorize::class,
78-
];
7963
}

app/Http/Middleware/Authenticate.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,5 @@
66

77
class Authenticate extends Middleware
88
{
9-
/**
10-
* Get the path the user should be redirected to when they are not authenticated.
11-
*
12-
* @param \Illuminate\Http\Request $request
13-
* @return string
14-
*/
15-
protected function redirectTo($request)
16-
{
17-
if (! $request->expectsJson()) {
18-
return route('login');
19-
}
20-
}
9+
2110
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance as Middleware;
6+
7+
class PreventRequestsDuringMaintenance extends Middleware
8+
{
9+
/**
10+
* The URIs that should be reachable while maintenance mode is enabled.
11+
*
12+
* @var array
13+
*/
14+
protected $except = [
15+
//
16+
];
17+
}

app/Http/Middleware/RedirectIfAuthenticated.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,35 @@
22

33
namespace App\Http\Middleware;
44

5+
use App\Providers\RouteServiceProvider;
56
use Closure;
67
use Illuminate\Support\Facades\Auth;
78

9+
/**
10+
* @codeCoverageIgnore
11+
* Class RedirectIfAuthenticated
12+
* @package App\Http\Middleware
13+
*/
814
class RedirectIfAuthenticated
915
{
1016
/**
1117
* Handle an incoming request.
1218
*
13-
* @param \Illuminate\Http\Request $request
14-
* @param \Closure $next
15-
* @param string|null $guard
19+
* @param \Illuminate\Http\Request $request
20+
* @param \Closure $next
21+
* @param string|null ...$guards
1622
* @return mixed
1723
*/
18-
public function handle($request, Closure $next, $guard = null)
24+
public function handle($request, Closure $next, ...$guards)
1925
{
20-
if (Auth::guard($guard)->check()) {
21-
return redirect('/home');
26+
$guards = empty($guards) ? [null] : $guards;
27+
28+
foreach ($guards as $guard) {
29+
if (Auth::guard($guard)->check()) {
30+
return redirect(RouteServiceProvider::HOME);
31+
}
2232
}
2333

2434
return $next($request);
2535
}
26-
}
36+
}

app/Providers/ErrorHandlerServiceProvider.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ public function register()
4242
return $this->response->error('The body is too large', 413);
4343
});
4444
app('Dingo\Api\Exception\Handler')->register(function (ValidationException $exception) {
45-
if (request()->expectsJson()) {
46-
throw new ResourceException('Validation Error', $exception->errors());
47-
}
48-
49-
return redirect()->back()->withInput(request()->input())->withErrors($exception->errors());
45+
throw new ResourceException('Validation Error', $exception->errors());
5046
});
5147
}
5248
}

app/Providers/RouteServiceProvider.php

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,37 @@
88
class RouteServiceProvider extends ServiceProvider
99
{
1010
/**
11-
* This namespace is applied to your controller routes.
11+
* The path to the "home" route for your application.
1212
*
13-
* In addition, it is set as the URL generator's root namespace.
13+
* This is used by Laravel authentication to redirect users after login.
1414
*
1515
* @var string
1616
*/
17-
protected $namespace = 'App\Http\Controllers';
17+
public const HOME = '/home';
1818

1919
/**
20-
* Define your route model bindings, pattern filters, etc.
21-
*
22-
* @return void
23-
*/
24-
public function boot()
25-
{
26-
//
27-
28-
parent::boot();
29-
}
30-
31-
/**
32-
* Define the routes for the application.
33-
*
34-
* @return void
35-
*/
36-
public function map()
37-
{
38-
$this->mapApiRoutes();
39-
40-
$this->mapWebRoutes();
41-
//
42-
}
43-
44-
/**
45-
* Define the "web" routes for the application.
20+
* The controller namespace for the application.
4621
*
47-
* These routes all receive session state, CSRF protection, etc.
22+
* When present, controller route declarations will automatically be prefixed with this namespace.
4823
*
49-
* @return void
24+
* @var string|null
5025
*/
51-
protected function mapWebRoutes()
52-
{
53-
Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));
54-
}
26+
// protected $namespace = 'App\\Http\\Controllers';
5527

5628
/**
57-
* Define the "api" routes for the application.
58-
* Since we are using Dingo, the web middleware group will not apply.
59-
*
60-
* These routes are typically stateless.
29+
* Define your route model bindings, pattern filters, etc.
6130
*
6231
* @return void
6332
*/
64-
protected function mapApiRoutes()
33+
public function boot()
6534
{
66-
require base_path('routes/api.php');
35+
$this->routes(function () {
36+
Route::namespace($this->namespace)
37+
->group(base_path('routes/api.php'));
38+
39+
Route::middleware('web')
40+
->namespace($this->namespace)
41+
->group(base_path('routes/web.php'));
42+
});
6743
}
6844
}

routes/api.php

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

55
$api->version('v1', function($api){
66

7-
$api->group(['middleware' => ['throttle:60,1', 'bindings'], 'namespace' => 'App\Http\Controllers'], function($api) {
7+
$api->group(['middleware' => ['throttle:60,1', \Illuminate\Routing\Middleware\SubstituteBindings::class], 'namespace' => 'App\Http\Controllers'], function($api) {
88

99
$api->get('ping', 'Api\PingController@index');
1010

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Tests\Feature\Console;
4+
5+
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Tests\TestCase;
8+
9+
class InstallAppTest extends TestCase
10+
{
11+
use RefreshDatabase;
12+
13+
public function test_it_installs_app_with_command()
14+
{
15+
$this->artisan('app:install')
16+
->expectsQuestion('What is the Admininstrator\'s name?', 'Taylor')
17+
->expectsQuestion('What is the Admininstrator\'s email?', '[email protected]')
18+
->expectsQuestion('What is the Admininstrator\'s password?', '123456789');
19+
20+
$this->assertDatabaseHas('roles', [
21+
'name' => 'Administrator'
22+
]);
23+
$this->assertDatabaseHas('permissions', [
24+
'name' => 'List users'
25+
]);
26+
$this->assertDatabaseHas('permissions', [
27+
'name' => 'Delete users'
28+
]);
29+
$this->assertDatabaseHas('permissions', [
30+
'name' => 'Update roles'
31+
]);
32+
$this->assertDatabaseHas('permissions', [
33+
'name' => 'List permissions'
34+
]);
35+
$this->assertDatabaseHas('users', [
36+
'name' => 'Taylor',
37+
'email' => '[email protected]',
38+
]);
39+
}
40+
}

0 commit comments

Comments
 (0)