Testing | laravel-analytics-go | Spatie       

 SPATIE  

laravel-analytics-go
====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-analytics-go](https://spatie.be/docs/laravel-analytics-go/v1)  Testing

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-analytics-go/v1) 

  Testing    
- [ Introduction ](https://spatie.be/docs/laravel-analytics-go/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-analytics-go/v1/support-us)
- [ Installation and setup ](https://spatie.be/docs/laravel-analytics-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-analytics-go/v1/usage)
- [ Testing ](https://spatie.be/docs/laravel-analytics-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-analytics-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-analytics-go/v1/changelog)

 Testing
=======

###  On this page 

1. [ Faking ](#content-faking)
2. [ Package tests ](#content-package-tests)

Faking
--------------------------------------------------------------------------

To fake calls within your own project, supply a replacement client when registering the package:

```
fake := analytics.Fake(analytics.Row{
    "pageTitle": "Test Page",
    "activeUsers": int64(10),
    "screenPageViews": int64(20),
})

app := laraveltest.New(t, laravel.Options{
    Packages: []laravel.Package{
        analytics.New(func(cfg *analytics.Config) { cfg.Client = fake }),
    },
    // Add the application's routes here.
})
```

Use `analytics.Fake()` for empty results. Every named report, `Get` and `GetRealtime` returns the supplied rows without reading credentials, accessing cache, or sending HTTP requests. Each call copies the row maps. Keep fake values to the normal scalar/date row types rather than nested mutable values.

The fake is app-scoped. Tests using different apps cannot overwrite each other's clients. It is a high-level result fake, not a raw Google SDK fake.

For request-level tests, create a Google SDK service with `option.WithHTTPClient(server.Client())` and `option.WithEndpoint(server.URL + "/")`, then supply it through `Config.Service`. Use `httptest.NewServer` for the local endpoint. Laravel's `httpclienttest` does not intercept Google's `net/http` SDK.

Use `Config.Cast` to replace casting and `Config.Cache` to inject cache errors or alternative backends. The test suite exercises both through the public client and real Laravel request contexts. No second service-container registration is needed for replacements.

Package tests
-----------------------------------------------------------------------------------------------

```
gofmt -w .
go vet ./...
go mod tidy -diff
go mod verify
go test -race -count=1 ./...
```

Tests use external Go packages, local HTTP servers, generated RSA keys and isolated `laraveltest.New` apps. They cover all nine PHP report definitions, pagination options, filters, standard and realtime request bodies, count/date casting, malformed reports, API errors, cancellation, cache hits/expiry, disabled caching, explicit replacements and repeated registration.

OAuth tests verify signed assertions, readonly scope, bearer headers, concurrent token reuse, cancellation recovery and blocked redirects. Generated keys have never been registered with Google. Tests do not require credentials or contact Google.

No live GA4 property has been tested. Local protocol tests do not establish real account access, quota or historical data. A credentialed check needs a separately approved test property.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
