You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在GitHub Actions中运行Laravel 11测试时出现gmp_mod()错误

问题:Laravel 11测试在GitHub Actions中触发gmp_mod()参数类型错误

本地使用SQLite或MySQL运行Laravel 11测试套件时,所有HTTP请求测试均正常通过,但在GitHub Actions中执行测试时,抛出如下错误:

gmp_mod(): Argument #1 ($num1) must be of type GMP|string|int, Illuminate\Support\Stringable given

怀疑是GitHub Actions工作流配置存在缺失项,以下是当前工作流配置:

name: tests

on:
  push:
    branches:
      - v2-dev
  pull_request:
  schedule:
    - cron: '0 0 * * *'

jobs:
  tests:
    runs-on: ubuntu-22.04

    strategy:
      fail-fast: true
      matrix:
        php: [8.3]
        laravel: [11]
        exclude:
          - php: 8.3
            laravel: 11

    name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: dom, curl, libxml, mbstring, zip, bcmath
          ini-values: error_reporting=E_ALL
          tools: composer:v2
          coverage: none

      - name: Copy .env
        run: php -r "file_exists('.env') || copy('.env.example', '.env');"

      - name: Install dependencies
        run: |
          composer install --prefer-dist --no-interaction --no-progress

      - name: Create SQLite database file
        run: |
          if [ ! -f database/database.sqlite ]; then
            touch database/database.sqlite
          fi

      - name: Run migrations
        run: php artisan migrate
        env:
          DB_CONNECTION: sqlite
          DB_DATABASE: database/database.sqlite

      - name: Execute tests
        run: vendor/bin/phpunit
        env:
          DB_CONNECTION: sqlite
          DB_DATABASE: database/database.sqlite

触发错误的测试代码:

public function test_retrieve_application_with_body_is_successful(): void
{
    $application = ApplicationEntry::factory()->make();

    $response = $this->post('/api/v2/applications', [
        'product_type' => 'tests',
        'data' => (new Honeycomb())->setApplication(
            collect($application?->data)->toArray()
        )->getMappedApplication()
    ]);

    $response->dump();

    $response = $this->get('/api/v2/applications/'.(string) Arr::get($response, 'model.hash', 'unknown'));

    $response->assertSuccessful()
        ->assertJsonStructure([
            'model',
            'metadata',
        ]);
}

GitHub Actions输出截图:
GitHub Actions错误输出


解决方案

1. 安装缺失的PHP扩展

错误核心原因是GitHub Actions环境未安装gmp扩展,而你的代码或依赖包(如Laravel加密/哈希逻辑)运行时调用了gmp_mod()函数。在工作流的Setup PHP步骤中添加gmp扩展即可修复:

- name: Setup PHP
  uses: shivammathur/setup-php@v2
  with:
    php-version: ${{ matrix.php }}
    extensions: dom, curl, libxml, mbstring, zip, bcmath, gmp # 新增gmp扩展
    ini-values: error_reporting=E_ALL
    tools: composer:v2
    coverage: none

2. 修正矩阵配置矛盾

当前工作流的矩阵配置存在逻辑错误:指定了php: [8.3]和laravel: [11]的组合,又立刻排除了该组合,导致没有可用的测试任务。删除exclude块即可:

strategy:
  fail-fast: true
  matrix:
    php: [8.3]
    laravel: [11]
    # 移除以下无效的exclude配置
    # exclude:
    #   - php: 8.3
    #     laravel: 11

补充说明

本地环境通常默认预装了gmp扩展,因此测试正常;而GitHub Actions的Ubuntu镜像默认不包含该扩展,需手动指定安装。gmp扩展多用于大数运算、加密哈希场景,你的业务代码或Laravel 11底层逻辑生成hash值时依赖它。

内容的提问来源于stack exchange,提问作者Ryan H

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 13:03:36