@@ -7,11 +7,6 @@ import (
77 "github.com/stretchr/testify/require"
88)
99
10- // resetModuleWorkers resets the moduleWorkerConfigs slice for testing
11- func resetModuleWorkers () {
12- moduleWorkerConfigs = make ([]workerConfig , 0 )
13- }
14-
1510func TestModuleWorkerDuplicateFilenamesFail (t * testing.T ) {
1611 // Create a test configuration with duplicate worker filenames
1712 configWithDuplicateFilenames := `
@@ -38,53 +33,6 @@ func TestModuleWorkerDuplicateFilenamesFail(t *testing.T) {
3833 // Verify that an error was returned
3934 require .Error (t , err , "Expected an error when two workers in the same module have the same filename" )
4035 require .Contains (t , err .Error (), "must not have duplicate filenames" , "Error message should mention duplicate filenames" )
41- resetModuleWorkers ()
42- }
43-
44- func TestModuleWorkersDuplicateNameFail (t * testing.T ) {
45- // Create a test configuration with a worker name
46- configWithWorkerName1 := `
47- {
48- php_server {
49- worker {
50- name test-worker
51- file ../testdata/worker-with-env.php
52- num 1
53- }
54- }
55- }`
56-
57- // Parse the first configuration
58- d1 := caddyfile .NewTestDispenser (configWithWorkerName1 )
59- module1 := & FrankenPHPModule {}
60-
61- // Unmarshal the first configuration
62- err := module1 .UnmarshalCaddyfile (d1 )
63- require .NoError (t , err , "First module should be configured without errors" )
64-
65- // Create a second test configuration with the same worker name
66- configWithWorkerName2 := `
67- {
68- php_server {
69- worker {
70- name test-worker
71- file ../testdata/worker-with-env.php
72- num 1
73- }
74- }
75- }`
76-
77- // Parse the second configuration
78- d2 := caddyfile .NewTestDispenser (configWithWorkerName2 )
79- module2 := & FrankenPHPModule {}
80-
81- // Unmarshal the second configuration
82- err = module2 .UnmarshalCaddyfile (d2 )
83-
84- // Verify that an error was returned
85- require .Error (t , err , "Expected an error when two workers have the same name, but different environments" )
86- require .Contains (t , err .Error (), "must not have duplicate names" , "Error message should mention duplicate names" )
87- resetModuleWorkers ()
8836}
8937
9038func TestModuleWorkersWithDifferentFilenames (t * testing.T ) {
@@ -111,8 +59,6 @@ func TestModuleWorkersWithDifferentFilenames(t *testing.T) {
11159 require .Len (t , module .Workers , 2 , "Expected two workers to be added to the module" )
11260 require .Equal (t , "../testdata/worker-with-env.php" , module .Workers [0 ].FileName , "First worker should have the correct filename" )
11361 require .Equal (t , "../testdata/worker-with-counter.php" , module .Workers [1 ].FileName , "Second worker should have the correct filename" )
114-
115- resetModuleWorkers ()
11662}
11763
11864func TestModuleWorkersDifferentNamesSucceed (t * testing.T ) {
@@ -130,6 +76,7 @@ func TestModuleWorkersDifferentNamesSucceed(t *testing.T) {
13076
13177 // Parse the first configuration
13278 d1 := caddyfile .NewTestDispenser (configWithWorkerName1 )
79+ app := & FrankenPHPApp {}
13380 module1 := & FrankenPHPModule {}
13481
13582 // Unmarshal the first configuration
@@ -158,12 +105,15 @@ func TestModuleWorkersDifferentNamesSucceed(t *testing.T) {
158105 // Verify that no error was returned
159106 require .NoError (t , err , "Expected no error when two workers have different names" )
160107
161- // Verify that both workers were added to moduleWorkerConfigs
162- require .Len (t , moduleWorkerConfigs , 2 , "Expected two workers to be added to moduleWorkerConfigs " )
163- require . Equal ( t , "m#test-worker-1" , moduleWorkerConfigs [ 0 ]. Name , "First worker should have the correct name" )
164- require .Equal (t , "m#test-worker-2" , moduleWorkerConfigs [ 1 ]. Name , "Second worker should have the correct name " )
108+ _ , err = app . addModuleWorkers ( module1 . Workers ... )
109+ require .NoError (t , err , "Expected no error when adding the first module workers " )
110+ _ , err = app . addModuleWorkers ( module2 . Workers ... )
111+ require .NoError (t , err , "Expected no error when adding the second module workers " )
165112
166- resetModuleWorkers ()
113+ // Verify that both workers were added
114+ require .Len (t , app .Workers , 2 , "Expected two workers in the app" )
115+ require .Equal (t , "m#test-worker-1" , app .Workers [0 ].Name , "First worker should have the correct name" )
116+ require .Equal (t , "m#test-worker-2" , app .Workers [1 ].Name , "Second worker should have the correct name" )
167117}
168118
169119func TestModuleWorkerWithEnvironmentVariables (t * testing.T ) {
@@ -198,8 +148,6 @@ func TestModuleWorkerWithEnvironmentVariables(t *testing.T) {
198148 require .Len (t , module .Workers [0 ].Env , 2 , "Expected two environment variables" )
199149 require .Equal (t , "production" , module .Workers [0 ].Env ["APP_ENV" ], "APP_ENV should be set to production" )
200150 require .Equal (t , "true" , module .Workers [0 ].Env ["DEBUG" ], "DEBUG should be set to true" )
201-
202- resetModuleWorkers ()
203151}
204152
205153func TestModuleWorkerWithWatchConfiguration (t * testing.T ) {
@@ -236,8 +184,6 @@ func TestModuleWorkerWithWatchConfiguration(t *testing.T) {
236184 require .Equal (t , "./**/*.{php,yaml,yml,twig,env}" , module .Workers [0 ].Watch [0 ], "First watch pattern should be the default" )
237185 require .Equal (t , "./src/**/*.php" , module .Workers [0 ].Watch [1 ], "Second watch pattern should match the configuration" )
238186 require .Equal (t , "./config/**/*.yaml" , module .Workers [0 ].Watch [2 ], "Third watch pattern should match the configuration" )
239-
240- resetModuleWorkers ()
241187}
242188
243189func TestModuleWorkerWithCustomName (t * testing.T ) {
@@ -256,6 +202,7 @@ func TestModuleWorkerWithCustomName(t *testing.T) {
256202 // Parse the configuration
257203 d := caddyfile .NewTestDispenser (configWithCustomName )
258204 module := & FrankenPHPModule {}
205+ app := & FrankenPHPApp {}
259206
260207 // Unmarshal the configuration
261208 err := module .UnmarshalCaddyfile (d )
@@ -267,8 +214,9 @@ func TestModuleWorkerWithCustomName(t *testing.T) {
267214 require .Len (t , module .Workers , 1 , "Expected one worker to be added to the module" )
268215 require .Equal (t , "../testdata/worker-with-env.php" , module .Workers [0 ].FileName , "Worker should have the correct filename" )
269216
270- // Verify that the worker was added to moduleWorkerConfigs with the m# prefix
271- require .Equal (t , "m#custom-worker-name" , module .Workers [0 ].Name , "Worker should have the custom name" )
272-
273- resetModuleWorkers ()
217+ // Verify that the worker was added to app.Workers with the m# prefix
218+ module .Workers , err = app .addModuleWorkers (module .Workers ... )
219+ require .NoError (t , err , "Expected no error when adding the worker to the app" )
220+ require .Equal (t , "m#custom-worker-name" , module .Workers [0 ].Name , "Worker should have the custom name, prefixed with m#" )
221+ require .Equal (t , "m#custom-worker-name" , app .Workers [0 ].Name , "Worker should have the custom name, prefixed with m#" )
274222}
0 commit comments