Callable fake 是 Tim Macdonald 的一個 PHP 測試實用程序,它 “允許您偽造、捕獲和斷言對可調用 / 閉包的調用”。在某些情況下,此套件可以幫助在測試中允許開發人員傳遞一個 callable。
它有一個受Laravel 虛構啟發的API,如下所示:
// Before, you might collect callables to assert later... public function testEachLoopsOverAllDependencies(): void { // arrange $received = []; $expected = factory(Dependency::class)->times(2)->create(); $repo = $this->app[DependencyRepository::class]; // act $repo->each(function (Dependency $dependency) use (&$received): void { $received[] = $dependency; }); // assert $this->assertCount(2, $received); $this->assertTrue($expected[0]->is($received[0])); $this->assertTrue($expected[1]->is($received[1])); }
使用此軟體包,您可以使用類似以下內容的內容:
public function testEachLoopsOverAllDependencies(): void { // arrange $callable = new CallableFake(); $expected = factory(Dependency::class)->times(2)->create(); $repo = $this->app[DependencyRepository::class]; // act $repo->each($callable); // assert $callable->assertTimesInvoked(2); $callable->assertCalled(function (Depedency $dependency) use ($expected): bool { return $dependency->is($expected[0]); }); $callable->assertCalled(function (Dependency $dependency) use ($expected): bool { return $dependency->is($expected[1]); }); }
該包提供了諸如assertCalled、assertNotCalled、assertInvoked 等斷言。有關詳細資訊和範例,請務必查看項目自述文件中的可用 assertions 的完整清單。
你可以在 GitHub 上了解更多關於此軟體包的信息,獲取完整的安裝說明,並在 timacdonald/callable-fake 上查看原始程式碼。
以上是【 callable-fake】虛構你的可呼叫函數以加速測試的詳細內容。更多資訊請關注PHP中文網其他相關文章!