2

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:

├──_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 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.

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

My .gitlab_ci.yml is as follows:

# 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

My phpunit.xml file contains:

<?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>

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

I appreciate any help you can give.

EDIT:

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 05 '16 at 18:38

2 Answers2

2

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:

<?php

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:

<?php

function calcFit() {
   $fitFile = MAIN_PATH . '/data/2016/federal/fit.json';
}
iRaS
  • 1,958
  • 1
  • 16
  • 29
  • 1
    Thanks for your response. I ended up having to reconfigure a couple of different portions of my config file (see edits above) and added a check for an environment variable that the GitLab-CI runner only would produce. – Caleb Williams Sep 06 '16 at 02:11
1

The issue is very probably that you are using a relative path in your require. See here for explanations and solutions : PHP - Failed to open stream : No such file or directory

Community
  • 1
  • 1
Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
  • Thank you for your response. I still have some things to clean up now that I have working solution and your link will come in handy. – Caleb Williams Sep 06 '16 at 02:18