【问题标题】:PHPunit test errors with `Parse error` on CICI上的“解析错误”的PHPunit测试错误
【发布时间】:2021-01-27 16:28:16
【问题描述】:

我在phpunit 9.3.8 的帮助下对我的代码进行单元测试。测试在我的本地开发环境中运行良好(我正在运行 Windows 10,PHP 7.4.2Xdebug 2.9.2)但是当我尝试在 Gitlab CI 上运行 phpunit 时由于解析错误而失败(CI 运行最新的 alpine linux 镜像)。

错误是:

$ 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

Reporter.php 文件的第 21 行如下:

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

ReporterTest.php的第12行是:

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

我使用的是PHPunit的fixtures函数,定义为:

PHPUnit 支持共享设置代码。在运行测试方法之前,调用名为 setUp() 的模板方法。 setUp() 是您创建要测试的对象的地方。

【问题讨论】:

  • 对象属性的类型提示在 PHP 7.4+ 中并且正在运行 - 运行时:PHP 7.3.23
  • 该死,非常感谢!

标签: php phpunit gitlab-ci


【解决方案1】:

对象属性的类型提示在 PHP 7.4 中,并且正在运行

Sebastian Bergmann 及其贡献者的 PHPUnit 9.3.8。

运行时:PHP 7.3.23 与 Xdebug 2.9.8

【讨论】:

    【解决方案2】:

    基本上是PHP版本冲突, 出于某种原因,PHPUnit 试图像旧 PHP 版本一样解析它,其中 private int $code; 无效(应该使用 private $code; 代替)。

    哦,刚刚注意到您的 Windows 系统正在运行 PHP 7.4 但是 Gitlab CI 镜像已经安装了PHP 7.3.23

    【讨论】:

      猜你喜欢
      • 2017-07-27
      • 1970-01-01
      • 2016-06-04
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      • 2013-06-19
      相关资源
      最近更新 更多