Laravel 9.5 Released

News

March 16th, 2022

Laravel 9.5 Released

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:

1{{-- Before --}}
2<span>{{ $user->cities->map(fn ($city) => $city->name.' ('.$city->state->name.')')->implode(', ') }}</span>
3 
4{{-- Using a callback --}}
5<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:

1// Before 9.5
2$this->assertEmpty(Storage::disk('temp')->allFiles('/foo'));
3 
4// +9.5
5Storage::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:

1Storage::fake('temp');
2Storage::disk('temp')->put('/foo/bar.txt', 'string');
3Storage::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:

1$response = TestResponse::fromBaseResponse(new Response([
2 'data' => ['foo' => 'bar'],
3]));
4 
5$response->assertJsonPath('data.foo', 'bar');
6$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:

1Queue::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:

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

New Wrap String Helper

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

1Str:wrap('value')->wrap('"');
2Str::of('value')->wrap('"');
3str('value')->wrap('"');
4 
5// Outputs: "value"
6 
7Str:wrap('is', 'This ', ' me!');
8Str::of('is')->wrap('This ', ' me!');
9str('is')->wrap('This ', ' me!');
10 
11// Outputs: This is me!

Freeze Time Helper for Tests

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

1public function test_something()
2{
3 $this->freezeTime();
4 
5 // Or set the time at the current second for dates
6 // that have no sub-second precision.
7 $this->freezeSecond();
8}

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

1$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":

1Http::baseUrl('https://api.example.org')
2 ->beforeSending([ $this, 'prepareRequest' ])
3 ->asJson()
4 ->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)

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.