Laravel 9.5 Released

Published on by

Laravel 9.5 Released image

The Laravel team released 9.5 with partial queue faking, a freezeTime() test helper, a storage assertDirectoryEmpty() assertion, closures in assertJsonPath(), and more:

Callback Support on Collections Implode Method

@Lito contributed callback support on Collect::implode() to simplify ->map()->implode() calls:

{{-- Before --}}
<span>{{ $user->cities->map(fn ($city) => $city->name.' ('.$city->state->name.')')->implode(', ') }}</span>
 
{{-- Using a callback --}}
<span>{{ $user->cities->implode(fn ($city) => $city->name.' ('.$city->state->name.')', ', ') }}</span>

Assert an Empty Directory With the Storage Fake

Mark Beech contributed the ability to assert an empty directory with a Storage::fake() instance:

// Before 9.5
$this->assertEmpty(Storage::disk('temp')->allFiles('/foo'));
 
// +9.5
Storage::disk('temp')->assertDirectoryEmpty('/foo');

If there are no files in the directory but other subdirectories, the assertion will fail because it contains other folders/files. Here's an example from the pull request discussion:

Storage::fake('temp');
Storage::disk('temp')->put('/foo/bar.txt', 'string');
Storage::disk('temp')->assertDirectoryEmpty('/'); // fail

JSON Assertion "assertJsonPath()" Now Accepts a Closure

Fabien Villepinte contributed the ability to pass a closure to assertJsonPath without any backwards-compatible breaks:

$response = TestResponse::fromBaseResponse(new Response([
'data' => ['foo' => 'bar'],
]));
 
$response->assertJsonPath('data.foo', 'bar');
$response->assertJsonPath('data.foo', fn ($value) => $value === 'bar');

While the example above seems more straightforward with the string version, you can now reach for a Closure if you need more complex logic around the path assertion.

Partial Queue Faking

Taylor Otwell contributed partial faking for queue jobs in tests:

Queue::fake([JobsToFake::class, /* ... */]);

New Method to Create "through" Models

Hafez Divandari contributed the ability to create a new "through" model without overriding the whole hasOneThrough or hasManyThrough methods:

// Define a `newThroughInstance` method
protected function newThroughInstance($resource)
{
return (new \App\Models\ExampleEntity)->setTable($resource);
}

New Wrap String Helper

Markus Hebenstreit contributed a wrap() string helper. Here's example usages from the pull request description:

Str:wrap('value')->wrap('"');
Str::of('value')->wrap('"');
str('value')->wrap('"');
 
// Outputs: "value"
 
Str:wrap('is', 'This ', ' me!');
Str::of('is')->wrap('This ', ' me!');
str('is')->wrap('This ', ' me!');
 
// Outputs: This is me!

Freeze Time Helper for Tests

@Italo contributed a freezeTime() test method which will freeze the current time in a test:

public function test_something()
{
$this->freezeTime();
 
// Or set the time at the current second for dates
// that have no sub-second precision.
$this->freezeSecond();
}

The freezeTime() method is syntactic sugar for the following:

$this->travelTo(Carbon::now());

Allow Callables in Http::beforeSending()

Dries Vints contributed to accepting callables in the Http::beforeSending() method instead of only invokable classes. Now, the following example will work instead of getting a "Call to member function __invoke() on array":

Http::baseUrl('https://api.example.org')
->beforeSending([ $this, 'prepareRequest' ])
->asJson()
->withoutVerifying();

Release Notes

You can see the complete list of new features and updates below and the diff between 9.4.0 and 9.5.0 on GitHub. The following release notes are directly from the changelog:

v9.5.0

Added

  • Added callback support on implode Collection method. (#41405)
  • Added Illuminate/Filesystem/FilesystemAdapter::assertDirectoryEmpty() (#41398)
  • Implement email "metadata" for SesTransport (#41422)
  • Make assertPath() accepts Closure (#41409)
  • Added callable support to operatorForWhere on Collection (#41414, #41424)
  • Added partial queue faking (#41425)
  • Added --name option to schedule:test command (#41439)
  • Define Illuminate/Database/Eloquent/Concerns/HasRelationships::newRelatedThroughInstance() (#41444)
  • Added Illuminate/Support/Stringable::wrap() (#41455)
  • Adds "freezeTime" helper for tests (#41460)
  • Allow for callables with beforeSending inIlluminate/Http/Client/PendingRequest.php::runBeforeSendingCallbacks() (#41489)

Fixed

  • Fixed deprecation warnings from route:list when filtering on name or domain (#41421)
  • Fixes HTTP::pool response when a URL returns a null status code (#41412)
  • Fixed recaller name resolution in Illuminate/Session/Middleware/AuthenticateSession.php (#41429)
  • Fixed the guard instance used in /Illuminate/Session/Middleware/AuthenticateSession.php (#41447)
  • Fixed route:list --except-vendor hiding Route::view() & Route::redirect() (#41465)

Changed

  • Add null typing to connection property in \Illuminate\Database\Eloquent\Factories\Factory (#41418)
  • Update reserved names in GeneratorCommand (#41441)
  • Redesign php artisan schedule:list Command (#41445)
  • Extend eloquent higher order proxy properties (#41449)
  • Allow passing named arguments to dynamic scopes (#41478)
  • Throw if tag is passed but is not supported in Illuminate/Encryption/Encrypter.php (#41479)
  • Update PackageManifest::$vendorPath initialisation for cases, when composer vendor dir is not in project director (#41463)
Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project.

Visit No Compromises
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
All Green logo

All Green

All Green is a SaaS test runner that can execute your whole Laravel test suite in mere seconds so that you don't get blocked – you get feedback almost instantly and you can deploy to production very quickly.

All Green
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Automatic Blade Formatting on Save in PhpStorm image

Automatic Blade Formatting on Save in PhpStorm

Read article
PhpStorm 2024.1 Is Released With a Integrated Terminal, Local AI Code Completion, and More image

PhpStorm 2024.1 Is Released With a Integrated Terminal, Local AI Code Completion, and More

Read article
Laravel Prompts Adds a Multi-line Textarea Input, Laravel 11.3 Released image

Laravel Prompts Adds a Multi-line Textarea Input, Laravel 11.3 Released

Read article
Bartender Is an Opinionated Way to Authenticate Users Using Laravel Socialite image

Bartender Is an Opinionated Way to Authenticate Users Using Laravel Socialite

Read article
Jeffrey Way's PhpStorm Setup in 2024 image

Jeffrey Way's PhpStorm Setup in 2024

Read article
Easily Optimize PDFs in Laravel with the Optimizer Package image

Easily Optimize PDFs in Laravel with the Optimizer Package

Read article