tests

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Example (RouteSharedMemory)
root, err := fs.Sub(fixturesFS, "fixtures/route")
if err != nil {
	fmt.Println(err)
	return
}

shm := ps.NewSharedMemory()
mux := http.NewServeMux()
_, err = route.NewService(root, mux, route.WithRuntimeFunc(func(rt *runner.Runtime) {
	rt.SetContext(ps.SharedMemoryContext(rt.Context(), shm))
	rt.RegisterConstructor("SharedMemory", ps.NewSharedMemoryBinding)
}))
if err != nil {
	fmt.Println(err)
	return
}

post := httptest.NewRequest(http.MethodPost, "/kv/color", strings.NewReader("value=blue"))
post.Header.Set("Content-Type", "application/x-www-form-urlencoded")
postRR := httptest.NewRecorder()
mux.ServeHTTP(postRR, post)
fmt.Println(postRR.Body.String())

getRR := httptest.NewRecorder()
mux.ServeHTTP(getRR, httptest.NewRequest(http.MethodGet, "/kv/color", nil))
fmt.Println(getRR.Body.String())

statsRR := httptest.NewRecorder()
mux.ServeHTTP(statsRR, httptest.NewRequest(http.MethodGet, "/stats/requests", nil))
fmt.Println(statsRR.Body.String())
Output:
ok
blue
2

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResetCaches added in v0.2.1

func ResetCaches()

ResetCaches clears all global shared caches between test suites.

func TestMain added in v0.2.0

func TestMain(m *testing.M)

func WriteHTMLReport added in v0.2.1

func WriteHTMLReport(w io.Writer, results []*TestResult) error

WriteHTMLReport outputs an HTML report of the test results.

func WriteJSONReport added in v0.2.1

func WriteJSONReport(w io.Writer, results []*TestResult) error

WriteJSONReport outputs a JSON report of the test results.

Types

type Fixture added in v0.2.1

type Fixture struct {
	Name        string          `yaml:"name"`
	Description string          `yaml:"description"`
	Error       string          `yaml:"error"`     // optional: expected uncaught error substring
	Stdin       string          `yaml:"stdin"`     // optional: top-level stdin contents
	Flatstack   bool            `yaml:"flatstack"` // optional: also test on flatstack bytecode
	Request     FixtureRequest  `yaml:"request"`
	Response    FixtureResponse `yaml:"response"`

	PHP      string `yaml:"-"`
	Expected string `yaml:"-"`
	Path     string `yaml:"-"`
}

Fixture is the parsed representation of a .phpt test file.

func FindFixtures added in v0.2.1

func FindFixtures(paths []string) ([]*Fixture, error)

FindFixtures locates all .phpt files in the specified paths (files or directories).

func ParseFixture added in v0.2.1

func ParseFixture(data []byte, path ...string) (*Fixture, error)

ParseFixture splits a .phpt file into its three sections and parses the YAML metadata.

type FixtureRequest added in v0.2.1

type FixtureRequest struct {
	Args    any               `yaml:"args"` // map[string]string or []string or scalar
	Get     map[string]string `yaml:"get"`
	Post    map[string]string `yaml:"post"`
	Cookie  map[string]string `yaml:"cookie"`
	Env     map[string]string `yaml:"env"`
	Headers map[string]string `yaml:"headers"`
	Stdin   string            `yaml:"stdin"`
	Body    string            `yaml:"body"`
}

FixtureRequest contains request data exposed to PHP as superglobals.

type FixtureResponse added in v0.2.1

type FixtureResponse struct {
	Headers map[string]string `yaml:"headers"`
}

FixtureResponse contains expected response assertions.

type Record

type Record struct {
	Key   string
	Value string
}

Record is a rich value type returned to PHP. Its exported fields are read from PHP via property access (`$rec->key`, `$rec->value`), matched case-insensitively.

type Storage

type Storage interface {
	Set(ctx context.Context, key, value string)
	Get(ctx context.Context, key string) (Record, error)
	All(ctx context.Context) ([]Record, error)
	Len() int64
	Tenant() string
}

Storage is a host-provided key/value capability whose context parameters are supplied automatically when PHP invokes its methods.

Example
ctx := context.WithValue(context.Background(), tenantKey, "acme")
storage, err := NewStorage(ctx)
if err != nil {
	fmt.Println(err)
	return
}

storage.Set(ctx, "color", "blue")
record, err := storage.Get(ctx, "color")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(storage.Tenant())
fmt.Println(record.Key, record.Value)
fmt.Println(storage.Len())
Output:
acme
color blue
1

func NewFailStorage

func NewFailStorage(ctx context.Context) (Storage, error)

NewFailStorage is a constructor that always fails, used to exercise the thrown-error path of `new`.

func NewStorage

func NewStorage(ctx context.Context) (Storage, error)

NewStorage is the constructor registered for `new Storage`. Its first parameter is a context.Context, filled in automatically by the runner, so PHP calls `new Storage` with no arguments.

type TestResult added in v0.2.1

type TestResult struct {
	Name          string `json:"name"`
	Description   string `json:"description"`
	Path          string `json:"path"`
	Passed        bool   `json:"passed"`
	DurationMs    int64  `json:"duration_ms"`
	FailureReason string `json:"failure_reason,omitempty"`
	GotOutput     string `json:"got_output,omitempty"`
	WantOutput    string `json:"want_output,omitempty"`
	Error         string `json:"error,omitempty"`
	FlatstackUsed bool   `json:"flatstack_used,omitempty"`
}

TestResult carries execution outcome for a single fixture.

func RunFixture added in v0.2.1

func RunFixture(ctx context.Context, f *Fixture) *TestResult

RunFixture executes a single fixture against the runner (and flatstack if requested).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL