savvywombat / laravel-test-utils
Laravel 基础应用测试的实用工具和辅助函数
0.6
2020-09-17 22:21 UTC
Requires
- php: ^7.2
- fzaninotto/faker: ^1.9
- guzzlehttp/guzzle: ^7.0
README
Laravel 基础应用测试的实用工具和辅助函数
composer require --dev savvywombat/laravel-test-utils
数据库类型转换辅助函数
use SavvyWombat\LaravelTestUtils\DBCast
DBCast::toJson
当需要在数据库中断言(如使用 assertDatabaseHas 或 assertDatabaseMissing)时,使用此辅助函数将数组或 JSON 字符串转换为 JSON 数据类型。
$this->assertDatabaseHas('vehicles', [ 'id' => 1, 'manufacturer' => 'Toyford', 'model' => 'Llama', 'attributes' => DBCast::toJson([ 'color' => 'indigo green', 'engine' => '2 litres 4-cylinder', 'gearbox' => '6-speed manual', 'doors' => '5', ]), ]);
DBCast::toTimestamp
使用此辅助函数来断言 Datetime 或 Carbon 实例与数据库中的时间戳。
$trip = Trip::factory()->create(); Carbon::setTestNow("2020-10-20 10:15:43"); $response = $this->get('/start-trip'); $response->assertStatus(200) ->assertSee('Trip started'); $this->assertDatabaseHas('trips', [ 'id' => $trip->id, 'trip_started_at' => DBCast::toTimestamp(Carbon::now()), ]); Carbon::setTestNow();
Mock Guzzle
此特性假设你正在使用 Laravel 的 IoC 将 Guzzle 客户端注入到你的代码中。
假设我们正在测试一个带有 Google Recaptcha 的联系表单
namespace App\Http\Controllers; use GuzzleHttp\Client; use Illuminate\Http\Request; class ContactController extends Controller { public function send(Request $request, Client $client) { // Validate ReCaptcha $response = $this->client->post(config('recaptcha.url'), [ 'query' => [ 'secret' => config('recaptcha.secret'), 'response' => $request->input(['g-recaptcha-token'], ''), ] ]); if (json_decode($response->getBody())->success) { redirect()->route('contact::success'); } else { redirect()->route('contact::form')->withErrors(['g-recaptcha-token' => 'Please confirm you are a human!']); } }
namespace Tests\Feature; use GuzzleHttp\Psr7\Response; use SavvyWombat\LaravelTestUtils\MocksGuzzle; use Tests\TestCase; class MyTest extends TestCase { use MocksGuzzle; /** @test */ public function it_reacts_appropriately_to_recaptcha_success() { $this->guzzle() // this is guzzle's MockHandler class ->append(new Response(200, [], json_encode(['success' => 'true']))); $this->post('/some-url', ['message' => 'some message', 'g-recaptcha-token' => 'blah']) ->assertStatus(302) ->assertSessionHasNoErrors() ->assertRedirect('/success'); } /** @test */ public function it_errors_on_recaptcha_failure() { $this->guzzle() // this is guzzle's MockHandler class ->append(new Response(200, [], json_encode(['success' => 'false']))); $this->post('/some-url', ['message' => 'some message', 'g-recaptcha-token' => 'blah']) ->assertStatus(302) ->assertSessionHasErrors('g-recaptcha-token') ->assertRedirect('/some-url'); } }
如果你的控制器或其他代码需要向 API 发送多个请求,你可以将响应链式调用到 Guzzle 处理程序
$this->guzzle() ->append(new Response(200, [], json_encode(['invoices' => ['id' => '1245', 'id' => '1247']]))) ->append(new Response(404));
注意事项
目前,Guzzle 模拟不会断言或检查你正在进行的请求的相关内容 - 它只是简单地测试你的代码对特定响应的反应,而不必实际向远程 API 发送请求。
支持
如果你在此存储库中遇到一般问题,请通过 SavvyWombat 网站联系我们。
请使用 GitHub 问题跟踪器 报告问题。你还可以fork存储库并提交拉取请求。
如果你正在使用此存储库,我们很乐意听听你的想法。谢谢!
许可证
此软件包根据 MIT 许可证 (MIT) 许可。