CI 上出现“解析错误”的 PHPunit 测试错误
PHPunit test errors with `Parse error` on CI
我正在 phpunit 9.3.8
的帮助下对我的代码进行单元测试。测试 运行 在我的本地开发环境中很好(我是 运行ning Windows 10,PHP 7.4.2
和 Xdebug 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 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.
对象属性的类型提示在 PHP 7.4 中,这是 运行
PHPUnit 9.3.8 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.23 with Xdebug 2.9.8
基本上是PHP版本冲突,
出于某种原因,PHPUnit
试图像旧的 PHP 版本一样解析它,其中 private int $code;
无效(应该使用 private $code;
)。
哦,刚注意到你的 Windows 系统是 运行 PHP 7.4
但是 Gitlab CI 图像已安装 PHP 7.3.23
!
我正在 phpunit 9.3.8
的帮助下对我的代码进行单元测试。测试 运行 在我的本地开发环境中很好(我是 运行ning Windows 10,PHP 7.4.2
和 Xdebug 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 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.
对象属性的类型提示在 PHP 7.4 中,这是 运行
PHPUnit 9.3.8 by Sebastian Bergmann and contributors.
Runtime: PHP 7.3.23 with Xdebug 2.9.8
基本上是PHP版本冲突,
出于某种原因,PHPUnit
试图像旧的 PHP 版本一样解析它,其中 private int $code;
无效(应该使用 private $code;
)。
哦,刚注意到你的 Windows 系统是 运行 PHP 7.4
但是 Gitlab CI 图像已安装 PHP 7.3.23
!