Skip to content

Commit 9904285

Browse files
committed
Reorganize library tests
1 parent 1e64837 commit 9904285

File tree

4 files changed

+46
-166
lines changed

4 files changed

+46
-166
lines changed

composer.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@
2929
}
3030
},
3131
"require-dev": {
32-
"ffi/env": "^0.1|^1.0",
33-
"ffi/location": "^0.1|^1.0",
34-
"vimeo/psalm": "^4.22.0",
35-
"phpunit/phpunit": "^9.5.15"
32+
"ffi-headers/testing": "^1.0",
33+
"vimeo/psalm": "^4.22"
3634
},
3735
"autoload-dev": {
3836
"psr-4": {

tests/BinaryCompatibilityTestCase.php

Lines changed: 20 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -11,82 +11,18 @@
1111

1212
namespace FFI\Headers\SDL2\Tests;
1313

14-
use FFI\Env\Runtime;
1514
use FFI\Headers\SDL2;
1615
use FFI\Headers\SDL2\Platform;
1716
use FFI\Headers\SDL2\Version;
17+
use FFI\Headers\Testing\Downloader;
1818
use FFI\Location\Locator;
1919

20-
/**
21-
* @group binary-compatibility
22-
* @requires extension ffi
23-
*/
2420
class 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
}

tests/ContentRenderingTestCase.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
use FFI\Headers\SDL2\Platform;
1616
use FFI\Headers\SDL2\Version;
1717

18-
/**
19-
* @group building
20-
*/
2118
final class ContentRenderingTestCase extends TestCase
2219
{
2320
/**
@@ -31,4 +28,17 @@ public function testRenderable(Platform $platform, Version $version): void
3128

3229
(string)SDL2::create($platform, $version);
3330
}
31+
32+
/**
33+
* @testdox Testing that headers contain correct syntax
34+
*
35+
* @depends testRenderable
36+
* @dataProvider configDataProvider
37+
*/
38+
public function testCompilation(Platform $platform, Version $version): void
39+
{
40+
$this->assertHeadersSyntaxValid(
41+
SDL2::create($platform, $version)
42+
);
43+
}
3444
}

tests/TestCase.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
use FFI\Headers\SDL2\Platform;
1515
use FFI\Headers\SDL2\Version;
16-
use FFI\ParserException;
16+
use FFI\Headers\Testing\TestingTrait;
1717
use PHPUnit\Framework\TestCase as BaseTestCase;
1818

1919
abstract class TestCase extends BaseTestCase
2020
{
21+
use TestingTrait;
22+
2123
/**
2224
* @return array<array{Platform, Version}>
2325
*/
@@ -35,25 +37,16 @@ public function configDataProvider(): array
3537
}
3638

3739
/**
38-
* @param ParserException $e
39-
* @param string $header
40-
* @return void
40+
* @return array<array{Version}>
4141
*/
42-
protected function dumpExceptionInfo(ParserException $e, string $header): void
42+
public function versionsDataProvider(): array
4343
{
44-
\preg_match('/at line (\d+)/isum', $e->getMessage(), $matches);
45-
46-
$size = 2;
47-
$line = (int)($matches[1] ?? 1) - 1;
48-
49-
$lines = \explode("\n", $header);
50-
echo "\n";
51-
for ($i = \max(0, $line) - $size, $to = $line + $size; $i <= $to; ++$i) {
52-
if ($line === $i) {
53-
echo \sprintf("%5d. | \u{001b}[41m\u{001b}[37;1m%s\u{001b}[0m", $i + 1, $lines[$i] ?? '') . "\n";
54-
} else {
55-
echo \sprintf('%5d. | %s', $i + 1, $lines[$i] ?? '') . "\n";
56-
}
44+
$result = [];
45+
46+
foreach (Version::cases() as $version) {
47+
$result[$version->toString()] = [$version];
5748
}
49+
50+
return $result;
5851
}
5952
}

0 commit comments

Comments
 (0)