2016-09-04 12 views
1

私は理解できないほど簡単な問題があります。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"; 

ビルドが完了しました。

回答いただいた両方の方々のお役に立てることに感謝します。

+0

[ストリームを開くことに失敗しました:このようなファイルやディレクトリはありません](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such -file-or-directory) –

答えて

2

ファイルがコミットされていることを確認します(通常、データをコミットしないで、リソースに置くことができます)。私はあなたがすでにやったと思う。

もう一つのことは、ちょっとした提案です。 PATH定数を定義してこれを使用してください。あなたのケースでは、現在の作業ディレクトリがどこにあるか分からないからです。

phpunitでブートストラップファイルを定義し、MAIN_PATHを定義します。例:あなたはあまりにもこのMAIN_PATHを提供しなければならないとcalcFitにあなたが書いたインデックスで

<?php 

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

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

<?php 

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

お返事ありがとうございます。私は設定ファイルのいくつかの異なる部分を再構成しなければならなくなり(上記の編集を参照)、GitLab-CIランナだけが生成する環境変数のチェックを追加しました。 –

0

問題は、あなたが必要との相対的なパスを使用していることは非常におそらくあります。説明と解決方法はこちらを参照してください。PHP - Failed to open stream : No such file or directory

+0

ありがとうございます。私はまだ解決するべきことがいくつかありますが、私は解決策があり、あなたのリンクが便利になるでしょう。 –

+0

あなたは歓迎です:)私はそれが助けてうれしいです –

関連する問題