-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathtypes_test.go
More file actions
134 lines (108 loc) · 3.34 KB
/
types_test.go
File metadata and controls
134 lines (108 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package frankenphp
import (
"io"
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
)
// execute the function on a PHP thread directly
// this is necessary if tests make use of PHP's internal allocation
func testOnDummyPHPThread(t *testing.T, test func()) {
t.Helper()
logger = slog.New(slog.NewTextHandler(io.Discard, nil))
_, err := initPHPThreads(1, 1, nil) // boot 1 thread
assert.NoError(t, err)
handler := convertToTaskThread(phpThreads[0])
task := newTask(test)
handler.execute(task)
task.waitForCompletion()
drainPHPThreads()
}
func TestGoString(t *testing.T) {
testOnDummyPHPThread(t, func() {
originalString := "Hello, World!"
phpString := PHPString(originalString, false)
defer zendStringRelease(phpString)
assert.Equal(t, originalString, GoString(phpString), "string -> zend_string -> string should yield an equal string")
})
}
func TestPHPMap(t *testing.T) {
testOnDummyPHPThread(t, func() {
originalMap := map[string]any{
"foo1": "bar1",
"foo2": "bar2",
}
phpArray := PHPMap(originalMap)
defer zendHashDestroy(phpArray)
assert.Equal(t, originalMap, GoMap(phpArray), "associative array should be equal after conversion")
})
}
func TestOrderedPHPAssociativeArray(t *testing.T) {
testOnDummyPHPThread(t, func() {
originalArray := AssociativeArray{
Map: map[string]any{
"foo1": "bar1",
"foo2": "bar2",
},
Order: []string{"foo2", "foo1"},
}
phpArray := PHPAssociativeArray(originalArray)
defer zendHashDestroy(phpArray)
assert.Equal(t, originalArray, GoAssociativeArray(phpArray), "associative array should be equal after conversion")
})
}
func TestPHPPackedArray(t *testing.T) {
testOnDummyPHPThread(t, func() {
originalSlice := []any{"bar1", "bar2"}
phpArray := PHPPackedArray(originalSlice)
defer zendHashDestroy(phpArray)
assert.Equal(t, originalSlice, GoPackedArray(phpArray), "slice should be equal after conversion")
})
}
func TestPHPPackedArrayToGoMap(t *testing.T) {
testOnDummyPHPThread(t, func() {
originalSlice := []any{"bar1", "bar2"}
expectedMap := map[string]any{
"0": "bar1",
"1": "bar2",
}
phpArray := PHPPackedArray(originalSlice)
defer zendHashDestroy(phpArray)
assert.Equal(t, expectedMap, GoMap(phpArray), "convert a packed to an associative array")
})
}
func TestPHPAssociativeArrayToPacked(t *testing.T) {
testOnDummyPHPThread(t, func() {
originalArray := AssociativeArray{
Map: map[string]any{
"foo1": "bar1",
"foo2": "bar2",
},
Order: []string{"foo1", "foo2"},
}
expectedSlice := []any{"bar1", "bar2"}
phpArray := PHPAssociativeArray(originalArray)
defer zendHashDestroy(phpArray)
assert.Equal(t, expectedSlice, GoPackedArray(phpArray), "convert an associative array to a slice")
})
}
func TestNestedMixedArray(t *testing.T) {
testOnDummyPHPThread(t, func() {
originalArray := map[string]any{
"string": "value",
"int": int64(123),
"float": float64(1.2),
"true": true,
"false": false,
"nil": nil,
"packedArray": []any{"bar1", "bar2"},
"associativeArray": AssociativeArray{
Map: map[string]any{"foo1": "bar1", "foo2": "bar2"},
Order: []string{"foo2", "foo1"},
},
}
phpArray := PHPMap(originalArray)
defer zendHashDestroy(phpArray)
assert.Equal(t, originalArray, GoMap(phpArray), "nested mixed array should be equal after conversion")
})
}