Just include the files here, so its easier to copy paste:
routes\web.php
<?php
use App\MyModel;
use App\Http\Middleware\ExampleMiddlware;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::post('create/{a}/{b}', 'ExampleController@store')->middleware(ExampleMiddlware::class);
app\Http\Middleware\ExampleMiddlware.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Requests\HestRequest;
use Illuminate\Support\Facades\Validator;
class ExampleMiddlware
{
/**
* Handle an incoming request.
* Will change validation language on response.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
Validator::make($request->all(), [
'my_example' => 'nullable|string',
'extra1' => 'nullable|string',
'extra2' => 'nullable|string',
'extra3' => 'nullable|string',
])->validate();
return $next($request);
}
}
app/Http/Kernel.php
...
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'ExampleMiddlware' => \App\Http\Middleware\ExampleMiddlware::class, // <<-- HERE
];
...
tests\Feature\ExampleTest.php
<?php
// https://laravel.com/docs/5.7/http-tests#testing-json-apis
namespace Tests\Feature;
use Tests\TestCase;
use App\Http\Controllers\ExampleController;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ExampleTest extends TestCase
{
public function testBasicTest()
{
// PART1: Call the method Directly -------------
$direct = (new ExampleController())->store('foo', 'bar');
// PART2: Call the method with Route and Validation -------------
$method = 'POST';
$endpoint = 'create/hello/world';
$response = $this->json($method, $endpoint,
[
'my_example' => 'called with request',
'extra' => 'called with request',
]
);
dump('PART1: CALL THE METHOD DIRECTLY', $direct);
dump('PART2: CALL THE METHOD WITH ROUTE AND VALIDATION', $response->json());
// PART3: Simulate wrong input for validation -------------------------------
$method = 'POST';
$endpoint = 'create/hello/world';
$response = $this->json($method, $endpoint,
[
// Type is number, should be String
'my_example' => 2321313,
]
);
dump('PART3: WRONG INPUT IN VALIDATION', $response->json());
}
}
app\Http\Controllers\ExampleController.php
<?php
namespace App\Http\Controllers;
use App\FunModel;
use Illuminate\Http\Request;
class ExampleController extends Controller
{
public function store($a, $b)
{
$strategy = null;
if (request()->has('my_example')) {
$strategy = "Was called with request";
}
return [$a, $b, request('my_example'), $strategy];
}
}