私は理解できないほど簡単な問題があります。PHPUnitテスト中にGitLab-CIがfile_get_contentsを発行する
私はOOPベースのPHPアプリケーションを開発するために、Composer Dependency Manager、PHPUnitを使ってテストしています。私はGitLabでリポジトリをホスティングしており、GitLab-CIを使ってPHPUnitテストを実行しています。
マイファイルのディレクトリは非常に簡単です:
├──_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は含まれています。私の本番サーバー上でうまく動作し、私は経由うまくPHPUnitテストを実行することができます
public static function calcFIT($taxableWages, array $taxStatus, int $frequency = 52):float {
$fitFile = "../data/2016/federal/fit.json";
...
PhupStormとのPHPUnitの統合は、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>
またはそれ以外のものを使用しても差はありません。
私は何か助けていただけることを感謝します。
編集:私は最終的にこれが動作するようになった
。
/**
* Required Functions
*/
function getDirectory(bool $html = false):string
{
$htmlBaseDirectory = '/webprojects/paycheckCalculator';
if ($html == true) {
return $htmlBaseDirectory;
} else {
return dirname(__DIR__);
}
}
私はindex.phpの私を更新でき意味:私は私ののconfig.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ランナーが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";
ビルドが完了しました。
回答いただいた両方の方々のお役に立てることに感謝します。
[ストリームを開くことに失敗しました:このようなファイルやディレクトリはありません](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such -file-or-directory) –