1111
1212namespace FFI \Headers \SDL2 \Tests ;
1313
14- use FFI \Env \Runtime ;
1514use FFI \Headers \SDL2 ;
1615use FFI \Headers \SDL2 \Platform ;
1716use FFI \Headers \SDL2 \Version ;
17+ use FFI \Headers \Testing \Downloader ;
1818use FFI \Location \Locator ;
1919
20- /**
21- * @group binary-compatibility
22- * @requires extension ffi
23- */
2420class BinaryCompatibilityTestCase extends TestCase
2521{
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
7823 {
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 ();
8725
88- protected function assertVersionCompare (Version $ version , string $ binary ): void
89- {
9026 $ ffi = \FFI ::cdef (<<<'CPP'
9127 typedef struct SDL_version {
9228 uint8_t major;
@@ -109,105 +45,48 @@ protected function assertVersionCompare(Version $version, string $binary): void
10945 }
11046 }
11147
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-
12648 /**
12749 * @requires OSFAMILY Windows
128- *
12950 * @dataProvider versionsDataProvider
13051 */
13152 public function testWin32PlatformWithoutContext (Version $ version ): void
13253 {
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 );
14659 }
60+
61+ $ this ->skipIfVersionNotCompatible ($ version , $ binary );
62+ $ this ->assertHeadersCompatibleWith (SDL2 ::create (Platform::WINDOWS , $ version ), $ binary );
14763 }
14864
14965 /**
15066 * @requires OSFAMILY Linux
151- *
15267 * @dataProvider versionsDataProvider
15368 */
15469 public function testLinuxPlatformWithoutContext (Version $ version ): void
15570 {
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 ' );
16973 }
74+
75+ $ this ->skipIfVersionNotCompatible ($ version , $ binary );
76+ $ this ->assertHeadersCompatibleWith (SDL2 ::create (Platform::LINUX , $ version ), $ binary );
17077 }
17178
17279 /**
17380 * @requires OSFAMILY Darwin
174- *
17581 * @dataProvider versionsDataProvider
17682 */
17783 public function testDarwinPlatformWithoutContext (Version $ version ): void
17884 {
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 ' );
19287 }
193- }
19488
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 );
21291 }
21392}
0 commit comments