Laravel 9.5版本发布

2023-06-01 00:00:00 laravel 版本 发布

Laravel 团队发布了 9.5,其中包含部分队列伪造、一个 freezeTime() 测试助手、一个存储 assertDirectoryEmpty() 断言、assertJsonPath() 中的闭包等等:


集合Implode方法的回调支持

@Lito提供了回调支持Collect::implode()以简化->map()->implode()调用:

{{-- 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>


使用 Storage Fake 断言一个空目录

Mark Beech贡献了使用 Storage::fake() 实例断言空目录的能力:

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

如果目录中没有文件但有其他子目录,则断言将失败,因为它包含其他文件夹/文件。

这是拉取请求讨论中的一个示例:

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


JSON 断言“assertJsonPath()”现在接受闭包

Fabien Villepinte贡献了将闭包传递给assertJsonPath的能力,而没有任何向后兼容的中断:

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

虽然上面的示例使用字符串版本似乎更简单,但如果您需要围绕路径断言更复杂的逻辑,您现在可以使用闭包。


部分队列伪造

Taylor Otwell在测试中为队列作业贡献了部分伪造:

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

创建“直通”模型的新方法

Hafez DivandarihasOneThrough贡献了在不覆盖整体或hasManyThrough方法的情况下创建新“通过”模型的能力:

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


新的换行字符串助手

Markus Hebenstreit贡献了一个wrap()字符串助手。以下是拉取请求描述中的示例用法:

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!


用于测试的冻结时间助手

@Italo提供了一个freezeTime()测试方法,它将冻结测试中的当前时间:

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


该freezeTime()方法是以下的语法糖:

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


允许在 Http::beforeSending() 中调用 Callables

Dries Vints有助于在Http::beforeSending()方法中接受可调用对象,而不仅仅是可调用的类。

现在,以下示例将起作用,而不是获取“调用数组上的成员函数 __invoke()”:

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


发行说明

您可以在下面查看新功能和更新的完整列表以及GitHub 上9.4.0 和 9.5.0之间的差异。

以下发行说明直接来自变更日志:

https://github.com/laravel/framework/compare/v9.4.0...v9.5.0
https://github.com/laravel/framework/blob/650ca875d3bd7e876ed89f803c705d5d6c121d90/CHANGELOG.md#v950---2022-03-15


v9.5.0

添加

增加了对 implode Collection 方法的回调支持。(#41405)
添加Illuminate/Filesystem/FilesystemAdapter::assertDirectoryEmpty()(#41398)
为 SesTransport 实施电子邮件“元数据”(#41422)
使 assertPath() 接受闭包 ( #41409 )
在 Collection 上添加了对 operatorForWhere 的可调用支持(#41414,#41424)
添加了部分队列伪造(#41425)
为 schedule:test 命令添加了 --name 选项(#41439)
定义Illuminate/Database/Eloquent/Concerns/HasRelationships::newRelatedThroughInstance()(#41444)
添加Illuminate/Support/Stringable::wrap()(#41455)
为测试添加“freezeTime”助手(#41460)
允许使用 beforeSending in Illuminate/Http/Client/PendingRequest.php::runBeforeSendingCallbacks()( #41489 )调用

修复

修复了在过滤名称或域时来自 route:list 的弃用警告 ( #41421 )
当 URL 返回空状态代码时修复 HTTP::pool 响应 ( #41412 )
Illuminate/Session/Middleware/AuthenticateSession.php修复了( #41429 )中的召回者名称解析
修复 /Illuminate/Session/Middleware/AuthenticateSession.php 中使用的保护实例 ( #41447 )
修复 route:list --except-vendor 隐藏 Route::view() & Route::redirect() ( #41465 )

改变

在 \Illuminate\Database\Eloquent\Factories\Factory 中为连接属性添加空类型(#41418)
更新 GeneratorCommand 中的保留名称 ( #41441 )
重新设计 php artisan schedule:list 命令 ( #41445 )
扩展 eloquent 高阶代理属性 ( #41449 )
允许将命名参数传递给动态范围 ( #41478 )
Illuminate/Encryption/Encrypter.php如果标签通过但在( #41479 )中不受支持,则抛出
更新 PackageManifest::$vendorPath 初始化案例,当作曲家供应商目录不在项目主管中时(#41463)

转:

https://laravel-news.com/laravel-9-5-0

相关文章