cerbero / octane-testbench
一套用于测试由 Octane 驱动的 Laravel 应用程序的实用工具。
Requires
- php: ^8.0
- laravel/octane: ^1.0
- mockery/mockery: ^1.0
- phpunit/phpunit: >=9.3
Requires (Dev)
- orchestra/testbench: >=6.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-09-11 19:42:42 UTC
README
一套用于测试由 Octane 驱动的 Laravel 应用程序的实用工具。
安装
通过 Composer
composer require --dev cerbero/octane-testbench
在 tests/TestCase.php
中,使用 TestsOctaneApplication
特性
use Cerbero\OctaneTestbench\TestsOctaneApplication; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use TestsOctaneApplication; }
现在所有扩展此类的测试都可以在 Octane 上运行,即使是之前创建的测试。
用法
总的来说,Octane Testbench
- 是渐进式的:现有测试仍能正常工作,使得现有 Laravel 应用的 Octane 采用更为容易
- 模拟工人和客户端:测试不需要 Swoole 或 RoadRunner 服务器来运行
- 在请求后保留应用程序状态,因此可以在响应后执行断言
- 提供针对 Octane 定制的流畅断言
public function test_octane_application() { $this ->assertOctaneCacheMissing('foo') ->assertOctaneTableMissing('example', 'row') ->assertOctaneTableCount('example', 0) ->expectsConcurrencyResults([1, 2, 3]) ->get('octane/route') ->assertOk() ->assertOctaneCacheHas('foo', 'bar') ->assertOctaneTableHas('example', 'row.votes', 123) ->assertOctaneTableCount('example', 1); }
请求和响应
使用与测试任何 Laravel 应用程序时相同的 方法 执行 HTTP 请求
Route::get('web-route', fn () => 123); Octane::route('POST', '/octane-route', fn () => new Response('foo')); public function test_web_route() { $this->get('web-route')->assertOk()->assertSee('123'); } public function test_octane_route() { $this->post('octane-route')->assertOk()->assertSee('foo'); }
响应被包装在 ResponseTestCase
实例中,允许我们调用 响应断言、任何 Laravel 测试套件 断言以及以下异常断言
$this ->get('failing-route') ->assertException(Exception::class) // assert exception instance ->assertException(new Exception('message')) // assert exception instance and message ->assertExceptionMessage('message'); // assert exception message
此外,可以通过调用 dd()
和 dump()
方法来调试响应和异常
$this ->get('failing-route') ->dump() // dump the whole response/exception ->dump('original') // dump only a specific property ->dd() // dump-and-die the whole response/exception ->dd('headers'); // dump-and-die only a specific property
并发
并发 在测试期间运行良好。然而,PHP 8 禁止序列化反射(因此是模拟)和并发任务在分发之前被序列化。如果任务涉及模拟,我们可以模拟并发
// code to test: Octane::concurrently([ fn () => $mockedService->run(), fn () => 123, ]); // test: $this ->mocks(Service::class, ['run' => 'foo']) ->expectsConcurrency() ->get('route');
在上面的测试中,我们正在按顺序运行任务而不进行序列化,允许模拟方法被执行(我们将在后面看到关于 模拟 的更多内容)。
如果我们需要更精确地控制并发任务的运行方式,我们可以向 expectsConcurrency()
传递一个闭包。例如,下面的测试只运行第一个任务
$this ->expectsConcurrency(fn (array $tasks) => [ $tasks[0]() ]) ->get('route');
要操作并发任务的结果,我们可以使用 expectsConcurrencyResults()
$this ->expectsConcurrencyResults([$firstTaskResult, $secondTaskResult]) ->get('route');
最后,我们可以使并发任务失败,以测试在发生错误时我们的代码
$this ->expectsConcurrencyException() // tasks fail due to a generic exception ->get('route'); $this ->expectsConcurrencyException(new Exception('message')) // tasks fail due to a specific exception ->get('route'); $this ->expectsConcurrencyTimeout() // tasks fail due to a timeout ->get('route');
缓存
Octane Testbench 提供以下断言来测试 Octane 缓存
$this ->assertOctaneCacheMissing($key) // assert the key is not set ->get('route') ->assertOctaneCacheHas($key) // assert the key is set ->assertOctaneCacheHas($key, $value); // assert the key has the given value
表
Octane 表 可以使用以下断言进行测试
$this ->assertOctaneTableMissing($table, $row) // assert the row is not present in the table ->assertOctaneTableCount($table, 0) // assert the number of rows in the table ->get('route') ->assertOctaneTableHas($table, $row) // assert the row is present in the table ->assertOctaneTableHas($table, 'row.column' $value) // assert the column in the row has the given value ->assertOctaneTableCount($table, 1);
事件
默认情况下,禁用 Octane RequestReceived
事件的监听器以执行应用程序状态断言。然而,如果需要,我们可以为任何 Octane 事件注册监听器
$this ->listensTo(RequestReceived::class, $listener1, $listener2) // register 2 listeners for RequestReceived ->get('route'); $this ->stopsListeningTo(TaskReceived::class, $listener1, $listener2) // unregister 2 listeners for TaskReceived ->get('route');
容器
Octane Testbench 还引入了以下辅助程序,以在保持流畅语法的同時綁定和 模拟服务
$this ->mocks(Service::class, ['expectedMethod' => $expectedValue]) // mock with simple expectations ->mocks(Service::class, fn ($mock) => $mock->shouldReceive('method')->twice()) // mock with advanced expectations ->partiallyMocks(Service::class, ['expectedMethod' => $expectedValue]) // same as above but partial ->partiallyMocks(Service::class, fn ($mock) => $mock->shouldReceive('method')->twice()) ->get('route');
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG
测试
composer test
贡献
请参阅 CONTRIBUTING 和 CODE_OF_CONDUCT 以获取详细信息。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 andrea.marco.sartori@gmail.com 联系,而不是使用问题跟踪器。
致谢
许可协议
MIT 许可协议 (MIT)。请参阅 许可文件 以获取更多信息。