GitLab-CI 在 PHPUnit 测试期间出现 file_get_contents 问题

GitLab-CI issues with file_get_contents during PHPUnit Tests

我有一个相当简单的问题,我似乎无法弄清楚。

我正在使用 Composer Dependency Manager 开发基于 OOP 的 PHP 应用程序,PHPUnit 用于测试。我在 GitLab 上托管存储库,并使用 GitLab-CI 来 运行 PHP 单元测试。

我的文件目录比较简单:

├──_data
   ├──_2016
      ├──_federal
         ├──fit.json
├──_libs
   ├──_paycheckCalculator
      ├──paycheck.php
      ├──taxCalc.php
├──_public_html
   ├──index.php
├──_vendor
   ├──[composer dependencies]
   ├──autoload.php
├──_tests
   ├──paycheckTest.php
   ├──taxCalcTest.php
├──_templates
   ├──[Twig templates]

taxCalc.php 包含:

public static function calcFIT($taxableWages, array $taxStatus, int $frequency = 52):float {
    $fitFile = "../data/2016/federal/fit.json";
    ...

这在我的生产服务器上运行良好,我可以通过 PHPUnit 与 PhpStorm 集成 运行 PHP 单元测试,但是当我尝试获取 GitLab 时- CI 工作我总是收到错误:

...
$ vendor/bin/phpunit --configuration phpunit.xml PHPUnit 5.5.4 by Sebastian Bergmann and contributors.

EE..EII 7 / 7 (100%)

Time: 32 ms, Memory: 4.00MB

There were 3 errors:

1) paycheckTest::calcNetTest file_get_contents(../data/2016/federal/fit.json): failed to open stream: No such file or directory

/builds/calebrw/paycheckCalculator/libs/paycheckCalculator/taxCalc.php:100 /builds/calebrw/paycheckCalculator/libs/paycheckCalculator/paycheck.php:49 /builds/calebrw/paycheckCalculator/libs/paycheckCalculator/paycheck.php:28 /builds/calebrw/paycheckCalculator/tests/paycheckTest.php:34

2) paycheckTest::calcTaxesTest file_get_contents(../data/2016/federal/fit.json): failed to open stream: No such file or directory

/builds/calebrw/paycheckCalculator/libs/paycheckCalculator/taxCalc.php:100 /builds/calebrw/paycheckCalculator/tests/paycheckTest.php:58

3) taxCalcTest::calcFITTest file_get_contents(../data/2016/federal/fit.json): failed to open stream: No such file or directory

/builds/calebrw/paycheckCalculator/libs/paycheckCalculator/taxCalc.php:100 /builds/calebrw/paycheckCalculator/tests/taxCalcTest.php:53

ERRORS! Tests: 7, Assertions: 11, Errors: 3, Incomplete: 2. ERROR: Build failed: exit code 1

我的.gitlab_ci.yml如下:

# Select image from https://hub.docker.com/_/php/
image: php:7.0

# Select what we should cache
cache:
  paths:
  - vendor/

before_script:
# Install git, the php image doesn't have installed
- apt-get update -yqq
- apt-get install git -yqq

# Install composer
- curl -sS https://getcomposer.org/installer | php

# Install all project dependencies
- php composer.phar install

PHPUnit:testsuite:
  script:
  - vendor/bin/phpunit --configuration phpunit.xml

我的 phpunit.xml 文件包含:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd"
        colors="false"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        stopOnFailure="false">

    <testsuites>
        <testsuite name="Paycheck Tests">
            <directory>tests/</directory>
        </testsuite>
    </testsuites>
    <php>
        <includePath>/builds/calebrw/paycheckCalculator</includePath>
    </php>
</phpunit>

请注意,无论是否使用 <includePath> 标签,我都使用过它,如果我使用 <includePath>/../</includePath> 或其他任何东西都没有区别。

感谢您提供的任何帮助。

编辑:

我终于让它工作了。我在 config.php 文件中添加了一个函数(目前在全局 space 中):

/**
 * Required Functions
 */

function getDirectory(bool $html = false):string
{
    $htmlBaseDirectory = '/webprojects/paycheckCalculator';

    if ($html == true) {
        return $htmlBaseDirectory;
    } else {
       return dirname(__DIR__);
    }
}

这意味着我可以更新我的 index.php:

require_once('config.php'); // Configuration Variables
require_once( getDirectory() . '/vendor/autoload.php'); // Composer Autoload
$dir = getDirectory(true) . $htmlPublicDirectory; // Combined variable needed for Twig compatibility

但我仍然遇到 GitLab 的问题-Ci 运行ner 有一个完全不同的环境,根本不会调用我的 config.php,所以我添加了一个修复(或真正破解)以使测试传递给 taxCalc.php:

if (getenv('CI_BUILD_ID') !== false) {
    define('MAIN_PATH', '/builds/calebrw/paycheckCalculator/');
} else {
    define('MAIN_PATH', dirname(__DIR__));
}

...
class taxCalc
{
...
    public static function calcFIT($taxableWages, array $taxStatus, int $frequency = 52):float {
        $fitFile = MAIN_PATH . "/data/2016/federal/fit.json";

现在构建通过了。

感谢两位回复者的帮助。

确保文件已提交(通常您不提交数据,您可以将其放在资源下)。我想你已经做到了。

第二件事也只是一个建议: 定义一个 PATH 常量并使用它。因为在你的情况下你永远不知道你当前的工作目录在哪里。

在phpunit中定义一个bootstrap文件,定义MAIN_PATH。示例:

<?php

define('MAIN_PATH', dirname(__DIR__));

require MAIN_PATH . '/vendor/autoload.php';

在索引中你也必须提供这个 MAIN_PATH 并且在 calcFit 中你写:

<?php

function calcFit() {
   $fitFile = MAIN_PATH . '/data/2016/federal/fit.json';
}

问题很可能是您在 require 中使用了相对路径。请参阅此处以获取解释和解决方案: