Using Tap, Pipe, and Each in Laravel Collections
In Laravel, a collection wraps an array and provides methods you can chain to filter, sort, and work with its values. tap(),…
Laravel feature tests send simulated HTTP requests through your Laravel routes and inspect the responses without contacting a real network. A test can check that a URL and HTTP method reach the intended named route, return the expected status code, and include the expected content.
A named route has a label, such as posts.show, so code can refer to the route without relying on its URL. In a test, use Laravel’s Route::currentRouteName() to find the name of the route that handled the request. Then use response assertions, Laravel test methods that check a response, to check its status and content.
Define a named route, then request its URL in a feature test. The test below checks three parts of the same request: the response status, the returned text, and the name of the route that handled it.
// routes/web.php
use Illuminate\Support\Facades\Route;
Route::get('/posts/{post}', function (string $post) {
return response("Post {$post}", 200);
})->name('posts.show');
get('/posts/42');
$response
->assertOk()
->assertSeeText('Post 42');
$this->assertSame('posts.show', Route::currentRouteName());
}
}
$this->get() sends a simulated GET request and returns a response object that provides Laravel’s HTTP-test assertions. assertOk() checks for HTTP 200, while assertSeeText() checks that the response contains the given text. Route::currentRouteName() checks the name of the route that handled the incoming request. Laravel documents these HTTP testing methods and response assertions, as well as the current route name method. The example uses these documented methods together in one test.
Keep the route-name assertion in the same test as the request it checks. Laravel’s HTTP-testing guidance recommends sending one request per test method, because multiple requests in one test can cause unexpected behavior.
Use a named status assertion when it makes the test’s intent clear. For example, assertNotFound() checks for HTTP 404, and assertMethodNotAllowed() checks for HTTP 405. Use assertStatus(200) when the exact numeric code is what the test needs to express.
$this->get('/missing')->assertNotFound();
$this->get('/posts/42')->assertStatus(200);
A 404 means the request was not found, or the application intentionally returned a not-found response. A 405 means the URI does not accept the HTTP method used in the request. Test the status that matches the behavior you expect; the two codes describe different outcomes. Laravel lists these methods and other status assertions in the TestResponse API.
A passing status check alone does not show that the response contains the right data. Choose a content assertion that matches the response: assertSeeText() for visible text, assertContent() for exact response content, or a JSON assertion such as assertJsonPath() for a value at a specific path.
Route::currentRouteName() identifies the route that handled the incoming request. A redirect assertion answers a different question: where the response sends the client. To check that a POST request redirects to a named route, use assertRedirectToRoute():
$response = $this->post('/posts');
$response->assertRedirectToRoute('posts.index');
Laravel names routes to support URL generation and redirects without tying callers to a literal URI, and route names should be unique. For an incoming request, check the current route. For a redirect, check the named destination. Laravel’s redirect assertions include assertRedirectToRoute().
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com