Caleb Williams Caleb Williams - 1 month ago 11x
PHP Question

GitLab-CI issues with file_get_contents during PHPUnit Tests

I have a fairly simple problem that I cannot seem to figure out.

I am developing an OOP-based PHP application using the Composer Dependency Manager, PHPUnit for testing. I am hosting the repository on GitLab and am using GitLab-CI to run the PHPUnit tests.

My file directory is fairly simple:

├──[composer dependencies]
├──[Twig templates]

taxCalc.php contains:

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

That works just fine on my production server and I can run the PHPunit tests just fine via PHPUnit integration with PhpStorm, but when I try to get GitLab-CI to work I consistently get an error:


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

/ 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


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


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


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

My .gitlab_ci.yml is as follows:

# Select image from
image: php:7.0

# Select what we should cache
- vendor/

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

# Install composer
- curl -sS | php

# Install all project dependencies
- php composer.phar install

- vendor/bin/phpunit --configuration phpunit.xml

My phpunit.xml file contains:

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

<testsuite name="Paycheck Tests">

Please note that I've used this with or without the tag and there is no difference if I use /../ or anything else for that matter.

I appreciate any help you can give.




I finally got this to work. I added a function (in the global space for now) to my config.php file:

* Required Functions

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

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

That meant I could update my 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

but I was still having problems with the GitLab-Ci runner having yet a completely different environment that doesn't call my config.php at all, so I added a fix (or hack really) to get the test to pass to 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";

And now the build passes.

Thanks for all the help to both people who responded.


Make sure that the file is commited (usually you don't commit data you may place it under resources). I think you did already.

The second thing is also just a suggestion: define a PATH constant and use this. Because in your case you never know where is your current working directory.

Define a bootstrap file in phpunit and define the MAIN_PATH. Example:


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

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

In the index you have to provide this MAIN_PATH too and in calcFit you write:


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