From b06db1f920c2c957867b0d12d245730162b92891 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Fri, 18 Apr 2025 14:01:03 +0800 Subject: [PATCH] Add more PHPUnit tests --- tests/SPC/GlobalDefinesTest.php | 25 +++++++++ tests/SPC/GlobalFunctionsTest.php | 33 ++++++++++++ tests/SPC/util/ConfigValidatorTest.php | 42 +++++++++++++++ tests/SPC/util/DependencyUtilTest.php | 10 ++++ tests/SPC/util/LicenseDumperTest.php | 15 ++++++ tests/SPC/util/SPCConfigUtilTest.php | 74 ++++++++++++++++++++++++++ 6 files changed, 199 insertions(+) create mode 100644 tests/SPC/GlobalDefinesTest.php create mode 100644 tests/SPC/GlobalFunctionsTest.php create mode 100644 tests/SPC/util/SPCConfigUtilTest.php diff --git a/tests/SPC/GlobalDefinesTest.php b/tests/SPC/GlobalDefinesTest.php new file mode 100644 index 00000000..956b35e2 --- /dev/null +++ b/tests/SPC/GlobalDefinesTest.php @@ -0,0 +1,25 @@ +assertTrue(defined('WORKING_DIR')); + } + + public function testInternalEnv(): void + { + require __DIR__ . '/../../src/globals/internal-env.php'; + $this->assertTrue(defined('GNU_ARCH')); + } +} diff --git a/tests/SPC/GlobalFunctionsTest.php b/tests/SPC/GlobalFunctionsTest.php new file mode 100644 index 00000000..6a8bd738 --- /dev/null +++ b/tests/SPC/GlobalFunctionsTest.php @@ -0,0 +1,33 @@ +assertEquals('abc', match_pattern('a*c', 'abc')); + $this->assertFalse(match_pattern('a*c', 'abcd')); + } + + public function testFExec(): void + { + $this->assertEquals('abc', f_exec('echo abc', $out, $ret)); + $this->assertEquals(0, $ret); + $this->assertEquals(['abc'], $out); + } + + public function testPatchPointInterrupt(): void + { + $except = patch_point_interrupt(0); + $this->assertInstanceOf(InterruptException::class, $except); + } +} diff --git a/tests/SPC/util/ConfigValidatorTest.php b/tests/SPC/util/ConfigValidatorTest.php index 78ecd27b..6805509c 100644 --- a/tests/SPC/util/ConfigValidatorTest.php +++ b/tests/SPC/util/ConfigValidatorTest.php @@ -137,6 +137,41 @@ class ConfigValidatorTest extends TestCase } catch (ValidationException) { $this->assertTrue(true); } + // lib.json is broken by not assoc array + try { + ConfigValidator::validateLibs(['lib1', 'lib2'], ['source1' => [], 'source2' => []]); + $this->fail('should throw ValidationException'); + } catch (ValidationException) { + $this->assertTrue(true); + } + // lib.json lib is not one of "lib", "package", "root", "target" + try { + ConfigValidator::validateLibs(['lib1' => ['source' => 'source1', 'type' => 'not one of']], ['source1' => [], 'source2' => []]); + $this->fail('should throw ValidationException'); + } catch (ValidationException) { + $this->assertTrue(true); + } + // lib.json lib if it is "lib" or "package", it must have "source" + try { + ConfigValidator::validateLibs(['lib1' => ['type' => 'lib']], ['source1' => [], 'source2' => []]); + $this->fail('should throw ValidationException'); + } catch (ValidationException) { + $this->assertTrue(true); + } + // lib.json static-libs must be a list + try { + ConfigValidator::validateLibs(['lib1' => ['source' => 'source1', 'static-libs-windows' => 'not list']], ['source1' => [], 'source2' => []]); + $this->fail('should throw ValidationException'); + } catch (ValidationException) { + $this->assertTrue(true); + } + // lib.json frameworks must be a list + try { + ConfigValidator::validateLibs(['lib1' => ['source' => 'source1', 'frameworks' => 'not list']], ['source1' => [], 'source2' => []]); + $this->fail('should throw ValidationException'); + } catch (ValidationException) { + $this->assertTrue(true); + } // source must be string try { ConfigValidator::validateLibs(['lib1' => ['source' => true]], ['source1' => [], 'source2' => []]); @@ -169,4 +204,11 @@ class ConfigValidatorTest extends TestCase $this->expectException(ValidationException::class); ConfigValidator::validateExts(null); } + + public function testValidatePkgs(): void + { + ConfigValidator::validatePkgs([]); + $this->expectException(ValidationException::class); + ConfigValidator::validatePkgs(null); + } } diff --git a/tests/SPC/util/DependencyUtilTest.php b/tests/SPC/util/DependencyUtilTest.php index a4f7839f..ffc515be 100644 --- a/tests/SPC/util/DependencyUtilTest.php +++ b/tests/SPC/util/DependencyUtilTest.php @@ -16,6 +16,12 @@ final class DependencyUtilTest extends TestCase { public function testGetExtLibsByDeps(): void { + // setup + $bak = [ + 'source' => Config::$source, + 'lib' => Config::$lib, + 'ext' => Config::$ext, + ]; // example Config::$source = [ 'test1' => [ @@ -82,6 +88,10 @@ final class DependencyUtilTest extends TestCase $this->assertTrue($b < $a); $this->assertTrue($c < $a); $this->assertTrue($c < $b); + // restore + Config::$source = $bak['source']; + Config::$lib = $bak['lib']; + Config::$ext = $bak['ext']; } public function testNotExistExtException(): void diff --git a/tests/SPC/util/LicenseDumperTest.php b/tests/SPC/util/LicenseDumperTest.php index c175ddbf..ed914296 100644 --- a/tests/SPC/util/LicenseDumperTest.php +++ b/tests/SPC/util/LicenseDumperTest.php @@ -33,6 +33,10 @@ final class LicenseDumperTest extends TestCase public function testDumpWithSingleLicense(): void { + $bak = [ + 'source' => Config::$source, + 'lib' => Config::$lib, + ]; Config::$lib = [ 'lib-base' => ['type' => 'root'], 'php' => ['type' => 'root'], @@ -54,10 +58,17 @@ final class LicenseDumperTest extends TestCase $dumper->dump(self::DIRECTORY); $this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_0.txt'); + // restore + Config::$source = $bak['source']; + Config::$lib = $bak['lib']; } public function testDumpWithMultipleLicenses(): void { + $bak = [ + 'source' => Config::$source, + 'lib' => Config::$lib, + ]; Config::$lib = [ 'lib-base' => ['type' => 'root'], 'php' => ['type' => 'root'], @@ -91,5 +102,9 @@ final class LicenseDumperTest extends TestCase $this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_0.txt'); $this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_1.txt'); $this->assertFileExists(self::DIRECTORY . '/lib_fake_lib_2.txt'); + + // restore + Config::$source = $bak['source']; + Config::$lib = $bak['lib']; } } diff --git a/tests/SPC/util/SPCConfigUtilTest.php b/tests/SPC/util/SPCConfigUtilTest.php new file mode 100644 index 00000000..ebd2f92a --- /dev/null +++ b/tests/SPC/util/SPCConfigUtilTest.php @@ -0,0 +1,74 @@ +assertInstanceOf(SPCConfigUtil::class, new SPCConfigUtil()); + $this->assertInstanceOf(SPCConfigUtil::class, new SPCConfigUtil(BuilderProvider::makeBuilderByInput(new ArgvInput()))); + } + + public function testConfig(): void + { + // normal + $result = (new SPCConfigUtil())->config(['bcmath']); + $this->assertStringContainsString(BUILD_ROOT_PATH . '/include', $result['cflags']); + $this->assertStringContainsString(BUILD_ROOT_PATH . '/lib', $result['ldflags']); + $this->assertStringContainsString('-lphp', $result['libs']); + + // has cpp + $result = (new SPCConfigUtil())->config(['swoole']); + $this->assertStringContainsString(PHP_OS_FAMILY === 'Darwin' ? '-lc++' : '-lstdc++', $result['libs']); + + // has mimalloc.o in lib dir + // backup first + if (file_exists(BUILD_LIB_PATH . '/mimalloc.o')) { + $bak = file_get_contents(BUILD_LIB_PATH . '/mimalloc.o'); + @unlink(BUILD_LIB_PATH . '/mimalloc.o'); + } + file_put_contents(BUILD_LIB_PATH . '/mimalloc.o', ''); + $result = (new SPCConfigUtil())->config(['bcmath'], ['mimalloc']); + $this->assertStringStartsWith(BUILD_LIB_PATH . '/mimalloc.o', $result['libs']); + @unlink(BUILD_LIB_PATH . '/mimalloc.o'); + if (isset($bak)) { + file_put_contents(BUILD_LIB_PATH . '/mimalloc.o', $bak); + } + } +}