Skip to content

Commit b06db1f

Browse files
committed
Add more PHPUnit tests
1 parent 5e67133 commit b06db1f

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

tests/SPC/GlobalDefinesTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
/**
10+
* @internal
11+
*/
12+
class GlobalDefinesTest extends TestCase
13+
{
14+
public function testGlobalDefines(): void
15+
{
16+
require __DIR__ . '/../../src/globals/defines.php';
17+
$this->assertTrue(defined('WORKING_DIR'));
18+
}
19+
20+
public function testInternalEnv(): void
21+
{
22+
require __DIR__ . '/../../src/globals/internal-env.php';
23+
$this->assertTrue(defined('GNU_ARCH'));
24+
}
25+
}

tests/SPC/GlobalFunctionsTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use SPC\exception\InterruptException;
9+
10+
/**
11+
* @internal
12+
*/
13+
class GlobalFunctionsTest extends TestCase
14+
{
15+
public function testMatchPattern(): void
16+
{
17+
$this->assertEquals('abc', match_pattern('a*c', 'abc'));
18+
$this->assertFalse(match_pattern('a*c', 'abcd'));
19+
}
20+
21+
public function testFExec(): void
22+
{
23+
$this->assertEquals('abc', f_exec('echo abc', $out, $ret));
24+
$this->assertEquals(0, $ret);
25+
$this->assertEquals(['abc'], $out);
26+
}
27+
28+
public function testPatchPointInterrupt(): void
29+
{
30+
$except = patch_point_interrupt(0);
31+
$this->assertInstanceOf(InterruptException::class, $except);
32+
}
33+
}

tests/SPC/util/ConfigValidatorTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,41 @@ public function testValidateLibsBad(): void
137137
} catch (ValidationException) {
138138
$this->assertTrue(true);
139139
}
140+
// lib.json is broken by not assoc array
141+
try {
142+
ConfigValidator::validateLibs(['lib1', 'lib2'], ['source1' => [], 'source2' => []]);
143+
$this->fail('should throw ValidationException');
144+
} catch (ValidationException) {
145+
$this->assertTrue(true);
146+
}
147+
// lib.json lib is not one of "lib", "package", "root", "target"
148+
try {
149+
ConfigValidator::validateLibs(['lib1' => ['source' => 'source1', 'type' => 'not one of']], ['source1' => [], 'source2' => []]);
150+
$this->fail('should throw ValidationException');
151+
} catch (ValidationException) {
152+
$this->assertTrue(true);
153+
}
154+
// lib.json lib if it is "lib" or "package", it must have "source"
155+
try {
156+
ConfigValidator::validateLibs(['lib1' => ['type' => 'lib']], ['source1' => [], 'source2' => []]);
157+
$this->fail('should throw ValidationException');
158+
} catch (ValidationException) {
159+
$this->assertTrue(true);
160+
}
161+
// lib.json static-libs must be a list
162+
try {
163+
ConfigValidator::validateLibs(['lib1' => ['source' => 'source1', 'static-libs-windows' => 'not list']], ['source1' => [], 'source2' => []]);
164+
$this->fail('should throw ValidationException');
165+
} catch (ValidationException) {
166+
$this->assertTrue(true);
167+
}
168+
// lib.json frameworks must be a list
169+
try {
170+
ConfigValidator::validateLibs(['lib1' => ['source' => 'source1', 'frameworks' => 'not list']], ['source1' => [], 'source2' => []]);
171+
$this->fail('should throw ValidationException');
172+
} catch (ValidationException) {
173+
$this->assertTrue(true);
174+
}
140175
// source must be string
141176
try {
142177
ConfigValidator::validateLibs(['lib1' => ['source' => true]], ['source1' => [], 'source2' => []]);
@@ -169,4 +204,11 @@ public function testValidateExts(): void
169204
$this->expectException(ValidationException::class);
170205
ConfigValidator::validateExts(null);
171206
}
207+
208+
public function testValidatePkgs(): void
209+
{
210+
ConfigValidator::validatePkgs([]);
211+
$this->expectException(ValidationException::class);
212+
ConfigValidator::validatePkgs(null);
213+
}
172214
}

tests/SPC/util/DependencyUtilTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ final class DependencyUtilTest extends TestCase
1616
{
1717
public function testGetExtLibsByDeps(): void
1818
{
19+
// setup
20+
$bak = [
21+
'source' => Config::$source,
22+
'lib' => Config::$lib,
23+
'ext' => Config::$ext,
24+
];
1925
// example
2026
Config::$source = [
2127
'test1' => [
@@ -82,6 +88,10 @@ public function testGetExtLibsByDeps(): void
8288
$this->assertTrue($b < $a);
8389
$this->assertTrue($c < $a);
8490
$this->assertTrue($c < $b);
91+
// restore
92+
Config::$source = $bak['source'];
93+
Config::$lib = $bak['lib'];
94+
Config::$ext = $bak['ext'];
8595
}
8696

8797
public function testNotExistExtException(): void

tests/SPC/util/LicenseDumperTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ protected function tearDown(): void
3333

3434
public function testDumpWithSingleLicense(): void
3535
{
36+
$bak = [
37+
'source' => Config::$source,
38+
'lib' => Config::$lib,
39+
];
3640
Config::$lib = [
3741
'lib-base' => ['type' => 'root'],
3842
'php' => ['type' => 'root'],
@@ -54,10 +58,17 @@ public function testDumpWithSingleLicense(): void
5458
$dumper->dump(self::DIRECTORY);
5559

5660
$this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_0.txt');
61+
// restore
62+
Config::$source = $bak['source'];
63+
Config::$lib = $bak['lib'];
5764
}
5865

5966
public function testDumpWithMultipleLicenses(): void
6067
{
68+
$bak = [
69+
'source' => Config::$source,
70+
'lib' => Config::$lib,
71+
];
6172
Config::$lib = [
6273
'lib-base' => ['type' => 'root'],
6374
'php' => ['type' => 'root'],
@@ -91,5 +102,9 @@ public function testDumpWithMultipleLicenses(): void
91102
$this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_0.txt');
92103
$this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_1.txt');
93104
$this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_2.txt');
105+
106+
// restore
107+
Config::$source = $bak['source'];
108+
Config::$lib = $bak['lib'];
94109
}
95110
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SPC\Tests\util;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use SPC\builder\BuilderProvider;
9+
use SPC\exception\FileSystemException;
10+
use SPC\store\FileSystem;
11+
use SPC\util\SPCConfigUtil;
12+
use Symfony\Component\Console\Input\ArgvInput;
13+
14+
/**
15+
* @internal
16+
*/
17+
class SPCConfigUtilTest extends TestCase
18+
{
19+
/**
20+
* @throws FileSystemException
21+
*/
22+
public static function setUpBeforeClass(): void
23+
{
24+
$testdir = WORKING_DIR . '/.configtest';
25+
FileSystem::createDir($testdir);
26+
FileSystem::writeFile($testdir . '/lib.json', file_get_contents(ROOT_DIR . '/config/lib.json'));
27+
FileSystem::writeFile($testdir . '/ext.json', file_get_contents(ROOT_DIR . '/config/ext.json'));
28+
FileSystem::writeFile($testdir . '/source.json', file_get_contents(ROOT_DIR . '/config/source.json'));
29+
FileSystem::loadConfigArray('lib', $testdir);
30+
FileSystem::loadConfigArray('ext', $testdir);
31+
FileSystem::loadConfigArray('source', $testdir);
32+
}
33+
34+
/**
35+
* @throws FileSystemException
36+
*/
37+
public static function tearDownAfterClass(): void
38+
{
39+
FileSystem::removeDir(WORKING_DIR . '/.configtest');
40+
}
41+
42+
public function testConstruct(): void
43+
{
44+
$this->assertInstanceOf(SPCConfigUtil::class, new SPCConfigUtil());
45+
$this->assertInstanceOf(SPCConfigUtil::class, new SPCConfigUtil(BuilderProvider::makeBuilderByInput(new ArgvInput())));
46+
}
47+
48+
public function testConfig(): void
49+
{
50+
// normal
51+
$result = (new SPCConfigUtil())->config(['bcmath']);
52+
$this->assertStringContainsString(BUILD_ROOT_PATH . '/include', $result['cflags']);
53+
$this->assertStringContainsString(BUILD_ROOT_PATH . '/lib', $result['ldflags']);
54+
$this->assertStringContainsString('-lphp', $result['libs']);
55+
56+
// has cpp
57+
$result = (new SPCConfigUtil())->config(['swoole']);
58+
$this->assertStringContainsString(PHP_OS_FAMILY === 'Darwin' ? '-lc++' : '-lstdc++', $result['libs']);
59+
60+
// has mimalloc.o in lib dir
61+
// backup first
62+
if (file_exists(BUILD_LIB_PATH . '/mimalloc.o')) {
63+
$bak = file_get_contents(BUILD_LIB_PATH . '/mimalloc.o');
64+
@unlink(BUILD_LIB_PATH . '/mimalloc.o');
65+
}
66+
file_put_contents(BUILD_LIB_PATH . '/mimalloc.o', '');
67+
$result = (new SPCConfigUtil())->config(['bcmath'], ['mimalloc']);
68+
$this->assertStringStartsWith(BUILD_LIB_PATH . '/mimalloc.o', $result['libs']);
69+
@unlink(BUILD_LIB_PATH . '/mimalloc.o');
70+
if (isset($bak)) {
71+
file_put_contents(BUILD_LIB_PATH . '/mimalloc.o', $bak);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)