The Laravel team shipped Laravel 5.7.10 yesterday with a handful of new features, fixes, and changes:
loadCount Eloquent Collection Method
First up, is the ability to load relationship counts on an Eloquent collection. Before this feature, you could only load relationships, but now you can call loadCount() to get counts for all relations.
The pull request illustrates how you could use loadCount() with the following example:
$events = Event::latest()->with('eventable')->paginate(); $groups = $events->map(function ($event) { return $event->eventable;})->groupBy(function ($eventable) { return get_class($eventable);}); $groups[Post::class]->loadCount('comments');$groups[Comment::class]->loadCount('hearts'); return new EventIndexResponse($events);
Now all the eventable models would have a count value for comments or hearts respectively. The loadCount feature was added by Tim MacDonald. Tim is the author of the Laravel Log Fake package that we featured earlier this month. Nice work Tim!
Pass a Model to assertSoftDeleted
You can use the assertSoftDeleted to assert that a model has been soft deleted. Before Laravel 5.7.10, the method call might look something like the following:
$this->assertSoftDeleted('users', [ 'id' => $user->id, 'name' => $user->name, 'email' => $deletedUser->email,]);
Now, thanks to an update to the assertSoftDeleted method, you can pass the model directly which will assert against the table’s primary key and primary key value:
$this->assertSoftDeleted($user); // Same as:$this->assertSoftDeleted('users', ['id' => $user->id]);
Thanks to Dwight Watson for this nice shortcut!
Add Mock and Spy Objects to the Container
Titas Gailius added the ability to more conveniently add mock and spy instances to the container in a test:
$this->mock(Mailer::class, function ($mock) { $mock->shouldReceive('send')->once();}); $this->spy(Dispatcher::class, function ($mock) { $mock->shouldReceive('dispatch')->andReturn(false);});
These methods are syntactic sugar for something like the following:
$this->instance(Mailer::class, Mockery::mock(Mailer::class, function ($mock) { $mock->shouldReceive('send')->once();}));
UUID Validation
André Filipe contributed a UUID validation method which uses a regex to avoid dependency of ramsey/uuid in the illuminate/validation component.
The name of the validation rule is uuid:
$request->validate([ 'post' => 'required|uuid']);
Notification Fake can Assert a Preferred Locale
Derek MacDonald added the ability to assert the preferred notification fake locale on notifications that implement the HasLocalePreference interface:
Notification::fake(); $user = factory(User::class)->create(['locale' => 'au']); // Call an artisan console command that invokes:Notification::send($user, new AFLTrade('Chad Wingard', 'Hawthorn')); Notification::assertSentTo($user, AFLTrade::class, function ($notification, $channels, $notifiable, $locale) use ($user) { return $notification->player === 'Chad Wingard' && $notification->club === 'Hawthorn' && $notifiable === $user && $locale === 'au';});
You can see the full diff between 5.7.9 and 5.7.10 on GitHub, and the full release notes below from the 5.7 changelog:
v5.7.10
Added
- Added loadCount method to eloquent collections (#25997)
- Added support for identity columns in PostgreSQL 10+ (#26096)
- Allowed passing a model instance directly to
assertSoftDeletedmethod inFoundation/Testing/Concerns/InteractsWithDatabase.php(#26133 , #26148) - Added possibility to define exclude methods on registered
apiResource(#26149) - Added
filp/whoopstosuggestincomposer.json(#26180) - Added
mockandspymethods toFoundation/Testing/Concerns/InteractsWithContainer.php(#26171, b50f9f3) - Added
uuidvalidation rule to validator (#26135) - NotificationFake can assert preferred locale (#26205)
Fixed
- Fixed
whereHasand$withCountbindings frompolymorphic relationships(#26145) - Fixed
getTablemethod in Model (#26085) - Fixed filesystem locking hangs in
PackageManifest::build()(#26010, 98b8256) - Fixed
Illuminate/Http/Testing/File.phpfor Symfony 4.1 components (#26080) - Fixed URL in
Notifications/resources/views/email.blade.php(22ca105) - Fixed
hasValidSignaturemethod when someone send anullsignature inUrlGenerator.php(#26132) - Fixed autocomplete for container in ServiceProvider for cases when someone developed packages (#26063)
- Fixed
ColumnDefinition::defaulttypehint (#26041)
Changed
- Define mix as const in
react-stubs/webpack.mix.jsandvue-stubs/webpack.mix.jspresets (#26119) - Make
assertSessionHasNoErrorsinTestResponse.phpprint the unexpected errors (#26039, e6bdf8a) - Replaced the remaining occurrences of
newQuery()tonewModelQuery()in UPDATE/DELETE queries. (#26158) - Improved
findOrFail()exceptions inBelongsToMany.phpandHasManyThrough.phprelations (#26182)
Changed realization
- Reversed ternary condition in
Arr::wrapto make it clearer (#26150) - Simplified
formatActioninUrlGenerator.php(#26121) - Simplified
isChainOfObjectsmethod inSupport/Testing/Fakes/QueueFake.php(#26151) - Deleted unneeded code (#26053, #26162, #26160, #26159, #26152)
- Prefer stricter comparison (#26139, #26157)
- Removed duplicated code from
Router::updateGroupStackmethod (#26206, 6debff6)