Laravel 9.12 Released

News

May 11th, 2022

laravel9-1646792144.jpg

The Laravel team released 9.12 with delaying notifications per channel, sharing logging context across channels, preventing stray HTTP requests while testing, and more:

Closure-based Exception Testing

Karel Faille contributed a assertThrows() method which adds a way to test exception handling using closures:

1// Ensure that an exception is thrown
2$this->assertThrows(fn () => throw Exception(''));
3 
4// Ensure that a specific type of exception is thrown
5$this->assertThrows(
6 fn () => (new SomeActionThatThrowsExceptions)->execute(),
7 CustomException::class
8);
9 
10// Ensure that an exception is thrown with a specific message
11$this->assertThrows(fn () => throw Exception('My message'), Exception::class, 'My message');

Force HTTP Requests to Be Faked in Tests

Tim MacDonald contributed the ability to force all HTTP requests using Laravel's HTTP client to be faked. If an HTTP request is not faked in a test, the test will throw an exception:

1protected function setUp(): void
2{
3 parent::setUp();
4 
5 Http::preventStrayRequests();
6}
7 
8public function testItDoesTheThingWithoutFaking(): void
9{
10 $this->post('endpoint-that-utilises-the-http-facade');
11 
12 // RuntimeException: Attempted request to [https://acme.com] without a matching fake.
13 
14 /* ... */
15}

"Throw If" HTTP Client Method

@denniseilander contributed a throwIf() method to the HTTP client that will throw an exception if the condition evaluates to true:

1// Throws RequestException
2return Http::baseUrl('https://foo.bar')
3 ->throwIf(true)
4 ->get('not-found');
5 
6// Doesn't throw
7return Http::baseUrl('https://foo.bar')
8 ->throwIf(false)
9 ->get('not-found');

Allow Passing Key/Value Arrays to Artisan Options and Arguments

Jesper Noordsij contributed passing key/value arrays to getArguments and getOptions on Artisan commands for a slightly easier interface.

1// Also, it is possible to skip those you don't want to use without providing/copying the default value
2 
3// e.g.
4public function getArguments()
5{
6 return [
7 ['name' => 'argument', 'default' => 'default']
8 ];
9 
10 // which previously would have been...
11 // return [
12 // ['argument', null, '', 'default']
13 // ];
14}

See Pull Request #42268 for more details.

New More Condition Methods

Patrick O'Meara contributed the inverse of whereMorphedTo and orWhereMorphedTo methods. Here's an example from the PR tests:

1$model = new EloquentBuilderTestModelParentStub;
2 
3$this->mockConnectionForModel($model, '');
4 
5$relatedModel = new EloquentBuilderTestModelCloseRelatedStub;
6 
7$relatedModel->id = 1;
8 
9$builder = $model->whereNotMorphedTo('morph', $relatedModel);
10 
11$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not ("morph_type" = ? and "morph_id" = ?)', $builder->toSql());
12 
13$this->assertEquals([$relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());

Check out Pull Request #42264 for more details.

Share Logging Context Across Channels and Stacks

Tim MacDonald contributed sharing contextual information across all logging channels:

1// In a service provider...
2public function boot()
3{
4 Log::shareContext([
5 'invocation-id' => (string) Str::uuid(),
6 ]);
7}

See contextual information in the logging documentation.

Delaying Notifications Per Channel

You can pass an array to the delay method to specify the delay amount for specific channels:

1$user->notify((new InvoicePaid($invoice))->delay([
2 'mail' => now()->addMinutes(5),
3 'sms' => now()->addMinutes(10),
4]));

You can also define a withDelay method on the notification class:

1/**
2 * Determine the notification's delivery delay.
3 *
4 * @param mixed $notifiable
5 * @return array
6 */
7public function withDelay($notifiable)
8{
9 return [
10 'mail' => now()->addMinutes(5),
11 'sms' => now()->addMinutes(10),
12 ];
13}

See Delaying Notifications Per Channel in the notifications documentation.

Release Notes

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

v9.12.0

Added

  • Added closure based exceptions testing (#42155)
  • Allow forcing requests made via the Http client to be faked (#42230)
  • Added 'throwIf' method to PendingRequest (#42260)
  • Allow passing key/value arrays to getArguments and getOptions (#42268)
  • Add whereNotMorphedTo, orWhereNotMorphedTo (#42264)
  • Add method to extend localeArray generation (#42275)
  • Added ability to set delay per channel based on notifiable instance (#42239)
  • Added Illuminate/Pagination/CursorPaginator::onLastPage() (#42301)
  • Added findOr method to Query/Builder (#42290)

Fixed

  • Fix too many channels with pusher broadcasting (#42287)
  • Fix Str::Mask() for repeating chars (#42295)
  • Fix EventFake::assertListening() for asserting string-based observer listeners (#42289)
  • Fixed Loose comparison causes the value not to be saved (#41337)
  • Fix multiple dots for digits_between rule (#42330)

Changed

  • Enable to modify HTTP Client request headers when using beforeSending() callback (#42244)
  • Make throttle lock acquisition retry configurable for concurrency limiter (#42242)
  • Defers expanding callables on Factories (#42241)
  • Add wherehas soft deleting scopes (#42100)
  • Improve password checks (#42248)
  • Set relation parent key when using forceCreate on HasOne and HasMany relations (#42281)
  • Make sure the prefix override behaviours are the same between phpredis and predis drivers (#42279)
  • Share logging context across channels and stacks (#42276)

Filed in:

Paul Redmond

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

Laravel News Partners