Browse Source

chore: add tests regarding persistent objects

Kévin Dunglas 2 years ago
parent
commit
4a87ad3609
5 changed files with 42 additions and 0 deletions
  1. 20 0
      frankenphp_test.go
  2. 2 0
      testdata/_executor.php
  3. 6 0
      testdata/persistent-object-require.php
  4. 13 0
      testdata/persistent-object.php
  5. 1 0
      worker.go

+ 20 - 0
frankenphp_test.go

@@ -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)

+ 2 - 0
testdata/_executor.php

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

+ 6 - 0
testdata/persistent-object-require.php

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

+ 13 - 0
testdata/persistent-object.php

@@ -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);
+};

+ 1 - 0
worker.go

@@ -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))
 		}()
 	}