持续集成

Mautic 在每次提交时运行多个测试,以确保代码按预期工作,使用 :xref:`GitHub Actions

  • :xref:`phpstan 用于静态分析。

  • :xref:`phpunit 用于单元、功能和集成测试。

  • :xref:`codecov 用于代码覆盖率报告,并确保覆盖率不会下降。

  • :xref:`rector 用于额外的代码质量检查,例如,检查是否使用了不受支持的 Symfony 或 PHP 语法。

  • :xref:`php_cs_fixer 用于检查整个代码库中是否存在统一的代码风格。

  • :xref:`twig_lint 用于检查所有 Twig 模板是否有效。

示例 .github/workflows/tests.yml

此文件基于其中的定义创建 GitHub Action 作业。 您可以在 :xref:`hello_world_plugin 中找到此文件的实时示例以及其工作原理。

name: Mautic Plugin tests

on:
push:
   branches:
      - main # 主分支
      - '[0-9]+\.[0-9]+'
pull_request:

env:
PLUGIN_DIR: plugins/HelloWorldBundle # 与 composer.json 中的 extra.install-directory-name 相同

jobs:
phpunit:
   runs-on: ubuntu-latest

   strategy:
      matrix:
      php-versions: ['7.4', '8.0'] # 支持的 PHP 版本
      db-types: ['mysql'] # 可以是: ['mysql', 'mariadb'],但对于此插件来说不是必需的,因为它不添加任何数据库模式
      mautic-versions: ['4.3', '4.4'] # 支持的 Mautic 版本

   name: 在 PHP ${{ matrix.php-versions }}, ${{ matrix.db-types }}, Mautic ${{ matrix.mautic-versions }} 上的测试

   services:
      database:
      image: ${{ matrix.db-types == 'mysql' && 'mysql:5.7' || 'mariadb:10.3' }}
      env:
         MYSQL_ALLOW_EMPTY_PASSWORD: yes
         MYSQL_DATABASE: mautictest
      ports:
         - 3306
      options: >-
         --shm-size=2gb
         --name=${{ matrix.db-types }}
         --tmpfs=/var/lib/mysql
         --health-cmd="mysqladmin ping"
         --health-interval=10s
         --health-timeout=5s
         --health-retries=3

   steps:
   - name: Checkout Mautic 4
      uses: actions/checkout@v3
      with:
      repository: mautic/mautic
      ref: ${{ matrix.mautic-versions }}

   - name: Checkout this plugin
      uses: actions/checkout@v3
      with:
      path: ${{ env.PLUGIN_DIR }}

   - name: Setup PHP, with composer and extensions
      uses: shivammathur/setup-php@v2
      with:
      php-version: ${{ matrix.php-versions }}
      ini-values: -dpcov.enabled=0, pcov.directory=."
      extensions: mbstring, xml, ctype, iconv, intl, pdo_sqlite, mysql, pdo_mysql
      coverage: pcov

   - name: add MySQL config file
      run: |
      mysqldump --version
      mysqldump --print-defaults
      cp .github/ci-files/.my.cnf ~/.my.cnf
      mysqldump --print-defaults
  • name: 设置 SYMFONY_ENV 为 test

    run: | echo “SYMFONY_ENV=test” >> $GITHUB_ENV echo “MAUTIC_ENV=test” >> $GITHUB_ENV

    • name: 获取 composer 缓存目录

      id: composer-cache run: echo “dir=$(composer config cache-files-dir)” >> $GITHUB_OUTPUT

    • name: 缓存 composer 依赖项

      uses: actions/cache@v3 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles(’**/composer.lock’) }} restore-keys: ${{ runner.os }}-composer-

    • name: 安装 Composer 依赖项

      run: composer install

    • name: 安装 Mautic

      env: DB_PORT: ${{ job.services.database.ports[3306] }} run: | cp ./.github/ci-files/local.php ./app/config/local.php php bin/console mautic:install –force http://localhost

    • name: 安装插件

      env: DB_PORT: ${{ job.services.database.ports[3306] }} run: php bin/console mautic:plugins:install –env=dev

    • name: 运行代码风格检查

      run: bin/php-cs-fixer fix ${{ env.PLUGIN_DIR }} –config=.php-cs-fixer.php -v –dry-run –show-progress=dots –diff

    • name: PHPSTAN

      run: composer phpstan – ${{ env.PLUGIN_DIR }}

    • name: Rector

      run: composer rector – –dry-run –no-progress-bar ${{ env.PLUGIN_DIR }}

    • name: Twig Lint

      run: bin/console lint:twig ${{ env.PLUGIN_DIR }}

    • name: 运行 PHPUNIT 测试

      env: DB_PORT: ${{ job.services.database.ports[3306] }} run: XDEBUG_MODE=coverage APP_DEBUG=0 php -dpcov.enabled=1 -dpcov.directory=. -dpcov.exclude=”~tests|themes|vendor~” bin/phpunit -d memory_limit=1G –bootstrap vendor/autoload.php –configuration ${{ env.PLUGIN_DIR }}/phpunit.xml –coverage-clover=${{ env.PLUGIN_DIR }}/coverage.xml –coverage-text

    • name: 代码覆盖率报告

      run: cat ${{ env.PLUGIN_DIR }}/coverage.xml

    • name: 上传代码覆盖率报告

      if: ${{ matrix.php-versions == ‘8.0’ && matrix.db-types == ‘mysql’ && matrix.mautic-versions == ‘4.4’ }} # 仅上传一次,根据您的矩阵进行更改 uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: true working-directory: ${{ env.PLUGIN_DIR }} verbose: true

    • name: 上传日志作为工件

      uses: actions/upload-artifact@v3 with: name: mautic-logs path: var/logs/

Note

plugins/HelloWorldBundle 替换为您的插件目录。相同的名称应在 composer.json 中的 extra.install-directory-name 中。 确保您的默认分支命名为 main,如果不是,请更新该文件。 更新 matrix 以设置支持的 PHP 和 Mautic 版本,以便在 MySQL、MariaDB 或两者上运行测试。

将此文件粘贴到 .github/workflows/tests.yml 文件中,并替换 PLUGIN_DIR 环境变量后,您可以提交并将其推送到 GitHub。GitHub 会自动创建作业,您可以在存储库的“Actions”选项卡中查看它们。

添加 PHPUnit.xml 文件

此时,由于缺少 phpunit.xml 文件,GitHub Action 将无法完成。在插件目录的根目录下创建一个文件,并将以下内容粘贴到其中:

<?xml version="1.0" encoding="UTF-8"?>

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
   colors                      = "true"
   failOnWarning               = "true"
   bootstrap                   = "autoload.php" >

   <testsuites>
      <testsuite name="unit">
            <directory>Tests/Unit</directory>
      </testsuite>
      <testsuite name="functional">
            <directory>Tests/Functional</directory>
      </testsuite>
      <testsuite name="all">
            <directory>Tests/Unit</directory>
            <directory>Tests/Functional</directory>
      </testsuite>
   </testsuites>

   <php>
      <env name="KERNEL_CLASS" value="AppTestKernel" />
      <server name="KERNEL_DIR" value="app" />
      <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
   </php>

   <filter>
      <whitelist>
            <directory>*</directory>
            <exclude>
               <directory>Assets</directory>
               <directory>Config</directory>
               <directory>Tests</directory>
               <directory>Translations</directory>
               <directory>Views</directory>
            </exclude>
      </whitelist>
   </filter>

   <listeners>
      <listener class="\Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
      <listener class="\Mautic\CoreBundle\Test\Listeners\CleanupListener" />
   </listeners>

</phpunit>

Note

如果您的目录结构不同,请更新 testsuite 目录。

设置 Codecov

此时,检查应该开始运行,并指出插件代码中的所有问题。在您修复了所有这些问题后,您会发现 Codecov 报告没有正确上传。以下是如何使其正常工作的方法:

  1. 您必须为您的存储库启用 Codecov GitHub App 以上传覆盖率报告。

  2. 该应用程序将为您提供一个 API 密钥。将其粘贴到 GitHub 存储库设置中的“secret variables”中,命名为 CODECOV_TOKEN

  3. 由于插件不在根目录中,因此您必须告诉它如何将报告中的文件映射到存储库中的文件。为此,您需要在插件目录的根目录下创建一个名为 codecov.yml 的文件,并将以下内容粘贴到其中:

codecov:
   disable_default_path_fixes: true
fixes:
   - "/home/runner/work/plugin-helloworld/plugin-helloworld/plugins/HelloWorldBundle/::"

Note

更新路径以匹配您的插件仓库。最安全的方法是查看“Coverage report”步骤中的路径,Codecov 会在此处打印覆盖率 XML 文件,然后再将其发送到 Codecov。