Laravel 11.20: Precision Testing and New Query Builders

The

ecosystem moves fast, and the latest 11.20 release proves that even small helper methods can significantly clean up your test suite and database queries. These updates focus on precision, ensuring your application doesn't just work, but behaves exactly as expected under the hood.

Granular Header Control with WithoutHeader

Testing HTTP requests often involves setting global headers, but sometimes you need to isolate a specific call. While we've long had withHeader, the new withoutHeader method allows you to strip away specific headers for a single test. This is vital when testing middleware that should fail if a specific token or content-type is missing. Instead of rebuilding your entire request state, you simply tell

what to ignore.

Enforcing Strict API Contracts

One of the most powerful additions is assertExactJsonStructure. Standard JSON assertions often pass as long as the required keys exist, even if the API returns twenty extra fields. In a production environment, leaking internal model attributes is a security risk. By using assertExactJsonStructure, the test fails if your response contains any keys outside of your defined schema. It forces your API to remain lean and strictly documented.

Seamless HTML Assertions

Borrowing a page from the

playbook,
Laravel
now includes assertSeeHtml. Testing raw HTML strings previously required passing false as a second argument to assertSee to prevent escaping. The new helper—along with its counterparts assertSeeHtmlInOrder and assertDontSeeHtml—makes the code more readable and intent-focused. It’s a cleaner way to verify that your UI components render specific tags correctly.

Expanding Query Logic with WhereNone

On the database side,

continues to improve its readability. Following the introduction of whereAny, we now have whereNone. This method allows you to verify that a search term does not exist across a collection of columns. It eliminates the need for messy nested where and orWhere callbacks, keeping your repository patterns clean and your logic easy to follow at a glance.

2 min read