| 296 | } |
| 297 | |
| 298 | func TestTypeToString(t *testing.T) { |
| 299 | tests := []struct { |
| 300 | name string |
| 301 | input string |
| 302 | expected []phpType |
| 303 | }{ |
| 304 | { |
| 305 | name: "basic types", |
| 306 | input: `package main |
| 307 | |
| 308 | //export_php:class TestClass |
| 309 | type TestStruct struct { |
| 310 | StringField string |
| 311 | IntField int |
| 312 | FloatField float64 |
| 313 | BoolField bool |
| 314 | }`, |
| 315 | expected: []phpType{phpString, phpInt, phpFloat, phpBool}, |
| 316 | }, |
| 317 | { |
| 318 | name: "pointer types", |
| 319 | input: `package main |
| 320 | |
| 321 | //export_php:class NullableClass |
| 322 | type NullableStruct struct { |
| 323 | NullableString *string |
| 324 | NullableInt *int |
| 325 | NullableFloat *float64 |
| 326 | NullableBool *bool |
| 327 | }`, |
| 328 | expected: []phpType{phpString, phpInt, phpFloat, phpBool}, |
| 329 | }, |
| 330 | { |
| 331 | name: "collection types", |
| 332 | input: `package main |
| 333 | |
| 334 | //export_php:class CollectionClass |
| 335 | type CollectionStruct struct { |
| 336 | StringSlice []string |
| 337 | IntMap map[string]int |
| 338 | MixedSlice []any |
| 339 | }`, |
| 340 | expected: []phpType{phpArray, phpArray, phpArray}, |
| 341 | }, |
| 342 | } |
| 343 | |
| 344 | for _, tt := range tests { |
| 345 | t.Run(tt.name, func(t *testing.T) { |
| 346 | tmpDir := t.TempDir() |
| 347 | fileName := filepath.Join(tmpDir, tt.name+".go") |
| 348 | require.NoError(t, os.WriteFile(fileName, []byte(tt.input), 0o644)) |
| 349 | |
| 350 | parser := classParser{} |
| 351 | classes, err := parser.parse(fileName) |
| 352 | require.NoError(t, err) |
| 353 | |
| 354 | require.Len(t, classes, 1, "Expected 1 class") |
| 355 | |