Laravel 9.12 发布

Laravel 团队发布了 9.12,其中包含延迟每个通道的通知、跨通道共享日志上下文、在测试时防止杂散的 HTTP 请求等等:

基于闭包的异常测试

Karel Faille 贡献了 assertThrows() 方法,使用闭包测试异常处理的方法:

// 确保抛出异常
$this->assertThrows(fn () => throw Exception(''));

// 确保抛出特定类型的异常
$this->assertThrows(
    fn () => (new SomeActionThatThrowsExceptions)->execute(),
    CustomException::class
);

// 确保抛出带有特定消息的异常
$this->assertThrows(fn () => throw Exception('My message'), Exception::class, 'My message');

强制在测试中伪造 HTTP 请求

Tim MacDonald 贡献了使用 Laravel 的 HTTP 客户端强制伪造所有 HTTP 请求。如果测试中没有伪造 HTTP 请求,则测试将抛出异常:

protected function setUp(): void
{
    parent::setUp();

    Http::preventStrayRequests();
}

public function testItDoesTheThingWithoutFaking(): void
{
    $this->post('endpoint-that-utilises-the-http-facade');

   // RuntimeException:在没有匹配伪造的情况下尝试向 [https://acme.com] 请求。

   /* ... */
}

"Throw If" HTTP 客户端方法

@denniseilander 为 HTTP 客户端贡献了一个 throwIf() 方法,如果条件为 true 则抛出异常:

// 抛出 RequestException 异常
return Http::baseUrl('https://foo.bar')
       ->throwIf(true)
       ->get('not-found');

// 不抛出
return Http::baseUrl('https://foo.bar')
       ->throwIf(false)
       ->get('not-found');

允许将 Key/Value 数组传递给 Artisan 选项和参数

Jesper Noordsij 将 key/value 数组传递给 Artisan 命令上的 getArguments 和 getOptions 为了稍微简单的接口。

// 此外,可以跳过你不想使用的那些,而不是 providing/copying 默认值

// 例如:
public function getArguments()
{
    return [
        ['name' => 'argument', 'default' => 'default']
    ];

    // which previously would have been...
    // return [
    //     ['argument', null, '', 'default']
    // ];

请参见 Pull Request #42268 了解更多详情。

更多新的条件方法

Patrick O'Meara 贡献了 whereMorphedToorWhereMorphedTo 方法。以下是一个来自 PR 测试的例子:

$model = new EloquentBuilderTestModelParentStub;

$this->mockConnectionForModel($model, '');

$relatedModel = new EloquentBuilderTestModelCloseRelatedStub;

$relatedModel->id = 1;

$builder = $model->whereNotMorphedTo('morph', $relatedModel);

$this->assertSame('select * from "eloquent_builder_test_model_parent_stubs" where not ("morph_type" = ? and "morph_id" = ?)', $builder->toSql());

$this->assertEquals([$relatedModel->getMorphClass(), $relatedModel->getKey()], $builder->getBindings());

查看 Pull Request #42264 了解更多详情。

跨通道和堆栈共享日志上下文

Tim MacDonald 贡献了跨所有日志通道共享上下文信息:

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

请参见 contextual information 日志文档。

每个通道延迟通知

你可以向延迟方法传递一个数组,以指定特定通道的延迟量:

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

你也可以在 notification 类中定义一个 withDelay 方法:

/**
 * Determine the notification's delivery delay.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function withDelay($notifiable)
{
    return [
        'mail' => now()->addMinutes(5),
        'sms' => now()->addMinutes(10),
    ];
}

请参见 Delaying Notifications Per Channel 通知文档。

发行说明

你可以在下面看到新的功能和更新,以及在 GitHub 看 9.11.0 和 9.12.0 之间的差异。以下发行说明直接来自 changelog:

v9.12.0

新增

  • 新增基于闭包的异常测试 (#42155)
  • 允许通过强制 Http 客户端发出的请求被伪造 (#42230)
  • 新增 'throwIf' 方法到 PendingRequest (#42260)
  • 允许传递 key/value 数组给 getArguments 和 getOptions 方法 (#42268)
  • 新增 whereNotMorphedTo, orWhereNotMorphedTo (#42264)
  • 新增扩展本地数组生成方法 (#42275)
  • 新增基于应通知实例 (#42239)
  • 新增 Illuminate/Pagination/CursorPaginator::onLastPage() (#42301)
  • 新增 findOr 方法到 Query/Builder (#42290)

已修复

  • 修复推送式广播频道过多的问题 (#42287)
  • 修复 Str::Mask() 用于重复字符 (#42295)
  • 修复 EventFake::assertListening() 用于断言基于字符串的观察者侦听器 (#42289)
  • 修复松散比较导致值无法保存的问题 (#41337)
  • 修复 Digits_between 规则的多个点 (#42330)

已更改

  • 启用在使用 beforeSending() 回调时修改HTTP客户端请求头 (#42244)
  • 使限制锁获取重试可配置为并发限制器 (#42242)
  • 推迟工厂可调用的扩展 (#42241)
  • 添加 wherehas 软删除作用域 (#42100)
  • 改进密码检查 (#42248)
  • 当使用 forceCreate 在 HasOne 和 HasMany 关系时设置父关系关联的 key (#42281)
  • 确保 phpredis 和 predis 驱动程序之间的前缀覆盖行为相同 (#42279)
  • 跨通道和堆栈共享日志上下文 (#42276)
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://laravel-news.com/laravel-9-12-0

译文地址:https://learnku.com/laravel/t/67902

本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 2

9.12.0有一个BUG:如果模型工厂的默认值定义了一个匿名函数会在保存到数据库的时候抛出异常。类似:

Error: Object of class \Database\Factories\PostFactory could not be converted to string

修复补丁已经合并等着发行修复版本了。

Ref: github.com/laravel/framework/pull/...

1年前 评论

发版狂躁症

1年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!