0
votes

I'm unit testing my code with the help of phpunit 9.3.8. The tests run fine on my local dev environment (I'm running Windows 10, PHP 7.4.2 with Xdebug 2.9.2) but fail because of a parse error when I try to run phpunit on Gitlab CI (The CI runs the latest alpine linux image).

The error is:

$ composer run test
> phpunit
PHPUnit 9.3.8 by Sebastian Bergmann and contributors.
Runtime:       PHP 7.3.23 with Xdebug 2.9.8
Configuration: /builds/gaspacchio/back-to-the-future/phpunit.xml
Reporter
 ✘ Can set short message [6.05 ms]
   │
   │ ParseError: syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
   │
   │ /builds/gaspacchio/back-to-the-future/src/api/utilities/Reporter.php:21
   │ /builds/gaspacchio/back-to-the-future/src/api/tests/ReporterTest.php:12
   │

// More errors

Time: 00:01.970, Memory: 310.00 MB
ERRORS!
Tests: 6, Assertions: 0, Errors: 6.
Generating code coverage report in Clover XML format ... done [00:00.330]
PHP Warning:  fopen(/builds/gaspacchio/back-to-the-future/): failed to open stream: Is a directory in /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/Util/Printer.php on line 89
PHP Stack trace:
PHP   1. {main}() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/phpunit:0
PHP   2. PHPUnit\TextUI\Command::main() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/phpunit:61
PHP   3. PHPUnit\TextUI\Command->run() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/TextUI/Command.php:100
PHP   4. PHPUnit\TextUI\TestRunner->run() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/TextUI/Command.php:147
PHP   5. PHPUnit\Util\Printer->__construct() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:756
PHP   6. fopen() /builds/gaspacchio/back-to-the-future/vendor/phpunit/phpunit/src/Util/Printer.php:89
Code Coverage Report:    
  2020-10-13 10:12:09    
                         
 Summary:                
  Classes:  0.00% (0/11) 
  Methods:  0.00% (0/38) 
  Paths:    0.00% (0/7)  
  Branches:    0.00% (0/7)
  Lines:    0.00% (0/397)
Script phpunit handling the test event returned with error code 2

Line 21 of the Reporter.php file is below:

<?php namespace utilities\Reporter;

/**
 * The Reporter class is responsible for returning data to the client.
 */
class Reporter
{
    /** This is the code of the answer.
     * (more comments)
     * @var int The status code.
     */
    private int $code; //<-- This is line number 21

And the line 12 of ReporterTest.php is:

<?php

use PHPUnit\Framework\TestCase;
use utilities\Reporter\Reporter;

class ReporterTest extends TestCase
{
    protected $reporter;

    protected function setUp(): void
    {
        $this->reporter = new Reporter(); // <-- Line 12 is here
    }
}

I'm using the fixtures function of PHPunit, defined as :

PHPUnit supports sharing the setup code. Before a test method is run, a template method called setUp() is invoked. setUp() is where you create the objects against which you will test.

2
Type hinting of object properties is in PHP 7.4+ and this is running - Runtime: PHP 7.3.23Nigel Ren
Damn, thanks so much!Puka

2 Answers

1
votes

Type hinting of object properties is in PHP 7.4 and this is running

PHPUnit 9.3.8 by Sebastian Bergmann and contributors.

Runtime: PHP 7.3.23 with Xdebug 2.9.8

1
votes

It's basically a PHP version conflict, for some reason PHPUnit is trying to parse it like older PHP versions where private int $code; is invalid (and private $code; should be used instead).

Oh, just noticed your Windows system is running PHP 7.4 but Gitlab CI image has PHP 7.3.23 installed!