Laravel团队发布了9.12版本

120 阅读3分钟

Laravel团队发布了9.12版本,每个渠道的通知延迟,跨渠道共享日志上下文,在测试时防止stray HTTP请求,以及更多。

基于闭合的异常测试

Karel Faille贡献了一个assertThrows() 方法,它增加了一种使用闭包测试异常处理的方法。

 // Ensure that an exception is thrown
$this->assertThrows(fn () => throw Exception(''));
 
// Ensure that a specific type of exception is thrown
$this->assertThrows(
    fn () => (new SomeActionThatThrowsExceptions)->execute(),
    CustomException::class
);
 
// Ensure that an exception is thrown with a specific message
$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: Attempted request to [https://acme.com] without a matching fake.
 
   /* ... */
}

"Throw If" HTTP客户端方法

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

// Throws RequestException
return Http::baseUrl('https://foo.bar')
       ->throwIf(true)
       ->get('not-found');
 
// Doesn't throw
return Http::baseUrl('https://foo.bar')
       ->throwIf(false)
       ->get('not-found');

允许向Artisan选项和参数传递键/值数组

Jesper Noordsij贡献了在 Artisan 命令中向getArgumentsgetOptions 传递键/值数组的功能,使界面更简单。

 // Also, it is possible to skip those you don't want to use without providing/copying the default value
 
// e.g.
public function getArguments()
{
    return [
        ['name' => 'argument', 'default' => 'default']
    ];
 
    // which previously would have been...
    // return [
    //     ['argument', null, '', 'default']
    // ];
}

新的更多条件方法

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());

在通道和堆栈间分享日志上下文

Tim MacDonald贡献了在所有日志通道中分享上下文信息。

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

延迟每个通道的通知

你可以给延迟方法传递一个数组来指定特定通道的延迟量。

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

你也可以在通知类上定义一个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),
    ];
}

v9.12.0

新增

  • 增加了基于封闭的异常测试(#42155)。
  • 允许强迫通过Http客户端发出的请求是假的(#42230)
  • 为PendingRequest添加了 "throwIf "方法(#42260)
  • 允许向getArguments和getOptions传递键/值数组(#42268)
  • 增加whereNotMorphedTo, orWhereNotMorphedTo(#42264)
  • 增加扩展localeArray生成的方法(#42275)。
  • 增加了根据可通知实例设置每个通道延迟的能力(#42239)。
  • 增加了Illuminate/Pagination/CursorPaginator::onLastPage()(#42301)
  • 为查询/生成器添加了findOr方法(#42290)

修复了

  • 修复了推送者广播中的太多通道(#42287)
  • 修复Str::Mask()的重复字符(#42295)
  • 修复EventFake::assertListening()用于断言基于字符串的观察者监听器(#42289)。
  • 修复了松散的比较导致值不被保存(#41337)
  • 修复digits_between规则的多个点(#42330)

改变了

  • 当使用beforeSending()回调时,启用修改HTTP客户端请求头信息(#42244)
  • 使节流锁获取重试可配置为并发限制器(#42242)。
  • 推迟在工厂上扩展可调用程序(#42241)。
  • 增加wherehas软删除作用域(#42100)。
  • 改进密码检查(#42248)
  • 在HasOne和HasMany关系上使用forceCreate时设置关系父键(#42281)
  • 确保前缀覆盖行为在phpredis和predis驱动之间是相同的(#42279)
  • 跨渠道和堆栈共享日志上下文(#42276)