diff --git a/tests/unit/python/utils/install/test_php.py b/tests/unit/python/utils/install/test_php.py index d5d9c0b543..cb9aea3965 100644 --- a/tests/unit/python/utils/install/test_php.py +++ b/tests/unit/python/utils/install/test_php.py @@ -10,22 +10,33 @@ from utils.install import php as php_mod class TestEnsurePhpToolchain(unittest.TestCase): - def test_installs_the_binaries_only(self) -> None: + def test_installs_the_binaries_and_the_phpunit_extensions(self) -> None: with ( mock.patch.object(php_mod, "ensure_command_present") as ensure, + mock.patch.object(php_mod, "ensure_php_extension_present") as extension, mock.patch.object(php_mod.subprocess, "run") as run, ): php_mod.ensure_php_toolchain() self.assertEqual( [c.args[0] for c in ensure.call_args_list], ["php", "composer"] ) + self.assertEqual( + [c.args[0] for c in extension.call_args_list], + list(php_mod._PHPUNIT_EXTENSIONS), + ) run.assert_not_called() + def test_covers_the_platform_requirements_composer_resolves(self) -> None: + self.assertLessEqual( + {"dom", "mbstring", "xmlwriter"}, set(php_mod._PHPUNIT_EXTENSIONS) + ) + class TestEnsurePhpPresent(unittest.TestCase): def test_vendor_tree_present_skips_composer(self) -> None: with ( mock.patch.object(php_mod, "ensure_command_present") as ensure, + mock.patch.object(php_mod, "ensure_php_extension_present"), mock.patch.object(php_mod, "_PHPUNIT") as phpunit, mock.patch.object(php_mod.subprocess, "run") as run, ): @@ -39,6 +50,7 @@ class TestEnsurePhpPresent(unittest.TestCase): def test_missing_vendor_tree_runs_composer_install(self) -> None: with ( mock.patch.object(php_mod, "ensure_command_present"), + mock.patch.object(php_mod, "ensure_php_extension_present"), mock.patch.object(php_mod, "_PHPUNIT") as phpunit, mock.patch.object(php_mod.subprocess, "run") as run, ): @@ -53,6 +65,7 @@ class TestEnsurePhpPresent(unittest.TestCase): def test_raises_when_phpunit_still_missing(self) -> None: with ( mock.patch.object(php_mod, "ensure_command_present"), + mock.patch.object(php_mod, "ensure_php_extension_present"), mock.patch.object(php_mod, "_PHPUNIT") as phpunit, mock.patch.object(php_mod.subprocess, "run"), self.assertRaises(RuntimeError), @@ -63,6 +76,7 @@ class TestEnsurePhpPresent(unittest.TestCase): def test_propagates_a_failed_composer_install(self) -> None: with ( mock.patch.object(php_mod, "ensure_command_present"), + mock.patch.object(php_mod, "ensure_php_extension_present"), mock.patch.object(php_mod, "_PHPUNIT") as phpunit, mock.patch.object( php_mod.subprocess, @@ -79,12 +93,29 @@ class TestEnsurePhpPresent(unittest.TestCase): mock.patch.object( php_mod, "ensure_command_present", side_effect=RuntimeError("boom") ), + mock.patch.object(php_mod, "ensure_php_extension_present"), mock.patch.object(php_mod.subprocess, "run") as run, self.assertRaises(RuntimeError), ): php_mod.ensure_php_present() run.assert_not_called() + def test_missing_extension_fails_before_composer(self) -> None: + with ( + mock.patch.object(php_mod, "ensure_command_present"), + mock.patch.object( + php_mod, + "ensure_php_extension_present", + side_effect=RuntimeError("ext-dom missing"), + ), + mock.patch.object(php_mod, "_PHPUNIT") as phpunit, + mock.patch.object(php_mod.subprocess, "run") as run, + self.assertRaises(RuntimeError), + ): + phpunit.is_file.return_value = False + php_mod.ensure_php_present() + run.assert_not_called() + if __name__ == "__main__": unittest.main() diff --git a/tests/unit/python/utils/install/test_system_pkg.py b/tests/unit/python/utils/install/test_system_pkg.py index 1462f4de86..4c9cda2901 100644 --- a/tests/unit/python/utils/install/test_system_pkg.py +++ b/tests/unit/python/utils/install/test_system_pkg.py @@ -101,5 +101,83 @@ class TestEnsureCommandPresent(unittest.TestCase): system_pkg.ensure_command_present("ruby") +PHP_M_OUTPUT = "[PHP Modules]\nCore\nmbstring\nPhar\n\n[Zend Modules]\n" + + +class TestLoadedPhpExtensions(unittest.TestCase): + def test_parses_php_m_lower_cased(self) -> None: + with mock.patch.object( + system_pkg.subprocess, + "run", + return_value=subprocess.CompletedProcess([], 0, PHP_M_OUTPUT, ""), + ): + self.assertEqual( + system_pkg.loaded_php_extensions(), + {"[php modules]", "core", "mbstring", "phar", "[zend modules]"}, + ) + + def test_unqueryable_php_yields_no_extensions(self) -> None: + with mock.patch.object( + system_pkg.subprocess, + "run", + return_value=subprocess.CompletedProcess([], 1, "", "no php"), + ): + self.assertEqual(system_pkg.loaded_php_extensions(), set()) + + +class TestEnsurePhpExtensionPresent(unittest.TestCase): + def test_loaded_extension_installs_nothing(self) -> None: + with ( + mock.patch.object( + system_pkg, "loaded_php_extensions", return_value={"dom"} + ), + mock.patch.object(system_pkg, "install_package_candidates") as install, + ): + system_pkg.ensure_php_extension_present("dom") + install.assert_not_called() + + def test_missing_extension_installs_the_mapped_package(self) -> None: + with ( + mock.patch.object( + system_pkg, "loaded_php_extensions", side_effect=[set(), {"dom"}] + ), + mock.patch.object( + system_pkg, "detect_package_manager", return_value="apt-get" + ), + mock.patch.object(system_pkg, "install_package_candidates") as install, + ): + system_pkg.ensure_php_extension_present("dom") + install.assert_called_once_with("apt-get", ["php-xml"]) + + def test_raises_when_extension_still_absent(self) -> None: + with ( + mock.patch.object(system_pkg, "loaded_php_extensions", return_value=set()), + mock.patch.object( + system_pkg, "detect_package_manager", return_value="apt-get" + ), + mock.patch.object(system_pkg, "install_package_candidates"), + self.assertRaises(RuntimeError), + ): + system_pkg.ensure_php_extension_present("dom") + + def test_raises_on_an_unmapped_extension(self) -> None: + with ( + mock.patch.object(system_pkg, "loaded_php_extensions", return_value=set()), + mock.patch.object( + system_pkg, "detect_package_manager", return_value="apt-get" + ), + mock.patch.object(system_pkg, "install_package_candidates") as install, + self.assertRaises(RuntimeError), + ): + system_pkg.ensure_php_extension_present("gd") + install.assert_not_called() + + def test_every_extension_maps_every_supported_manager(self) -> None: + for extension, mapping in system_pkg._PHP_EXTENSION_PACKAGES.items(): + self.assertEqual( + sorted(mapping), sorted(system_pkg._SUPPORTED), msg=extension + ) + + if __name__ == "__main__": unittest.main() diff --git a/utils/install/php.py b/utils/install/php.py index 2bc9e42925..cd1e14017c 100644 --- a/utils/install/php.py +++ b/utils/install/php.py @@ -6,19 +6,32 @@ import subprocess from utils import PROJECT_ROOT from utils.install.primitives import log -from utils.install.system_pkg import ensure_command_present +from utils.install.system_pkg import ( + ensure_command_present, + ensure_php_extension_present, +) _PHPUNIT = PROJECT_ROOT / "vendor" / "bin" / "phpunit" +_PHPUNIT_EXTENSIONS = ("dom", "mbstring", "xmlwriter") + def ensure_php_toolchain() -> None: - """Install the PHP interpreter and Composer. + """Install the PHP interpreter, Composer and the extensions PHPUnit needs. + + ``phpunit/phpunit ^11`` declares ``_PHPUNIT_EXTENSIONS`` as platform + requirements and Composer refuses to resolve the vendor tree without them, + so they are part of the toolchain rather than of the suite consuming it. + A distro ships them as packages separate from the interpreter, which is why + a present ``php`` binary does not imply they are there. Raises: - RuntimeError: php or composer is still absent afterwards. + RuntimeError: php, composer or one of the extensions is still absent. """ ensure_command_present("php") ensure_command_present("composer") + for extension in _PHPUNIT_EXTENSIONS: + ensure_php_extension_present(extension) def ensure_php_present() -> None: diff --git a/utils/install/system_pkg.py b/utils/install/system_pkg.py index a98ef39050..3f82afc8eb 100644 --- a/utils/install/system_pkg.py +++ b/utils/install/system_pkg.py @@ -128,6 +128,76 @@ _COMMAND_PACKAGES: dict[str, dict[str, list[str]]] = { } +_PHP_EXTENSION_PACKAGES: dict[str, dict[str, list[str]]] = { + "dom": { + "pacman": ["php"], + "apt-get": ["php-xml"], + "dnf": ["php-xml"], + "yum": ["php-xml"], + "brew": ["php"], + }, + "mbstring": { + "pacman": ["php"], + "apt-get": ["php-mbstring"], + "dnf": ["php-mbstring"], + "yum": ["php-mbstring"], + "brew": ["php"], + }, + "xmlwriter": { + "pacman": ["php"], + "apt-get": ["php-xml"], + "dnf": ["php-xml"], + "yum": ["php-xml"], + "brew": ["php"], + }, +} + + +def loaded_php_extensions() -> set[str]: + """Return the extensions the php CLI currently loads, lower-cased. + + Returns: + the set reported by ``php -m``, empty when php cannot be queried. + """ + result = subprocess.run(["php", "-m"], capture_output=True, text=True, check=False) + return {line.strip().lower() for line in result.stdout.splitlines() if line.strip()} + + +def ensure_php_extension_present(extension: str) -> None: + """Make a PHP extension available, installing its package if absent. + + A distro ships the interpreter and its extensions as separate packages, so + a present ``php`` binary says nothing about them and ``ensure_command_present`` + returns early on one. The extension is therefore probed against ``php -m``. + + Args: + extension: extension name as ``php -m`` reports it. + + Raises: + RuntimeError: no mapping for this extension on the detected package + manager, or the extension is still absent after the install. + """ + if extension in loaded_php_extensions(): + return + + manager = detect_package_manager() + mapping = _PHP_EXTENSION_PACKAGES.get(extension) + if mapping is None or manager not in mapping: + raise RuntimeError( + f"No installer mapping defined for PHP extension " + f"'{extension}' on '{manager}'." + ) + + log(f"Missing PHP extension '{extension}'. Attempting installation via {manager}.") + install_package_candidates(manager, mapping[manager]) + + if extension not in loaded_php_extensions(): + raise RuntimeError( + f"PHP extension '{extension}' still absent after installing " + f"{mapping[manager]} via {manager}" + ) + + def ensure_command_present(command_name: str) -> None: """Make a command available, installing it if the host lacks it. @@ -161,6 +231,8 @@ def install_command_via_pkg(command_name: str) -> None: __all__ = [ "detect_package_manager", "ensure_command_present", + "ensure_php_extension_present", "install_command_via_pkg", "install_package_candidates", + "loaded_php_extensions", ]