PHP code example of savvywombat / laravel-test-utils
1. Go to this page and download the library: Download savvywombat/laravel-test-utils library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
savvywombat / laravel-test-utils example snippets
use SavvyWombat\LaravelTestUtils\DBCast
$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',
]),
]);
$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();
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');
}
}
$this->guzzle()
->append(new Response(200, [], json_encode(['invoices' => ['id' => '1245', 'id' => '1247']])))
->append(new Response(404));