Laravel Go integration
Beta
The Laravel Go integration is tested in CI, but its API may still change.
The go.puth.io/laravel module connects the Go client to Laravel Go. The application of a test answers the requests of its browsers inside the test process, through portal. The tests need no running server, and the browser sees the database, the sessions, the clock and the fakes of the test.
Install the module
go get go.puth.io/laravelThe integration needs Laravel Go 0.2 and the Go version that Laravel Go asks for, which is newer than the 1.25 of the client alone.
Start the server
Run a Puth server as described in Getting started. Tests connect to http://127.0.0.1:7345; set PUTH_INSTANCE_URL to use another instance.
Write a test
puthlaravel.NewBrowser(t, client) starts a browser for the test client of your application. Paths like /notes are resolved against the APP_URL of the application:
package feature_test
import (
"testing"
"go.laravel.com/db"
puthlaravel "go.puth.io/laravel"
"app/tests"
)
func TestANoteIsAdded(t *testing.T) {
client := tests.New(t)
browser := puthlaravel.NewBrowser(t, client)
browser.Visit("/notes").Type("body", "Buy milk").Press("Add").AssertSee("Buy milk")
client.AssertDatabaseHas("notes", db.Values{"body": "Buy milk"})
}Run your tests
go test ./...Open the Puth GUI to follow each test while it runs.
How a test runs
puthlaravel.NewBrowser is puth.NewBrowser with a portal for the APP_URL of the application, and returns a browser with every method of the Go client. The browser is closed when the test ends. A request of the browser is sent through the application with the Do of a test client, as client.Get and client.Post do it. Requests to other URLs than APP_URL, like a CDN, load normally.
The browser reaches the application of its own test, also when tests run in parallel with t.Parallel().
Requests are answered on the goroutine of the test, while the test is inside a call on its browser. Pass puth.Concurrent() to answer them at any time instead, with what that costs. The options of the Go client go after the client:
browser := puthlaravel.NewBrowser(t, client, puth.Concurrent(), puth.Viewport(390, 844))Server-sent events, long polling and WebSockets do not work through the application of the test; see the limits of portal.
Everything else about a browser is described under Go client: method names, optional parameters and failures.
Shared state
The test and the requests of its browser use the same application, so they share what the test set up:
func TestTheBrowserSeesWhatTheTestWrote(t *testing.T) {
client := tests.New(t)
if err := model.Create(client.Context(), &models.Note{Body: "Written by the test"}); err != nil {
t.Fatal(err)
}
puthlaravel.NewBrowser(t, client).Visit("/notes").AssertSee("Written by the test")
}This also holds for what the test changes in its application, such as the time:
func TestTheBrowserSeesTheTimeOfTheTest(t *testing.T) {
client := tests.New(t)
client.TravelTo(client.Now().AddDate(2, 0, 0))
puthlaravel.NewBrowser(t, client).Visit("/").AssertSee(fmt.Sprintf("Year %d", time.Now().Year()+2))
}Fakes and swapped services
A fake that the test installs, and a service that it replaces, are those of the requests of its browser too:
func TestTheBrowserGetsAFakedHTTPResponse(t *testing.T) {
client := tests.New(t)
stock := httpclienttest.New(client).Respond("*/stock", httpclienttest.Text("7"))
services.From(client.Context()).Shipping = &express{}
browser := puthlaravel.NewBrowser(t, client)
browser.Visit("/orders").AssertSee("In stock: 7").AssertSee("Express shipping")
stock.AssertSentCount(1)
}The same holds for eventtest, queuetest, mailtest and the other fakes of Laravel: the browser triggers what the fake records. A mock of your own may call t.Fatal when a request calls it in a way the test did not expect, because requests are answered on the goroutine of the test. Install fakes before the browser makes the requests that use them.
Give every test a database of its own, for example SQLite in memory with DATABASE_URL=sqlite::memory: and laraveltest.RefreshDatabase in tests.New.
Several browsers
Every puthlaravel.NewBrowser(t, client) is a browser with a session of its own. All of them get the same client:
func TestEveryBrowserHasItsOwnSession(t *testing.T) {
client := tests.New(t)
ada, grace := puthlaravel.NewBrowser(t, client), puthlaravel.NewBrowser(t, client)
ada.Visit("/").Type("name", "Ada").Press("Save").AssertSee("Hello Ada")
grace.Visit("/").Type("name", "Grace").Press("Save").AssertSee("Hello Grace")
}A browser keeps its own cookies. It does not get those that the test client kept from client.Get or client.Post, and the client does not get those of a browser.
Act as a user
browser.ActingAs(&user) makes the requests of a browser those of a user from then on, without the login form. It is what client.ActingAs is for a request of a test, and needs nothing in the application:
func TestTheDashboardGreetsTheUser(t *testing.T) {
client := tests.New(t)
ada := models.User{Name: "Ada", Email: "ada@example.com", Password: "password"}
if err := model.Create(client.Context(), &ada); err != nil {
t.Fatal(err)
}
browser := puthlaravel.NewBrowser(t, client)
browser.ActingAs(&ada).Visit("/dashboard").AssertSee("Dashboard of Ada")
}Every browser has its own user, and a browser starts as a guest, also when the client it was started with acts as a user. browser.Guest() makes it one again:
first.ActingAs(&ada).Visit("/dashboard").AssertSee("Dashboard of Ada")
second.ActingAs(&grace).Visit("/dashboard").AssertSee("Dashboard of Grace")
first.Guest().Refresh().AssertDontSee("Dashboard")This is not a login in the session, so the logout of the application does not end it, and Guest() leaves a login in the session as it is.
ActingAs, Guest and As start a chain. The other methods return the browser of the Go client, which does not have them, so browser.Visit("/").ActingAs(&ada) does not compile.
A browser with a client of your own
browser.As(client) lets a browser be answered by a test client as you set it up, with its headers or its fakes:
tenant := client.NewBrowser().WithHeader("X-Tenant", "acme")
browser.As(tenant).Visit("/orders")Start from client.NewBrowser(). A test client keeps the cookies of its responses and adds them to every request that lacks them, so two browsers on clients that share those cookies get the session of each other. Without As, a browser is answered by a client without kept cookies for this reason, which is also why the headers of the client passed to NewBrowser don't count.
Example application
workspaces/clients/go/workspaces/laravel in the Puth repository is a Laravel Go application with Inertia and Vue. Its tests cover pages, forms and sessions, the shared database, fakes and swapped services, ActingAs with several browsers, validation errors, and every browser method once passing and once failing. Use it as a starting point or to check your setup.
