Files
frankenphp/watcher_test.go
Kévin Dunglas 225ca409d3 feat: hot reload (#2031)
This patch brings hot reloading capabilities to PHP apps: in
development, the browser will automatically refresh the page when any
source file changes!
It's similar to HMR in JavaScript.

It is built on top of [the watcher
mechanism](https://frankenphp.dev/docs/config/#watching-for-file-changes)
and of the [Mercure](https://frankenphp.dev/docs/mercure/) integration.

Each time a watched file is modified, a Mercure update is sent, giving
the ability to the client to reload the page, or part of the page
(assets, images...).

Here is an example implementation:

```caddyfile
root ./public


mercure {
      subscriber_jwt {env.MERCURE_SUBSCRIBER_JWT_KEY}
      anonymous
}

php_server {
      hot_reload
}
```

```php
<?php
header('Content-Type: text/html');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<script>
    const es = new EventSource('<?=$_SERVER['FRANKENPHP_HOT_RELOAD']?>');
    es.onmessage = () => location.reload();
</script>
</head>
<body>
Hello
```

I plan to create a helper JS library to handle more advanced cases
(reloading CSS, JS, etc), similar to [HotWire
Spark](https://github.com/hotwired/spark). Be sure to attend my
SymfonyCon to learn more!

There is still room for improvement:

- Provide an option to only trigger the update without reloading the
worker for some files (ex, images, JS, CSS...)
- Support classic mode (currently, only the worker mode is supported)
- Don't reload all workers when only the files used by one change

However, this PR is working as-is and can be merged as a first step.

This patch heavily refactors the watcher module. Maybe it will be
possible to extract it as a standalone library at some point (would be
useful to add a similar feature but not tight to PHP as a Caddy module).

---------

Signed-off-by: Kévin Dunglas <kevin@dunglas.fr>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-12 14:29:18 +01:00

76 lines
2.4 KiB
Go

//go:build !nowatcher
package frankenphp_test
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// we have to wait a few milliseconds for the watcher debounce to take effect
const pollingTime = 250
// in tests checking for no reload: we will poll 3x250ms = 0.75s
const minTimesToPollForChanges = 3
// in tests checking for a reload: we will poll a maximum of 60x250ms = 15s
const maxTimesToPollForChanges = 60
func TestWorkersShouldReloadOnMatchingPattern(t *testing.T) {
watch := []string{"./testdata/**/*.txt"}
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
requestBodyHasReset := pollForWorkerReset(t, handler, maxTimesToPollForChanges)
assert.True(t, requestBodyHasReset)
}, &testOptions{nbParallelRequests: 1, nbWorkers: 1, workerScript: "worker-with-counter.php", watch: watch})
}
func TestWorkersShouldNotReloadOnExcludingPattern(t *testing.T) {
watch := []string{"./testdata/**/*.php"}
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
requestBodyHasReset := pollForWorkerReset(t, handler, minTimesToPollForChanges)
assert.False(t, requestBodyHasReset)
}, &testOptions{nbParallelRequests: 1, nbWorkers: 1, workerScript: "worker-with-counter.php", watch: watch})
}
func pollForWorkerReset(t *testing.T, handler func(http.ResponseWriter, *http.Request), limit int) bool {
t.Helper()
// first we make an initial request to start the request counter
body, _ := testGet("http://example.com/worker-with-counter.php", handler, t)
assert.Equal(t, "requests:1", body)
// now we spam file updates and check if the request counter resets
for range limit {
updateTestFile("./testdata/files/test.txt", "updated", t)
time.Sleep(pollingTime * time.Millisecond)
body, _ := testGet("http://example.com/worker-with-counter.php", handler, t)
if body == "requests:1" {
return true
}
}
return false
}
func updateTestFile(fileName string, content string, t *testing.T) {
absFileName, err := filepath.Abs(fileName)
require.NoError(t, err)
dirName := filepath.Dir(absFileName)
if _, err = os.Stat(dirName); os.IsNotExist(err) {
err = os.MkdirAll(dirName, 0700)
}
require.NoError(t, err)
require.NoError(t, os.WriteFile(absFileName, []byte(content), 0644))
}