11
11
12
12
namespace FFI \Headers \SDL2 \Tests ;
13
13
14
- use FFI \Env \Runtime ;
15
14
use FFI \Headers \SDL2 ;
16
15
use FFI \Headers \SDL2 \Platform ;
17
16
use FFI \Headers \SDL2 \Version ;
17
+ use FFI \Headers \Testing \Downloader ;
18
18
use FFI \Location \Locator ;
19
19
20
- /**
21
- * @group binary-compatibility
22
- * @requires extension ffi
23
- */
24
20
class BinaryCompatibilityTestCase extends TestCase
25
21
{
26
- private const WIN32_ARCHIVE_DIRECTORY = __DIR__ . '/storage/sdl2.win64.zip ' ;
27
- private const WIN32_BINARY = __DIR__ . '/storage/SDL2.dll ' ;
28
-
29
- public function setUp (): void
30
- {
31
- if (!Runtime::isAvailable ()) {
32
- $ this ->markTestSkipped ('An ext-ffi extension must be available and enabled ' );
33
- }
34
-
35
- parent ::setUp ();
36
- }
37
-
38
- protected function getWindowsBinary (): string
39
- {
40
- $ version = Version::LATEST ->toString ();
41
-
42
- // Download glfw archive
43
- if (!\is_file (self ::WIN32_ARCHIVE_DIRECTORY )) {
44
- $ url = \vsprintf ('https://www.libsdl.org/release/SDL2-%s-win32-x64.zip ' , [
45
- $ version
46
- ]);
47
-
48
- \stream_copy_to_stream (\fopen ($ url , 'rb ' ), \fopen (self ::WIN32_ARCHIVE_DIRECTORY , 'ab+ ' ));
49
- }
50
-
51
- if (!\is_file (self ::WIN32_BINARY )) {
52
- $ directory = \dirname (self ::WIN32_ARCHIVE_DIRECTORY );
53
- $ pathname = $ directory . '/SDL2.dll ' ;
54
-
55
- if (!\is_file ($ pathname )) {
56
- $ phar = new \PharData (self ::WIN32_ARCHIVE_DIRECTORY );
57
- $ phar ->extractTo ($ directory , 'SDL2.dll ' );
58
- }
59
-
60
- \rename ($ pathname , self ::WIN32_BINARY );
61
- }
62
-
63
- return self ::WIN32_BINARY ;
64
- }
65
-
66
- protected function getLinuxBinary (): string
67
- {
68
- $ binary = Locator::resolve ('libSDL2-2.0.so.0 ' );
69
-
70
- if ($ binary === null ) {
71
- $ this ->markTestSkipped ('The [libsdl] library must be installed ' );
72
- }
73
-
74
- return (string )$ binary ;
75
- }
76
-
77
- protected function getDarwinBinary (): string
22
+ protected function skipIfVersionNotCompatible (Version $ version , string $ binary ): void
78
23
{
79
- $ binary = Locator::resolve ('libSDL2-2.0.0.dylib ' );
80
-
81
- if ($ binary === null ) {
82
- $ this ->markTestSkipped ('The [libsdl] library must be installed ' );
83
- }
84
-
85
- return (string )$ binary ;
86
- }
24
+ $ this ->skipIfNoFFISupport ();
87
25
88
- protected function assertVersionCompare (Version $ version , string $ binary ): void
89
- {
90
26
$ ffi = \FFI ::cdef (<<<'CPP'
91
27
typedef struct SDL_version {
92
28
uint8_t major;
@@ -109,105 +45,48 @@ protected function assertVersionCompare(Version $version, string $binary): void
109
45
}
110
46
}
111
47
112
- /**
113
- * @return array<array{Version}>
114
- */
115
- public function versionsDataProvider (): array
116
- {
117
- $ result = [];
118
-
119
- foreach (Version::cases () as $ version ) {
120
- $ result [$ version ->toString ()] = [$ version ];
121
- }
122
-
123
- return $ result ;
124
- }
125
-
126
48
/**
127
49
* @requires OSFAMILY Windows
128
- *
129
50
* @dataProvider versionsDataProvider
130
51
*/
131
52
public function testWin32PlatformWithoutContext (Version $ version ): void
132
53
{
133
- $ this ->expectNotToPerformAssertions ();
134
-
135
- $ binary = $ this ->getWindowsBinary ();
136
-
137
- $ this ->assertVersionCompare ($ version , $ binary );
138
- $ headers = (string )SDL2 ::create (Platform::WINDOWS , $ version );
139
-
140
- try {
141
- \FFI ::cdef ($ headers , $ binary );
142
- } catch (\FFI \ParserException $ e ) {
143
- $ this ->dumpExceptionInfo ($ e , $ headers );
144
-
145
- throw $ e ;
54
+ if (!\is_file ($ binary = __DIR__ . '/storage/SDL2.dll ' )) {
55
+ Downloader::zip ('https://www.libsdl.org/release/SDL2-%s-win32-x64.zip ' , [
56
+ Version::LATEST ->toString (),
57
+ ])
58
+ ->extract ('SDL2.dll ' , $ binary );
146
59
}
60
+
61
+ $ this ->skipIfVersionNotCompatible ($ version , $ binary );
62
+ $ this ->assertHeadersCompatibleWith (SDL2 ::create (Platform::WINDOWS , $ version ), $ binary );
147
63
}
148
64
149
65
/**
150
66
* @requires OSFAMILY Linux
151
- *
152
67
* @dataProvider versionsDataProvider
153
68
*/
154
69
public function testLinuxPlatformWithoutContext (Version $ version ): void
155
70
{
156
- $ this ->expectNotToPerformAssertions ();
157
-
158
- $ binary = $ this ->getLinuxBinary ();
159
-
160
- $ this ->assertVersionCompare ($ version , $ binary );
161
- $ headers = (string )SDL2 ::create (Platform::LINUX , $ version );
162
-
163
- try {
164
- \FFI ::cdef ($ headers , $ binary );
165
- } catch (\FFI \ParserException $ e ) {
166
- $ this ->dumpExceptionInfo ($ e , $ headers );
167
-
168
- throw $ e ;
71
+ if (($ binary = Locator::resolve ('libSDL2-2.0.so.0 ' )) === null ) {
72
+ $ this ->markTestSkipped ('The [libsdl] library must be installed ' );
169
73
}
74
+
75
+ $ this ->skipIfVersionNotCompatible ($ version , $ binary );
76
+ $ this ->assertHeadersCompatibleWith (SDL2 ::create (Platform::LINUX , $ version ), $ binary );
170
77
}
171
78
172
79
/**
173
80
* @requires OSFAMILY Darwin
174
- *
175
81
* @dataProvider versionsDataProvider
176
82
*/
177
83
public function testDarwinPlatformWithoutContext (Version $ version ): void
178
84
{
179
- $ this ->expectNotToPerformAssertions ();
180
-
181
- $ binary = $ this ->getLinuxBinary ();
182
-
183
- $ this ->assertVersionCompare ($ version , $ binary );
184
- $ headers = (string )SDL2 ::create (Platform::DARWIN , $ version );
185
-
186
- try {
187
- \FFI ::cdef ($ headers , $ binary );
188
- } catch (\FFI \ParserException $ e ) {
189
- $ this ->dumpExceptionInfo ($ e , $ headers );
190
-
191
- throw $ e ;
85
+ if (($ binary = Locator::resolve ('libSDL2-2.0.0.dylib ' )) === null ) {
86
+ $ this ->markTestSkipped ('The [libsdl] library must be installed ' );
192
87
}
193
- }
194
88
195
- /**
196
- * @dataProvider configDataProvider
197
- */
198
- public function testCompilation (Platform $ platform , Version $ version ): void
199
- {
200
- $ headers = (string )SDL2 ::create ($ platform , $ version );
201
-
202
- try {
203
- \FFI ::cdef ($ headers );
204
- $ this ->expectNotToPerformAssertions ();
205
- } catch (\FFI \Exception $ e ) {
206
- $ this ->assertStringStartsWith ('Failed resolving C function ' , $ e ->getMessage ());
207
-
208
- if ($ e instanceof \FFI \ParserException) {
209
- $ this ->dumpExceptionInfo ($ e , $ headers );
210
- }
211
- }
89
+ $ this ->skipIfVersionNotCompatible ($ version , $ binary );
90
+ $ this ->assertHeadersCompatibleWith (SDL2 ::create (Platform::DARWIN , $ version ), $ binary );
212
91
}
213
92
}
0 commit comments