chore: add tests regarding persistent objects

This commit is contained in:
Kévin Dunglas
2022-10-05 15:44:02 +02:00
parent d61c96a4c3
commit 4a87ad3609
5 changed files with 42 additions and 0 deletions

View File

@@ -294,6 +294,26 @@ func testPhpInfo(t *testing.T, opts *testOptions) {
}, opts)
}
func TestPersistentObject_module(t *testing.T) { testPersistentObject(t, nil) }
func TestPersistentObject_worker(t *testing.T) {
testPersistentObject(t, &testOptions{workerScript: "persistent-object.php"})
}
func testPersistentObject(t *testing.T, opts *testOptions) {
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
req := httptest.NewRequest("GET", fmt.Sprintf("http://example.com/persistent-object.php?i=%d", i), nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, string(body), fmt.Sprintf(`request: %d
class exists: 1
id: obj1
object id: 1`, i))
}, opts)
}
func ExampleExecuteScript() {
if err := frankenphp.Init(); err != nil {
panic(err)

View File

@@ -7,3 +7,5 @@ if (!isset($_SERVER['FRANKENPHP_WORKER'])) {
}
while (frankenphp_handle_request($fn)) {}
return;

View File

@@ -0,0 +1,6 @@
<?php
class MyObject
{
public function __construct(public string $id) {}
}

13
testdata/persistent-object.php vendored Normal file
View File

@@ -0,0 +1,13 @@
<?php
require_once __DIR__.'/_executor.php';
require_once __DIR__.'/persistent-object-require.php';
$foo = new MyObject('obj1');
return function () use ($foo) {
echo 'request: ' . $_GET['i'] . "\n";
echo 'class exists: ' . class_exists(MyObject::class) . "\n";
echo 'id: ' . $foo->id . "\n";
echo 'object id: '. spl_object_id($foo);
};

View File

@@ -64,6 +64,7 @@ func startWorkers(fileName string, nbWorkers int) error {
return
}
// TODO: check if the termination is expected
l.Debug("terminated", zap.String("worker", fileName))
}()
}